blob: 1bce84b566304115c2834a1742f03284e2c25ce9 [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 Petkov01a63ae2008-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 Zolnierkiewicz74e63e72008-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 Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100923 if (stat & SEEK_STAT) {
924 if (stat & ERR_STAT) {
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
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100981 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
982 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jens Axboe4aff5e22006-08-10 08:44:47 +0200985 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100986 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200988 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ide_end_request(drive, 0, 0);
990 return ide_stopped;
991 }
992
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100993 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200994 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
995 pc = tape->failed_pc;
996 goto out;
997 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (postponed_rq != NULL)
1000 if (rq != postponed_rq) {
1001 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1002 "Two DSC requests were queued\n");
1003 idetape_end_request(drive, 0, 0);
1004 return ide_stopped;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 tape->postponed_rq = NULL;
1008
1009 /*
1010 * If the tape is still busy, postpone our request and service
1011 * the other device meanwhile.
1012 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001013 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Borislav Petkov83dd5732008-07-23 19:56:00 +02001015 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001016 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001019 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 drive->post_reset = 0;
1021 }
1022
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001023 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001024 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (postponed_rq == NULL) {
1026 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001027 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1029 } else if (time_after(jiffies, tape->dsc_timeout)) {
1030 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1031 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +02001032 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 idetape_media_access_finished(drive);
1034 return ide_stopped;
1035 } else {
1036 return ide_do_reset(drive);
1037 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001038 } else if (time_after(jiffies,
1039 tape->dsc_polling_start +
1040 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001041 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 idetape_postpone_request(drive);
1043 return ide_stopped;
1044 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001045 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001047 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 goto out;
1049 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001050 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001052 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto out;
1054 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001055 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001056 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001057 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1058 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto out;
1060 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001061 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 idetape_media_access_finished(drive);
1063 return ide_stopped;
1064 }
1065 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001066
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001067out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001068 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001072 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001073 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001074 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001076 * It returns a pointer to the newly allocated buffer, or NULL in case of
1077 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001079static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1080 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001082 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001083 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001084 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 char *b_data = NULL;
1086
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001087 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1088 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (bh == NULL)
1090 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001091
1092 order = fls(pages) - 1;
1093 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001094 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001096 b_allocd = (1 << order) * PAGE_SIZE;
1097 pages &= (order-1);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001100 memset(bh->b_data, 0, b_allocd);
1101 bh->b_reqnext = NULL;
1102 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1104
Borislav Petkov41aa1702008-04-27 15:38:32 +02001105 while (pages) {
1106 order = fls(pages) - 1;
1107 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001108 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001110 b_allocd = (1 << order) * PAGE_SIZE;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001113 memset(b_data, 0, b_allocd);
1114
1115 /* newly allocated page frames below buffer header or ...*/
1116 if (bh->b_data == b_data + b_allocd) {
1117 bh->b_size += b_allocd;
1118 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001120 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 continue;
1122 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001123 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001125 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001127 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 continue;
1129 }
1130 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001131 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1132 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001133 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto abort;
1135 }
1136 bh->b_reqnext = NULL;
1137 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001138 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1140 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001141
1142 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 bh->b_size -= tape->excess_bh_size;
1146 if (full)
1147 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001148 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001150 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return NULL;
1152}
1153
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001154static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001155 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct idetape_bh *bh = tape->bh;
1158 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001159 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001163 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1164 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001165 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001167 count = min((unsigned int)
1168 (bh->b_size - atomic_read(&bh->b_count)),
1169 (unsigned int)n);
1170 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1171 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001172 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 n -= count;
1174 atomic_add(count, &bh->b_count);
1175 buf += count;
1176 if (atomic_read(&bh->b_count) == bh->b_size) {
1177 bh = bh->b_reqnext;
1178 if (bh)
1179 atomic_set(&bh->b_count, 0);
1180 }
1181 }
1182 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001183 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001186static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001187 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct idetape_bh *bh = tape->bh;
1190 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001191 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001195 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1196 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001197 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001200 if (copy_to_user(buf, tape->b_data, count))
1201 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 n -= count;
1203 tape->b_data += count;
1204 tape->b_count -= count;
1205 buf += count;
1206 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001207 bh = bh->b_reqnext;
1208 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (bh) {
1210 tape->b_data = bh->b_data;
1211 tape->b_count = atomic_read(&bh->b_count);
1212 }
1213 }
1214 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001218static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001220 struct idetape_bh *bh = tape->merge_bh;
1221 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001222
Borislav Petkov54abf372008-02-06 02:57:52 +01001223 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 atomic_set(&bh->b_count, 0);
1225 else {
1226 tape->b_data = bh->b_data;
1227 tape->b_count = atomic_read(&bh->b_count);
1228 }
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001232 * Write a filemark if write_filemark=1. Flush the device buffers without
1233 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001235static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001236 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001239 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001241 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Borislav Petkovd236d742008-04-18 00:46:27 +02001244static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001247 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001251 * We add a special packet command request to the tail of the request queue, and
1252 * wait for it to be serviced. This is not to be called from within the request
1253 * handling part of the driver! We allocate here data on the stack and it is
1254 * valid until the request is finished. This is not the case for the bottom part
1255 * of the driver, where we are always leaving the functions to wait for an
1256 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001258 * From the bottom part of the driver, we should allocate safe memory using
1259 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1260 * to the request list without waiting for it to be serviced! In that case, we
1261 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001263static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001266 struct request *rq;
1267 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001269 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1270 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001271 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001272 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001273 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001274 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1275 blk_put_request(rq);
1276 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Borislav Petkovd236d742008-04-18 00:46:27 +02001279static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1280 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001283 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001285 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1289{
1290 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001291 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int load_attempted = 0;
1293
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001294 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001295 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 timeout += jiffies;
1297 while (time_before(jiffies, timeout)) {
1298 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001299 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
1301 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001302 || (tape->asc == 0x3A)) {
1303 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (load_attempted)
1305 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001306 idetape_create_load_unload_cmd(drive, &pc,
1307 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001308 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 load_attempted = 1;
1310 /* not about to be ready */
1311 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1312 (tape->ascq == 1 || tape->ascq == 8)))
1313 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001314 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 return -EIO;
1317}
1318
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001319static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Borislav Petkovd236d742008-04-18 00:46:27 +02001321 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int rc;
1323
1324 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001325 rc = idetape_queue_pc_tail(drive, &pc);
1326 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return rc;
1328 idetape_wait_ready(drive, 60 * 5 * HZ);
1329 return 0;
1330}
1331
Borislav Petkovd236d742008-04-18 00:46:27 +02001332static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001335 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001336 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001342 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int position;
1344
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001345 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 idetape_create_read_position_cmd(&pc);
1348 if (idetape_queue_pc_tail(drive, &pc))
1349 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001350 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return position;
1352}
1353
Borislav Petkovd236d742008-04-18 00:46:27 +02001354static void idetape_create_locate_cmd(ide_drive_t *drive,
1355 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001356 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001359 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001361 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001363 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Borislav Petkovd236d742008-04-18 00:46:27 +02001366static int idetape_create_prevent_cmd(ide_drive_t *drive,
1367 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 idetape_tape_t *tape = drive->driver_data;
1370
Borislav Petkovb6422012008-02-02 19:56:49 +01001371 /* device supports locking according to capabilities page */
1372 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374
1375 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001376 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 1;
1379}
1380
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001381static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Borislav Petkov54abf372008-02-06 02:57:52 +01001385 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001386 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001388 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001389 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001390 if (tape->merge_bh != NULL) {
1391 ide_tape_kfree_buffer(tape);
1392 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
Borislav Petkov54abf372008-02-06 02:57:52 +01001395 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001399 * Position the tape to the requested block using the LOCATE packet command.
1400 * A READ POSITION command is then issued to check where we are positioned. Like
1401 * all higher level operations, we queue the commands at the tail of the request
1402 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001404static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1405 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 idetape_tape_t *tape = drive->driver_data;
1408 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001409 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Borislav Petkov54abf372008-02-06 02:57:52 +01001411 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001412 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 idetape_wait_ready(drive, 60 * 5 * HZ);
1414 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1415 retval = idetape_queue_pc_tail(drive, &pc);
1416 if (retval)
1417 return (retval);
1418
1419 idetape_create_read_position_cmd(&pc);
1420 return (idetape_queue_pc_tail(drive, &pc));
1421}
1422
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001423static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001424 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int seek, position;
1428
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001429 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (restore_position) {
1431 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001432 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001434 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001435 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return;
1437 }
1438 }
1439}
1440
1441/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001442 * Generate a read/write request for the block device interface and wait for it
1443 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001445static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1446 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001449 struct request *rq;
1450 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001452 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1453
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001454 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1455 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001456 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001457 rq->rq_disk = tape->disk;
1458 rq->special = (void *)bh;
1459 rq->sector = tape->first_frame;
1460 rq->nr_sectors = blocks;
1461 rq->current_nr_sectors = blocks;
1462 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1463
1464 errors = rq->errors;
1465 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1466 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1469 return 0;
1470
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001471 if (tape->merge_bh)
1472 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001473 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Borislav Petkovd236d742008-04-18 00:46:27 +02001478static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001481 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001482 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001483 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Borislav Petkovd236d742008-04-18 00:46:27 +02001486static void idetape_create_rewind_cmd(ide_drive_t *drive,
1487 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001490 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001491 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Borislav Petkovd236d742008-04-18 00:46:27 +02001494static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001497 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001499 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Borislav Petkovd236d742008-04-18 00:46:27 +02001502static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001505 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001506 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001508 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Borislav Petkov97c566c2008-04-27 15:38:25 +02001511/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001512static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001516 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001518 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001519 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Borislav Petkovd9df9372008-04-27 15:38:34 +02001522static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 idetape_tape_t *tape = drive->driver_data;
1525 int blocks, min;
1526 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001527
Borislav Petkov54abf372008-02-06 02:57:52 +01001528 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001529 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001530 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return;
1532 }
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001533 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001535 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001537 if (tape->merge_bh_size) {
1538 blocks = tape->merge_bh_size / tape->blk_size;
1539 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 unsigned int i;
1541
1542 blocks++;
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001543 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001544 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 bh = tape->bh->b_reqnext;
1546 while (bh) {
1547 atomic_set(&bh->b_count, 0);
1548 bh = bh->b_reqnext;
1549 }
1550 bh = tape->bh;
1551 while (i) {
1552 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001553 printk(KERN_INFO "ide-tape: bug,"
1554 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001557 min = min(i, (unsigned int)(bh->b_size -
1558 atomic_read(&bh->b_count)));
1559 memset(bh->b_data + atomic_read(&bh->b_count),
1560 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 atomic_add(min, &bh->b_count);
1562 i -= min;
1563 bh = bh->b_reqnext;
1564 }
1565 }
1566 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001567 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001569 if (tape->merge_bh != NULL) {
1570 ide_tape_kfree_buffer(tape);
1571 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001573 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Borislav Petkov83042b22008-04-27 15:38:27 +02001576static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001582 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1583 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001584 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 idetape_flush_tape_buffers(drive);
1586 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001587 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001588 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001589 " 0 now\n");
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001590 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001592 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1593 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001595 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001598 * Issue a read 0 command to ensure that DSC handshake is
1599 * switched from completion mode to buffer available mode.
1600 * No point in issuing this if DSC overlap isn't supported, some
1601 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
1603 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001604 bytes_read = idetape_queue_rw_tail(drive,
1605 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001606 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001608 ide_tape_kfree_buffer(tape);
1609 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001610 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return bytes_read;
1612 }
1613 }
1614 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
1617}
1618
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001619/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001620static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001624 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001626 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001627 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629
Borislav Petkov83042b22008-04-27 15:38:27 +02001630 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001632 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001633 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001636static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 idetape_tape_t *tape = drive->driver_data;
1639 struct idetape_bh *bh;
1640 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 while (bcount) {
1643 unsigned int count;
1644
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001645 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001646 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001648 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001650 atomic_set(&bh->b_count,
1651 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1653 count -= atomic_read(&bh->b_count);
1654 bh = bh->b_reqnext;
1655 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001656 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001657 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659}
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001662 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1663 * currently support only one partition.
1664 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001665static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001668 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001669 idetape_tape_t *tape;
1670 tape = drive->driver_data;
1671
1672 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 idetape_create_rewind_cmd(drive, &pc);
1675 retval = idetape_queue_pc_tail(drive, &pc);
1676 if (retval)
1677 return retval;
1678
1679 idetape_create_read_position_cmd(&pc);
1680 retval = idetape_queue_pc_tail(drive, &pc);
1681 if (retval)
1682 return retval;
1683 return 0;
1684}
1685
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001686/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001687static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1688 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 void __user *argp = (void __user *)arg;
1692
Borislav Petkovd59823f2008-02-02 19:56:51 +01001693 struct idetape_config {
1694 int dsc_rw_frequency;
1695 int dsc_media_access_frequency;
1696 int nr_stages;
1697 } config;
1698
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001699 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001702 case 0x0340:
1703 if (copy_from_user(&config, argp, sizeof(config)))
1704 return -EFAULT;
1705 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001706 break;
1707 case 0x0350:
1708 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001709 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001710 if (copy_to_user(argp, &config, sizeof(config)))
1711 return -EFAULT;
1712 break;
1713 default:
1714 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 return 0;
1717}
1718
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001719static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1720 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001723 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001724 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001725 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 if (mt_count == 0)
1728 return 0;
1729 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001730 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001732 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734
Borislav Petkov54abf372008-02-06 02:57:52 +01001735 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001736 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001737 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001739 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001743 case MTFSF:
1744 case MTBSF:
1745 idetape_create_space_cmd(&pc, mt_count - count,
1746 IDETAPE_SPACE_OVER_FILEMARK);
1747 return idetape_queue_pc_tail(drive, &pc);
1748 case MTFSFM:
1749 case MTBSFM:
1750 if (!sprev)
1751 return -EIO;
1752 retval = idetape_space_over_filemarks(drive, MTFSF,
1753 mt_count - count);
1754 if (retval)
1755 return retval;
1756 count = (MTBSFM == mt_op ? 1 : -1);
1757 return idetape_space_over_filemarks(drive, MTFSF, count);
1758 default:
1759 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1760 mt_op);
1761 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001766 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001768 * The tape is optimized to maximize throughput when it is transferring an
1769 * integral number of the "continuous transfer limit", which is a parameter of
1770 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001772 * As of version 1.3 of the driver, the character device provides an abstract
1773 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1774 * same backup/restore procedure is supported. The driver will internally
1775 * convert the requests to the recommended transfer unit, so that an unmatch
1776 * between the user's block size to the recommended size will only result in a
1777 * (slightly) increased driver overhead, but will no longer hit performance.
1778 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001780static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1781 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 struct ide_tape_obj *tape = ide_tape_f(file);
1784 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001785 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001786 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001787 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001789 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Borislav Petkov54abf372008-02-06 02:57:52 +01001791 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001792 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001793 if (count > tape->blk_size &&
1794 (count % tape->blk_size) == 0)
1795 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001797 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001798 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return rc;
1800 if (count == 0)
1801 return (0);
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001802 if (tape->merge_bh_size) {
1803 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001804 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001805 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001806 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 buf += actually_read;
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001808 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 count -= actually_read;
1810 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001811 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001812 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (bytes_read <= 0)
1814 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001815 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001816 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 buf += bytes_read;
1818 count -= bytes_read;
1819 actually_read += bytes_read;
1820 }
1821 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001822 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (bytes_read <= 0)
1824 goto finish;
1825 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001826 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001827 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 actually_read += temp;
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001829 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001832 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001833 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 idetape_space_over_filemarks(drive, MTFSF, 1);
1836 return 0;
1837 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001838
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001839 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001842static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 size_t count, loff_t *ppos)
1844{
1845 struct ide_tape_obj *tape = ide_tape_f(file);
1846 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001847 ssize_t actually_written = 0;
1848 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001849 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* The drive is write protected. */
1852 if (tape->write_prot)
1853 return -EACCES;
1854
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001855 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001858 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1859 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001860 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001861 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001862 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 "should be 0 now\n");
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001864 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001866 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1867 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001869 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001870 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001873 * Issue a write 0 command to ensure that DSC handshake is
1874 * switched from completion mode to buffer available mode. No
1875 * point in issuing this if DSC overlap isn't supported, some
1876 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 */
1878 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001879 ssize_t retval = idetape_queue_rw_tail(drive,
1880 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001881 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001883 ide_tape_kfree_buffer(tape);
1884 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001885 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return retval;
1887 }
1888 }
1889 }
1890 if (count == 0)
1891 return (0);
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001892 if (tape->merge_bh_size) {
1893 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001894 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001895 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001897 actually_written = min((unsigned int)
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001898 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001899 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001900 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001901 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 buf += actually_written;
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001903 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 count -= actually_written;
1905
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001906 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001907 ssize_t retval;
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001908 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001909 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (retval <= 0)
1911 return (retval);
1912 }
1913 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001914 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001915 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001916 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001917 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001918 buf += tape->buffer_size;
1919 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001920 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001921 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (retval <= 0)
1923 return (retval);
1924 }
1925 if (count) {
1926 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001927 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001928 ret = -EFAULT;
Borislav Petkov01a63ae2008-04-27 15:38:34 +02001929 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001931 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001934static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Borislav Petkovd236d742008-04-18 00:46:27 +02001936 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* Write a filemark */
1939 idetape_create_write_filemark_cmd(drive, &pc, 1);
1940 if (idetape_queue_pc_tail(drive, &pc)) {
1941 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1942 return -EIO;
1943 }
1944 return 0;
1945}
1946
1947/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001948 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1949 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001951 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1952 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001953 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001955 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001957 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1958 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001960static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001963 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001964 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001966 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1967 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001970 case MTFSF:
1971 case MTFSFM:
1972 case MTBSF:
1973 case MTBSFM:
1974 if (!mt_count)
1975 return 0;
1976 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1977 default:
1978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001982 case MTWEOF:
1983 if (tape->write_prot)
1984 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001985 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001986 for (i = 0; i < mt_count; i++) {
1987 retval = idetape_write_filemark(drive);
1988 if (retval)
1989 return retval;
1990 }
1991 return 0;
1992 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001993 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001994 if (idetape_rewind_tape(drive))
1995 return -EIO;
1996 return 0;
1997 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001998 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001999 idetape_create_load_unload_cmd(drive, &pc,
2000 IDETAPE_LU_LOAD_MASK);
2001 return idetape_queue_pc_tail(drive, &pc);
2002 case MTUNLOAD:
2003 case MTOFFL:
2004 /*
2005 * If door is locked, attempt to unlock before
2006 * attempting to eject.
2007 */
2008 if (tape->door_locked) {
2009 if (idetape_create_prevent_cmd(drive, &pc, 0))
2010 if (!idetape_queue_pc_tail(drive, &pc))
2011 tape->door_locked = DOOR_UNLOCKED;
2012 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002013 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002014 idetape_create_load_unload_cmd(drive, &pc,
2015 !IDETAPE_LU_LOAD_MASK);
2016 retval = idetape_queue_pc_tail(drive, &pc);
2017 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002018 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002019 return retval;
2020 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002021 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002022 return idetape_flush_tape_buffers(drive);
2023 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002024 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002025 idetape_create_load_unload_cmd(drive, &pc,
2026 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2027 return idetape_queue_pc_tail(drive, &pc);
2028 case MTEOM:
2029 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2030 return idetape_queue_pc_tail(drive, &pc);
2031 case MTERASE:
2032 (void)idetape_rewind_tape(drive);
2033 idetape_create_erase_cmd(&pc);
2034 return idetape_queue_pc_tail(drive, &pc);
2035 case MTSETBLK:
2036 if (mt_count) {
2037 if (mt_count < tape->blk_size ||
2038 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002040 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002041 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002042 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002043 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002044 return 0;
2045 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002046 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002047 return idetape_position_tape(drive,
2048 mt_count * tape->user_bs_factor, tape->partition, 0);
2049 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002050 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002051 return idetape_position_tape(drive, 0, mt_count, 0);
2052 case MTFSR:
2053 case MTBSR:
2054 case MTLOCK:
2055 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002057 retval = idetape_queue_pc_tail(drive, &pc);
2058 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002060 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2061 return 0;
2062 case MTUNLOCK:
2063 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002065 retval = idetape_queue_pc_tail(drive, &pc);
2066 if (retval)
2067 return retval;
2068 tape->door_locked = DOOR_UNLOCKED;
2069 return 0;
2070 default:
2071 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2072 mt_op);
2073 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075}
2076
2077/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002078 * Our character device ioctls. General mtio.h magnetic io commands are
2079 * supported here, and not in the corresponding block interface. Our own
2080 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002082static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2083 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct ide_tape_obj *tape = ide_tape_f(file);
2086 ide_drive_t *drive = tape->drive;
2087 struct mtop mtop;
2088 struct mtget mtget;
2089 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002090 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 void __user *argp = (void __user *)arg;
2092
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002093 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Borislav Petkov54abf372008-02-06 02:57:52 +01002095 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002096 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 idetape_flush_tape_buffers(drive);
2098 }
2099 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63ae2008-04-27 15:38:34 +02002100 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002101 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002102 position = idetape_read_position(drive);
2103 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return -EIO;
2105 }
2106 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002107 case MTIOCTOP:
2108 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2109 return -EFAULT;
2110 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2111 case MTIOCGET:
2112 memset(&mtget, 0, sizeof(struct mtget));
2113 mtget.mt_type = MT_ISSCSI2;
2114 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2115 mtget.mt_dsreg =
2116 ((tape->blk_size * tape->user_bs_factor)
2117 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002118
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002119 if (tape->drv_write_prot)
2120 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2121
2122 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2123 return -EFAULT;
2124 return 0;
2125 case MTIOCPOS:
2126 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2127 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2128 return -EFAULT;
2129 return 0;
2130 default:
2131 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002132 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002133 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135}
2136
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002137/*
2138 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2139 * block size with the reported value.
2140 */
2141static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2142{
2143 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002144 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002145
2146 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2147 if (idetape_queue_pc_tail(drive, &pc)) {
2148 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002149 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002150 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2151 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002152 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002153 }
2154 return;
2155 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002156 tape->blk_size = (pc.buf[4 + 5] << 16) +
2157 (pc.buf[4 + 6] << 8) +
2158 pc.buf[4 + 7];
2159 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002162static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2165 ide_drive_t *drive;
2166 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002167 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int retval;
2169
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002170 if (i >= MAX_HWIFS * MAX_DRIVES)
2171 return -ENXIO;
2172
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002173 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002174 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002175 if (!tape) {
2176 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002177 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002178 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002179
2180 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
2183 * We really want to do nonseekable_open(inode, filp); here, but some
2184 * versions of tar incorrectly call lseek on tapes and bail out if that
2185 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2186 */
2187 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 drive = tape->drive;
2190
2191 filp->private_data = tape;
2192
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002193 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 retval = -EBUSY;
2195 goto out_put_tape;
2196 }
2197
2198 retval = idetape_wait_ready(drive, 60 * HZ);
2199 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002200 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2202 goto out_put_tape;
2203 }
2204
2205 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002206 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 (void)idetape_rewind_tape(drive);
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002210 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /* Set write protect flag if device is opened as read-only. */
2213 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2214 tape->write_prot = 1;
2215 else
2216 tape->write_prot = tape->drv_write_prot;
2217
2218 /* Make sure drive isn't write protected if user wants to write. */
2219 if (tape->write_prot) {
2220 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2221 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002222 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 retval = -EROFS;
2224 goto out_put_tape;
2225 }
2226 }
2227
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002228 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002229 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2231 if (!idetape_queue_pc_tail(drive, &pc)) {
2232 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2233 tape->door_locked = DOOR_LOCKED;
2234 }
2235 }
2236 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002237 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return 0;
2239
2240out_put_tape:
2241 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002242 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return retval;
2244}
2245
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002246static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 idetape_tape_t *tape = drive->driver_data;
2249
Borislav Petkovd9df9372008-04-27 15:38:34 +02002250 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002251 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2252 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002253 idetape_pad_zeros(drive, tape->blk_size *
2254 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002255 ide_tape_kfree_buffer(tape);
2256 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 idetape_write_filemark(drive);
2259 idetape_flush_tape_buffers(drive);
2260 idetape_flush_tape_buffers(drive);
2261}
2262
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002263static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 struct ide_tape_obj *tape = ide_tape_f(filp);
2266 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002267 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 unsigned int minor = iminor(inode);
2269
2270 lock_kernel();
2271 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002272
2273 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Borislav Petkov54abf372008-02-06 02:57:52 +01002275 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002277 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002279 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002281
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002282 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002284 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (tape->door_locked == DOOR_LOCKED) {
2286 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2287 if (!idetape_queue_pc_tail(drive, &pc))
2288 tape->door_locked = DOOR_UNLOCKED;
2289 }
2290 }
2291 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002292 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 ide_tape_put(tape);
2294 unlock_kernel();
2295 return 0;
2296}
2297
2298/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002299 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002301 * 1 - If the tape can be supported by us, based on the information we have so
2302 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002304 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002306static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Borislav Petkov71071b82008-02-06 02:57:53 +01002308 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 if (drive->id_read == 0)
2311 return 1;
2312
Borislav Petkov71071b82008-02-06 02:57:53 +01002313 *((unsigned short *) &gcw) = drive->id->config;
2314
2315 protocol = (gcw[1] & 0xC0) >> 6;
2316 device_type = gcw[1] & 0x1F;
2317 removable = !!(gcw[0] & 0x80);
2318 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002321 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002322 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002323 protocol);
2324 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002325 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002326 "to tape\n", device_type);
2327 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002329 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002330 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2331 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 } else
2333 return 1;
2334 return 0;
2335}
2336
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002337static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002340 struct ide_atapi_pc pc;
Borislav Petkov41f81d52008-02-06 02:57:52 +01002341 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 idetape_create_inquiry_cmd(&pc);
2344 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002345 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2346 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return;
2348 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002349 memcpy(vendor_id, &pc.buf[8], 8);
2350 memcpy(product_id, &pc.buf[16], 16);
2351 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002352
Borislav Petkov41f81d52008-02-06 02:57:52 +01002353 ide_fixstring(vendor_id, 10, 0);
2354 ide_fixstring(product_id, 18, 0);
2355 ide_fixstring(fw_rev, 6, 0);
2356
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002357 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d52008-02-06 02:57:52 +01002358 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
2361/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002362 * Ask the tape about its various parameters. In particular, we will adjust our
2363 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002365static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002368 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002369 u8 *caps;
2370 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2373 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002374 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2375 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002376 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002377 put_unaligned(52, (u16 *)&tape->caps[12]);
2378 put_unaligned(540, (u16 *)&tape->caps[14]);
2379 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return;
2381 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002382 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Borislav Petkovb6422012008-02-02 19:56:49 +01002384 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002385 speed = be16_to_cpup((__be16 *)&caps[14]);
2386 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002388 *(u16 *)&caps[8] = max_speed;
2389 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2390 *(u16 *)&caps[14] = speed;
2391 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002392
2393 if (!speed) {
2394 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2395 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002396 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002398 if (!max_speed) {
2399 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2400 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002401 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403
Borislav Petkovb6422012008-02-02 19:56:49 +01002404 memcpy(&tape->caps, caps, 20);
2405 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002406 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002407 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002408 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002411#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002412static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 idetape_tape_t *tape = drive->driver_data;
2415
Borislav Petkovb6422012008-02-02 19:56:49 +01002416 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2417 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002418 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2419 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkovf73850a2008-04-27 15:38:33 +02002420 ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
2421 1, 1024, &tape->buffer_size, NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002422 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2423 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2424 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002425 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2426 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002427 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2428 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002429 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2430 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002432#else
2433static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002437 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002439 * 1. Initialize our various state variables.
2440 * 2. Ask the tape for its capabilities.
2441 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2442 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002444 * Note that at this point ide.c already assigned us an irq, so that we can
2445 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002447static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Borislav Petkov83042b22008-04-27 15:38:27 +02002449 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002451 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002452 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002453 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Borislav Petkov776bb022008-07-23 19:55:59 +02002455 drive->pc_callback = ide_tape_callback;
2456
Borislav Petkov54bb2072008-02-06 02:57:52 +01002457 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002459 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2460 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2461 tape->name);
2462 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /* Seagate Travan drives do not support DSC overlap. */
2465 if (strstr(drive->id->model, "Seagate STT3401"))
2466 drive->dsc_overlap = 0;
2467 tape->minor = minor;
2468 tape->name[0] = 'h';
2469 tape->name[1] = 't';
2470 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002471 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002474
2475 /* Command packet DRQ type */
2476 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002477 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 idetape_get_inquiry_results(drive);
2480 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002481 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002483 tape->buffer_size = *ctl * tape->blk_size;
2484 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002486 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002487 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002489 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002490 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002491 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002492 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002493 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495
Borislav Petkov83042b22008-04-27 15:38:27 +02002496 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002497 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Borislav Petkovf73850a2008-04-27 15:38:33 +02002499 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002502 * Ensure that the number we got makes sense; limit it within
2503 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002505 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2506 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002508 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002509 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002510 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2511 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002512 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 drive->using_dma ? ", DMA":"");
2514
2515 idetape_add_settings(drive);
2516}
2517
Russell King4031bbe2006-01-06 11:41:00 +00002518static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002522 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 ide_unregister_region(tape->disk);
2525
2526 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529static void ide_tape_release(struct kref *kref)
2530{
2531 struct ide_tape_obj *tape = to_ide_tape(kref);
2532 ide_drive_t *drive = tape->drive;
2533 struct gendisk *g = tape->disk;
2534
Borislav Petkov01a63ae2008-04-27 15:38:34 +02002535 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 drive->dsc_overlap = 0;
2538 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002539 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002540 device_destroy(idetape_sysfs_class,
2541 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 idetape_devs[tape->minor] = NULL;
2543 g->private_data = NULL;
2544 put_disk(g);
2545 kfree(tape);
2546}
2547
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002548#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549static int proc_idetape_read_name
2550 (char *page, char **start, off_t off, int count, int *eof, void *data)
2551{
2552 ide_drive_t *drive = (ide_drive_t *) data;
2553 idetape_tape_t *tape = drive->driver_data;
2554 char *out = page;
2555 int len;
2556
2557 len = sprintf(out, "%s\n", tape->name);
2558 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2559}
2560
2561static ide_proc_entry_t idetape_proc[] = {
2562 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2563 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2564 { NULL, 0, NULL, NULL }
2565};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566#endif
2567
Russell King4031bbe2006-01-06 11:41:00 +00002568static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002571 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002572 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002573 .name = "ide-tape",
2574 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002575 },
Russell King4031bbe2006-01-06 11:41:00 +00002576 .probe = ide_tape_probe,
2577 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .version = IDETAPE_VERSION,
2579 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 .do_request = idetape_do_request,
2582 .end_request = idetape_end_request,
2583 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002584#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002586#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587};
2588
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002589/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002590static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 .owner = THIS_MODULE,
2592 .read = idetape_chrdev_read,
2593 .write = idetape_chrdev_write,
2594 .ioctl = idetape_chrdev_ioctl,
2595 .open = idetape_chrdev_open,
2596 .release = idetape_chrdev_release,
2597};
2598
2599static int idetape_open(struct inode *inode, struct file *filp)
2600{
2601 struct gendisk *disk = inode->i_bdev->bd_disk;
2602 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002604 tape = ide_tape_get(disk);
2605 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return -ENXIO;
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return 0;
2609}
2610
2611static int idetape_release(struct inode *inode, struct file *filp)
2612{
2613 struct gendisk *disk = inode->i_bdev->bd_disk;
2614 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 ide_tape_put(tape);
2617
2618 return 0;
2619}
2620
2621static int idetape_ioctl(struct inode *inode, struct file *file,
2622 unsigned int cmd, unsigned long arg)
2623{
2624 struct block_device *bdev = inode->i_bdev;
2625 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2626 ide_drive_t *drive = tape->drive;
2627 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2628 if (err == -EINVAL)
2629 err = idetape_blkdev_ioctl(drive, cmd, arg);
2630 return err;
2631}
2632
2633static struct block_device_operations idetape_block_ops = {
2634 .owner = THIS_MODULE,
2635 .open = idetape_open,
2636 .release = idetape_release,
2637 .ioctl = idetape_ioctl,
2638};
2639
Russell King4031bbe2006-01-06 11:41:00 +00002640static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 idetape_tape_t *tape;
2643 struct gendisk *g;
2644 int minor;
2645
2646 if (!strstr("ide-tape", drive->driver_req))
2647 goto failed;
2648 if (!drive->present)
2649 goto failed;
2650 if (drive->media != ide_tape)
2651 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002652 if (!idetape_identify_device(drive)) {
2653 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2654 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 goto failed;
2656 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002657 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002659 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2660 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 goto failed;
2662 }
2663
2664 g = alloc_disk(1 << PARTN_BITS);
2665 if (!g)
2666 goto out_free_tape;
2667
2668 ide_init_disk(g, drive);
2669
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002670 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 kref_init(&tape->kref);
2673
2674 tape->drive = drive;
2675 tape->driver = &idetape_driver;
2676 tape->disk = g;
2677
2678 g->private_data = &tape->driver;
2679
2680 drive->driver_data = tape;
2681
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002682 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 for (minor = 0; idetape_devs[minor]; minor++)
2684 ;
2685 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002686 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 idetape_setup(drive, tape, minor);
2689
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002690 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2691 MKDEV(IDETAPE_MAJOR, minor), NULL,
2692 "%s", tape->name);
2693 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2694 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2695 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 g->fops = &idetape_block_ops;
2698 ide_register_region(g);
2699
2700 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702out_free_tape:
2703 kfree(tape);
2704failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002705 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002708static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002710 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002711 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 unregister_chrdev(IDETAPE_MAJOR, "ht");
2713}
2714
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002715static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Will Dysond5dee802005-09-16 02:55:07 -07002717 int error = 1;
2718 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2719 if (IS_ERR(idetape_sysfs_class)) {
2720 idetape_sysfs_class = NULL;
2721 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2722 error = -EBUSY;
2723 goto out;
2724 }
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002727 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2728 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002729 error = -EBUSY;
2730 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Will Dysond5dee802005-09-16 02:55:07 -07002732
2733 error = driver_register(&idetape_driver.gen_driver);
2734 if (error)
2735 goto out_free_driver;
2736
2737 return 0;
2738
2739out_free_driver:
2740 driver_unregister(&idetape_driver.gen_driver);
2741out_free_class:
2742 class_destroy(idetape_sysfs_class);
2743out:
2744 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Kay Sievers263756e2005-12-12 18:03:44 +01002747MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748module_init(idetape_init);
2749module_exit(idetape_exit);
2750MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002751MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2752MODULE_LICENSE("GPL");