blob: 6962ca4891a134517774fa9dbf7ec533ee938b5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Borislav Petkovdfe79932008-02-06 02:57:55 +010018#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/genhd.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/ide.h>
35#include <linux/smp_lock.h>
36#include <linux/completion.h>
37#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080038#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010039#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010042#include <linux/irq.h>
43#include <linux/uaccess.h>
44#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
85 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87#define IDETAPE_PC_BUFFER_SIZE 256
88
89/*
90 * In various places in the driver, we need to allocate storage
91 * for packet commands and requests, which will remain valid while
92 * we leave the driver to wait for an interrupt or a timeout event.
93 */
94#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
95
96/*
97 * Some drives (for example, Seagate STT3401A Travan) require a very long
98 * timeout, because they don't return an interrupt or clear their busy bit
99 * until after the command completes (even retension commands).
100 */
101#define IDETAPE_WAIT_CMD (900*HZ)
102
103/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * The following parameter is used to select the point in the internal tape fifo
105 * in which we will start to refill the buffer. Decreasing the following
106 * parameter will improve the system's latency and interactive response, while
107 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100109#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100114 * Polling for DSC (a single bit in the status register) is a very important
115 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100117 * 1. Before a read/write packet command, to ensure that we can transfer data
118 * from/to the tape's data buffers, without causing an actual media access.
119 * In case the tape is not ready yet, we take out our request from the device
120 * request queue, so that ide.c could service requests from the other device
121 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100123 * 2. After the successful initialization of a "media access packet command",
124 * which is a command that can take a long time to complete (the interval can
125 * range from several seconds to even an hour). Again, we postpone our request
126 * in the middle to free the bus for the other device. The polling frequency
127 * here should be lower than the read/write frequency since those media access
128 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
129 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
130 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100132 * We also set a timeout for the timer, in case something goes wrong. The
133 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100135
136/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
138#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
139#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
140#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
141#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
142#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
143#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
144
145/*************************** End of tunable parameters ***********************/
146
Borislav Petkov54abf372008-02-06 02:57:52 +0100147/* tape directions */
148enum {
149 IDETAPE_DIR_NONE = (1 << 0),
150 IDETAPE_DIR_READ = (1 << 1),
151 IDETAPE_DIR_WRITE = (1 << 2),
152};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200155 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 atomic_t b_count;
157 struct idetape_bh *b_reqnext;
158 char *b_data;
159};
160
Borislav Petkov03056b92008-04-18 00:46:26 +0200161/* Tape door status */
162#define DOOR_UNLOCKED 0
163#define DOOR_LOCKED 1
164#define DOOR_EXPLICITLY_LOCKED 2
165
166/* Some defines for the SPACE command */
167#define IDETAPE_SPACE_OVER_FILEMARK 1
168#define IDETAPE_SPACE_TO_EOD 3
169
170/* Some defines for the LOAD UNLOAD command */
171#define IDETAPE_LU_LOAD_MASK 1
172#define IDETAPE_LU_RETENSION_MASK 2
173#define IDETAPE_LU_EOT_MASK 4
174
175/*
176 * Special requests for our block device strategy routine.
177 *
178 * In order to service a character device command, we add special requests to
179 * the tail of our block device request queue and wait for their completion.
180 */
181
182enum {
183 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
184 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
185 REQ_IDETAPE_READ = (1 << 2),
186 REQ_IDETAPE_WRITE = (1 << 3),
187};
188
189/* Error codes returned in rq->errors to the higher part of the driver. */
190#define IDETAPE_ERROR_GENERAL 101
191#define IDETAPE_ERROR_FILEMARK 102
192#define IDETAPE_ERROR_EOD 103
193
194/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
195#define IDETAPE_BLOCK_DESCRIPTOR 0
196#define IDETAPE_CAPABILITIES_PAGE 0x2a
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100199 * Most of our global data which we need to save even as we leave the driver due
200 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202typedef struct ide_tape_obj {
203 ide_drive_t *drive;
204 ide_driver_t *driver;
205 struct gendisk *disk;
206 struct kref kref;
207
208 /*
209 * Since a typical character device operation requires more
210 * than one packet command, we provide here enough memory
211 * for the maximum of interconnected packet commands.
212 * The packet commands are stored in the circular array pc_stack.
213 * pc_stack_index points to the last used entry, and warps around
214 * to the start when we get to the last array entry.
215 *
216 * pc points to the current processed packet command.
217 *
218 * failed_pc points to the last failed packet command, or contains
219 * NULL if we do not need to retry any packet command. This is
220 * required since an additional packet command is needed before the
221 * retry, to get detailed information on what went wrong.
222 */
223 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200224 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200226 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200228 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Next free packet command storage space */
230 int pc_stack_index;
231 struct request rq_stack[IDETAPE_PC_STACK];
232 /* We implement a circular array */
233 int rq_stack_index;
234
235 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100236 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100238 * While polling for DSC we use postponed_rq to postpone the current
239 * request so that ide.c will be able to service pending requests on the
240 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200241 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 struct request *postponed_rq;
244 /* The time in which we started polling for DSC */
245 unsigned long dsc_polling_start;
246 /* Timer used to poll for dsc */
247 struct timer_list dsc_timer;
248 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100249 unsigned long best_dsc_rw_freq;
250 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long dsc_timeout;
252
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100253 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 u8 partition;
255 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100256 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100258 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 u8 sense_key, asc, ascq;
260
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100261 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int minor;
263 /* device name */
264 char name[4];
265 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100266 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Borislav Petkov54bb2072008-02-06 02:57:52 +0100268 /* tape block size, usually 512 or 1024 bytes */
269 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100273 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100276 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100278 * At most, there is only one ide-tape originated data transfer request
279 * in the device request queue. This allows ide.c to easily service
280 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200282
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100283 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200284 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200285 /* merge buffer */
286 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200287 /* size of the merge buffer */
288 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200289 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct idetape_bh *bh;
291 char *b_data;
292 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100293
Borislav Petkova997a432008-04-27 15:38:33 +0200294 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Wasted space in each stage */
296 int excess_bh_size;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100299 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100301 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 unsigned long avg_time;
303 int avg_size;
304 int avg_speed;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* the door is currently locked */
307 int door_locked;
308 /* the tape hardware is write protected */
309 char drv_write_prot;
310 /* the tape is write protected (hardware or opened as read-only) */
311 char write_prot;
312
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100313 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314} idetape_tape_t;
315
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800316static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Will Dysond5dee802005-09-16 02:55:07 -0700318static struct class *idetape_sysfs_class;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
321
322#define ide_tape_g(disk) \
323 container_of((disk)->private_data, struct ide_tape_obj, driver)
324
325static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
326{
327 struct ide_tape_obj *tape = NULL;
328
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800329 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 tape = ide_tape_g(disk);
331 if (tape)
332 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800333 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return tape;
335}
336
337static void ide_tape_release(struct kref *);
338
339static void ide_tape_put(struct ide_tape_obj *tape)
340{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800341 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800343 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100347 * The variables below are used for the character device interface. Additional
348 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100350static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352#define ide_tape_f(file) ((file)->private_data)
353
354static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
355{
356 struct ide_tape_obj *tape = NULL;
357
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800358 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 tape = idetape_devs[i];
360 if (tape)
361 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800362 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return tape;
364}
365
Borislav Petkovd236d742008-04-18 00:46:27 +0200366static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100367 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct idetape_bh *bh = pc->bh;
370 int count;
371
372 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (bh == NULL) {
374 printk(KERN_ERR "ide-tape: bh == NULL in "
375 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200376 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
378 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100379 count = min(
380 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
381 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200382 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100383 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 bcount -= count;
385 atomic_add(count, &bh->b_count);
386 if (atomic_read(&bh->b_count) == bh->b_size) {
387 bh = bh->b_reqnext;
388 if (bh)
389 atomic_set(&bh->b_count, 0);
390 }
391 }
392 pc->bh = bh;
393}
394
Borislav Petkovd236d742008-04-18 00:46:27 +0200395static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100396 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct idetape_bh *bh = pc->bh;
399 int count;
400
401 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100403 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
404 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200408 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 bcount -= count;
410 pc->b_data += count;
411 pc->b_count -= count;
412 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100413 bh = bh->b_reqnext;
414 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (bh) {
416 pc->b_data = bh->b_data;
417 pc->b_count = atomic_read(&bh->b_count);
418 }
419 }
420 }
421}
422
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200423static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct idetape_bh *bh = pc->bh;
426 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200427 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Borislav Petkov346331f2008-04-18 00:46:26 +0200429 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return;
431 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100433 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
434 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
438 atomic_set(&bh->b_count, count);
439 if (atomic_read(&bh->b_count) == bh->b_size)
440 bh = bh->b_reqnext;
441 bcount -= count;
442 }
443 pc->bh = bh;
444}
445
446/*
447 * idetape_next_pc_storage returns a pointer to a place in which we can
448 * safely store a packet command, even though we intend to leave the
449 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
450 * commands is allocated at initialization time.
451 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200452static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 idetape_tape_t *tape = drive->driver_data;
455
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100456 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100459 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return (&tape->pc_stack[tape->pc_stack_index++]);
461}
462
463/*
464 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
465 * Since we queue packet commands in the request queue, we need to
466 * allocate a request, along with the allocation of a packet command.
467 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/**************************************************************
470 * *
471 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
472 * followed later on by kfree(). -ml *
473 * *
474 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100475
476static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 idetape_tape_t *tape = drive->driver_data;
479
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100480 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100483 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return (&tape->rq_stack[tape->rq_stack_index++]);
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100488 * called on each failed packet command retry to analyze the request sense. We
489 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100491static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200494 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Borislav Petkov1b5db432008-02-02 19:56:48 +0100496 tape->sense_key = sense[2] & 0xF;
497 tape->asc = sense[12];
498 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100499
500 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
501 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Borislav Petkovd236d742008-04-18 00:46:27 +0200503 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200504 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200505 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100506 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200507 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200508 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 /*
512 * If error was the result of a zero-length read or write command,
513 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
514 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
515 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100516 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100517 /* length == 0 */
518 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
519 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* don't report an error, everything's ok */
521 pc->error = 0;
522 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200523 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100526 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200528 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100530 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100531 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
532 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200534 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100537 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100538 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200540 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200542 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200543 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
545 }
546}
547
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200548/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200549static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200551 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200553 while (bh) {
554 u32 size = bh->b_size;
555
556 while (size) {
557 unsigned int order = fls(size >> PAGE_SHIFT)-1;
558
559 if (bh->b_data)
560 free_pages((unsigned long)bh->b_data, order);
561
562 size &= (order-1);
563 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 prev_bh = bh;
566 bh = bh->b_reqnext;
567 kfree(prev_bh);
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
572{
573 struct request *rq = HWGROUP(drive)->rq;
574 idetape_tape_t *tape = drive->driver_data;
575 unsigned long flags;
576 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100578 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100581 case 0: error = IDETAPE_ERROR_GENERAL; break;
582 case 1: error = 0; break;
583 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 rq->errors = error;
586 if (error)
587 tape->failed_pc = NULL;
588
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100589 if (!blk_special_request(rq)) {
590 ide_end_request(drive, uptodate, nr_sects);
591 return 0;
592 }
593
Borislav Petkov54bb2072008-02-06 02:57:52 +0100594 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Borislav Petkov54bb2072008-02-06 02:57:52 +0100598 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600}
601
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200602static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200605 struct ide_atapi_pc *pc = tape->pc;
606 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100608 debug_log(DBG_PROCS, "Enter %s\n", __func__);
609
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200610 if (tape->failed_pc == pc)
611 tape->failed_pc = NULL;
612
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200613 if (pc->c[0] == REQUEST_SENSE) {
614 if (uptodate)
615 idetape_analyze_error(drive, pc->buf);
616 else
617 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
618 "itself - Aborting request!\n");
619 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
620 struct request *rq = drive->hwif->hwgroup->rq;
621 int blocks = pc->xferred / tape->blk_size;
622
623 tape->avg_size += blocks * tape->blk_size;
624
625 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
626 tape->avg_speed = tape->avg_size * HZ /
627 (jiffies - tape->avg_time) / 1024;
628 tape->avg_size = 0;
629 tape->avg_time = jiffies;
630 }
631
632 tape->first_frame += blocks;
633 rq->current_nr_sectors -= blocks;
634
635 if (pc->error)
636 uptodate = pc->error;
637 } else if (pc->c[0] == READ_POSITION && uptodate) {
638 u8 *readpos = tape->pc->buf;
639
640 debug_log(DBG_SENSE, "BOP - %s\n",
641 (readpos[0] & 0x80) ? "Yes" : "No");
642 debug_log(DBG_SENSE, "EOP - %s\n",
643 (readpos[0] & 0x40) ? "Yes" : "No");
644
645 if (readpos[0] & 0x4) {
646 printk(KERN_INFO "ide-tape: Block location is unknown"
647 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200648 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200649 uptodate = 0;
650 } else {
651 debug_log(DBG_SENSE, "Block Location - %u\n",
652 be32_to_cpu(*(u32 *)&readpos[4]));
653
654 tape->partition = readpos[1];
655 tape->first_frame = be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200656 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200659
660 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200663static void idetape_init_pc(struct ide_atapi_pc *pc)
664{
665 memset(pc->c, 0, 12);
666 pc->retries = 0;
667 pc->flags = 0;
668 pc->req_xfer = 0;
669 pc->buf = pc->pc_buf;
670 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
671 pc->bh = NULL;
672 pc->b_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Borislav Petkovd236d742008-04-18 00:46:27 +0200675static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100677 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100678 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200680 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683static void idetape_init_rq(struct request *rq, u8 cmd)
684{
FUJITA Tomonorie7b241a2008-04-29 09:54:38 +0200685 blk_rq_init(NULL, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200686 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200687 rq->cmd[13] = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100691 * Generate a new packet command request in front of the request queue, before
692 * the current request, so that it will be processed immediately, on the next
693 * pass through the driver. The function below is called from the request
694 * handling part of the driver (the "bottom" part). Safe storage for the request
695 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100697 * Memory for those requests is pre-allocated at initialization time, and is
698 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
699 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100701 * The higher level of the driver - The ioctl handler and the character device
702 * handling functions should queue request to the lower level part and wait for
703 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200705static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100706 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct ide_tape_obj *tape = drive->driver_data;
709
710 idetape_init_rq(rq, REQ_IDETAPE_PC1);
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200711 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 rq->buffer = (char *) pc;
713 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200714 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200715 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * idetape_retry_pc is called when an error was detected during the
720 * last packet command. We queue a request sense packet command in
721 * the head of the request list.
722 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200723static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Borislav Petkovd236d742008-04-18 00:46:27 +0200725 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100728 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 pc = idetape_next_pc_storage(drive);
730 rq = idetape_next_rq_storage(drive);
731 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200732 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100737 * Postpone the current request so that ide.c will be able to service requests
738 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100740static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 idetape_tape_t *tape = drive->driver_data;
743
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100744 debug_log(DBG_PROCS, "Enter %s\n", __func__);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100747 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200750static void ide_tape_handle_dsc(ide_drive_t *drive)
751{
752 idetape_tape_t *tape = drive->driver_data;
753
754 /* Media access command */
755 tape->dsc_polling_start = jiffies;
756 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
757 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
758 /* Allow ide.c to handle other requests */
759 idetape_postpone_request(drive);
760}
761
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200762static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
763 unsigned int bcount, int write)
764{
765 if (write)
766 idetape_output_buffers(drive, pc, bcount);
767 else
768 idetape_input_buffers(drive, pc, bcount);
769}
Borislav Petkova1efc852008-02-06 02:57:52 +0100770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100772 * This is the usual interrupt handler which will be called during a packet
773 * command. We will transfer some of the data (as requested by the drive) and
774 * will re-point interrupt handler to us. When data transfer is finished, we
775 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100776 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100778static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200782 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
783 NULL, idetape_update_buffers, idetape_retry_pc,
784 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100788 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100790 * The current Packet Command is available in tape->pc, and will not change
791 * until we finish handling it. Each packet command is associated with a
792 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100794 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100796 * 1. idetape_issue_pc will send the packet command to the drive, and will set
797 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100799 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
800 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100802 * 3. ATAPI Tape media access commands have immediate status with a delayed
803 * process. In case of a successful initiation of a media access packet command,
804 * the DSC bit will be set when the actual execution of the command is finished.
805 * Since the tape drive will not issue an interrupt, we have to poll for this
806 * event. In this case, we define the request as "low priority request" by
807 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
808 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100810 * ide.c will then give higher priority to requests which originate from the
811 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100813 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100815 * 5. In case an error was found, we queue a request sense packet command in
816 * front of the request queue and retry the operation up to
817 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100819 * 6. In case no error was found, or we decided to give up and not to retry
820 * again, the callback function will be called and then we will handle the next
821 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
824{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200827 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
828 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Borislav Petkovd236d742008-04-18 00:46:27 +0200831static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
832 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Borislav Petkov90699ce2008-02-02 19:56:50 +0100836 if (tape->pc->c[0] == REQUEST_SENSE &&
837 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
839 "Two request sense in serial were issued\n");
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Borislav Petkov90699ce2008-02-02 19:56:50 +0100842 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tape->failed_pc = pc;
844 /* Set the current packet command */
845 tape->pc = pc;
846
847 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200848 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100850 * We will "abort" retrying a packet command in case legitimate
851 * error code was received (crossing a filemark, or end of the
852 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200854 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100855 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 tape->sense_key == 2 && tape->asc == 4 &&
857 (tape->ascq == 1 || tape->ascq == 8))) {
858 printk(KERN_ERR "ide-tape: %s: I/O error, "
859 "pc = %2x, key = %2x, "
860 "asc = %2x, ascq = %2x\n",
861 tape->name, pc->c[0],
862 tape->sense_key, tape->asc,
863 tape->ascq);
864 }
865 /* Giving up */
866 pc->error = IDETAPE_ERROR_GENERAL;
867 }
868 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200869 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200870 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100872 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200876 return ide_issue_pc(drive, pc, idetape_transfer_pc,
877 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100880/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200881static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100884 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100886 /* DBD = 1 - Don't return block descriptors */
887 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 pc->c[2] = page_code;
889 /*
890 * Changed pc->c[3] to 0 (255 will at best return unused info).
891 *
892 * For SCSI this byte is defined as subpage instead of high byte
893 * of length and some IDE drives seem to interpret it this way
894 * and return an error when 255 is used.
895 */
896 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100897 /* We will just discard data in that case */
898 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200900 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200902 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200904 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100907static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200909 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200911 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100912 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200914 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100915
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100916 if (stat & SEEK_STAT) {
917 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100919 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 printk(KERN_ERR "ide-tape: %s: I/O error, ",
921 tape->name);
922 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200923 idetape_retry_pc(drive);
924 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 } else {
928 pc->error = IDETAPE_ERROR_GENERAL;
929 tape->failed_pc = NULL;
930 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200931 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return ide_stopped;
933}
934
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200935static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200936 struct ide_atapi_pc *pc, struct request *rq,
937 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Borislav Petkov0014c752008-07-23 19:56:00 +0200939 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
940 unsigned int length = rq->current_nr_sectors;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100943 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200946 pc->buf = NULL;
947 pc->buf_size = length * tape->blk_size;
948 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200949 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200950 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200952 if (opcode == READ_6) {
953 pc->c[0] = READ_6;
954 atomic_set(&bh->b_count, 0);
955 } else if (opcode == WRITE_6) {
956 pc->c[0] = WRITE_6;
957 pc->flags |= PC_FLAG_WRITING;
958 pc->b_data = bh->b_data;
959 pc->b_count = atomic_read(&bh->b_count);
960 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200961
962 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static ide_startstop_t idetape_do_request(ide_drive_t *drive,
966 struct request *rq, sector_t block)
967{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200968 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200970 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100972 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100974 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
975 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jens Axboe4aff5e22006-08-10 08:44:47 +0200978 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100979 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200981 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ide_end_request(drive, 0, 0);
983 return ide_stopped;
984 }
985
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100986 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200987 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
988 pc = tape->failed_pc;
989 goto out;
990 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (postponed_rq != NULL)
993 if (rq != postponed_rq) {
994 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
995 "Two DSC requests were queued\n");
996 idetape_end_request(drive, 0, 0);
997 return ide_stopped;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 tape->postponed_rq = NULL;
1001
1002 /*
1003 * If the tape is still busy, postpone our request and service
1004 * the other device meanwhile.
1005 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001006 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Borislav Petkov83dd5732008-07-23 19:56:00 +02001008 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001009 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001012 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 drive->post_reset = 0;
1014 }
1015
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001016 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001017 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (postponed_rq == NULL) {
1019 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001020 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1022 } else if (time_after(jiffies, tape->dsc_timeout)) {
1023 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1024 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +02001025 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 idetape_media_access_finished(drive);
1027 return ide_stopped;
1028 } else {
1029 return ide_do_reset(drive);
1030 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001031 } else if (time_after(jiffies,
1032 tape->dsc_polling_start +
1033 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001034 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 idetape_postpone_request(drive);
1036 return ide_stopped;
1037 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001038 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001040 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto out;
1042 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001043 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001045 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto out;
1047 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001048 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001049 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001050 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1051 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto out;
1053 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001054 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 idetape_media_access_finished(drive);
1056 return ide_stopped;
1057 }
1058 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001059
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001060out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001061 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001065 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001066 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001067 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001069 * It returns a pointer to the newly allocated buffer, or NULL in case of
1070 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001072static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1073 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001075 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001076 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001077 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 char *b_data = NULL;
1079
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001080 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1081 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (bh == NULL)
1083 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001084
1085 order = fls(pages) - 1;
1086 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001087 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001089 b_allocd = (1 << order) * PAGE_SIZE;
1090 pages &= (order-1);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001093 memset(bh->b_data, 0, b_allocd);
1094 bh->b_reqnext = NULL;
1095 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1097
Borislav Petkov41aa1702008-04-27 15:38:32 +02001098 while (pages) {
1099 order = fls(pages) - 1;
1100 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001101 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001103 b_allocd = (1 << order) * PAGE_SIZE;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001106 memset(b_data, 0, b_allocd);
1107
1108 /* newly allocated page frames below buffer header or ...*/
1109 if (bh->b_data == b_data + b_allocd) {
1110 bh->b_size += b_allocd;
1111 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001113 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 continue;
1115 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001116 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001118 bh->b_size += 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 }
1123 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001124 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1125 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001126 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto abort;
1128 }
1129 bh->b_reqnext = NULL;
1130 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001131 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1133 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001134
1135 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 bh->b_size -= tape->excess_bh_size;
1139 if (full)
1140 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001141 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001143 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return NULL;
1145}
1146
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001147static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001148 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 struct idetape_bh *bh = tape->bh;
1151 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001152 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001156 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1157 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001158 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001160 count = min((unsigned int)
1161 (bh->b_size - atomic_read(&bh->b_count)),
1162 (unsigned int)n);
1163 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1164 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001165 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 n -= count;
1167 atomic_add(count, &bh->b_count);
1168 buf += count;
1169 if (atomic_read(&bh->b_count) == bh->b_size) {
1170 bh = bh->b_reqnext;
1171 if (bh)
1172 atomic_set(&bh->b_count, 0);
1173 }
1174 }
1175 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001179static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001180 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct idetape_bh *bh = tape->bh;
1183 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001184 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001188 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1189 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001190 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001193 if (copy_to_user(buf, tape->b_data, count))
1194 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 n -= count;
1196 tape->b_data += count;
1197 tape->b_count -= count;
1198 buf += count;
1199 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001200 bh = bh->b_reqnext;
1201 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (bh) {
1203 tape->b_data = bh->b_data;
1204 tape->b_count = atomic_read(&bh->b_count);
1205 }
1206 }
1207 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001208 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001211static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001213 struct idetape_bh *bh = tape->merge_bh;
1214 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001215
Borislav Petkov54abf372008-02-06 02:57:52 +01001216 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 atomic_set(&bh->b_count, 0);
1218 else {
1219 tape->b_data = bh->b_data;
1220 tape->b_count = atomic_read(&bh->b_count);
1221 }
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001225 * Write a filemark if write_filemark=1. Flush the device buffers without
1226 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001228static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001229 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001232 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001234 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Borislav Petkovd236d742008-04-18 00:46:27 +02001237static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001240 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001244 * We add a special packet command request to the tail of the request queue, and
1245 * wait for it to be serviced. This is not to be called from within the request
1246 * handling part of the driver! We allocate here data on the stack and it is
1247 * valid until the request is finished. This is not the case for the bottom part
1248 * of the driver, where we are always leaving the functions to wait for an
1249 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001251 * From the bottom part of the driver, we should allocate safe memory using
1252 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1253 * to the request list without waiting for it to be serviced! In that case, we
1254 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001256static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001259 struct request *rq;
1260 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001262 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1263 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001264 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001265 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001266 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001267 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1268 blk_put_request(rq);
1269 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Borislav Petkovd236d742008-04-18 00:46:27 +02001272static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1273 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001276 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001278 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1282{
1283 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001284 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int load_attempted = 0;
1286
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001287 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001288 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 timeout += jiffies;
1290 while (time_before(jiffies, timeout)) {
1291 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001292 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return 0;
1294 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001295 || (tape->asc == 0x3A)) {
1296 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (load_attempted)
1298 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001299 idetape_create_load_unload_cmd(drive, &pc,
1300 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001301 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 load_attempted = 1;
1303 /* not about to be ready */
1304 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1305 (tape->ascq == 1 || tape->ascq == 8)))
1306 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001307 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 return -EIO;
1310}
1311
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Borislav Petkovd236d742008-04-18 00:46:27 +02001314 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int rc;
1316
1317 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001318 rc = idetape_queue_pc_tail(drive, &pc);
1319 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return rc;
1321 idetape_wait_ready(drive, 60 * 5 * HZ);
1322 return 0;
1323}
1324
Borislav Petkovd236d742008-04-18 00:46:27 +02001325static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001328 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001329 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001335 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int position;
1337
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001338 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 idetape_create_read_position_cmd(&pc);
1341 if (idetape_queue_pc_tail(drive, &pc))
1342 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001343 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return position;
1345}
1346
Borislav Petkovd236d742008-04-18 00:46:27 +02001347static void idetape_create_locate_cmd(ide_drive_t *drive,
1348 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001352 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001354 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001356 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Borislav Petkovd236d742008-04-18 00:46:27 +02001359static int idetape_create_prevent_cmd(ide_drive_t *drive,
1360 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 idetape_tape_t *tape = drive->driver_data;
1363
Borislav Petkovb6422012008-02-02 19:56:49 +01001364 /* device supports locking according to capabilities page */
1365 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367
1368 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001369 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 1;
1372}
1373
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001374static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Borislav Petkov54abf372008-02-06 02:57:52 +01001378 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001379 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001381 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001382 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001383 if (tape->merge_bh != NULL) {
1384 ide_tape_kfree_buffer(tape);
1385 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Borislav Petkov54abf372008-02-06 02:57:52 +01001388 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001392 * Position the tape to the requested block using the LOCATE packet command.
1393 * A READ POSITION command is then issued to check where we are positioned. Like
1394 * all higher level operations, we queue the commands at the tail of the request
1395 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001397static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1398 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 idetape_tape_t *tape = drive->driver_data;
1401 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001402 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Borislav Petkov54abf372008-02-06 02:57:52 +01001404 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001405 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 idetape_wait_ready(drive, 60 * 5 * HZ);
1407 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1408 retval = idetape_queue_pc_tail(drive, &pc);
1409 if (retval)
1410 return (retval);
1411
1412 idetape_create_read_position_cmd(&pc);
1413 return (idetape_queue_pc_tail(drive, &pc));
1414}
1415
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001416static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001417 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int seek, position;
1421
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001422 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (restore_position) {
1424 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001425 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001427 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001428 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return;
1430 }
1431 }
1432}
1433
1434/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001435 * Generate a read/write request for the block device interface and wait for it
1436 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001438static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1439 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001442 struct request *rq;
1443 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001445 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1446
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001447 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1448 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001449 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001450 rq->rq_disk = tape->disk;
1451 rq->special = (void *)bh;
1452 rq->sector = tape->first_frame;
1453 rq->nr_sectors = blocks;
1454 rq->current_nr_sectors = blocks;
1455 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1456
1457 errors = rq->errors;
1458 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1459 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1462 return 0;
1463
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001464 if (tape->merge_bh)
1465 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001466 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Borislav Petkovd236d742008-04-18 00:46:27 +02001471static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001474 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001475 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001476 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Borislav Petkovd236d742008-04-18 00:46:27 +02001479static void idetape_create_rewind_cmd(ide_drive_t *drive,
1480 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001483 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001484 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Borislav Petkovd236d742008-04-18 00:46:27 +02001487static void idetape_create_erase_cmd(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] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001492 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Borislav Petkovd236d742008-04-18 00:46:27 +02001495static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001498 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001499 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001501 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Borislav Petkov97c566c2008-04-27 15:38:25 +02001504/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001505static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001509 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001511 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001512 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Borislav Petkovd9df9372008-04-27 15:38:34 +02001515static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 idetape_tape_t *tape = drive->driver_data;
1518 int blocks, min;
1519 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001520
Borislav Petkov54abf372008-02-06 02:57:52 +01001521 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001522 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001523 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return;
1525 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001526 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001528 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001530 if (tape->merge_bh_size) {
1531 blocks = tape->merge_bh_size / tape->blk_size;
1532 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 unsigned int i;
1534
1535 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001536 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001537 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 bh = tape->bh->b_reqnext;
1539 while (bh) {
1540 atomic_set(&bh->b_count, 0);
1541 bh = bh->b_reqnext;
1542 }
1543 bh = tape->bh;
1544 while (i) {
1545 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001546 printk(KERN_INFO "ide-tape: bug,"
1547 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 break;
1549 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001550 min = min(i, (unsigned int)(bh->b_size -
1551 atomic_read(&bh->b_count)));
1552 memset(bh->b_data + atomic_read(&bh->b_count),
1553 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 atomic_add(min, &bh->b_count);
1555 i -= min;
1556 bh = bh->b_reqnext;
1557 }
1558 }
1559 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001560 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001562 if (tape->merge_bh != NULL) {
1563 ide_tape_kfree_buffer(tape);
1564 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001566 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Borislav Petkov83042b22008-04-27 15:38:27 +02001569static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001575 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1576 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001577 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 idetape_flush_tape_buffers(drive);
1579 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001580 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001581 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001582 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001583 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001585 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1586 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001588 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001591 * Issue a read 0 command to ensure that DSC handshake is
1592 * switched from completion mode to buffer available mode.
1593 * No point in issuing this if DSC overlap isn't supported, some
1594 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
1596 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001597 bytes_read = idetape_queue_rw_tail(drive,
1598 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001599 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001601 ide_tape_kfree_buffer(tape);
1602 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001603 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return bytes_read;
1605 }
1606 }
1607 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return 0;
1610}
1611
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001612/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001613static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001617 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001619 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001620 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return 0;
1622
Borislav Petkov83042b22008-04-27 15:38:27 +02001623 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001625 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001626 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 idetape_tape_t *tape = drive->driver_data;
1632 struct idetape_bh *bh;
1633 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 while (bcount) {
1636 unsigned int count;
1637
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001638 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001639 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001641 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001643 atomic_set(&bh->b_count,
1644 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1646 count -= atomic_read(&bh->b_count);
1647 bh = bh->b_reqnext;
1648 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001649 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001650 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001655 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1656 * currently support only one partition.
1657 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001658static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001661 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001662 idetape_tape_t *tape;
1663 tape = drive->driver_data;
1664
1665 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 idetape_create_rewind_cmd(drive, &pc);
1668 retval = idetape_queue_pc_tail(drive, &pc);
1669 if (retval)
1670 return retval;
1671
1672 idetape_create_read_position_cmd(&pc);
1673 retval = idetape_queue_pc_tail(drive, &pc);
1674 if (retval)
1675 return retval;
1676 return 0;
1677}
1678
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001679/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001680static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1681 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 void __user *argp = (void __user *)arg;
1685
Borislav Petkovd59823f2008-02-02 19:56:51 +01001686 struct idetape_config {
1687 int dsc_rw_frequency;
1688 int dsc_media_access_frequency;
1689 int nr_stages;
1690 } config;
1691
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001692 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001695 case 0x0340:
1696 if (copy_from_user(&config, argp, sizeof(config)))
1697 return -EFAULT;
1698 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001699 break;
1700 case 0x0350:
1701 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001702 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001703 if (copy_to_user(argp, &config, sizeof(config)))
1704 return -EFAULT;
1705 break;
1706 default:
1707 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 return 0;
1710}
1711
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001712static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1713 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001716 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001717 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001718 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (mt_count == 0)
1721 return 0;
1722 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001723 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001725 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727
Borislav Petkov54abf372008-02-06 02:57:52 +01001728 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001729 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001730 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001732 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001736 case MTFSF:
1737 case MTBSF:
1738 idetape_create_space_cmd(&pc, mt_count - count,
1739 IDETAPE_SPACE_OVER_FILEMARK);
1740 return idetape_queue_pc_tail(drive, &pc);
1741 case MTFSFM:
1742 case MTBSFM:
1743 if (!sprev)
1744 return -EIO;
1745 retval = idetape_space_over_filemarks(drive, MTFSF,
1746 mt_count - count);
1747 if (retval)
1748 return retval;
1749 count = (MTBSFM == mt_op ? 1 : -1);
1750 return idetape_space_over_filemarks(drive, MTFSF, count);
1751 default:
1752 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1753 mt_op);
1754 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001759 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001761 * The tape is optimized to maximize throughput when it is transferring an
1762 * integral number of the "continuous transfer limit", which is a parameter of
1763 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001765 * As of version 1.3 of the driver, the character device provides an abstract
1766 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1767 * same backup/restore procedure is supported. The driver will internally
1768 * convert the requests to the recommended transfer unit, so that an unmatch
1769 * between the user's block size to the recommended size will only result in a
1770 * (slightly) increased driver overhead, but will no longer hit performance.
1771 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001773static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1774 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct ide_tape_obj *tape = ide_tape_f(file);
1777 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001778 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001779 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001780 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001782 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Borislav Petkov54abf372008-02-06 02:57:52 +01001784 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001785 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001786 if (count > tape->blk_size &&
1787 (count % tape->blk_size) == 0)
1788 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001790 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001791 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return rc;
1793 if (count == 0)
1794 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001795 if (tape->merge_bh_size) {
1796 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001797 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001798 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001799 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001801 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 count -= actually_read;
1803 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001804 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001805 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (bytes_read <= 0)
1807 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001808 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001809 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 buf += bytes_read;
1811 count -= bytes_read;
1812 actually_read += bytes_read;
1813 }
1814 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001815 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (bytes_read <= 0)
1817 goto finish;
1818 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001819 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001820 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001822 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001825 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001826 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 idetape_space_over_filemarks(drive, MTFSF, 1);
1829 return 0;
1830 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001831
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001832 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001835static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 size_t count, loff_t *ppos)
1837{
1838 struct ide_tape_obj *tape = ide_tape_f(file);
1839 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001840 ssize_t actually_written = 0;
1841 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001842 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* The drive is write protected. */
1845 if (tape->write_prot)
1846 return -EACCES;
1847
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001848 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001851 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1852 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001853 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001854 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001855 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001857 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001859 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1860 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001862 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001863 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001866 * Issue a write 0 command to ensure that DSC handshake is
1867 * switched from completion mode to buffer available mode. No
1868 * point in issuing this if DSC overlap isn't supported, some
1869 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
1871 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001872 ssize_t retval = idetape_queue_rw_tail(drive,
1873 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001874 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001876 ide_tape_kfree_buffer(tape);
1877 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001878 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return retval;
1880 }
1881 }
1882 }
1883 if (count == 0)
1884 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001885 if (tape->merge_bh_size) {
1886 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001887 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001888 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001890 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001891 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001892 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001893 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001894 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001896 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 count -= actually_written;
1898
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001899 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001900 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001901 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001902 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (retval <= 0)
1904 return (retval);
1905 }
1906 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001907 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001908 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001909 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001910 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001911 buf += tape->buffer_size;
1912 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001913 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001914 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (retval <= 0)
1916 return (retval);
1917 }
1918 if (count) {
1919 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001920 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001921 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001922 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001924 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001927static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Borislav Petkovd236d742008-04-18 00:46:27 +02001929 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* Write a filemark */
1932 idetape_create_write_filemark_cmd(drive, &pc, 1);
1933 if (idetape_queue_pc_tail(drive, &pc)) {
1934 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1935 return -EIO;
1936 }
1937 return 0;
1938}
1939
1940/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001941 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1942 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001944 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1945 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001946 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001948 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001950 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1951 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001953static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001956 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001957 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001959 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1960 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001963 case MTFSF:
1964 case MTFSFM:
1965 case MTBSF:
1966 case MTBSFM:
1967 if (!mt_count)
1968 return 0;
1969 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1970 default:
1971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001975 case MTWEOF:
1976 if (tape->write_prot)
1977 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001978 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001979 for (i = 0; i < mt_count; i++) {
1980 retval = idetape_write_filemark(drive);
1981 if (retval)
1982 return retval;
1983 }
1984 return 0;
1985 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001986 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001987 if (idetape_rewind_tape(drive))
1988 return -EIO;
1989 return 0;
1990 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001991 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001992 idetape_create_load_unload_cmd(drive, &pc,
1993 IDETAPE_LU_LOAD_MASK);
1994 return idetape_queue_pc_tail(drive, &pc);
1995 case MTUNLOAD:
1996 case MTOFFL:
1997 /*
1998 * If door is locked, attempt to unlock before
1999 * attempting to eject.
2000 */
2001 if (tape->door_locked) {
2002 if (idetape_create_prevent_cmd(drive, &pc, 0))
2003 if (!idetape_queue_pc_tail(drive, &pc))
2004 tape->door_locked = DOOR_UNLOCKED;
2005 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002006 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002007 idetape_create_load_unload_cmd(drive, &pc,
2008 !IDETAPE_LU_LOAD_MASK);
2009 retval = idetape_queue_pc_tail(drive, &pc);
2010 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002011 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002012 return retval;
2013 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002014 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002015 return idetape_flush_tape_buffers(drive);
2016 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002017 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002018 idetape_create_load_unload_cmd(drive, &pc,
2019 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2020 return idetape_queue_pc_tail(drive, &pc);
2021 case MTEOM:
2022 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2023 return idetape_queue_pc_tail(drive, &pc);
2024 case MTERASE:
2025 (void)idetape_rewind_tape(drive);
2026 idetape_create_erase_cmd(&pc);
2027 return idetape_queue_pc_tail(drive, &pc);
2028 case MTSETBLK:
2029 if (mt_count) {
2030 if (mt_count < tape->blk_size ||
2031 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002033 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002034 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002035 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002036 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002037 return 0;
2038 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002039 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002040 return idetape_position_tape(drive,
2041 mt_count * tape->user_bs_factor, tape->partition, 0);
2042 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002043 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002044 return idetape_position_tape(drive, 0, mt_count, 0);
2045 case MTFSR:
2046 case MTBSR:
2047 case MTLOCK:
2048 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002050 retval = idetape_queue_pc_tail(drive, &pc);
2051 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002053 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2054 return 0;
2055 case MTUNLOCK:
2056 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002058 retval = idetape_queue_pc_tail(drive, &pc);
2059 if (retval)
2060 return retval;
2061 tape->door_locked = DOOR_UNLOCKED;
2062 return 0;
2063 default:
2064 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2065 mt_op);
2066 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068}
2069
2070/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002071 * Our character device ioctls. General mtio.h magnetic io commands are
2072 * supported here, and not in the corresponding block interface. Our own
2073 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002075static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2076 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 struct ide_tape_obj *tape = ide_tape_f(file);
2079 ide_drive_t *drive = tape->drive;
2080 struct mtop mtop;
2081 struct mtget mtget;
2082 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002083 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 void __user *argp = (void __user *)arg;
2085
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002086 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Borislav Petkov54abf372008-02-06 02:57:52 +01002088 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002089 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 idetape_flush_tape_buffers(drive);
2091 }
2092 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002093 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002094 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002095 position = idetape_read_position(drive);
2096 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return -EIO;
2098 }
2099 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002100 case MTIOCTOP:
2101 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2102 return -EFAULT;
2103 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2104 case MTIOCGET:
2105 memset(&mtget, 0, sizeof(struct mtget));
2106 mtget.mt_type = MT_ISSCSI2;
2107 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2108 mtget.mt_dsreg =
2109 ((tape->blk_size * tape->user_bs_factor)
2110 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002111
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002112 if (tape->drv_write_prot)
2113 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2114
2115 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2116 return -EFAULT;
2117 return 0;
2118 case MTIOCPOS:
2119 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2120 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2121 return -EFAULT;
2122 return 0;
2123 default:
2124 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002125 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002126 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128}
2129
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002130/*
2131 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2132 * block size with the reported value.
2133 */
2134static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2135{
2136 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002137 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002138
2139 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2140 if (idetape_queue_pc_tail(drive, &pc)) {
2141 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002142 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002143 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2144 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002145 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002146 }
2147 return;
2148 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002149 tape->blk_size = (pc.buf[4 + 5] << 16) +
2150 (pc.buf[4 + 6] << 8) +
2151 pc.buf[4 + 7];
2152 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002155static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2158 ide_drive_t *drive;
2159 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002160 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int retval;
2162
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002163 if (i >= MAX_HWIFS * MAX_DRIVES)
2164 return -ENXIO;
2165
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002166 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002167 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002168 if (!tape) {
2169 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002170 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002171 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002172
2173 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /*
2176 * We really want to do nonseekable_open(inode, filp); here, but some
2177 * versions of tar incorrectly call lseek on tapes and bail out if that
2178 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2179 */
2180 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 drive = tape->drive;
2183
2184 filp->private_data = tape;
2185
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002186 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 retval = -EBUSY;
2188 goto out_put_tape;
2189 }
2190
2191 retval = idetape_wait_ready(drive, 60 * HZ);
2192 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002193 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2195 goto out_put_tape;
2196 }
2197
2198 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002199 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 (void)idetape_rewind_tape(drive);
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002203 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /* Set write protect flag if device is opened as read-only. */
2206 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2207 tape->write_prot = 1;
2208 else
2209 tape->write_prot = tape->drv_write_prot;
2210
2211 /* Make sure drive isn't write protected if user wants to write. */
2212 if (tape->write_prot) {
2213 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2214 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002215 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 retval = -EROFS;
2217 goto out_put_tape;
2218 }
2219 }
2220
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002221 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002222 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2224 if (!idetape_queue_pc_tail(drive, &pc)) {
2225 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2226 tape->door_locked = DOOR_LOCKED;
2227 }
2228 }
2229 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002230 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 0;
2232
2233out_put_tape:
2234 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002235 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return retval;
2237}
2238
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002239static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 idetape_tape_t *tape = drive->driver_data;
2242
Borislav Petkovd9df9372008-04-27 15:38:34 +02002243 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002244 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2245 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002246 idetape_pad_zeros(drive, tape->blk_size *
2247 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002248 ide_tape_kfree_buffer(tape);
2249 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251 idetape_write_filemark(drive);
2252 idetape_flush_tape_buffers(drive);
2253 idetape_flush_tape_buffers(drive);
2254}
2255
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002256static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 struct ide_tape_obj *tape = ide_tape_f(filp);
2259 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002260 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 unsigned int minor = iminor(inode);
2262
2263 lock_kernel();
2264 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002265
2266 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Borislav Petkov54abf372008-02-06 02:57:52 +01002268 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002270 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002272 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002274
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002275 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002277 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (tape->door_locked == DOOR_LOCKED) {
2279 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2280 if (!idetape_queue_pc_tail(drive, &pc))
2281 tape->door_locked = DOOR_UNLOCKED;
2282 }
2283 }
2284 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002285 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 ide_tape_put(tape);
2287 unlock_kernel();
2288 return 0;
2289}
2290
2291/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002292 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002294 * 1 - If the tape can be supported by us, based on the information we have so
2295 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002297 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002299static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Borislav Petkov71071b82008-02-06 02:57:53 +01002301 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (drive->id_read == 0)
2304 return 1;
2305
Borislav Petkov71071b82008-02-06 02:57:53 +01002306 *((unsigned short *) &gcw) = drive->id->config;
2307
2308 protocol = (gcw[1] & 0xC0) >> 6;
2309 device_type = gcw[1] & 0x1F;
2310 removable = !!(gcw[0] & 0x80);
2311 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002314 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002315 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002316 protocol);
2317 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002318 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002319 "to tape\n", device_type);
2320 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002322 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002323 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2324 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 } else
2326 return 1;
2327 return 0;
2328}
2329
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002330static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002333 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002334 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 idetape_create_inquiry_cmd(&pc);
2337 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002338 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2339 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return;
2341 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002342 memcpy(vendor_id, &pc.buf[8], 8);
2343 memcpy(product_id, &pc.buf[16], 16);
2344 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002345
Borislav Petkov41f81d542008-02-06 02:57:52 +01002346 ide_fixstring(vendor_id, 10, 0);
2347 ide_fixstring(product_id, 18, 0);
2348 ide_fixstring(fw_rev, 6, 0);
2349
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002350 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002351 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
2354/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002355 * Ask the tape about its various parameters. In particular, we will adjust our
2356 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002358static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002361 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002362 u8 *caps;
2363 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2366 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002367 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2368 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002369 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002370 put_unaligned(52, (u16 *)&tape->caps[12]);
2371 put_unaligned(540, (u16 *)&tape->caps[14]);
2372 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return;
2374 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002375 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Borislav Petkovb6422012008-02-02 19:56:49 +01002377 /* convert to host order and save for later use */
2378 speed = be16_to_cpu(*(u16 *)&caps[14]);
2379 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Borislav Petkovb6422012008-02-02 19:56:49 +01002381 put_unaligned(max_speed, (u16 *)&caps[8]);
2382 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2383 put_unaligned(speed, (u16 *)&caps[14]);
2384 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2385
2386 if (!speed) {
2387 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2388 "(assuming 650KB/sec)\n", drive->name);
2389 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002391 if (!max_speed) {
2392 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2393 "(assuming 650KB/sec)\n", drive->name);
2394 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396
Borislav Petkovb6422012008-02-02 19:56:49 +01002397 memcpy(&tape->caps, caps, 20);
2398 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002399 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002400 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002401 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002404#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002405static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 idetape_tape_t *tape = drive->driver_data;
2408
Borislav Petkovb6422012008-02-02 19:56:49 +01002409 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2410 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002411 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2412 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkovf73850a2008-04-27 15:38:33 +02002413 ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
2414 1, 1024, &tape->buffer_size, NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002415 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2416 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2417 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002418 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2419 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002420 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2421 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002422 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2423 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002425#else
2426static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002430 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002432 * 1. Initialize our various state variables.
2433 * 2. Ask the tape for its capabilities.
2434 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2435 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002437 * Note that at this point ide.c already assigned us an irq, so that we can
2438 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002440static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Borislav Petkov83042b22008-04-27 15:38:27 +02002442 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002444 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002445 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002446 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Borislav Petkov776bb022008-07-23 19:55:59 +02002448 drive->pc_callback = ide_tape_callback;
2449
Borislav Petkov54bb2072008-02-06 02:57:52 +01002450 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002452 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2453 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2454 tape->name);
2455 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /* Seagate Travan drives do not support DSC overlap. */
2458 if (strstr(drive->id->model, "Seagate STT3401"))
2459 drive->dsc_overlap = 0;
2460 tape->minor = minor;
2461 tape->name[0] = 'h';
2462 tape->name[1] = 't';
2463 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002464 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002467
2468 /* Command packet DRQ type */
2469 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002470 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 idetape_get_inquiry_results(drive);
2473 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002474 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002476 tape->buffer_size = *ctl * tape->blk_size;
2477 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002479 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002480 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002482 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002483 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002484 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002485 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002486 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
Borislav Petkov83042b22008-04-27 15:38:27 +02002489 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002490 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Borislav Petkovf73850a2008-04-27 15:38:33 +02002492 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002495 * Ensure that the number we got makes sense; limit it within
2496 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002498 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2499 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002501 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002502 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002503 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2504 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002505 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 drive->using_dma ? ", DMA":"");
2507
2508 idetape_add_settings(drive);
2509}
2510
Russell King4031bbe2006-01-06 11:41:00 +00002511static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002515 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 ide_unregister_region(tape->disk);
2518
2519 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522static void ide_tape_release(struct kref *kref)
2523{
2524 struct ide_tape_obj *tape = to_ide_tape(kref);
2525 ide_drive_t *drive = tape->drive;
2526 struct gendisk *g = tape->disk;
2527
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002528 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 drive->dsc_overlap = 0;
2531 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002532 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002533 device_destroy(idetape_sysfs_class,
2534 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 idetape_devs[tape->minor] = NULL;
2536 g->private_data = NULL;
2537 put_disk(g);
2538 kfree(tape);
2539}
2540
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002541#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542static int proc_idetape_read_name
2543 (char *page, char **start, off_t off, int count, int *eof, void *data)
2544{
2545 ide_drive_t *drive = (ide_drive_t *) data;
2546 idetape_tape_t *tape = drive->driver_data;
2547 char *out = page;
2548 int len;
2549
2550 len = sprintf(out, "%s\n", tape->name);
2551 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2552}
2553
2554static ide_proc_entry_t idetape_proc[] = {
2555 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2556 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2557 { NULL, 0, NULL, NULL }
2558};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559#endif
2560
Russell King4031bbe2006-01-06 11:41:00 +00002561static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002564 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002565 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002566 .name = "ide-tape",
2567 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002568 },
Russell King4031bbe2006-01-06 11:41:00 +00002569 .probe = ide_tape_probe,
2570 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .version = IDETAPE_VERSION,
2572 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 .do_request = idetape_do_request,
2575 .end_request = idetape_end_request,
2576 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002577#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580};
2581
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002582/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002583static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 .owner = THIS_MODULE,
2585 .read = idetape_chrdev_read,
2586 .write = idetape_chrdev_write,
2587 .ioctl = idetape_chrdev_ioctl,
2588 .open = idetape_chrdev_open,
2589 .release = idetape_chrdev_release,
2590};
2591
2592static int idetape_open(struct inode *inode, struct file *filp)
2593{
2594 struct gendisk *disk = inode->i_bdev->bd_disk;
2595 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002597 tape = ide_tape_get(disk);
2598 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return -ENXIO;
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
2602}
2603
2604static int idetape_release(struct inode *inode, struct file *filp)
2605{
2606 struct gendisk *disk = inode->i_bdev->bd_disk;
2607 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 ide_tape_put(tape);
2610
2611 return 0;
2612}
2613
2614static int idetape_ioctl(struct inode *inode, struct file *file,
2615 unsigned int cmd, unsigned long arg)
2616{
2617 struct block_device *bdev = inode->i_bdev;
2618 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2619 ide_drive_t *drive = tape->drive;
2620 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2621 if (err == -EINVAL)
2622 err = idetape_blkdev_ioctl(drive, cmd, arg);
2623 return err;
2624}
2625
2626static struct block_device_operations idetape_block_ops = {
2627 .owner = THIS_MODULE,
2628 .open = idetape_open,
2629 .release = idetape_release,
2630 .ioctl = idetape_ioctl,
2631};
2632
Russell King4031bbe2006-01-06 11:41:00 +00002633static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 idetape_tape_t *tape;
2636 struct gendisk *g;
2637 int minor;
2638
2639 if (!strstr("ide-tape", drive->driver_req))
2640 goto failed;
2641 if (!drive->present)
2642 goto failed;
2643 if (drive->media != ide_tape)
2644 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002645 if (!idetape_identify_device(drive)) {
2646 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2647 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 goto failed;
2649 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002650 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002652 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2653 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 goto failed;
2655 }
2656
2657 g = alloc_disk(1 << PARTN_BITS);
2658 if (!g)
2659 goto out_free_tape;
2660
2661 ide_init_disk(g, drive);
2662
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002663 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 kref_init(&tape->kref);
2666
2667 tape->drive = drive;
2668 tape->driver = &idetape_driver;
2669 tape->disk = g;
2670
2671 g->private_data = &tape->driver;
2672
2673 drive->driver_data = tape;
2674
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002675 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 for (minor = 0; idetape_devs[minor]; minor++)
2677 ;
2678 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002679 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 idetape_setup(drive, tape, minor);
2682
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002683 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2684 MKDEV(IDETAPE_MAJOR, minor), NULL,
2685 "%s", tape->name);
2686 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2687 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2688 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 g->fops = &idetape_block_ops;
2691 ide_register_region(g);
2692
2693 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695out_free_tape:
2696 kfree(tape);
2697failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002698 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699}
2700
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002701static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002703 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002704 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 unregister_chrdev(IDETAPE_MAJOR, "ht");
2706}
2707
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002708static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Will Dysond5dee802005-09-16 02:55:07 -07002710 int error = 1;
2711 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2712 if (IS_ERR(idetape_sysfs_class)) {
2713 idetape_sysfs_class = NULL;
2714 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2715 error = -EBUSY;
2716 goto out;
2717 }
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002720 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2721 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002722 error = -EBUSY;
2723 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 }
Will Dysond5dee802005-09-16 02:55:07 -07002725
2726 error = driver_register(&idetape_driver.gen_driver);
2727 if (error)
2728 goto out_free_driver;
2729
2730 return 0;
2731
2732out_free_driver:
2733 driver_unregister(&idetape_driver.gen_driver);
2734out_free_class:
2735 class_destroy(idetape_sysfs_class);
2736out:
2737 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Kay Sievers263756e2005-12-12 18:03:44 +01002740MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741module_init(idetape_init);
2742module_exit(idetape_exit);
2743MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002744MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2745MODULE_LICENSE("GPL");