blob: 26d5b0576f0afbf94964a15c549952855dd1b24e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-tape"
19
Borislav Petkovdfe79932008-02-06 02:57:55 +010020#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/genhd.h>
34#include <linux/slab.h>
35#include <linux/pci.h>
36#include <linux/ide.h>
37#include <linux/smp_lock.h>
38#include <linux/completion.h>
39#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtio.h>
49
Borislav Petkov8004a8c2008-02-06 02:57:51 +010050enum {
51 /* output errors only */
52 DBG_ERR = (1 << 0),
53 /* output all sense key/asc */
54 DBG_SENSE = (1 << 1),
55 /* info regarding all chrdev-related procedures */
56 DBG_CHRDEV = (1 << 2),
57 /* all remaining procedures */
58 DBG_PROCS = (1 << 3),
Borislav Petkov8004a8c2008-02-06 02:57:51 +010059};
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Some drives (for example, Seagate STT3401A Travan) require a very long
91 * timeout, because they don't return an interrupt or clear their busy bit
92 * until after the command completes (even retension commands).
93 */
94#define IDETAPE_WAIT_CMD (900*HZ)
95
96/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * The following parameter is used to select the point in the internal tape fifo
98 * in which we will start to refill the buffer. Decreasing the following
99 * parameter will improve the system's latency and interactive response, while
100 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100102#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100105 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100107 * Polling for DSC (a single bit in the status register) is a very important
108 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100110 * 1. Before a read/write packet command, to ensure that we can transfer data
111 * from/to the tape's data buffers, without causing an actual media access.
112 * In case the tape is not ready yet, we take out our request from the device
113 * request queue, so that ide.c could service requests from the other device
114 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100116 * 2. After the successful initialization of a "media access packet command",
117 * which is a command that can take a long time to complete (the interval can
118 * range from several seconds to even an hour). Again, we postpone our request
119 * in the middle to free the bus for the other device. The polling frequency
120 * here should be lower than the read/write frequency since those media access
121 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
122 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
123 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100125 * We also set a timeout for the timer, in case something goes wrong. The
126 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100128
129/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
131#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
132#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
133#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
134#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
135#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
136#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
137
138/*************************** End of tunable parameters ***********************/
139
Borislav Petkov54abf372008-02-06 02:57:52 +0100140/* tape directions */
141enum {
142 IDETAPE_DIR_NONE = (1 << 0),
143 IDETAPE_DIR_READ = (1 << 1),
144 IDETAPE_DIR_WRITE = (1 << 2),
145};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200148 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 atomic_t b_count;
150 struct idetape_bh *b_reqnext;
151 char *b_data;
152};
153
Borislav Petkov03056b92008-04-18 00:46:26 +0200154/* Tape door status */
155#define DOOR_UNLOCKED 0
156#define DOOR_LOCKED 1
157#define DOOR_EXPLICITLY_LOCKED 2
158
159/* Some defines for the SPACE command */
160#define IDETAPE_SPACE_OVER_FILEMARK 1
161#define IDETAPE_SPACE_TO_EOD 3
162
163/* Some defines for the LOAD UNLOAD command */
164#define IDETAPE_LU_LOAD_MASK 1
165#define IDETAPE_LU_RETENSION_MASK 2
166#define IDETAPE_LU_EOT_MASK 4
167
168/*
169 * Special requests for our block device strategy routine.
170 *
171 * In order to service a character device command, we add special requests to
172 * the tail of our block device request queue and wait for their completion.
173 */
174
175enum {
176 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
177 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
178 REQ_IDETAPE_READ = (1 << 2),
179 REQ_IDETAPE_WRITE = (1 << 3),
180};
181
182/* Error codes returned in rq->errors to the higher part of the driver. */
183#define IDETAPE_ERROR_GENERAL 101
184#define IDETAPE_ERROR_FILEMARK 102
185#define IDETAPE_ERROR_EOD 103
186
187/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
188#define IDETAPE_BLOCK_DESCRIPTOR 0
189#define IDETAPE_CAPABILITIES_PAGE 0x2a
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100192 * Most of our global data which we need to save even as we leave the driver due
193 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195typedef struct ide_tape_obj {
196 ide_drive_t *drive;
197 ide_driver_t *driver;
198 struct gendisk *disk;
199 struct kref kref;
200
201 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * pc points to the current processed packet command.
203 *
204 * failed_pc points to the last failed packet command, or contains
205 * NULL if we do not need to retry any packet command. This is
206 * required since an additional packet command is needed before the
207 * retry, to get detailed information on what went wrong.
208 */
209 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200210 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200212 struct ide_atapi_pc *failed_pc;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200213 /* used by REQ_IDETAPE_{READ,WRITE} requests */
214 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200215
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200216 struct ide_atapi_pc request_sense_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200217 struct request request_sense_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100220 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100222 * While polling for DSC we use postponed_rq to postpone the current
223 * request so that ide.c will be able to service pending requests on the
224 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200225 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227 struct request *postponed_rq;
228 /* The time in which we started polling for DSC */
229 unsigned long dsc_polling_start;
230 /* Timer used to poll for dsc */
231 struct timer_list dsc_timer;
232 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100233 unsigned long best_dsc_rw_freq;
234 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 unsigned long dsc_timeout;
236
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100237 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 u8 partition;
239 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100240 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100242 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 u8 sense_key, asc, ascq;
244
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100245 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned int minor;
247 /* device name */
248 char name[4];
249 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100250 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Borislav Petkov54bb2072008-02-06 02:57:52 +0100252 /* tape block size, usually 512 or 1024 bytes */
253 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100257 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100260 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100262 * At most, there is only one ide-tape originated data transfer request
263 * in the device request queue. This allows ide.c to easily service
264 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200266
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100267 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200268 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200269 /* merge buffer */
270 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200271 /* size of the merge buffer */
272 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200273 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct idetape_bh *bh;
275 char *b_data;
276 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100277
Borislav Petkova997a432008-04-27 15:38:33 +0200278 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* Wasted space in each stage */
280 int excess_bh_size;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100283 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100285 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned long avg_time;
287 int avg_size;
288 int avg_speed;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* the door is currently locked */
291 int door_locked;
292 /* the tape hardware is write protected */
293 char drv_write_prot;
294 /* the tape is write protected (hardware or opened as read-only) */
295 char write_prot;
296
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100297 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298} idetape_tape_t;
299
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800300static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Will Dysond5dee802005-09-16 02:55:07 -0700302static struct class *idetape_sysfs_class;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
305
306#define ide_tape_g(disk) \
307 container_of((disk)->private_data, struct ide_tape_obj, driver)
308
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200309static void ide_tape_release(struct kref *);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
312{
313 struct ide_tape_obj *tape = NULL;
314
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800315 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 tape = ide_tape_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200317 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200318 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200319 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200320 else
321 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200322 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800323 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return tape;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static void ide_tape_put(struct ide_tape_obj *tape)
328{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200329 ide_drive_t *drive = tape->drive;
330
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800331 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 kref_put(&tape->kref, ide_tape_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200333 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800334 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100338 * The variables below are used for the character device interface. Additional
339 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100341static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343#define ide_tape_f(file) ((file)->private_data)
344
345static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
346{
347 struct ide_tape_obj *tape = NULL;
348
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800349 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 tape = idetape_devs[i];
351 if (tape)
352 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800353 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return tape;
355}
356
Borislav Petkovd236d742008-04-18 00:46:27 +0200357static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100358 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct idetape_bh *bh = pc->bh;
361 int count;
362
363 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (bh == NULL) {
365 printk(KERN_ERR "ide-tape: bh == NULL in "
366 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200367 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return;
369 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100370 count = min(
371 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
372 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200373 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100374 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 bcount -= count;
376 atomic_add(count, &bh->b_count);
377 if (atomic_read(&bh->b_count) == bh->b_size) {
378 bh = bh->b_reqnext;
379 if (bh)
380 atomic_set(&bh->b_count, 0);
381 }
382 }
383 pc->bh = bh;
384}
385
Borislav Petkovd236d742008-04-18 00:46:27 +0200386static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100387 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct idetape_bh *bh = pc->bh;
390 int count;
391
392 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100394 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
395 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200399 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 bcount -= count;
401 pc->b_data += count;
402 pc->b_count -= count;
403 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100404 bh = bh->b_reqnext;
405 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (bh) {
407 pc->b_data = bh->b_data;
408 pc->b_count = atomic_read(&bh->b_count);
409 }
410 }
411 }
412}
413
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200414static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct idetape_bh *bh = pc->bh;
417 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200418 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Borislav Petkov346331f2008-04-18 00:46:26 +0200420 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return;
422 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100424 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
425 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
429 atomic_set(&bh->b_count, count);
430 if (atomic_read(&bh->b_count) == bh->b_size)
431 bh = bh->b_reqnext;
432 bcount -= count;
433 }
434 pc->bh = bh;
435}
436
437/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100438 * called on each failed packet command retry to analyze the request sense. We
439 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100441static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200444 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Borislav Petkov1b5db432008-02-02 19:56:48 +0100446 tape->sense_key = sense[2] & 0xF;
447 tape->asc = sense[12];
448 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100449
450 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
451 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Borislav Petkovd236d742008-04-18 00:46:27 +0200453 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200454 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200455 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100456 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200457 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200458 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 /*
462 * If error was the result of a zero-length read or write command,
463 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
464 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
465 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100466 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100467 /* length == 0 */
468 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
469 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* don't report an error, everything's ok */
471 pc->error = 0;
472 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200473 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100476 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200478 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100480 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100481 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
482 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200484 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100487 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100488 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200490 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200492 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200493 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
495 }
496}
497
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200498/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200499static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200501 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200503 while (bh) {
504 u32 size = bh->b_size;
505
506 while (size) {
507 unsigned int order = fls(size >> PAGE_SHIFT)-1;
508
509 if (bh->b_data)
510 free_pages((unsigned long)bh->b_data, order);
511
512 size &= (order-1);
513 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 prev_bh = bh;
516 bh = bh->b_reqnext;
517 kfree(prev_bh);
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
522{
523 struct request *rq = HWGROUP(drive)->rq;
524 idetape_tape_t *tape = drive->driver_data;
525 unsigned long flags;
526 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100528 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100531 case 0: error = IDETAPE_ERROR_GENERAL; break;
532 case 1: error = 0; break;
533 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 rq->errors = error;
536 if (error)
537 tape->failed_pc = NULL;
538
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100539 if (!blk_special_request(rq)) {
540 ide_end_request(drive, uptodate, nr_sects);
541 return 0;
542 }
543
Borislav Petkov54bb2072008-02-06 02:57:52 +0100544 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Borislav Petkov54bb2072008-02-06 02:57:52 +0100548 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
550}
551
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200552static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200555 struct ide_atapi_pc *pc = tape->pc;
556 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100558 debug_log(DBG_PROCS, "Enter %s\n", __func__);
559
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200560 if (tape->failed_pc == pc)
561 tape->failed_pc = NULL;
562
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200563 if (pc->c[0] == REQUEST_SENSE) {
564 if (uptodate)
565 idetape_analyze_error(drive, pc->buf);
566 else
567 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
568 "itself - Aborting request!\n");
569 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
570 struct request *rq = drive->hwif->hwgroup->rq;
571 int blocks = pc->xferred / tape->blk_size;
572
573 tape->avg_size += blocks * tape->blk_size;
574
575 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
576 tape->avg_speed = tape->avg_size * HZ /
577 (jiffies - tape->avg_time) / 1024;
578 tape->avg_size = 0;
579 tape->avg_time = jiffies;
580 }
581
582 tape->first_frame += blocks;
583 rq->current_nr_sectors -= blocks;
584
585 if (pc->error)
586 uptodate = pc->error;
587 } else if (pc->c[0] == READ_POSITION && uptodate) {
588 u8 *readpos = tape->pc->buf;
589
590 debug_log(DBG_SENSE, "BOP - %s\n",
591 (readpos[0] & 0x80) ? "Yes" : "No");
592 debug_log(DBG_SENSE, "EOP - %s\n",
593 (readpos[0] & 0x40) ? "Yes" : "No");
594
595 if (readpos[0] & 0x4) {
596 printk(KERN_INFO "ide-tape: Block location is unknown"
597 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200598 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200599 uptodate = 0;
600 } else {
601 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200602 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200603
604 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200605 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200606 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200609
610 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200613static void idetape_init_pc(struct ide_atapi_pc *pc)
614{
615 memset(pc->c, 0, 12);
616 pc->retries = 0;
617 pc->flags = 0;
618 pc->req_xfer = 0;
619 pc->buf = pc->pc_buf;
620 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
621 pc->bh = NULL;
622 pc->b_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Borislav Petkovd236d742008-04-18 00:46:27 +0200625static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100627 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100628 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200630 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100634 * Generate a new packet command request in front of the request queue, before
635 * the current request, so that it will be processed immediately, on the next
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200636 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200638static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100639 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct ide_tape_obj *tape = drive->driver_data;
642
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200643 blk_rq_init(NULL, rq);
644 rq->cmd_type = REQ_TYPE_SPECIAL;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200645 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 rq->buffer = (char *) pc;
647 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200648 memcpy(rq->cmd, pc->c, 12);
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200649 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200650 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/*
654 * idetape_retry_pc is called when an error was detected during the
655 * last packet command. We queue a request sense packet command in
656 * the head of the request list.
657 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200658static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200660 struct ide_tape_obj *tape = drive->driver_data;
661 struct request *rq = &tape->request_sense_rq;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200662 struct ide_atapi_pc *pc = &tape->request_sense_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100664 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200666 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100671 * Postpone the current request so that ide.c will be able to service requests
672 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100674static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 idetape_tape_t *tape = drive->driver_data;
677
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100678 debug_log(DBG_PROCS, "Enter %s\n", __func__);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100681 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200684static void ide_tape_handle_dsc(ide_drive_t *drive)
685{
686 idetape_tape_t *tape = drive->driver_data;
687
688 /* Media access command */
689 tape->dsc_polling_start = jiffies;
690 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
691 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
692 /* Allow ide.c to handle other requests */
693 idetape_postpone_request(drive);
694}
695
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200696static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200697 unsigned int bcount, int write)
698{
699 if (write)
700 idetape_output_buffers(drive, pc, bcount);
701 else
702 idetape_input_buffers(drive, pc, bcount);
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200703
704 return bcount;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200705}
Borislav Petkova1efc852008-02-06 02:57:52 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100708 * This is the usual interrupt handler which will be called during a packet
709 * command. We will transfer some of the data (as requested by the drive) and
710 * will re-point interrupt handler to us. When data transfer is finished, we
711 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100712 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100714static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200718 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
719 NULL, idetape_update_buffers, idetape_retry_pc,
720 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100724 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100726 * The current Packet Command is available in tape->pc, and will not change
727 * until we finish handling it. Each packet command is associated with a
728 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100730 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100732 * 1. idetape_issue_pc will send the packet command to the drive, and will set
733 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100735 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
736 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100738 * 3. ATAPI Tape media access commands have immediate status with a delayed
739 * process. In case of a successful initiation of a media access packet command,
740 * the DSC bit will be set when the actual execution of the command is finished.
741 * Since the tape drive will not issue an interrupt, we have to poll for this
742 * event. In this case, we define the request as "low priority request" by
743 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
744 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100746 * ide.c will then give higher priority to requests which originate from the
747 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100749 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100751 * 5. In case an error was found, we queue a request sense packet command in
752 * front of the request queue and retry the operation up to
753 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100755 * 6. In case no error was found, or we decided to give up and not to retry
756 * again, the callback function will be called and then we will handle the next
757 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
759static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200763 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
764 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Borislav Petkovd236d742008-04-18 00:46:27 +0200767static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
768 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Borislav Petkov90699ce2008-02-02 19:56:50 +0100772 if (tape->pc->c[0] == REQUEST_SENSE &&
773 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
775 "Two request sense in serial were issued\n");
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Borislav Petkov90699ce2008-02-02 19:56:50 +0100778 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 tape->failed_pc = pc;
780 /* Set the current packet command */
781 tape->pc = pc;
782
783 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200784 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100786 * We will "abort" retrying a packet command in case legitimate
787 * error code was received (crossing a filemark, or end of the
788 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200790 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100791 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 tape->sense_key == 2 && tape->asc == 4 &&
793 (tape->ascq == 1 || tape->ascq == 8))) {
794 printk(KERN_ERR "ide-tape: %s: I/O error, "
795 "pc = %2x, key = %2x, "
796 "asc = %2x, ascq = %2x\n",
797 tape->name, pc->c[0],
798 tape->sense_key, tape->asc,
799 tape->ascq);
800 }
801 /* Giving up */
802 pc->error = IDETAPE_ERROR_GENERAL;
803 }
804 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200805 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200806 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100808 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200812 return ide_issue_pc(drive, pc, idetape_transfer_pc,
813 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100816/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200817static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100820 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100822 /* DBD = 1 - Don't return block descriptors */
823 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 pc->c[2] = page_code;
825 /*
826 * Changed pc->c[3] to 0 (255 will at best return unused info).
827 *
828 * For SCSI this byte is defined as subpage instead of high byte
829 * of length and some IDE drives seem to interpret it this way
830 * and return an error when 255 is used.
831 */
832 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100833 /* We will just discard data in that case */
834 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200836 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200838 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200840 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100843static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200845 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200847 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100848 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200850 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100851
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200852 if (stat & ATA_DSC) {
853 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100855 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 printk(KERN_ERR "ide-tape: %s: I/O error, ",
857 tape->name);
858 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200859 idetape_retry_pc(drive);
860 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 } else {
864 pc->error = IDETAPE_ERROR_GENERAL;
865 tape->failed_pc = NULL;
866 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200867 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return ide_stopped;
869}
870
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200871static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200872 struct ide_atapi_pc *pc, struct request *rq,
873 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Borislav Petkov0014c752008-07-23 19:56:00 +0200875 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
876 unsigned int length = rq->current_nr_sectors;
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100879 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200882 pc->buf = NULL;
883 pc->buf_size = length * tape->blk_size;
884 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200885 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200886 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200888 if (opcode == READ_6) {
889 pc->c[0] = READ_6;
890 atomic_set(&bh->b_count, 0);
891 } else if (opcode == WRITE_6) {
892 pc->c[0] = WRITE_6;
893 pc->flags |= PC_FLAG_WRITING;
894 pc->b_data = bh->b_data;
895 pc->b_count = atomic_read(&bh->b_count);
896 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200897
898 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static ide_startstop_t idetape_do_request(ide_drive_t *drive,
902 struct request *rq, sector_t block)
903{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200904 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200906 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100908 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Mark de Wever730616b2008-10-10 22:39:17 +0200910 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
911 " current_nr_sectors: %u\n",
912 (unsigned long long)rq->sector, rq->nr_sectors,
913 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jens Axboe4aff5e22006-08-10 08:44:47 +0200915 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100916 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200918 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ide_end_request(drive, 0, 0);
920 return ide_stopped;
921 }
922
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100923 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200924 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
925 pc = tape->failed_pc;
926 goto out;
927 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (postponed_rq != NULL)
930 if (rq != postponed_rq) {
931 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
932 "Two DSC requests were queued\n");
933 idetape_end_request(drive, 0, 0);
934 return ide_stopped;
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 tape->postponed_rq = NULL;
938
939 /*
940 * If the tape is still busy, postpone our request and service
941 * the other device meanwhile.
942 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200943 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Borislav Petkov83dd5732008-07-23 19:56:00 +0200945 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200946 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200949 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 drive->post_reset = 0;
951 }
952
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200953 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200954 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (postponed_rq == NULL) {
956 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100957 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
959 } else if (time_after(jiffies, tape->dsc_timeout)) {
960 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
961 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200962 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 idetape_media_access_finished(drive);
964 return ide_stopped;
965 } else {
966 return ide_do_reset(drive);
967 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100968 } else if (time_after(jiffies,
969 tape->dsc_polling_start +
970 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100971 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 idetape_postpone_request(drive);
973 return ide_stopped;
974 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200975 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200976 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200977 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto out;
979 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200980 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200981 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200982 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out;
984 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200985 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200986 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200987 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
988 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto out;
990 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200991 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 idetape_media_access_finished(drive);
993 return ide_stopped;
994 }
995 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200996
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200997out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100998 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001002 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001003 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001004 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001006 * It returns a pointer to the newly allocated buffer, or NULL in case of
1007 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001009static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1010 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001012 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001013 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001014 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 char *b_data = NULL;
1016
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001017 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1018 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (bh == NULL)
1020 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001021
1022 order = fls(pages) - 1;
1023 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001024 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001026 b_allocd = (1 << order) * PAGE_SIZE;
1027 pages &= (order-1);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001030 memset(bh->b_data, 0, b_allocd);
1031 bh->b_reqnext = NULL;
1032 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1034
Borislav Petkov41aa1702008-04-27 15:38:32 +02001035 while (pages) {
1036 order = fls(pages) - 1;
1037 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001038 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001040 b_allocd = (1 << order) * PAGE_SIZE;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001043 memset(b_data, 0, b_allocd);
1044
1045 /* newly allocated page frames below buffer header or ...*/
1046 if (bh->b_data == b_data + b_allocd) {
1047 bh->b_size += b_allocd;
1048 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001050 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 continue;
1052 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001053 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001055 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001057 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 continue;
1059 }
1060 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001061 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1062 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001063 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto abort;
1065 }
1066 bh->b_reqnext = NULL;
1067 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001068 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1070 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001071
1072 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 bh->b_size -= tape->excess_bh_size;
1076 if (full)
1077 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001078 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001080 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return NULL;
1082}
1083
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001084static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001085 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 struct idetape_bh *bh = tape->bh;
1088 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001089 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001093 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1094 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001095 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001097 count = min((unsigned int)
1098 (bh->b_size - atomic_read(&bh->b_count)),
1099 (unsigned int)n);
1100 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1101 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001102 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 n -= count;
1104 atomic_add(count, &bh->b_count);
1105 buf += count;
1106 if (atomic_read(&bh->b_count) == bh->b_size) {
1107 bh = bh->b_reqnext;
1108 if (bh)
1109 atomic_set(&bh->b_count, 0);
1110 }
1111 }
1112 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001116static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001117 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct idetape_bh *bh = tape->bh;
1120 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001121 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001125 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1126 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001127 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001130 if (copy_to_user(buf, tape->b_data, count))
1131 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 n -= count;
1133 tape->b_data += count;
1134 tape->b_count -= count;
1135 buf += count;
1136 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001137 bh = bh->b_reqnext;
1138 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (bh) {
1140 tape->b_data = bh->b_data;
1141 tape->b_count = atomic_read(&bh->b_count);
1142 }
1143 }
1144 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001145 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001148static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001150 struct idetape_bh *bh = tape->merge_bh;
1151 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001152
Borislav Petkov54abf372008-02-06 02:57:52 +01001153 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 atomic_set(&bh->b_count, 0);
1155 else {
1156 tape->b_data = bh->b_data;
1157 tape->b_count = atomic_read(&bh->b_count);
1158 }
1159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001162 * Write a filemark if write_filemark=1. Flush the device buffers without
1163 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001165static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001166 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001169 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001171 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Borislav Petkovd236d742008-04-18 00:46:27 +02001174static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001177 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001181 * We add a special packet command request to the tail of the request queue, and
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +02001182 * wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001184static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001187 struct request *rq;
1188 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001190 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1191 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001192 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001193 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001194 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001195 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1196 blk_put_request(rq);
1197 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Borislav Petkovd236d742008-04-18 00:46:27 +02001200static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1201 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001204 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001206 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1210{
1211 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001212 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int load_attempted = 0;
1214
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001215 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001216 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 timeout += jiffies;
1218 while (time_before(jiffies, timeout)) {
1219 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001220 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
1222 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001223 || (tape->asc == 0x3A)) {
1224 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (load_attempted)
1226 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001227 idetape_create_load_unload_cmd(drive, &pc,
1228 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001229 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 load_attempted = 1;
1231 /* not about to be ready */
1232 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1233 (tape->ascq == 1 || tape->ascq == 8)))
1234 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001235 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 return -EIO;
1238}
1239
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001240static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Borislav Petkovd236d742008-04-18 00:46:27 +02001242 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int rc;
1244
1245 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001246 rc = idetape_queue_pc_tail(drive, &pc);
1247 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return rc;
1249 idetape_wait_ready(drive, 60 * 5 * HZ);
1250 return 0;
1251}
1252
Borislav Petkovd236d742008-04-18 00:46:27 +02001253static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001256 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001257 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001260static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001263 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int position;
1265
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001266 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 idetape_create_read_position_cmd(&pc);
1269 if (idetape_queue_pc_tail(drive, &pc))
1270 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001271 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return position;
1273}
1274
Borislav Petkovd236d742008-04-18 00:46:27 +02001275static void idetape_create_locate_cmd(ide_drive_t *drive,
1276 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001277 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001280 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001282 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001284 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Borislav Petkovd236d742008-04-18 00:46:27 +02001287static int idetape_create_prevent_cmd(ide_drive_t *drive,
1288 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 idetape_tape_t *tape = drive->driver_data;
1291
Borislav Petkovb6422012008-02-02 19:56:49 +01001292 /* device supports locking according to capabilities page */
1293 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295
1296 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001297 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 1;
1300}
1301
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001302static int ide_tape_set_media_lock(ide_drive_t *drive, int on)
1303{
1304 struct ide_atapi_pc pc;
1305
1306 if (!idetape_create_prevent_cmd(drive, &pc, on))
1307 return 0;
1308
1309 return idetape_queue_pc_tail(drive, &pc);
1310}
1311
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001312static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Borislav Petkov54abf372008-02-06 02:57:52 +01001316 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001319 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001320 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001321 if (tape->merge_bh != NULL) {
1322 ide_tape_kfree_buffer(tape);
1323 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Borislav Petkov54abf372008-02-06 02:57:52 +01001326 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001330 * Position the tape to the requested block using the LOCATE packet command.
1331 * A READ POSITION command is then issued to check where we are positioned. Like
1332 * all higher level operations, we queue the commands at the tail of the request
1333 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001335static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1336 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 idetape_tape_t *tape = drive->driver_data;
1339 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001340 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Borislav Petkov54abf372008-02-06 02:57:52 +01001342 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001343 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 idetape_wait_ready(drive, 60 * 5 * HZ);
1345 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1346 retval = idetape_queue_pc_tail(drive, &pc);
1347 if (retval)
1348 return (retval);
1349
1350 idetape_create_read_position_cmd(&pc);
1351 return (idetape_queue_pc_tail(drive, &pc));
1352}
1353
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001354static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001355 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int seek, position;
1359
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001360 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (restore_position) {
1362 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001363 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001365 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001366 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return;
1368 }
1369 }
1370}
1371
1372/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001373 * Generate a read/write request for the block device interface and wait for it
1374 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001376static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1377 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001380 struct request *rq;
1381 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001383 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1384
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001385 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1386 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001387 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001388 rq->rq_disk = tape->disk;
1389 rq->special = (void *)bh;
1390 rq->sector = tape->first_frame;
1391 rq->nr_sectors = blocks;
1392 rq->current_nr_sectors = blocks;
1393 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1394
1395 errors = rq->errors;
1396 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1397 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1400 return 0;
1401
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001402 if (tape->merge_bh)
1403 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001404 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Borislav Petkovd236d742008-04-18 00:46:27 +02001409static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001412 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001413 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001414 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Borislav Petkovd236d742008-04-18 00:46:27 +02001417static void idetape_create_rewind_cmd(ide_drive_t *drive,
1418 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001421 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001422 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Borislav Petkovd236d742008-04-18 00:46:27 +02001425static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001428 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001430 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Borislav Petkovd236d742008-04-18 00:46:27 +02001433static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001436 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001437 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001439 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Borislav Petkov97c566c2008-04-27 15:38:25 +02001442/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001443static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001447 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001449 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001450 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Borislav Petkovd9df9372008-04-27 15:38:34 +02001453static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 idetape_tape_t *tape = drive->driver_data;
1456 int blocks, min;
1457 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001458
Borislav Petkov54abf372008-02-06 02:57:52 +01001459 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001460 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001461 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return;
1463 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001464 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001466 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001468 if (tape->merge_bh_size) {
1469 blocks = tape->merge_bh_size / tape->blk_size;
1470 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 unsigned int i;
1472
1473 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001474 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001475 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 bh = tape->bh->b_reqnext;
1477 while (bh) {
1478 atomic_set(&bh->b_count, 0);
1479 bh = bh->b_reqnext;
1480 }
1481 bh = tape->bh;
1482 while (i) {
1483 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001484 printk(KERN_INFO "ide-tape: bug,"
1485 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001488 min = min(i, (unsigned int)(bh->b_size -
1489 atomic_read(&bh->b_count)));
1490 memset(bh->b_data + atomic_read(&bh->b_count),
1491 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 atomic_add(min, &bh->b_count);
1493 i -= min;
1494 bh = bh->b_reqnext;
1495 }
1496 }
1497 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001498 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001500 if (tape->merge_bh != NULL) {
1501 ide_tape_kfree_buffer(tape);
1502 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001504 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Borislav Petkov83042b22008-04-27 15:38:27 +02001507static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001513 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1514 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001515 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 idetape_flush_tape_buffers(drive);
1517 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001518 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001519 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001520 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001521 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001523 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1524 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001526 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001529 * Issue a read 0 command to ensure that DSC handshake is
1530 * switched from completion mode to buffer available mode.
1531 * No point in issuing this if DSC overlap isn't supported, some
1532 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 */
1534 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001535 bytes_read = idetape_queue_rw_tail(drive,
1536 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001537 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001539 ide_tape_kfree_buffer(tape);
1540 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001541 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return bytes_read;
1543 }
1544 }
1545 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 0;
1548}
1549
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001550/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001551static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001555 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001557 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001558 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return 0;
1560
Borislav Petkov83042b22008-04-27 15:38:27 +02001561 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001563 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001564 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001567static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 idetape_tape_t *tape = drive->driver_data;
1570 struct idetape_bh *bh;
1571 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 while (bcount) {
1574 unsigned int count;
1575
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001576 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001577 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001579 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001581 atomic_set(&bh->b_count,
1582 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1584 count -= atomic_read(&bh->b_count);
1585 bh = bh->b_reqnext;
1586 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001587 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001588 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001593 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1594 * currently support only one partition.
1595 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001596static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001599 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001600 idetape_tape_t *tape;
1601 tape = drive->driver_data;
1602
1603 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 idetape_create_rewind_cmd(drive, &pc);
1606 retval = idetape_queue_pc_tail(drive, &pc);
1607 if (retval)
1608 return retval;
1609
1610 idetape_create_read_position_cmd(&pc);
1611 retval = idetape_queue_pc_tail(drive, &pc);
1612 if (retval)
1613 return retval;
1614 return 0;
1615}
1616
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001617/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001618static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1619 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 void __user *argp = (void __user *)arg;
1623
Borislav Petkovd59823f2008-02-02 19:56:51 +01001624 struct idetape_config {
1625 int dsc_rw_frequency;
1626 int dsc_media_access_frequency;
1627 int nr_stages;
1628 } config;
1629
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001630 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001633 case 0x0340:
1634 if (copy_from_user(&config, argp, sizeof(config)))
1635 return -EFAULT;
1636 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001637 break;
1638 case 0x0350:
1639 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001640 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001641 if (copy_to_user(argp, &config, sizeof(config)))
1642 return -EFAULT;
1643 break;
1644 default:
1645 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647 return 0;
1648}
1649
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001650static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1651 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001654 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001655 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001656 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 if (mt_count == 0)
1659 return 0;
1660 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001661 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001663 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
Borislav Petkov54abf372008-02-06 02:57:52 +01001666 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001667 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001668 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001670 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001674 case MTFSF:
1675 case MTBSF:
1676 idetape_create_space_cmd(&pc, mt_count - count,
1677 IDETAPE_SPACE_OVER_FILEMARK);
1678 return idetape_queue_pc_tail(drive, &pc);
1679 case MTFSFM:
1680 case MTBSFM:
1681 if (!sprev)
1682 return -EIO;
1683 retval = idetape_space_over_filemarks(drive, MTFSF,
1684 mt_count - count);
1685 if (retval)
1686 return retval;
1687 count = (MTBSFM == mt_op ? 1 : -1);
1688 return idetape_space_over_filemarks(drive, MTFSF, count);
1689 default:
1690 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1691 mt_op);
1692 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001697 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001699 * The tape is optimized to maximize throughput when it is transferring an
1700 * integral number of the "continuous transfer limit", which is a parameter of
1701 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001703 * As of version 1.3 of the driver, the character device provides an abstract
1704 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1705 * same backup/restore procedure is supported. The driver will internally
1706 * convert the requests to the recommended transfer unit, so that an unmatch
1707 * between the user's block size to the recommended size will only result in a
1708 * (slightly) increased driver overhead, but will no longer hit performance.
1709 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001711static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1712 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct ide_tape_obj *tape = ide_tape_f(file);
1715 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001716 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001717 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001718 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001720 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Borislav Petkov54abf372008-02-06 02:57:52 +01001722 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001723 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001724 if (count > tape->blk_size &&
1725 (count % tape->blk_size) == 0)
1726 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001728 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001729 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return rc;
1731 if (count == 0)
1732 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001733 if (tape->merge_bh_size) {
1734 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001735 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001736 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001737 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001739 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 count -= actually_read;
1741 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001742 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001743 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (bytes_read <= 0)
1745 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001746 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001747 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 buf += bytes_read;
1749 count -= bytes_read;
1750 actually_read += bytes_read;
1751 }
1752 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001753 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (bytes_read <= 0)
1755 goto finish;
1756 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001757 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001758 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001760 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001763 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001764 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 idetape_space_over_filemarks(drive, MTFSF, 1);
1767 return 0;
1768 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001769
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001770 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001773static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 size_t count, loff_t *ppos)
1775{
1776 struct ide_tape_obj *tape = ide_tape_f(file);
1777 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001778 ssize_t actually_written = 0;
1779 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
1782 /* The drive is write protected. */
1783 if (tape->write_prot)
1784 return -EACCES;
1785
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001786 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001789 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1790 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001791 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001792 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001793 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001795 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001797 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1798 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001800 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001801 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001804 * Issue a write 0 command to ensure that DSC handshake is
1805 * switched from completion mode to buffer available mode. No
1806 * point in issuing this if DSC overlap isn't supported, some
1807 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 */
1809 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001810 ssize_t retval = idetape_queue_rw_tail(drive,
1811 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001812 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001814 ide_tape_kfree_buffer(tape);
1815 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001816 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return retval;
1818 }
1819 }
1820 }
1821 if (count == 0)
1822 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001823 if (tape->merge_bh_size) {
1824 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001825 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001826 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001828 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001829 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001830 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001831 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001832 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001834 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 count -= actually_written;
1836
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001837 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001838 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001839 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001840 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (retval <= 0)
1842 return (retval);
1843 }
1844 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001845 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001846 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001847 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001848 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001849 buf += tape->buffer_size;
1850 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001851 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001852 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (retval <= 0)
1854 return (retval);
1855 }
1856 if (count) {
1857 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001858 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001859 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001860 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001862 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001865static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Borislav Petkovd236d742008-04-18 00:46:27 +02001867 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* Write a filemark */
1870 idetape_create_write_filemark_cmd(drive, &pc, 1);
1871 if (idetape_queue_pc_tail(drive, &pc)) {
1872 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1873 return -EIO;
1874 }
1875 return 0;
1876}
1877
1878/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001879 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1880 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001882 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1883 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001884 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001886 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001888 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1889 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001891static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001894 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001895 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001897 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1898 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001901 case MTFSF:
1902 case MTFSFM:
1903 case MTBSF:
1904 case MTBSFM:
1905 if (!mt_count)
1906 return 0;
1907 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1908 default:
1909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001913 case MTWEOF:
1914 if (tape->write_prot)
1915 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001916 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001917 for (i = 0; i < mt_count; i++) {
1918 retval = idetape_write_filemark(drive);
1919 if (retval)
1920 return retval;
1921 }
1922 return 0;
1923 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001924 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001925 if (idetape_rewind_tape(drive))
1926 return -EIO;
1927 return 0;
1928 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001929 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001930 idetape_create_load_unload_cmd(drive, &pc,
1931 IDETAPE_LU_LOAD_MASK);
1932 return idetape_queue_pc_tail(drive, &pc);
1933 case MTUNLOAD:
1934 case MTOFFL:
1935 /*
1936 * If door is locked, attempt to unlock before
1937 * attempting to eject.
1938 */
1939 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001940 if (!ide_tape_set_media_lock(drive, 0))
1941 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001942 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001943 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001944 idetape_create_load_unload_cmd(drive, &pc,
1945 !IDETAPE_LU_LOAD_MASK);
1946 retval = idetape_queue_pc_tail(drive, &pc);
1947 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001948 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001949 return retval;
1950 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001951 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001952 return idetape_flush_tape_buffers(drive);
1953 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001954 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001955 idetape_create_load_unload_cmd(drive, &pc,
1956 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1957 return idetape_queue_pc_tail(drive, &pc);
1958 case MTEOM:
1959 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1960 return idetape_queue_pc_tail(drive, &pc);
1961 case MTERASE:
1962 (void)idetape_rewind_tape(drive);
1963 idetape_create_erase_cmd(&pc);
1964 return idetape_queue_pc_tail(drive, &pc);
1965 case MTSETBLK:
1966 if (mt_count) {
1967 if (mt_count < tape->blk_size ||
1968 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001970 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001971 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001972 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001973 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001974 return 0;
1975 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001976 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001977 return idetape_position_tape(drive,
1978 mt_count * tape->user_bs_factor, tape->partition, 0);
1979 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001980 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981 return idetape_position_tape(drive, 0, mt_count, 0);
1982 case MTFSR:
1983 case MTBSR:
1984 case MTLOCK:
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001985 retval = ide_tape_set_media_lock(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001986 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001988 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1989 return 0;
1990 case MTUNLOCK:
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001991 retval = ide_tape_set_media_lock(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001992 if (retval)
1993 return retval;
1994 tape->door_locked = DOOR_UNLOCKED;
1995 return 0;
1996 default:
1997 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1998 mt_op);
1999 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001}
2002
2003/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002004 * Our character device ioctls. General mtio.h magnetic io commands are
2005 * supported here, and not in the corresponding block interface. Our own
2006 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002008static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2009 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct ide_tape_obj *tape = ide_tape_f(file);
2012 ide_drive_t *drive = tape->drive;
2013 struct mtop mtop;
2014 struct mtget mtget;
2015 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002016 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 void __user *argp = (void __user *)arg;
2018
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002019 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Borislav Petkov54abf372008-02-06 02:57:52 +01002021 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002022 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 idetape_flush_tape_buffers(drive);
2024 }
2025 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002026 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002027 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002028 position = idetape_read_position(drive);
2029 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return -EIO;
2031 }
2032 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002033 case MTIOCTOP:
2034 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2035 return -EFAULT;
2036 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2037 case MTIOCGET:
2038 memset(&mtget, 0, sizeof(struct mtget));
2039 mtget.mt_type = MT_ISSCSI2;
2040 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2041 mtget.mt_dsreg =
2042 ((tape->blk_size * tape->user_bs_factor)
2043 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002044
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002045 if (tape->drv_write_prot)
2046 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2047
2048 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2049 return -EFAULT;
2050 return 0;
2051 case MTIOCPOS:
2052 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2053 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2054 return -EFAULT;
2055 return 0;
2056 default:
2057 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002058 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002059 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061}
2062
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002063/*
2064 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2065 * block size with the reported value.
2066 */
2067static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2068{
2069 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002070 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002071
2072 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2073 if (idetape_queue_pc_tail(drive, &pc)) {
2074 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002075 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002076 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2077 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002078 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002079 }
2080 return;
2081 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002082 tape->blk_size = (pc.buf[4 + 5] << 16) +
2083 (pc.buf[4 + 6] << 8) +
2084 pc.buf[4 + 7];
2085 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002088static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2091 ide_drive_t *drive;
2092 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int retval;
2094
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002095 if (i >= MAX_HWIFS * MAX_DRIVES)
2096 return -ENXIO;
2097
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002098 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002099 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002100 if (!tape) {
2101 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002102 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002103 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002104
2105 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * We really want to do nonseekable_open(inode, filp); here, but some
2109 * versions of tar incorrectly call lseek on tapes and bail out if that
2110 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2111 */
2112 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 drive = tape->drive;
2115
2116 filp->private_data = tape;
2117
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002118 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 retval = -EBUSY;
2120 goto out_put_tape;
2121 }
2122
2123 retval = idetape_wait_ready(drive, 60 * HZ);
2124 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002125 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2127 goto out_put_tape;
2128 }
2129
2130 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002131 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 (void)idetape_rewind_tape(drive);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002135 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 /* Set write protect flag if device is opened as read-only. */
2138 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2139 tape->write_prot = 1;
2140 else
2141 tape->write_prot = tape->drv_write_prot;
2142
2143 /* Make sure drive isn't write protected if user wants to write. */
2144 if (tape->write_prot) {
2145 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2146 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002147 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 retval = -EROFS;
2149 goto out_put_tape;
2150 }
2151 }
2152
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002153 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002154 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002155 if (!ide_tape_set_media_lock(drive, 1)) {
2156 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2157 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002160 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return 0;
2162
2163out_put_tape:
2164 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002165 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return retval;
2167}
2168
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002169static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 idetape_tape_t *tape = drive->driver_data;
2172
Borislav Petkovd9df9372008-04-27 15:38:34 +02002173 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002174 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2175 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002176 idetape_pad_zeros(drive, tape->blk_size *
2177 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002178 ide_tape_kfree_buffer(tape);
2179 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 idetape_write_filemark(drive);
2182 idetape_flush_tape_buffers(drive);
2183 idetape_flush_tape_buffers(drive);
2184}
2185
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002186static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct ide_tape_obj *tape = ide_tape_f(filp);
2189 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 unsigned int minor = iminor(inode);
2191
2192 lock_kernel();
2193 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002194
2195 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Borislav Petkov54abf372008-02-06 02:57:52 +01002197 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002199 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002201 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002203
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002204 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002206 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002208 if (!ide_tape_set_media_lock(drive, 0))
2209 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002212 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 ide_tape_put(tape);
2214 unlock_kernel();
2215 return 0;
2216}
2217
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002218static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002221 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002222 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 idetape_create_inquiry_cmd(&pc);
2225 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002226 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2227 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return;
2229 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002230 memcpy(vendor_id, &pc.buf[8], 8);
2231 memcpy(product_id, &pc.buf[16], 16);
2232 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002233
Borislav Petkov801bd322008-09-27 19:32:17 +02002234 ide_fixstring(vendor_id, 8, 0);
2235 ide_fixstring(product_id, 16, 0);
2236 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002237
Borislav Petkov801bd322008-09-27 19:32:17 +02002238 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002239 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002243 * Ask the tape about its various parameters. In particular, we will adjust our
2244 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002246static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002249 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002250 u8 *caps;
2251 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2254 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002255 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2256 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002257 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002258 put_unaligned(52, (u16 *)&tape->caps[12]);
2259 put_unaligned(540, (u16 *)&tape->caps[14]);
2260 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return;
2262 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002263 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Borislav Petkovb6422012008-02-02 19:56:49 +01002265 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002266 speed = be16_to_cpup((__be16 *)&caps[14]);
2267 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002269 *(u16 *)&caps[8] = max_speed;
2270 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2271 *(u16 *)&caps[14] = speed;
2272 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002273
2274 if (!speed) {
2275 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2276 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002277 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002279 if (!max_speed) {
2280 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2281 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002282 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284
Borislav Petkovb6422012008-02-02 19:56:49 +01002285 memcpy(&tape->caps, caps, 20);
2286 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002287 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002288 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002289 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002292#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002293#define ide_tape_devset_get(name, field) \
2294static int get_##name(ide_drive_t *drive) \
2295{ \
2296 idetape_tape_t *tape = drive->driver_data; \
2297 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002299
2300#define ide_tape_devset_set(name, field) \
2301static int set_##name(ide_drive_t *drive, int arg) \
2302{ \
2303 idetape_tape_t *tape = drive->driver_data; \
2304 tape->field = arg; \
2305 return 0; \
2306}
2307
2308#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2309ide_tape_devset_get(_name, _field) \
2310ide_tape_devset_set(_name, _field) \
2311__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2312
2313#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2314ide_tape_devset_get(_name, _field) \
2315__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2316
2317static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2318static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2319static int divf_buffer(ide_drive_t *drive) { return 2; }
2320static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2321
2322ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2323
2324ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2325ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2326 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2327
2328ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2329ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2330ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2331ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2332
2333static const struct ide_devset *idetape_settings[] = {
2334 &ide_devset_avg_speed,
2335 &ide_devset_buffer,
2336 &ide_devset_buffer_size,
2337 &ide_devset_debug_mask,
2338 &ide_devset_dsc_overlap,
2339 &ide_devset_speed,
2340 &ide_devset_tdsc,
2341 NULL
2342};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002343#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002346 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002348 * 1. Initialize our various state variables.
2349 * 2. Ask the tape for its capabilities.
2350 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2351 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002353 * Note that at this point ide.c already assigned us an irq, so that we can
2354 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002356static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Borislav Petkov83042b22008-04-27 15:38:27 +02002358 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002360 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002361 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002362 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Borislav Petkov776bb022008-07-23 19:55:59 +02002364 drive->pc_callback = ide_tape_callback;
2365
Borislav Petkov54bb2072008-02-06 02:57:52 +01002366 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002368 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2369 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2370 tape->name);
2371 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002374 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 drive->dsc_overlap = 0;
2376 tape->minor = minor;
2377 tape->name[0] = 'h';
2378 tape->name[1] = 't';
2379 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002380 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002381
2382 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002383
2384 /* Command packet DRQ type */
2385 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002386 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 idetape_get_inquiry_results(drive);
2389 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002390 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002392 tape->buffer_size = *ctl * tape->blk_size;
2393 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002395 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002396 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002398 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002399 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002400 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002401 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002402 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
Borislav Petkov83042b22008-04-27 15:38:27 +02002405 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002406 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Borislav Petkovf73850a2008-04-27 15:38:33 +02002408 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002411 * Ensure that the number we got makes sense; limit it within
2412 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002414 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2415 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002417 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002418 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002419 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2420 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002421 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 drive->using_dma ? ", DMA":"");
2423
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002424 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Russell King4031bbe2006-01-06 11:41:00 +00002427static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002431 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 ide_unregister_region(tape->disk);
2434
2435 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438static void ide_tape_release(struct kref *kref)
2439{
2440 struct ide_tape_obj *tape = to_ide_tape(kref);
2441 ide_drive_t *drive = tape->drive;
2442 struct gendisk *g = tape->disk;
2443
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002444 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 drive->dsc_overlap = 0;
2447 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002448 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002449 device_destroy(idetape_sysfs_class,
2450 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 idetape_devs[tape->minor] = NULL;
2452 g->private_data = NULL;
2453 put_disk(g);
2454 kfree(tape);
2455}
2456
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002457#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458static int proc_idetape_read_name
2459 (char *page, char **start, off_t off, int count, int *eof, void *data)
2460{
2461 ide_drive_t *drive = (ide_drive_t *) data;
2462 idetape_tape_t *tape = drive->driver_data;
2463 char *out = page;
2464 int len;
2465
2466 len = sprintf(out, "%s\n", tape->name);
2467 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2468}
2469
2470static ide_proc_entry_t idetape_proc[] = {
2471 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2472 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2473 { NULL, 0, NULL, NULL }
2474};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475#endif
2476
Russell King4031bbe2006-01-06 11:41:00 +00002477static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002480 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002481 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002482 .name = "ide-tape",
2483 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002484 },
Russell King4031bbe2006-01-06 11:41:00 +00002485 .probe = ide_tape_probe,
2486 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 .version = IDETAPE_VERSION,
2488 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 .do_request = idetape_do_request,
2490 .end_request = idetape_end_request,
2491 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002492#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002494 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496};
2497
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002498/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002499static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .owner = THIS_MODULE,
2501 .read = idetape_chrdev_read,
2502 .write = idetape_chrdev_write,
2503 .ioctl = idetape_chrdev_ioctl,
2504 .open = idetape_chrdev_open,
2505 .release = idetape_chrdev_release,
2506};
2507
2508static int idetape_open(struct inode *inode, struct file *filp)
2509{
2510 struct gendisk *disk = inode->i_bdev->bd_disk;
2511 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002513 tape = ide_tape_get(disk);
2514 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return -ENXIO;
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
2518}
2519
2520static int idetape_release(struct inode *inode, struct file *filp)
2521{
2522 struct gendisk *disk = inode->i_bdev->bd_disk;
2523 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 ide_tape_put(tape);
2526
2527 return 0;
2528}
2529
2530static int idetape_ioctl(struct inode *inode, struct file *file,
2531 unsigned int cmd, unsigned long arg)
2532{
2533 struct block_device *bdev = inode->i_bdev;
2534 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2535 ide_drive_t *drive = tape->drive;
2536 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2537 if (err == -EINVAL)
2538 err = idetape_blkdev_ioctl(drive, cmd, arg);
2539 return err;
2540}
2541
2542static struct block_device_operations idetape_block_ops = {
2543 .owner = THIS_MODULE,
2544 .open = idetape_open,
2545 .release = idetape_release,
2546 .ioctl = idetape_ioctl,
2547};
2548
Russell King4031bbe2006-01-06 11:41:00 +00002549static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
2551 idetape_tape_t *tape;
2552 struct gendisk *g;
2553 int minor;
2554
2555 if (!strstr("ide-tape", drive->driver_req))
2556 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (drive->media != ide_tape)
2559 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002560
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002561 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002562 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2563 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 goto failed;
2565 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002566 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002568 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2569 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 goto failed;
2571 }
2572
2573 g = alloc_disk(1 << PARTN_BITS);
2574 if (!g)
2575 goto out_free_tape;
2576
2577 ide_init_disk(g, drive);
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 kref_init(&tape->kref);
2580
2581 tape->drive = drive;
2582 tape->driver = &idetape_driver;
2583 tape->disk = g;
2584
2585 g->private_data = &tape->driver;
2586
2587 drive->driver_data = tape;
2588
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002589 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 for (minor = 0; idetape_devs[minor]; minor++)
2591 ;
2592 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002593 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 idetape_setup(drive, tape, minor);
2596
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002597 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2598 MKDEV(IDETAPE_MAJOR, minor), NULL,
2599 "%s", tape->name);
2600 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2601 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2602 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 g->fops = &idetape_block_ops;
2605 ide_register_region(g);
2606
2607 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609out_free_tape:
2610 kfree(tape);
2611failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002612 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002615static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002617 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002618 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 unregister_chrdev(IDETAPE_MAJOR, "ht");
2620}
2621
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002622static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
Will Dysond5dee802005-09-16 02:55:07 -07002624 int error = 1;
2625 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2626 if (IS_ERR(idetape_sysfs_class)) {
2627 idetape_sysfs_class = NULL;
2628 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2629 error = -EBUSY;
2630 goto out;
2631 }
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002634 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2635 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002636 error = -EBUSY;
2637 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Will Dysond5dee802005-09-16 02:55:07 -07002639
2640 error = driver_register(&idetape_driver.gen_driver);
2641 if (error)
2642 goto out_free_driver;
2643
2644 return 0;
2645
2646out_free_driver:
2647 driver_unregister(&idetape_driver.gen_driver);
2648out_free_class:
2649 class_destroy(idetape_sysfs_class);
2650out:
2651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
Kay Sievers263756e2005-12-12 18:03:44 +01002654MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655module_init(idetape_init);
2656module_exit(idetape_exit);
2657MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002658MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2659MODULE_LICENSE("GPL");