blob: 7037accb0589df1822d100df9e60a9f5e3784156 [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) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200334 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200335 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200336 else
337 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200338 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800339 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return tape;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static void ide_tape_put(struct ide_tape_obj *tape)
344{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200345 ide_drive_t *drive = tape->drive;
346
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800347 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 kref_put(&tape->kref, ide_tape_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200349 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800350 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100354 * The variables below are used for the character device interface. Additional
355 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100357static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359#define ide_tape_f(file) ((file)->private_data)
360
361static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
362{
363 struct ide_tape_obj *tape = NULL;
364
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800365 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 tape = idetape_devs[i];
367 if (tape)
368 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800369 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return tape;
371}
372
Borislav Petkovd236d742008-04-18 00:46:27 +0200373static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100374 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct idetape_bh *bh = pc->bh;
377 int count;
378
379 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (bh == NULL) {
381 printk(KERN_ERR "ide-tape: bh == NULL in "
382 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200383 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return;
385 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100386 count = min(
387 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
388 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200389 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100390 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 bcount -= count;
392 atomic_add(count, &bh->b_count);
393 if (atomic_read(&bh->b_count) == bh->b_size) {
394 bh = bh->b_reqnext;
395 if (bh)
396 atomic_set(&bh->b_count, 0);
397 }
398 }
399 pc->bh = bh;
400}
401
Borislav Petkovd236d742008-04-18 00:46:27 +0200402static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100403 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct idetape_bh *bh = pc->bh;
406 int count;
407
408 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100410 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
411 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200415 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 bcount -= count;
417 pc->b_data += count;
418 pc->b_count -= count;
419 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100420 bh = bh->b_reqnext;
421 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (bh) {
423 pc->b_data = bh->b_data;
424 pc->b_count = atomic_read(&bh->b_count);
425 }
426 }
427 }
428}
429
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200430static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct idetape_bh *bh = pc->bh;
433 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200434 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Borislav Petkov346331f2008-04-18 00:46:26 +0200436 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
438 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100440 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
441 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
445 atomic_set(&bh->b_count, count);
446 if (atomic_read(&bh->b_count) == bh->b_size)
447 bh = bh->b_reqnext;
448 bcount -= count;
449 }
450 pc->bh = bh;
451}
452
453/*
454 * idetape_next_pc_storage returns a pointer to a place in which we can
455 * safely store a packet command, even though we intend to leave the
456 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
457 * commands is allocated at initialization time.
458 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200459static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 idetape_tape_t *tape = drive->driver_data;
462
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100463 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100466 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return (&tape->pc_stack[tape->pc_stack_index++]);
468}
469
470/*
471 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
472 * Since we queue packet commands in the request queue, we need to
473 * allocate a request, along with the allocation of a packet command.
474 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/**************************************************************
477 * *
478 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
479 * followed later on by kfree(). -ml *
480 * *
481 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100482
483static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 idetape_tape_t *tape = drive->driver_data;
486
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100487 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100490 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return (&tape->rq_stack[tape->rq_stack_index++]);
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100495 * called on each failed packet command retry to analyze the request sense. We
496 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100498static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200501 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Borislav Petkov1b5db432008-02-02 19:56:48 +0100503 tape->sense_key = sense[2] & 0xF;
504 tape->asc = sense[12];
505 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100506
507 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
508 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Borislav Petkovd236d742008-04-18 00:46:27 +0200510 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200511 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200512 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100513 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200514 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200515 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 /*
519 * If error was the result of a zero-length read or write command,
520 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
521 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
522 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100523 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100524 /* length == 0 */
525 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
526 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* don't report an error, everything's ok */
528 pc->error = 0;
529 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200530 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100533 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200535 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100537 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100538 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
539 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200541 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100544 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100545 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200547 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200549 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200550 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
552 }
553}
554
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200555/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200556static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200558 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200560 while (bh) {
561 u32 size = bh->b_size;
562
563 while (size) {
564 unsigned int order = fls(size >> PAGE_SHIFT)-1;
565
566 if (bh->b_data)
567 free_pages((unsigned long)bh->b_data, order);
568
569 size &= (order-1);
570 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 prev_bh = bh;
573 bh = bh->b_reqnext;
574 kfree(prev_bh);
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
579{
580 struct request *rq = HWGROUP(drive)->rq;
581 idetape_tape_t *tape = drive->driver_data;
582 unsigned long flags;
583 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100585 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100588 case 0: error = IDETAPE_ERROR_GENERAL; break;
589 case 1: error = 0; break;
590 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 rq->errors = error;
593 if (error)
594 tape->failed_pc = NULL;
595
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100596 if (!blk_special_request(rq)) {
597 ide_end_request(drive, uptodate, nr_sects);
598 return 0;
599 }
600
Borislav Petkov54bb2072008-02-06 02:57:52 +0100601 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Borislav Petkov54bb2072008-02-06 02:57:52 +0100605 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
607}
608
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200609static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200612 struct ide_atapi_pc *pc = tape->pc;
613 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100615 debug_log(DBG_PROCS, "Enter %s\n", __func__);
616
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200617 if (tape->failed_pc == pc)
618 tape->failed_pc = NULL;
619
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200620 if (pc->c[0] == REQUEST_SENSE) {
621 if (uptodate)
622 idetape_analyze_error(drive, pc->buf);
623 else
624 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
625 "itself - Aborting request!\n");
626 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
627 struct request *rq = drive->hwif->hwgroup->rq;
628 int blocks = pc->xferred / tape->blk_size;
629
630 tape->avg_size += blocks * tape->blk_size;
631
632 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
633 tape->avg_speed = tape->avg_size * HZ /
634 (jiffies - tape->avg_time) / 1024;
635 tape->avg_size = 0;
636 tape->avg_time = jiffies;
637 }
638
639 tape->first_frame += blocks;
640 rq->current_nr_sectors -= blocks;
641
642 if (pc->error)
643 uptodate = pc->error;
644 } else if (pc->c[0] == READ_POSITION && uptodate) {
645 u8 *readpos = tape->pc->buf;
646
647 debug_log(DBG_SENSE, "BOP - %s\n",
648 (readpos[0] & 0x80) ? "Yes" : "No");
649 debug_log(DBG_SENSE, "EOP - %s\n",
650 (readpos[0] & 0x40) ? "Yes" : "No");
651
652 if (readpos[0] & 0x4) {
653 printk(KERN_INFO "ide-tape: Block location is unknown"
654 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200655 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200656 uptodate = 0;
657 } else {
658 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200659 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200660
661 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200662 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200663 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200666
667 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200670static void idetape_init_pc(struct ide_atapi_pc *pc)
671{
672 memset(pc->c, 0, 12);
673 pc->retries = 0;
674 pc->flags = 0;
675 pc->req_xfer = 0;
676 pc->buf = pc->pc_buf;
677 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
678 pc->bh = NULL;
679 pc->b_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Borislav Petkovd236d742008-04-18 00:46:27 +0200682static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100684 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100685 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200687 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static void idetape_init_rq(struct request *rq, u8 cmd)
691{
FUJITA Tomonorie7b241a2008-04-29 09:54:38 +0200692 blk_rq_init(NULL, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200693 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200694 rq->cmd[13] = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100698 * Generate a new packet command request in front of the request queue, before
699 * the current request, so that it will be processed immediately, on the next
700 * pass through the driver. The function below is called from the request
701 * handling part of the driver (the "bottom" part). Safe storage for the request
702 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100704 * Memory for those requests is pre-allocated at initialization time, and is
705 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
706 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100708 * The higher level of the driver - The ioctl handler and the character device
709 * handling functions should queue request to the lower level part and wait for
710 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200712static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100713 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct ide_tape_obj *tape = drive->driver_data;
716
717 idetape_init_rq(rq, REQ_IDETAPE_PC1);
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200718 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 rq->buffer = (char *) pc;
720 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200721 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200722 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/*
726 * idetape_retry_pc is called when an error was detected during the
727 * last packet command. We queue a request sense packet command in
728 * the head of the request list.
729 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200730static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Borislav Petkovd236d742008-04-18 00:46:27 +0200732 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100735 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 pc = idetape_next_pc_storage(drive);
737 rq = idetape_next_rq_storage(drive);
738 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200739 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100744 * Postpone the current request so that ide.c will be able to service requests
745 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100747static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 idetape_tape_t *tape = drive->driver_data;
750
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100751 debug_log(DBG_PROCS, "Enter %s\n", __func__);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100754 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200757static void ide_tape_handle_dsc(ide_drive_t *drive)
758{
759 idetape_tape_t *tape = drive->driver_data;
760
761 /* Media access command */
762 tape->dsc_polling_start = jiffies;
763 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
764 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
765 /* Allow ide.c to handle other requests */
766 idetape_postpone_request(drive);
767}
768
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200769static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
770 unsigned int bcount, int write)
771{
772 if (write)
773 idetape_output_buffers(drive, pc, bcount);
774 else
775 idetape_input_buffers(drive, pc, bcount);
776}
Borislav Petkova1efc852008-02-06 02:57:52 +0100777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100779 * This is the usual interrupt handler which will be called during a packet
780 * command. We will transfer some of the data (as requested by the drive) and
781 * will re-point interrupt handler to us. When data transfer is finished, we
782 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100783 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100785static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200789 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
790 NULL, idetape_update_buffers, idetape_retry_pc,
791 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100795 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100797 * The current Packet Command is available in tape->pc, and will not change
798 * until we finish handling it. Each packet command is associated with a
799 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100801 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100803 * 1. idetape_issue_pc will send the packet command to the drive, and will set
804 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100806 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
807 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100809 * 3. ATAPI Tape media access commands have immediate status with a delayed
810 * process. In case of a successful initiation of a media access packet command,
811 * the DSC bit will be set when the actual execution of the command is finished.
812 * Since the tape drive will not issue an interrupt, we have to poll for this
813 * event. In this case, we define the request as "low priority request" by
814 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
815 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100817 * ide.c will then give higher priority to requests which originate from the
818 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100820 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100822 * 5. In case an error was found, we queue a request sense packet command in
823 * front of the request queue and retry the operation up to
824 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100826 * 6. In case no error was found, or we decided to give up and not to retry
827 * again, the callback function will be called and then we will handle the next
828 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
830static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200834 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
835 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Borislav Petkovd236d742008-04-18 00:46:27 +0200838static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
839 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Borislav Petkov90699ce2008-02-02 19:56:50 +0100843 if (tape->pc->c[0] == REQUEST_SENSE &&
844 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
846 "Two request sense in serial were issued\n");
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Borislav Petkov90699ce2008-02-02 19:56:50 +0100849 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 tape->failed_pc = pc;
851 /* Set the current packet command */
852 tape->pc = pc;
853
854 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200855 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100857 * We will "abort" retrying a packet command in case legitimate
858 * error code was received (crossing a filemark, or end of the
859 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200861 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100862 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 tape->sense_key == 2 && tape->asc == 4 &&
864 (tape->ascq == 1 || tape->ascq == 8))) {
865 printk(KERN_ERR "ide-tape: %s: I/O error, "
866 "pc = %2x, key = %2x, "
867 "asc = %2x, ascq = %2x\n",
868 tape->name, pc->c[0],
869 tape->sense_key, tape->asc,
870 tape->ascq);
871 }
872 /* Giving up */
873 pc->error = IDETAPE_ERROR_GENERAL;
874 }
875 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200876 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200877 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100879 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200883 return ide_issue_pc(drive, pc, idetape_transfer_pc,
884 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100887/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200888static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100891 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100893 /* DBD = 1 - Don't return block descriptors */
894 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 pc->c[2] = page_code;
896 /*
897 * Changed pc->c[3] to 0 (255 will at best return unused info).
898 *
899 * For SCSI this byte is defined as subpage instead of high byte
900 * of length and some IDE drives seem to interpret it this way
901 * and return an error when 255 is used.
902 */
903 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100904 /* We will just discard data in that case */
905 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200907 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200909 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200911 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100914static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200916 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200918 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100919 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200921 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100922
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200923 if (stat & ATA_DSC) {
924 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100926 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 printk(KERN_ERR "ide-tape: %s: I/O error, ",
928 tape->name);
929 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200930 idetape_retry_pc(drive);
931 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 } else {
935 pc->error = IDETAPE_ERROR_GENERAL;
936 tape->failed_pc = NULL;
937 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200938 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return ide_stopped;
940}
941
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200942static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200943 struct ide_atapi_pc *pc, struct request *rq,
944 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Borislav Petkov0014c752008-07-23 19:56:00 +0200946 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
947 unsigned int length = rq->current_nr_sectors;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100950 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200953 pc->buf = NULL;
954 pc->buf_size = length * tape->blk_size;
955 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200956 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200957 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200959 if (opcode == READ_6) {
960 pc->c[0] = READ_6;
961 atomic_set(&bh->b_count, 0);
962 } else if (opcode == WRITE_6) {
963 pc->c[0] = WRITE_6;
964 pc->flags |= PC_FLAG_WRITING;
965 pc->b_data = bh->b_data;
966 pc->b_count = atomic_read(&bh->b_count);
967 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200968
969 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static ide_startstop_t idetape_do_request(ide_drive_t *drive,
973 struct request *rq, sector_t block)
974{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200975 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200977 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100979 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Mark de Wever730616b2008-10-10 22:39:17 +0200981 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
982 " current_nr_sectors: %u\n",
983 (unsigned long long)rq->sector, rq->nr_sectors,
984 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jens Axboe4aff5e22006-08-10 08:44:47 +0200986 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100987 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200989 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ide_end_request(drive, 0, 0);
991 return ide_stopped;
992 }
993
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100994 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200995 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
996 pc = tape->failed_pc;
997 goto out;
998 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (postponed_rq != NULL)
1001 if (rq != postponed_rq) {
1002 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1003 "Two DSC requests were queued\n");
1004 idetape_end_request(drive, 0, 0);
1005 return ide_stopped;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 tape->postponed_rq = NULL;
1009
1010 /*
1011 * If the tape is still busy, postpone our request and service
1012 * the other device meanwhile.
1013 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001014 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Borislav Petkov83dd5732008-07-23 19:56:00 +02001016 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001017 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001020 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 drive->post_reset = 0;
1022 }
1023
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001024 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +02001025 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (postponed_rq == NULL) {
1027 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001028 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1030 } else if (time_after(jiffies, tape->dsc_timeout)) {
1031 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1032 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +02001033 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 idetape_media_access_finished(drive);
1035 return ide_stopped;
1036 } else {
1037 return ide_do_reset(drive);
1038 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001039 } else if (time_after(jiffies,
1040 tape->dsc_polling_start +
1041 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001042 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 idetape_postpone_request(drive);
1044 return ide_stopped;
1045 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001046 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001048 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 goto out;
1050 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001051 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001053 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 goto out;
1055 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001056 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001057 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001058 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1059 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto out;
1061 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001062 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 idetape_media_access_finished(drive);
1064 return ide_stopped;
1065 }
1066 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001067
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001068out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001069 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001073 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001074 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001075 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001077 * It returns a pointer to the newly allocated buffer, or NULL in case of
1078 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001080static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1081 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001083 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001084 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001085 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 char *b_data = NULL;
1087
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001088 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1089 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (bh == NULL)
1091 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001092
1093 order = fls(pages) - 1;
1094 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001095 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001097 b_allocd = (1 << order) * PAGE_SIZE;
1098 pages &= (order-1);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001101 memset(bh->b_data, 0, b_allocd);
1102 bh->b_reqnext = NULL;
1103 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1105
Borislav Petkov41aa1702008-04-27 15:38:32 +02001106 while (pages) {
1107 order = fls(pages) - 1;
1108 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001109 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001111 b_allocd = (1 << order) * PAGE_SIZE;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001114 memset(b_data, 0, b_allocd);
1115
1116 /* newly allocated page frames below buffer header or ...*/
1117 if (bh->b_data == b_data + b_allocd) {
1118 bh->b_size += b_allocd;
1119 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001121 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 continue;
1123 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001124 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001126 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001128 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 continue;
1130 }
1131 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001132 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1133 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001134 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto abort;
1136 }
1137 bh->b_reqnext = NULL;
1138 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001139 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1141 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001142
1143 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 bh->b_size -= tape->excess_bh_size;
1147 if (full)
1148 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001149 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001151 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return NULL;
1153}
1154
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001155static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001156 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct idetape_bh *bh = tape->bh;
1159 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001160 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001164 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1165 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001166 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001168 count = min((unsigned int)
1169 (bh->b_size - atomic_read(&bh->b_count)),
1170 (unsigned int)n);
1171 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1172 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001173 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 n -= count;
1175 atomic_add(count, &bh->b_count);
1176 buf += count;
1177 if (atomic_read(&bh->b_count) == bh->b_size) {
1178 bh = bh->b_reqnext;
1179 if (bh)
1180 atomic_set(&bh->b_count, 0);
1181 }
1182 }
1183 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001184 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001187static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001188 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct idetape_bh *bh = tape->bh;
1191 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001192 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001196 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1197 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001198 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001201 if (copy_to_user(buf, tape->b_data, count))
1202 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 n -= count;
1204 tape->b_data += count;
1205 tape->b_count -= count;
1206 buf += count;
1207 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001208 bh = bh->b_reqnext;
1209 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (bh) {
1211 tape->b_data = bh->b_data;
1212 tape->b_count = atomic_read(&bh->b_count);
1213 }
1214 }
1215 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001216 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001219static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001221 struct idetape_bh *bh = tape->merge_bh;
1222 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001223
Borislav Petkov54abf372008-02-06 02:57:52 +01001224 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 atomic_set(&bh->b_count, 0);
1226 else {
1227 tape->b_data = bh->b_data;
1228 tape->b_count = atomic_read(&bh->b_count);
1229 }
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001233 * Write a filemark if write_filemark=1. Flush the device buffers without
1234 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001236static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001237 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001240 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001242 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Borislav Petkovd236d742008-04-18 00:46:27 +02001245static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001248 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001252 * We add a special packet command request to the tail of the request queue, and
1253 * wait for it to be serviced. This is not to be called from within the request
1254 * handling part of the driver! We allocate here data on the stack and it is
1255 * valid until the request is finished. This is not the case for the bottom part
1256 * of the driver, where we are always leaving the functions to wait for an
1257 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001259 * From the bottom part of the driver, we should allocate safe memory using
1260 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1261 * to the request list without waiting for it to be serviced! In that case, we
1262 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001264static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001267 struct request *rq;
1268 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001270 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1271 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001272 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001273 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001274 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001275 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1276 blk_put_request(rq);
1277 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Borislav Petkovd236d742008-04-18 00:46:27 +02001280static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1281 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001284 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001286 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1290{
1291 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001292 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int load_attempted = 0;
1294
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001295 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001296 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 timeout += jiffies;
1298 while (time_before(jiffies, timeout)) {
1299 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001300 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
1302 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001303 || (tape->asc == 0x3A)) {
1304 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (load_attempted)
1306 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001307 idetape_create_load_unload_cmd(drive, &pc,
1308 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001309 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 load_attempted = 1;
1311 /* not about to be ready */
1312 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1313 (tape->ascq == 1 || tape->ascq == 8)))
1314 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001315 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 return -EIO;
1318}
1319
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001320static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Borislav Petkovd236d742008-04-18 00:46:27 +02001322 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int rc;
1324
1325 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001326 rc = idetape_queue_pc_tail(drive, &pc);
1327 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return rc;
1329 idetape_wait_ready(drive, 60 * 5 * HZ);
1330 return 0;
1331}
1332
Borislav Petkovd236d742008-04-18 00:46:27 +02001333static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001336 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001337 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001340static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001343 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int position;
1345
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001346 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 idetape_create_read_position_cmd(&pc);
1349 if (idetape_queue_pc_tail(drive, &pc))
1350 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001351 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return position;
1353}
1354
Borislav Petkovd236d742008-04-18 00:46:27 +02001355static void idetape_create_locate_cmd(ide_drive_t *drive,
1356 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001357 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001360 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001362 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001364 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Borislav Petkovd236d742008-04-18 00:46:27 +02001367static int idetape_create_prevent_cmd(ide_drive_t *drive,
1368 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 idetape_tape_t *tape = drive->driver_data;
1371
Borislav Petkovb6422012008-02-02 19:56:49 +01001372 /* device supports locking according to capabilities page */
1373 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375
1376 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001377 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return 1;
1380}
1381
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001382static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Borislav Petkov54abf372008-02-06 02:57:52 +01001386 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001387 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001389 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001390 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001391 if (tape->merge_bh != NULL) {
1392 ide_tape_kfree_buffer(tape);
1393 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
Borislav Petkov54abf372008-02-06 02:57:52 +01001396 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001400 * Position the tape to the requested block using the LOCATE packet command.
1401 * A READ POSITION command is then issued to check where we are positioned. Like
1402 * all higher level operations, we queue the commands at the tail of the request
1403 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001405static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1406 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 idetape_tape_t *tape = drive->driver_data;
1409 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001410 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Borislav Petkov54abf372008-02-06 02:57:52 +01001412 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001413 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 idetape_wait_ready(drive, 60 * 5 * HZ);
1415 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1416 retval = idetape_queue_pc_tail(drive, &pc);
1417 if (retval)
1418 return (retval);
1419
1420 idetape_create_read_position_cmd(&pc);
1421 return (idetape_queue_pc_tail(drive, &pc));
1422}
1423
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001424static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001425 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int seek, position;
1429
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001430 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (restore_position) {
1432 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001433 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001435 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001436 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return;
1438 }
1439 }
1440}
1441
1442/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001443 * Generate a read/write request for the block device interface and wait for it
1444 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001446static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1447 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001450 struct request *rq;
1451 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001453 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1454
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001455 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1456 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001457 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001458 rq->rq_disk = tape->disk;
1459 rq->special = (void *)bh;
1460 rq->sector = tape->first_frame;
1461 rq->nr_sectors = blocks;
1462 rq->current_nr_sectors = blocks;
1463 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1464
1465 errors = rq->errors;
1466 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1467 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1470 return 0;
1471
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001472 if (tape->merge_bh)
1473 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001474 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Borislav Petkovd236d742008-04-18 00:46:27 +02001479static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001482 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001483 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001484 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Borislav Petkovd236d742008-04-18 00:46:27 +02001487static void idetape_create_rewind_cmd(ide_drive_t *drive,
1488 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001491 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001492 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Borislav Petkovd236d742008-04-18 00:46:27 +02001495static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001498 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001500 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Borislav Petkovd236d742008-04-18 00:46:27 +02001503static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001506 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001507 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001509 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Borislav Petkov97c566c2008-04-27 15:38:25 +02001512/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001513static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001517 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001519 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001520 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Borislav Petkovd9df9372008-04-27 15:38:34 +02001523static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 idetape_tape_t *tape = drive->driver_data;
1526 int blocks, min;
1527 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001528
Borislav Petkov54abf372008-02-06 02:57:52 +01001529 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001530 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001531 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return;
1533 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001534 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001536 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001538 if (tape->merge_bh_size) {
1539 blocks = tape->merge_bh_size / tape->blk_size;
1540 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 unsigned int i;
1542
1543 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001544 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001545 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 bh = tape->bh->b_reqnext;
1547 while (bh) {
1548 atomic_set(&bh->b_count, 0);
1549 bh = bh->b_reqnext;
1550 }
1551 bh = tape->bh;
1552 while (i) {
1553 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001554 printk(KERN_INFO "ide-tape: bug,"
1555 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 break;
1557 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001558 min = min(i, (unsigned int)(bh->b_size -
1559 atomic_read(&bh->b_count)));
1560 memset(bh->b_data + atomic_read(&bh->b_count),
1561 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 atomic_add(min, &bh->b_count);
1563 i -= min;
1564 bh = bh->b_reqnext;
1565 }
1566 }
1567 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001568 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001570 if (tape->merge_bh != NULL) {
1571 ide_tape_kfree_buffer(tape);
1572 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001574 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Borislav Petkov83042b22008-04-27 15:38:27 +02001577static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001583 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1584 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001585 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 idetape_flush_tape_buffers(drive);
1587 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001588 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001589 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001590 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001591 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001593 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1594 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001596 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001599 * Issue a read 0 command to ensure that DSC handshake is
1600 * switched from completion mode to buffer available mode.
1601 * No point in issuing this if DSC overlap isn't supported, some
1602 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 */
1604 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001605 bytes_read = idetape_queue_rw_tail(drive,
1606 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001607 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001609 ide_tape_kfree_buffer(tape);
1610 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001611 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return bytes_read;
1613 }
1614 }
1615 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
1618}
1619
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001620/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001625 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001627 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001628 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return 0;
1630
Borislav Petkov83042b22008-04-27 15:38:27 +02001631 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001633 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001634 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001637static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 idetape_tape_t *tape = drive->driver_data;
1640 struct idetape_bh *bh;
1641 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 while (bcount) {
1644 unsigned int count;
1645
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001646 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001647 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001649 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001651 atomic_set(&bh->b_count,
1652 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1654 count -= atomic_read(&bh->b_count);
1655 bh = bh->b_reqnext;
1656 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001657 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001658 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001663 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1664 * currently support only one partition.
1665 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001666static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001669 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001670 idetape_tape_t *tape;
1671 tape = drive->driver_data;
1672
1673 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 idetape_create_rewind_cmd(drive, &pc);
1676 retval = idetape_queue_pc_tail(drive, &pc);
1677 if (retval)
1678 return retval;
1679
1680 idetape_create_read_position_cmd(&pc);
1681 retval = idetape_queue_pc_tail(drive, &pc);
1682 if (retval)
1683 return retval;
1684 return 0;
1685}
1686
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001687/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001688static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1689 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 void __user *argp = (void __user *)arg;
1693
Borislav Petkovd59823f2008-02-02 19:56:51 +01001694 struct idetape_config {
1695 int dsc_rw_frequency;
1696 int dsc_media_access_frequency;
1697 int nr_stages;
1698 } config;
1699
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001700 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001703 case 0x0340:
1704 if (copy_from_user(&config, argp, sizeof(config)))
1705 return -EFAULT;
1706 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001707 break;
1708 case 0x0350:
1709 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001710 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001711 if (copy_to_user(argp, &config, sizeof(config)))
1712 return -EFAULT;
1713 break;
1714 default:
1715 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 return 0;
1718}
1719
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001720static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1721 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001724 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001725 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001726 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 if (mt_count == 0)
1729 return 0;
1730 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001731 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001733 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
Borislav Petkov54abf372008-02-06 02:57:52 +01001736 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001737 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001738 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001740 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001744 case MTFSF:
1745 case MTBSF:
1746 idetape_create_space_cmd(&pc, mt_count - count,
1747 IDETAPE_SPACE_OVER_FILEMARK);
1748 return idetape_queue_pc_tail(drive, &pc);
1749 case MTFSFM:
1750 case MTBSFM:
1751 if (!sprev)
1752 return -EIO;
1753 retval = idetape_space_over_filemarks(drive, MTFSF,
1754 mt_count - count);
1755 if (retval)
1756 return retval;
1757 count = (MTBSFM == mt_op ? 1 : -1);
1758 return idetape_space_over_filemarks(drive, MTFSF, count);
1759 default:
1760 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1761 mt_op);
1762 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001767 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001769 * The tape is optimized to maximize throughput when it is transferring an
1770 * integral number of the "continuous transfer limit", which is a parameter of
1771 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001773 * As of version 1.3 of the driver, the character device provides an abstract
1774 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1775 * same backup/restore procedure is supported. The driver will internally
1776 * convert the requests to the recommended transfer unit, so that an unmatch
1777 * between the user's block size to the recommended size will only result in a
1778 * (slightly) increased driver overhead, but will no longer hit performance.
1779 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001781static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1782 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct ide_tape_obj *tape = ide_tape_f(file);
1785 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001786 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001787 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001788 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001790 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Borislav Petkov54abf372008-02-06 02:57:52 +01001792 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001793 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001794 if (count > tape->blk_size &&
1795 (count % tape->blk_size) == 0)
1796 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001798 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001799 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return rc;
1801 if (count == 0)
1802 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001803 if (tape->merge_bh_size) {
1804 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001805 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001806 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001807 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001809 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 count -= actually_read;
1811 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001812 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001813 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (bytes_read <= 0)
1815 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001816 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001817 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 buf += bytes_read;
1819 count -= bytes_read;
1820 actually_read += bytes_read;
1821 }
1822 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001823 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (bytes_read <= 0)
1825 goto finish;
1826 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001827 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001828 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001830 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001833 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001834 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 idetape_space_over_filemarks(drive, MTFSF, 1);
1837 return 0;
1838 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001839
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001840 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001843static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 size_t count, loff_t *ppos)
1845{
1846 struct ide_tape_obj *tape = ide_tape_f(file);
1847 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001848 ssize_t actually_written = 0;
1849 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001850 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* The drive is write protected. */
1853 if (tape->write_prot)
1854 return -EACCES;
1855
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001856 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001859 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1860 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001861 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001862 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001863 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001865 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001867 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1868 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001870 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001871 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001874 * Issue a write 0 command to ensure that DSC handshake is
1875 * switched from completion mode to buffer available mode. No
1876 * point in issuing this if DSC overlap isn't supported, some
1877 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
1879 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001880 ssize_t retval = idetape_queue_rw_tail(drive,
1881 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001882 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001884 ide_tape_kfree_buffer(tape);
1885 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001886 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return retval;
1888 }
1889 }
1890 }
1891 if (count == 0)
1892 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001893 if (tape->merge_bh_size) {
1894 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001895 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001896 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001898 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001899 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001900 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001901 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001902 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001904 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 count -= actually_written;
1906
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001907 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001908 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001909 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001910 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (retval <= 0)
1912 return (retval);
1913 }
1914 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001915 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001916 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001917 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001918 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001919 buf += tape->buffer_size;
1920 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001921 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001922 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (retval <= 0)
1924 return (retval);
1925 }
1926 if (count) {
1927 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001928 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001929 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001930 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001932 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001935static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Borislav Petkovd236d742008-04-18 00:46:27 +02001937 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* Write a filemark */
1940 idetape_create_write_filemark_cmd(drive, &pc, 1);
1941 if (idetape_queue_pc_tail(drive, &pc)) {
1942 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1943 return -EIO;
1944 }
1945 return 0;
1946}
1947
1948/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001949 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1950 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001952 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1953 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001954 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001956 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001958 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1959 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001961static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001964 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001965 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001967 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1968 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001971 case MTFSF:
1972 case MTFSFM:
1973 case MTBSF:
1974 case MTBSFM:
1975 if (!mt_count)
1976 return 0;
1977 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1978 default:
1979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001983 case MTWEOF:
1984 if (tape->write_prot)
1985 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001986 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001987 for (i = 0; i < mt_count; i++) {
1988 retval = idetape_write_filemark(drive);
1989 if (retval)
1990 return retval;
1991 }
1992 return 0;
1993 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001994 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001995 if (idetape_rewind_tape(drive))
1996 return -EIO;
1997 return 0;
1998 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001999 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002000 idetape_create_load_unload_cmd(drive, &pc,
2001 IDETAPE_LU_LOAD_MASK);
2002 return idetape_queue_pc_tail(drive, &pc);
2003 case MTUNLOAD:
2004 case MTOFFL:
2005 /*
2006 * If door is locked, attempt to unlock before
2007 * attempting to eject.
2008 */
2009 if (tape->door_locked) {
2010 if (idetape_create_prevent_cmd(drive, &pc, 0))
2011 if (!idetape_queue_pc_tail(drive, &pc))
2012 tape->door_locked = DOOR_UNLOCKED;
2013 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002014 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002015 idetape_create_load_unload_cmd(drive, &pc,
2016 !IDETAPE_LU_LOAD_MASK);
2017 retval = idetape_queue_pc_tail(drive, &pc);
2018 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002019 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002020 return retval;
2021 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002022 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002023 return idetape_flush_tape_buffers(drive);
2024 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002025 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002026 idetape_create_load_unload_cmd(drive, &pc,
2027 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2028 return idetape_queue_pc_tail(drive, &pc);
2029 case MTEOM:
2030 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2031 return idetape_queue_pc_tail(drive, &pc);
2032 case MTERASE:
2033 (void)idetape_rewind_tape(drive);
2034 idetape_create_erase_cmd(&pc);
2035 return idetape_queue_pc_tail(drive, &pc);
2036 case MTSETBLK:
2037 if (mt_count) {
2038 if (mt_count < tape->blk_size ||
2039 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002041 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002042 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002043 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002044 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002045 return 0;
2046 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002047 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002048 return idetape_position_tape(drive,
2049 mt_count * tape->user_bs_factor, tape->partition, 0);
2050 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002051 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002052 return idetape_position_tape(drive, 0, mt_count, 0);
2053 case MTFSR:
2054 case MTBSR:
2055 case MTLOCK:
2056 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002058 retval = idetape_queue_pc_tail(drive, &pc);
2059 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002061 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2062 return 0;
2063 case MTUNLOCK:
2064 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002066 retval = idetape_queue_pc_tail(drive, &pc);
2067 if (retval)
2068 return retval;
2069 tape->door_locked = DOOR_UNLOCKED;
2070 return 0;
2071 default:
2072 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2073 mt_op);
2074 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076}
2077
2078/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002079 * Our character device ioctls. General mtio.h magnetic io commands are
2080 * supported here, and not in the corresponding block interface. Our own
2081 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002083static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2084 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 struct ide_tape_obj *tape = ide_tape_f(file);
2087 ide_drive_t *drive = tape->drive;
2088 struct mtop mtop;
2089 struct mtget mtget;
2090 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002091 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 void __user *argp = (void __user *)arg;
2093
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002094 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Borislav Petkov54abf372008-02-06 02:57:52 +01002096 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002097 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 idetape_flush_tape_buffers(drive);
2099 }
2100 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002101 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002102 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002103 position = idetape_read_position(drive);
2104 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return -EIO;
2106 }
2107 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002108 case MTIOCTOP:
2109 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2110 return -EFAULT;
2111 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2112 case MTIOCGET:
2113 memset(&mtget, 0, sizeof(struct mtget));
2114 mtget.mt_type = MT_ISSCSI2;
2115 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2116 mtget.mt_dsreg =
2117 ((tape->blk_size * tape->user_bs_factor)
2118 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002119
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002120 if (tape->drv_write_prot)
2121 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2122
2123 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2124 return -EFAULT;
2125 return 0;
2126 case MTIOCPOS:
2127 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2128 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2129 return -EFAULT;
2130 return 0;
2131 default:
2132 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002133 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002134 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136}
2137
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002138/*
2139 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2140 * block size with the reported value.
2141 */
2142static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2143{
2144 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002145 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002146
2147 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2148 if (idetape_queue_pc_tail(drive, &pc)) {
2149 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002150 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002151 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2152 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002153 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002154 }
2155 return;
2156 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002157 tape->blk_size = (pc.buf[4 + 5] << 16) +
2158 (pc.buf[4 + 6] << 8) +
2159 pc.buf[4 + 7];
2160 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002163static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2166 ide_drive_t *drive;
2167 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002168 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int retval;
2170
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002171 if (i >= MAX_HWIFS * MAX_DRIVES)
2172 return -ENXIO;
2173
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002174 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002175 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002176 if (!tape) {
2177 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002178 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002179 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002180
2181 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 /*
2184 * We really want to do nonseekable_open(inode, filp); here, but some
2185 * versions of tar incorrectly call lseek on tapes and bail out if that
2186 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2187 */
2188 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 drive = tape->drive;
2191
2192 filp->private_data = tape;
2193
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002194 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 retval = -EBUSY;
2196 goto out_put_tape;
2197 }
2198
2199 retval = idetape_wait_ready(drive, 60 * HZ);
2200 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002201 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2203 goto out_put_tape;
2204 }
2205
2206 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002207 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 (void)idetape_rewind_tape(drive);
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002211 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* Set write protect flag if device is opened as read-only. */
2214 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2215 tape->write_prot = 1;
2216 else
2217 tape->write_prot = tape->drv_write_prot;
2218
2219 /* Make sure drive isn't write protected if user wants to write. */
2220 if (tape->write_prot) {
2221 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2222 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002223 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 retval = -EROFS;
2225 goto out_put_tape;
2226 }
2227 }
2228
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002229 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002230 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2232 if (!idetape_queue_pc_tail(drive, &pc)) {
2233 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2234 tape->door_locked = DOOR_LOCKED;
2235 }
2236 }
2237 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002238 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return 0;
2240
2241out_put_tape:
2242 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002243 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return retval;
2245}
2246
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002247static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 idetape_tape_t *tape = drive->driver_data;
2250
Borislav Petkovd9df9372008-04-27 15:38:34 +02002251 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002252 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2253 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002254 idetape_pad_zeros(drive, tape->blk_size *
2255 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002256 ide_tape_kfree_buffer(tape);
2257 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 idetape_write_filemark(drive);
2260 idetape_flush_tape_buffers(drive);
2261 idetape_flush_tape_buffers(drive);
2262}
2263
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002264static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 struct ide_tape_obj *tape = ide_tape_f(filp);
2267 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002268 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 unsigned int minor = iminor(inode);
2270
2271 lock_kernel();
2272 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002273
2274 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Borislav Petkov54abf372008-02-06 02:57:52 +01002276 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002278 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002280 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002282
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002283 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002285 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (tape->door_locked == DOOR_LOCKED) {
2287 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2288 if (!idetape_queue_pc_tail(drive, &pc))
2289 tape->door_locked = DOOR_UNLOCKED;
2290 }
2291 }
2292 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002293 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 ide_tape_put(tape);
2295 unlock_kernel();
2296 return 0;
2297}
2298
2299/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002300 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002302 * 1 - If the tape can be supported by us, based on the information we have so
2303 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002305 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002307static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Borislav Petkov71071b82008-02-06 02:57:53 +01002309 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 if (drive->id_read == 0)
2312 return 1;
2313
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002314 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002315
2316 protocol = (gcw[1] & 0xC0) >> 6;
2317 device_type = gcw[1] & 0x1F;
2318 removable = !!(gcw[0] & 0x80);
2319 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002322 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002323 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002324 protocol);
2325 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002326 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002327 "to tape\n", device_type);
2328 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002330 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002331 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2332 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 } else
2334 return 1;
2335 return 0;
2336}
2337
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002338static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002341 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002342 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 idetape_create_inquiry_cmd(&pc);
2345 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002346 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2347 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return;
2349 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002350 memcpy(vendor_id, &pc.buf[8], 8);
2351 memcpy(product_id, &pc.buf[16], 16);
2352 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002353
Borislav Petkov801bd322008-09-27 19:32:17 +02002354 ide_fixstring(vendor_id, 8, 0);
2355 ide_fixstring(product_id, 16, 0);
2356 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002357
Borislav Petkov801bd322008-09-27 19:32:17 +02002358 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002359 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
2362/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002363 * Ask the tape about its various parameters. In particular, we will adjust our
2364 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002366static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
2368 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002369 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002370 u8 *caps;
2371 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2374 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002375 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2376 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002377 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002378 put_unaligned(52, (u16 *)&tape->caps[12]);
2379 put_unaligned(540, (u16 *)&tape->caps[14]);
2380 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return;
2382 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002383 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Borislav Petkovb6422012008-02-02 19:56:49 +01002385 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002386 speed = be16_to_cpup((__be16 *)&caps[14]);
2387 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002389 *(u16 *)&caps[8] = max_speed;
2390 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2391 *(u16 *)&caps[14] = speed;
2392 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002393
2394 if (!speed) {
2395 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2396 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002397 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002399 if (!max_speed) {
2400 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2401 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002402 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
Borislav Petkovb6422012008-02-02 19:56:49 +01002405 memcpy(&tape->caps, caps, 20);
2406 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002407 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002408 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002409 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002412#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002413#define ide_tape_devset_get(name, field) \
2414static int get_##name(ide_drive_t *drive) \
2415{ \
2416 idetape_tape_t *tape = drive->driver_data; \
2417 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002419
2420#define ide_tape_devset_set(name, field) \
2421static int set_##name(ide_drive_t *drive, int arg) \
2422{ \
2423 idetape_tape_t *tape = drive->driver_data; \
2424 tape->field = arg; \
2425 return 0; \
2426}
2427
2428#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2429ide_tape_devset_get(_name, _field) \
2430ide_tape_devset_set(_name, _field) \
2431__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2432
2433#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2434ide_tape_devset_get(_name, _field) \
2435__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2436
2437static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2438static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2439static int divf_buffer(ide_drive_t *drive) { return 2; }
2440static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2441
2442ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2443
2444ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2445ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2446 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2447
2448ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2449ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2450ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2451ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2452
2453static const struct ide_devset *idetape_settings[] = {
2454 &ide_devset_avg_speed,
2455 &ide_devset_buffer,
2456 &ide_devset_buffer_size,
2457 &ide_devset_debug_mask,
2458 &ide_devset_dsc_overlap,
2459 &ide_devset_speed,
2460 &ide_devset_tdsc,
2461 NULL
2462};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002463#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002466 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002468 * 1. Initialize our various state variables.
2469 * 2. Ask the tape for its capabilities.
2470 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2471 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002473 * Note that at this point ide.c already assigned us an irq, so that we can
2474 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002476static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Borislav Petkov83042b22008-04-27 15:38:27 +02002478 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002480 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002481 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002482 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Borislav Petkov776bb022008-07-23 19:55:59 +02002484 drive->pc_callback = ide_tape_callback;
2485
Borislav Petkov54bb2072008-02-06 02:57:52 +01002486 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002488 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2489 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2490 tape->name);
2491 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002494 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 drive->dsc_overlap = 0;
2496 tape->minor = minor;
2497 tape->name[0] = 'h';
2498 tape->name[1] = 't';
2499 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002500 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 tape->pc = tape->pc_stack;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002502
2503 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002504
2505 /* Command packet DRQ type */
2506 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002507 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 idetape_get_inquiry_results(drive);
2510 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002511 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002513 tape->buffer_size = *ctl * tape->blk_size;
2514 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002516 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002517 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002519 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002520 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002521 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002522 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002523 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525
Borislav Petkov83042b22008-04-27 15:38:27 +02002526 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002527 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Borislav Petkovf73850a2008-04-27 15:38:33 +02002529 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002532 * Ensure that the number we got makes sense; limit it within
2533 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002535 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2536 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002538 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002539 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002540 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2541 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002542 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 drive->using_dma ? ", DMA":"");
2544
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002545 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
Russell King4031bbe2006-01-06 11:41:00 +00002548static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002552 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 ide_unregister_region(tape->disk);
2555
2556 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559static void ide_tape_release(struct kref *kref)
2560{
2561 struct ide_tape_obj *tape = to_ide_tape(kref);
2562 ide_drive_t *drive = tape->drive;
2563 struct gendisk *g = tape->disk;
2564
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002565 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 drive->dsc_overlap = 0;
2568 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002569 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002570 device_destroy(idetape_sysfs_class,
2571 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 idetape_devs[tape->minor] = NULL;
2573 g->private_data = NULL;
2574 put_disk(g);
2575 kfree(tape);
2576}
2577
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002578#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579static int proc_idetape_read_name
2580 (char *page, char **start, off_t off, int count, int *eof, void *data)
2581{
2582 ide_drive_t *drive = (ide_drive_t *) data;
2583 idetape_tape_t *tape = drive->driver_data;
2584 char *out = page;
2585 int len;
2586
2587 len = sprintf(out, "%s\n", tape->name);
2588 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2589}
2590
2591static ide_proc_entry_t idetape_proc[] = {
2592 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2593 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2594 { NULL, 0, NULL, NULL }
2595};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#endif
2597
Russell King4031bbe2006-01-06 11:41:00 +00002598static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002601 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002602 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002603 .name = "ide-tape",
2604 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002605 },
Russell King4031bbe2006-01-06 11:41:00 +00002606 .probe = ide_tape_probe,
2607 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 .version = IDETAPE_VERSION,
2609 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 .do_request = idetape_do_request,
2612 .end_request = idetape_end_request,
2613 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002614#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002616 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618};
2619
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002620/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002621static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 .owner = THIS_MODULE,
2623 .read = idetape_chrdev_read,
2624 .write = idetape_chrdev_write,
2625 .ioctl = idetape_chrdev_ioctl,
2626 .open = idetape_chrdev_open,
2627 .release = idetape_chrdev_release,
2628};
2629
2630static int idetape_open(struct inode *inode, struct file *filp)
2631{
2632 struct gendisk *disk = inode->i_bdev->bd_disk;
2633 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002635 tape = ide_tape_get(disk);
2636 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return -ENXIO;
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return 0;
2640}
2641
2642static int idetape_release(struct inode *inode, struct file *filp)
2643{
2644 struct gendisk *disk = inode->i_bdev->bd_disk;
2645 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 ide_tape_put(tape);
2648
2649 return 0;
2650}
2651
2652static int idetape_ioctl(struct inode *inode, struct file *file,
2653 unsigned int cmd, unsigned long arg)
2654{
2655 struct block_device *bdev = inode->i_bdev;
2656 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2657 ide_drive_t *drive = tape->drive;
2658 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2659 if (err == -EINVAL)
2660 err = idetape_blkdev_ioctl(drive, cmd, arg);
2661 return err;
2662}
2663
2664static struct block_device_operations idetape_block_ops = {
2665 .owner = THIS_MODULE,
2666 .open = idetape_open,
2667 .release = idetape_release,
2668 .ioctl = idetape_ioctl,
2669};
2670
Russell King4031bbe2006-01-06 11:41:00 +00002671static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 idetape_tape_t *tape;
2674 struct gendisk *g;
2675 int minor;
2676
2677 if (!strstr("ide-tape", drive->driver_req))
2678 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (drive->media != ide_tape)
2681 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002682
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002683 if (!idetape_identify_device(drive)) {
2684 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2685 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto failed;
2687 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002688 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002690 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2691 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 goto failed;
2693 }
2694
2695 g = alloc_disk(1 << PARTN_BITS);
2696 if (!g)
2697 goto out_free_tape;
2698
2699 ide_init_disk(g, drive);
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 kref_init(&tape->kref);
2702
2703 tape->drive = drive;
2704 tape->driver = &idetape_driver;
2705 tape->disk = g;
2706
2707 g->private_data = &tape->driver;
2708
2709 drive->driver_data = tape;
2710
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002711 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 for (minor = 0; idetape_devs[minor]; minor++)
2713 ;
2714 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002715 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 idetape_setup(drive, tape, minor);
2718
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002719 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2720 MKDEV(IDETAPE_MAJOR, minor), NULL,
2721 "%s", tape->name);
2722 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2723 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2724 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 g->fops = &idetape_block_ops;
2727 ide_register_region(g);
2728
2729 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731out_free_tape:
2732 kfree(tape);
2733failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002734 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002737static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002739 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002740 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 unregister_chrdev(IDETAPE_MAJOR, "ht");
2742}
2743
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002744static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Will Dysond5dee802005-09-16 02:55:07 -07002746 int error = 1;
2747 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2748 if (IS_ERR(idetape_sysfs_class)) {
2749 idetape_sysfs_class = NULL;
2750 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2751 error = -EBUSY;
2752 goto out;
2753 }
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002756 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2757 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002758 error = -EBUSY;
2759 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
Will Dysond5dee802005-09-16 02:55:07 -07002761
2762 error = driver_register(&idetape_driver.gen_driver);
2763 if (error)
2764 goto out_free_driver;
2765
2766 return 0;
2767
2768out_free_driver:
2769 driver_unregister(&idetape_driver.gen_driver);
2770out_free_class:
2771 class_destroy(idetape_sysfs_class);
2772out:
2773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Kay Sievers263756e2005-12-12 18:03:44 +01002776MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777module_init(idetape_init);
2778module_exit(idetape_exit);
2779MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002780MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2781MODULE_LICENSE("GPL");