blob: ba05e03f482bed8cd33b6f4080ec83f55a3e3c3c [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 Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200696static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
697 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);
703}
Borislav Petkova1efc852008-02-06 02:57:52 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100706 * This is the usual interrupt handler which will be called during a packet
707 * command. We will transfer some of the data (as requested by the drive) and
708 * will re-point interrupt handler to us. When data transfer is finished, we
709 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100710 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100712static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200716 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
717 NULL, idetape_update_buffers, idetape_retry_pc,
718 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100722 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100724 * The current Packet Command is available in tape->pc, and will not change
725 * until we finish handling it. Each packet command is associated with a
726 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100728 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100730 * 1. idetape_issue_pc will send the packet command to the drive, and will set
731 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100733 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
734 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100736 * 3. ATAPI Tape media access commands have immediate status with a delayed
737 * process. In case of a successful initiation of a media access packet command,
738 * the DSC bit will be set when the actual execution of the command is finished.
739 * Since the tape drive will not issue an interrupt, we have to poll for this
740 * event. In this case, we define the request as "low priority request" by
741 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
742 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100744 * ide.c will then give higher priority to requests which originate from the
745 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100747 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100749 * 5. In case an error was found, we queue a request sense packet command in
750 * front of the request queue and retry the operation up to
751 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100753 * 6. In case no error was found, or we decided to give up and not to retry
754 * again, the callback function will be called and then we will handle the next
755 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
757static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
758{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200761 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
762 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Borislav Petkovd236d742008-04-18 00:46:27 +0200765static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
766 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Borislav Petkov90699ce2008-02-02 19:56:50 +0100770 if (tape->pc->c[0] == REQUEST_SENSE &&
771 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
773 "Two request sense in serial were issued\n");
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Borislav Petkov90699ce2008-02-02 19:56:50 +0100776 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 tape->failed_pc = pc;
778 /* Set the current packet command */
779 tape->pc = pc;
780
781 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200782 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100784 * We will "abort" retrying a packet command in case legitimate
785 * error code was received (crossing a filemark, or end of the
786 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200788 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100789 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 tape->sense_key == 2 && tape->asc == 4 &&
791 (tape->ascq == 1 || tape->ascq == 8))) {
792 printk(KERN_ERR "ide-tape: %s: I/O error, "
793 "pc = %2x, key = %2x, "
794 "asc = %2x, ascq = %2x\n",
795 tape->name, pc->c[0],
796 tape->sense_key, tape->asc,
797 tape->ascq);
798 }
799 /* Giving up */
800 pc->error = IDETAPE_ERROR_GENERAL;
801 }
802 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200803 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200804 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100806 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200810 return ide_issue_pc(drive, pc, idetape_transfer_pc,
811 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100814/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200815static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100818 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100820 /* DBD = 1 - Don't return block descriptors */
821 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 pc->c[2] = page_code;
823 /*
824 * Changed pc->c[3] to 0 (255 will at best return unused info).
825 *
826 * For SCSI this byte is defined as subpage instead of high byte
827 * of length and some IDE drives seem to interpret it this way
828 * and return an error when 255 is used.
829 */
830 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100831 /* We will just discard data in that case */
832 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200834 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200836 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200838 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100841static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200843 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200845 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100846 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200848 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100849
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200850 if (stat & ATA_DSC) {
851 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100853 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 printk(KERN_ERR "ide-tape: %s: I/O error, ",
855 tape->name);
856 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200857 idetape_retry_pc(drive);
858 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 } else {
862 pc->error = IDETAPE_ERROR_GENERAL;
863 tape->failed_pc = NULL;
864 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200865 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return ide_stopped;
867}
868
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200869static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200870 struct ide_atapi_pc *pc, struct request *rq,
871 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Borislav Petkov0014c752008-07-23 19:56:00 +0200873 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
874 unsigned int length = rq->current_nr_sectors;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100877 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200880 pc->buf = NULL;
881 pc->buf_size = length * tape->blk_size;
882 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200883 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200884 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200886 if (opcode == READ_6) {
887 pc->c[0] = READ_6;
888 atomic_set(&bh->b_count, 0);
889 } else if (opcode == WRITE_6) {
890 pc->c[0] = WRITE_6;
891 pc->flags |= PC_FLAG_WRITING;
892 pc->b_data = bh->b_data;
893 pc->b_count = atomic_read(&bh->b_count);
894 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200895
896 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static ide_startstop_t idetape_do_request(ide_drive_t *drive,
900 struct request *rq, sector_t block)
901{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200902 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200904 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100906 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Mark de Wever730616b2008-10-10 22:39:17 +0200908 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
909 " current_nr_sectors: %u\n",
910 (unsigned long long)rq->sector, rq->nr_sectors,
911 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Jens Axboe4aff5e22006-08-10 08:44:47 +0200913 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100914 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200916 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 ide_end_request(drive, 0, 0);
918 return ide_stopped;
919 }
920
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100921 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200922 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
923 pc = tape->failed_pc;
924 goto out;
925 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (postponed_rq != NULL)
928 if (rq != postponed_rq) {
929 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
930 "Two DSC requests were queued\n");
931 idetape_end_request(drive, 0, 0);
932 return ide_stopped;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 tape->postponed_rq = NULL;
936
937 /*
938 * If the tape is still busy, postpone our request and service
939 * the other device meanwhile.
940 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200941 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Borislav Petkov83dd5732008-07-23 19:56:00 +0200943 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200944 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200947 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 drive->post_reset = 0;
949 }
950
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200951 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200952 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (postponed_rq == NULL) {
954 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100955 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
957 } else if (time_after(jiffies, tape->dsc_timeout)) {
958 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
959 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200960 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 idetape_media_access_finished(drive);
962 return ide_stopped;
963 } else {
964 return ide_do_reset(drive);
965 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100966 } else if (time_after(jiffies,
967 tape->dsc_polling_start +
968 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100969 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 idetape_postpone_request(drive);
971 return ide_stopped;
972 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200973 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200974 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200975 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto out;
977 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200978 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200979 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200980 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto out;
982 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200983 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200984 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200985 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
986 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 goto out;
988 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200989 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 idetape_media_access_finished(drive);
991 return ide_stopped;
992 }
993 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200994
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200995out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100996 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001000 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001001 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001002 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001004 * It returns a pointer to the newly allocated buffer, or NULL in case of
1005 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001007static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1008 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001010 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001011 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001012 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 char *b_data = NULL;
1014
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001015 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1016 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (bh == NULL)
1018 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001019
1020 order = fls(pages) - 1;
1021 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001022 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001024 b_allocd = (1 << order) * PAGE_SIZE;
1025 pages &= (order-1);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001028 memset(bh->b_data, 0, b_allocd);
1029 bh->b_reqnext = NULL;
1030 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1032
Borislav Petkov41aa1702008-04-27 15:38:32 +02001033 while (pages) {
1034 order = fls(pages) - 1;
1035 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001036 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001038 b_allocd = (1 << order) * PAGE_SIZE;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001041 memset(b_data, 0, b_allocd);
1042
1043 /* newly allocated page frames below buffer header or ...*/
1044 if (bh->b_data == b_data + b_allocd) {
1045 bh->b_size += b_allocd;
1046 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001048 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 continue;
1050 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001051 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001053 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001055 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 continue;
1057 }
1058 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001059 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1060 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001061 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto abort;
1063 }
1064 bh->b_reqnext = NULL;
1065 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001066 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1068 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001069
1070 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 bh->b_size -= tape->excess_bh_size;
1074 if (full)
1075 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001076 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001078 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return NULL;
1080}
1081
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001082static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001083 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct idetape_bh *bh = tape->bh;
1086 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001087 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001091 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1092 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001093 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001095 count = min((unsigned int)
1096 (bh->b_size - atomic_read(&bh->b_count)),
1097 (unsigned int)n);
1098 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1099 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001100 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 n -= count;
1102 atomic_add(count, &bh->b_count);
1103 buf += count;
1104 if (atomic_read(&bh->b_count) == bh->b_size) {
1105 bh = bh->b_reqnext;
1106 if (bh)
1107 atomic_set(&bh->b_count, 0);
1108 }
1109 }
1110 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001114static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001115 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct idetape_bh *bh = tape->bh;
1118 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001119 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001123 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1124 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001128 if (copy_to_user(buf, tape->b_data, count))
1129 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 n -= count;
1131 tape->b_data += count;
1132 tape->b_count -= count;
1133 buf += count;
1134 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001135 bh = bh->b_reqnext;
1136 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (bh) {
1138 tape->b_data = bh->b_data;
1139 tape->b_count = atomic_read(&bh->b_count);
1140 }
1141 }
1142 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001146static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001148 struct idetape_bh *bh = tape->merge_bh;
1149 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001150
Borislav Petkov54abf372008-02-06 02:57:52 +01001151 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 atomic_set(&bh->b_count, 0);
1153 else {
1154 tape->b_data = bh->b_data;
1155 tape->b_count = atomic_read(&bh->b_count);
1156 }
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001160 * Write a filemark if write_filemark=1. Flush the device buffers without
1161 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001163static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001164 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001167 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001169 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Borislav Petkovd236d742008-04-18 00:46:27 +02001172static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001175 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001179 * We add a special packet command request to the tail of the request queue, and
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +02001180 * wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001182static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001185 struct request *rq;
1186 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001188 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1189 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001190 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001191 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001192 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001193 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1194 blk_put_request(rq);
1195 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Borislav Petkovd236d742008-04-18 00:46:27 +02001198static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1199 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001202 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001204 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1208{
1209 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001210 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int load_attempted = 0;
1212
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001213 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001214 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 timeout += jiffies;
1216 while (time_before(jiffies, timeout)) {
1217 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001218 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return 0;
1220 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001221 || (tape->asc == 0x3A)) {
1222 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (load_attempted)
1224 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001225 idetape_create_load_unload_cmd(drive, &pc,
1226 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001227 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 load_attempted = 1;
1229 /* not about to be ready */
1230 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1231 (tape->ascq == 1 || tape->ascq == 8)))
1232 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001233 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 return -EIO;
1236}
1237
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001238static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Borislav Petkovd236d742008-04-18 00:46:27 +02001240 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int rc;
1242
1243 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001244 rc = idetape_queue_pc_tail(drive, &pc);
1245 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return rc;
1247 idetape_wait_ready(drive, 60 * 5 * HZ);
1248 return 0;
1249}
1250
Borislav Petkovd236d742008-04-18 00:46:27 +02001251static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001254 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001255 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001258static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001261 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int position;
1263
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001264 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 idetape_create_read_position_cmd(&pc);
1267 if (idetape_queue_pc_tail(drive, &pc))
1268 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001269 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return position;
1271}
1272
Borislav Petkovd236d742008-04-18 00:46:27 +02001273static void idetape_create_locate_cmd(ide_drive_t *drive,
1274 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001275 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001278 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001280 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001282 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Borislav Petkovd236d742008-04-18 00:46:27 +02001285static int idetape_create_prevent_cmd(ide_drive_t *drive,
1286 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 idetape_tape_t *tape = drive->driver_data;
1289
Borislav Petkovb6422012008-02-02 19:56:49 +01001290 /* device supports locking according to capabilities page */
1291 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 0;
1293
1294 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001295 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 1;
1298}
1299
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001300static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Borislav Petkov54abf372008-02-06 02:57:52 +01001304 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001305 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001307 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001308 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001309 if (tape->merge_bh != NULL) {
1310 ide_tape_kfree_buffer(tape);
1311 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Borislav Petkov54abf372008-02-06 02:57:52 +01001314 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001318 * Position the tape to the requested block using the LOCATE packet command.
1319 * A READ POSITION command is then issued to check where we are positioned. Like
1320 * all higher level operations, we queue the commands at the tail of the request
1321 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001323static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1324 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 idetape_tape_t *tape = drive->driver_data;
1327 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001328 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Borislav Petkov54abf372008-02-06 02:57:52 +01001330 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001331 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 idetape_wait_ready(drive, 60 * 5 * HZ);
1333 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1334 retval = idetape_queue_pc_tail(drive, &pc);
1335 if (retval)
1336 return (retval);
1337
1338 idetape_create_read_position_cmd(&pc);
1339 return (idetape_queue_pc_tail(drive, &pc));
1340}
1341
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001342static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001343 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int seek, position;
1347
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001348 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (restore_position) {
1350 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001351 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001353 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001354 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return;
1356 }
1357 }
1358}
1359
1360/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001361 * Generate a read/write request for the block device interface and wait for it
1362 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001364static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1365 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001368 struct request *rq;
1369 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001371 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1372
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001373 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1374 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001375 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001376 rq->rq_disk = tape->disk;
1377 rq->special = (void *)bh;
1378 rq->sector = tape->first_frame;
1379 rq->nr_sectors = blocks;
1380 rq->current_nr_sectors = blocks;
1381 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1382
1383 errors = rq->errors;
1384 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1385 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1388 return 0;
1389
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001390 if (tape->merge_bh)
1391 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001392 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Borislav Petkovd236d742008-04-18 00:46:27 +02001397static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001400 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001401 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001402 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Borislav Petkovd236d742008-04-18 00:46:27 +02001405static void idetape_create_rewind_cmd(ide_drive_t *drive,
1406 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001409 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001410 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Borislav Petkovd236d742008-04-18 00:46:27 +02001413static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001416 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001418 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Borislav Petkovd236d742008-04-18 00:46:27 +02001421static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001424 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001425 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001427 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Borislav Petkov97c566c2008-04-27 15:38:25 +02001430/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001431static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001435 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001437 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001438 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Borislav Petkovd9df9372008-04-27 15:38:34 +02001441static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 idetape_tape_t *tape = drive->driver_data;
1444 int blocks, min;
1445 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001446
Borislav Petkov54abf372008-02-06 02:57:52 +01001447 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001448 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001449 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return;
1451 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001452 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001454 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001456 if (tape->merge_bh_size) {
1457 blocks = tape->merge_bh_size / tape->blk_size;
1458 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 unsigned int i;
1460
1461 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001462 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001463 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 bh = tape->bh->b_reqnext;
1465 while (bh) {
1466 atomic_set(&bh->b_count, 0);
1467 bh = bh->b_reqnext;
1468 }
1469 bh = tape->bh;
1470 while (i) {
1471 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001472 printk(KERN_INFO "ide-tape: bug,"
1473 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001476 min = min(i, (unsigned int)(bh->b_size -
1477 atomic_read(&bh->b_count)));
1478 memset(bh->b_data + atomic_read(&bh->b_count),
1479 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 atomic_add(min, &bh->b_count);
1481 i -= min;
1482 bh = bh->b_reqnext;
1483 }
1484 }
1485 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001486 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001488 if (tape->merge_bh != NULL) {
1489 ide_tape_kfree_buffer(tape);
1490 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001492 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Borislav Petkov83042b22008-04-27 15:38:27 +02001495static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001501 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1502 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001503 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 idetape_flush_tape_buffers(drive);
1505 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001506 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001507 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001508 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001509 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001511 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1512 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001514 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001517 * Issue a read 0 command to ensure that DSC handshake is
1518 * switched from completion mode to buffer available mode.
1519 * No point in issuing this if DSC overlap isn't supported, some
1520 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
1522 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001523 bytes_read = idetape_queue_rw_tail(drive,
1524 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001525 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001527 ide_tape_kfree_buffer(tape);
1528 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001529 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return bytes_read;
1531 }
1532 }
1533 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return 0;
1536}
1537
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001538/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001539static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001543 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001545 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001546 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 0;
1548
Borislav Petkov83042b22008-04-27 15:38:27 +02001549 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001551 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001552 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001555static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 idetape_tape_t *tape = drive->driver_data;
1558 struct idetape_bh *bh;
1559 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 while (bcount) {
1562 unsigned int count;
1563
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001564 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001565 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001567 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001569 atomic_set(&bh->b_count,
1570 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1572 count -= atomic_read(&bh->b_count);
1573 bh = bh->b_reqnext;
1574 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001575 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001576 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001581 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1582 * currently support only one partition.
1583 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001584static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001587 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001588 idetape_tape_t *tape;
1589 tape = drive->driver_data;
1590
1591 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 idetape_create_rewind_cmd(drive, &pc);
1594 retval = idetape_queue_pc_tail(drive, &pc);
1595 if (retval)
1596 return retval;
1597
1598 idetape_create_read_position_cmd(&pc);
1599 retval = idetape_queue_pc_tail(drive, &pc);
1600 if (retval)
1601 return retval;
1602 return 0;
1603}
1604
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001605/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001606static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1607 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 void __user *argp = (void __user *)arg;
1611
Borislav Petkovd59823f2008-02-02 19:56:51 +01001612 struct idetape_config {
1613 int dsc_rw_frequency;
1614 int dsc_media_access_frequency;
1615 int nr_stages;
1616 } config;
1617
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001618 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621 case 0x0340:
1622 if (copy_from_user(&config, argp, sizeof(config)))
1623 return -EFAULT;
1624 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001625 break;
1626 case 0x0350:
1627 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001628 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629 if (copy_to_user(argp, &config, sizeof(config)))
1630 return -EFAULT;
1631 break;
1632 default:
1633 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 return 0;
1636}
1637
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001638static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1639 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001642 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001643 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001644 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (mt_count == 0)
1647 return 0;
1648 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001649 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001651 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Borislav Petkov54abf372008-02-06 02:57:52 +01001654 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001655 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001656 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001658 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001662 case MTFSF:
1663 case MTBSF:
1664 idetape_create_space_cmd(&pc, mt_count - count,
1665 IDETAPE_SPACE_OVER_FILEMARK);
1666 return idetape_queue_pc_tail(drive, &pc);
1667 case MTFSFM:
1668 case MTBSFM:
1669 if (!sprev)
1670 return -EIO;
1671 retval = idetape_space_over_filemarks(drive, MTFSF,
1672 mt_count - count);
1673 if (retval)
1674 return retval;
1675 count = (MTBSFM == mt_op ? 1 : -1);
1676 return idetape_space_over_filemarks(drive, MTFSF, count);
1677 default:
1678 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1679 mt_op);
1680 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001685 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001687 * The tape is optimized to maximize throughput when it is transferring an
1688 * integral number of the "continuous transfer limit", which is a parameter of
1689 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001691 * As of version 1.3 of the driver, the character device provides an abstract
1692 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1693 * same backup/restore procedure is supported. The driver will internally
1694 * convert the requests to the recommended transfer unit, so that an unmatch
1695 * between the user's block size to the recommended size will only result in a
1696 * (slightly) increased driver overhead, but will no longer hit performance.
1697 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001699static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1700 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 struct ide_tape_obj *tape = ide_tape_f(file);
1703 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001704 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001705 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001706 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001708 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Borislav Petkov54abf372008-02-06 02:57:52 +01001710 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001711 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001712 if (count > tape->blk_size &&
1713 (count % tape->blk_size) == 0)
1714 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001716 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001717 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return rc;
1719 if (count == 0)
1720 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001721 if (tape->merge_bh_size) {
1722 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001723 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001724 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001725 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001727 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 count -= actually_read;
1729 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001730 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001731 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (bytes_read <= 0)
1733 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001734 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001735 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 buf += bytes_read;
1737 count -= bytes_read;
1738 actually_read += bytes_read;
1739 }
1740 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001741 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (bytes_read <= 0)
1743 goto finish;
1744 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001745 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001746 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001748 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001751 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001752 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 idetape_space_over_filemarks(drive, MTFSF, 1);
1755 return 0;
1756 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001757
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001758 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001761static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 size_t count, loff_t *ppos)
1763{
1764 struct ide_tape_obj *tape = ide_tape_f(file);
1765 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001766 ssize_t actually_written = 0;
1767 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001768 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /* The drive is write protected. */
1771 if (tape->write_prot)
1772 return -EACCES;
1773
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001774 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001777 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1778 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001779 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001780 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001781 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001783 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001785 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1786 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001788 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001789 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001792 * Issue a write 0 command to ensure that DSC handshake is
1793 * switched from completion mode to buffer available mode. No
1794 * point in issuing this if DSC overlap isn't supported, some
1795 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
1797 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001798 ssize_t retval = idetape_queue_rw_tail(drive,
1799 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001800 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001802 ide_tape_kfree_buffer(tape);
1803 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001804 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return retval;
1806 }
1807 }
1808 }
1809 if (count == 0)
1810 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001811 if (tape->merge_bh_size) {
1812 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001813 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001814 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001816 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001817 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001818 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001819 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001820 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001822 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 count -= actually_written;
1824
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001825 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001826 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001827 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001828 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (retval <= 0)
1830 return (retval);
1831 }
1832 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001833 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001834 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001835 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001836 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001837 buf += tape->buffer_size;
1838 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001839 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001840 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (retval <= 0)
1842 return (retval);
1843 }
1844 if (count) {
1845 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001846 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001847 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001848 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001850 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001853static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Borislav Petkovd236d742008-04-18 00:46:27 +02001855 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Write a filemark */
1858 idetape_create_write_filemark_cmd(drive, &pc, 1);
1859 if (idetape_queue_pc_tail(drive, &pc)) {
1860 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1861 return -EIO;
1862 }
1863 return 0;
1864}
1865
1866/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001867 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1868 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001870 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1871 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001872 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001874 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001876 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1877 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001879static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001882 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001883 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001885 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1886 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001889 case MTFSF:
1890 case MTFSFM:
1891 case MTBSF:
1892 case MTBSFM:
1893 if (!mt_count)
1894 return 0;
1895 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1896 default:
1897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
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 MTWEOF:
1902 if (tape->write_prot)
1903 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001904 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001905 for (i = 0; i < mt_count; i++) {
1906 retval = idetape_write_filemark(drive);
1907 if (retval)
1908 return retval;
1909 }
1910 return 0;
1911 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001912 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001913 if (idetape_rewind_tape(drive))
1914 return -EIO;
1915 return 0;
1916 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001917 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001918 idetape_create_load_unload_cmd(drive, &pc,
1919 IDETAPE_LU_LOAD_MASK);
1920 return idetape_queue_pc_tail(drive, &pc);
1921 case MTUNLOAD:
1922 case MTOFFL:
1923 /*
1924 * If door is locked, attempt to unlock before
1925 * attempting to eject.
1926 */
1927 if (tape->door_locked) {
1928 if (idetape_create_prevent_cmd(drive, &pc, 0))
1929 if (!idetape_queue_pc_tail(drive, &pc))
1930 tape->door_locked = DOOR_UNLOCKED;
1931 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001932 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001933 idetape_create_load_unload_cmd(drive, &pc,
1934 !IDETAPE_LU_LOAD_MASK);
1935 retval = idetape_queue_pc_tail(drive, &pc);
1936 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001937 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001938 return retval;
1939 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001940 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001941 return idetape_flush_tape_buffers(drive);
1942 case MTRETEN:
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_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1946 return idetape_queue_pc_tail(drive, &pc);
1947 case MTEOM:
1948 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1949 return idetape_queue_pc_tail(drive, &pc);
1950 case MTERASE:
1951 (void)idetape_rewind_tape(drive);
1952 idetape_create_erase_cmd(&pc);
1953 return idetape_queue_pc_tail(drive, &pc);
1954 case MTSETBLK:
1955 if (mt_count) {
1956 if (mt_count < tape->blk_size ||
1957 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001959 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001960 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001961 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001962 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001963 return 0;
1964 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001965 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001966 return idetape_position_tape(drive,
1967 mt_count * tape->user_bs_factor, tape->partition, 0);
1968 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001969 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001970 return idetape_position_tape(drive, 0, mt_count, 0);
1971 case MTFSR:
1972 case MTBSR:
1973 case MTLOCK:
1974 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001976 retval = idetape_queue_pc_tail(drive, &pc);
1977 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001979 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1980 return 0;
1981 case MTUNLOCK:
1982 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001984 retval = idetape_queue_pc_tail(drive, &pc);
1985 if (retval)
1986 return retval;
1987 tape->door_locked = DOOR_UNLOCKED;
1988 return 0;
1989 default:
1990 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1991 mt_op);
1992 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994}
1995
1996/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001997 * Our character device ioctls. General mtio.h magnetic io commands are
1998 * supported here, and not in the corresponding block interface. Our own
1999 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002001static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2002 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct ide_tape_obj *tape = ide_tape_f(file);
2005 ide_drive_t *drive = tape->drive;
2006 struct mtop mtop;
2007 struct mtget mtget;
2008 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002009 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 void __user *argp = (void __user *)arg;
2011
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002012 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Borislav Petkov54abf372008-02-06 02:57:52 +01002014 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002015 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 idetape_flush_tape_buffers(drive);
2017 }
2018 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002019 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002020 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002021 position = idetape_read_position(drive);
2022 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return -EIO;
2024 }
2025 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002026 case MTIOCTOP:
2027 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2028 return -EFAULT;
2029 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2030 case MTIOCGET:
2031 memset(&mtget, 0, sizeof(struct mtget));
2032 mtget.mt_type = MT_ISSCSI2;
2033 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2034 mtget.mt_dsreg =
2035 ((tape->blk_size * tape->user_bs_factor)
2036 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002037
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002038 if (tape->drv_write_prot)
2039 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2040
2041 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2042 return -EFAULT;
2043 return 0;
2044 case MTIOCPOS:
2045 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2046 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2047 return -EFAULT;
2048 return 0;
2049 default:
2050 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002051 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002052 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054}
2055
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002056/*
2057 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2058 * block size with the reported value.
2059 */
2060static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2061{
2062 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002063 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002064
2065 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2066 if (idetape_queue_pc_tail(drive, &pc)) {
2067 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002068 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002069 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2070 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002071 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002072 }
2073 return;
2074 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002075 tape->blk_size = (pc.buf[4 + 5] << 16) +
2076 (pc.buf[4 + 6] << 8) +
2077 pc.buf[4 + 7];
2078 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002081static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2084 ide_drive_t *drive;
2085 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002086 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int retval;
2088
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002089 if (i >= MAX_HWIFS * MAX_DRIVES)
2090 return -ENXIO;
2091
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002092 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002093 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002094 if (!tape) {
2095 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002096 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002097 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002098
2099 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
2102 * We really want to do nonseekable_open(inode, filp); here, but some
2103 * versions of tar incorrectly call lseek on tapes and bail out if that
2104 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2105 */
2106 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 drive = tape->drive;
2109
2110 filp->private_data = tape;
2111
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002112 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 retval = -EBUSY;
2114 goto out_put_tape;
2115 }
2116
2117 retval = idetape_wait_ready(drive, 60 * HZ);
2118 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002119 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2121 goto out_put_tape;
2122 }
2123
2124 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002125 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 (void)idetape_rewind_tape(drive);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002129 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 /* Set write protect flag if device is opened as read-only. */
2132 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2133 tape->write_prot = 1;
2134 else
2135 tape->write_prot = tape->drv_write_prot;
2136
2137 /* Make sure drive isn't write protected if user wants to write. */
2138 if (tape->write_prot) {
2139 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2140 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002141 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 retval = -EROFS;
2143 goto out_put_tape;
2144 }
2145 }
2146
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002147 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002148 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2150 if (!idetape_queue_pc_tail(drive, &pc)) {
2151 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2152 tape->door_locked = DOOR_LOCKED;
2153 }
2154 }
2155 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002156 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return 0;
2158
2159out_put_tape:
2160 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002161 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return retval;
2163}
2164
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002165static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 idetape_tape_t *tape = drive->driver_data;
2168
Borislav Petkovd9df9372008-04-27 15:38:34 +02002169 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002170 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2171 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002172 idetape_pad_zeros(drive, tape->blk_size *
2173 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002174 ide_tape_kfree_buffer(tape);
2175 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177 idetape_write_filemark(drive);
2178 idetape_flush_tape_buffers(drive);
2179 idetape_flush_tape_buffers(drive);
2180}
2181
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002182static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 struct ide_tape_obj *tape = ide_tape_f(filp);
2185 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002186 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 unsigned int minor = iminor(inode);
2188
2189 lock_kernel();
2190 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002191
2192 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Borislav Petkov54abf372008-02-06 02:57:52 +01002194 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002196 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002198 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002200
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002201 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002203 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (tape->door_locked == DOOR_LOCKED) {
2205 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2206 if (!idetape_queue_pc_tail(drive, &pc))
2207 tape->door_locked = DOOR_UNLOCKED;
2208 }
2209 }
2210 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002211 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 ide_tape_put(tape);
2213 unlock_kernel();
2214 return 0;
2215}
2216
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002217static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002220 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002221 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 idetape_create_inquiry_cmd(&pc);
2224 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002225 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2226 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return;
2228 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002229 memcpy(vendor_id, &pc.buf[8], 8);
2230 memcpy(product_id, &pc.buf[16], 16);
2231 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002232
Borislav Petkov801bd322008-09-27 19:32:17 +02002233 ide_fixstring(vendor_id, 8, 0);
2234 ide_fixstring(product_id, 16, 0);
2235 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002236
Borislav Petkov801bd322008-09-27 19:32:17 +02002237 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002238 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
2241/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002242 * Ask the tape about its various parameters. In particular, we will adjust our
2243 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002245static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002248 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002249 u8 *caps;
2250 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2253 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002254 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2255 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002256 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002257 put_unaligned(52, (u16 *)&tape->caps[12]);
2258 put_unaligned(540, (u16 *)&tape->caps[14]);
2259 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return;
2261 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002262 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Borislav Petkovb6422012008-02-02 19:56:49 +01002264 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002265 speed = be16_to_cpup((__be16 *)&caps[14]);
2266 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002268 *(u16 *)&caps[8] = max_speed;
2269 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2270 *(u16 *)&caps[14] = speed;
2271 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002272
2273 if (!speed) {
2274 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2275 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002276 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002278 if (!max_speed) {
2279 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2280 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002281 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283
Borislav Petkovb6422012008-02-02 19:56:49 +01002284 memcpy(&tape->caps, caps, 20);
2285 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002286 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002287 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002288 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002291#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002292#define ide_tape_devset_get(name, field) \
2293static int get_##name(ide_drive_t *drive) \
2294{ \
2295 idetape_tape_t *tape = drive->driver_data; \
2296 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002298
2299#define ide_tape_devset_set(name, field) \
2300static int set_##name(ide_drive_t *drive, int arg) \
2301{ \
2302 idetape_tape_t *tape = drive->driver_data; \
2303 tape->field = arg; \
2304 return 0; \
2305}
2306
2307#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2308ide_tape_devset_get(_name, _field) \
2309ide_tape_devset_set(_name, _field) \
2310__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2311
2312#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2313ide_tape_devset_get(_name, _field) \
2314__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2315
2316static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2317static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2318static int divf_buffer(ide_drive_t *drive) { return 2; }
2319static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2320
2321ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2322
2323ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2324ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2325 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2326
2327ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2328ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2329ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2330ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2331
2332static const struct ide_devset *idetape_settings[] = {
2333 &ide_devset_avg_speed,
2334 &ide_devset_buffer,
2335 &ide_devset_buffer_size,
2336 &ide_devset_debug_mask,
2337 &ide_devset_dsc_overlap,
2338 &ide_devset_speed,
2339 &ide_devset_tdsc,
2340 NULL
2341};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002345 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002347 * 1. Initialize our various state variables.
2348 * 2. Ask the tape for its capabilities.
2349 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2350 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002352 * Note that at this point ide.c already assigned us an irq, so that we can
2353 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002355static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Borislav Petkov83042b22008-04-27 15:38:27 +02002357 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002359 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002360 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002361 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Borislav Petkov776bb022008-07-23 19:55:59 +02002363 drive->pc_callback = ide_tape_callback;
2364
Borislav Petkov54bb2072008-02-06 02:57:52 +01002365 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002367 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2368 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2369 tape->name);
2370 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002373 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 drive->dsc_overlap = 0;
2375 tape->minor = minor;
2376 tape->name[0] = 'h';
2377 tape->name[1] = 't';
2378 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002379 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002380
2381 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002382
2383 /* Command packet DRQ type */
2384 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002385 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 idetape_get_inquiry_results(drive);
2388 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002389 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002391 tape->buffer_size = *ctl * tape->blk_size;
2392 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002394 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002395 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002397 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002398 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002399 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002400 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002401 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403
Borislav Petkov83042b22008-04-27 15:38:27 +02002404 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002405 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Borislav Petkovf73850a2008-04-27 15:38:33 +02002407 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002410 * Ensure that the number we got makes sense; limit it within
2411 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002413 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2414 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002416 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002417 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002418 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2419 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002420 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 drive->using_dma ? ", DMA":"");
2422
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002423 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Russell King4031bbe2006-01-06 11:41:00 +00002426static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002430 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 ide_unregister_region(tape->disk);
2433
2434 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
2437static void ide_tape_release(struct kref *kref)
2438{
2439 struct ide_tape_obj *tape = to_ide_tape(kref);
2440 ide_drive_t *drive = tape->drive;
2441 struct gendisk *g = tape->disk;
2442
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002443 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 drive->dsc_overlap = 0;
2446 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002447 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002448 device_destroy(idetape_sysfs_class,
2449 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 idetape_devs[tape->minor] = NULL;
2451 g->private_data = NULL;
2452 put_disk(g);
2453 kfree(tape);
2454}
2455
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002456#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static int proc_idetape_read_name
2458 (char *page, char **start, off_t off, int count, int *eof, void *data)
2459{
2460 ide_drive_t *drive = (ide_drive_t *) data;
2461 idetape_tape_t *tape = drive->driver_data;
2462 char *out = page;
2463 int len;
2464
2465 len = sprintf(out, "%s\n", tape->name);
2466 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2467}
2468
2469static ide_proc_entry_t idetape_proc[] = {
2470 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2471 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2472 { NULL, 0, NULL, NULL }
2473};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474#endif
2475
Russell King4031bbe2006-01-06 11:41:00 +00002476static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002479 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002480 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002481 .name = "ide-tape",
2482 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002483 },
Russell King4031bbe2006-01-06 11:41:00 +00002484 .probe = ide_tape_probe,
2485 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 .version = IDETAPE_VERSION,
2487 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 .do_request = idetape_do_request,
2489 .end_request = idetape_end_request,
2490 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002491#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002493 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002494#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495};
2496
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002497/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002498static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 .owner = THIS_MODULE,
2500 .read = idetape_chrdev_read,
2501 .write = idetape_chrdev_write,
2502 .ioctl = idetape_chrdev_ioctl,
2503 .open = idetape_chrdev_open,
2504 .release = idetape_chrdev_release,
2505};
2506
2507static int idetape_open(struct inode *inode, struct file *filp)
2508{
2509 struct gendisk *disk = inode->i_bdev->bd_disk;
2510 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002512 tape = ide_tape_get(disk);
2513 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return -ENXIO;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
2517}
2518
2519static int idetape_release(struct inode *inode, struct file *filp)
2520{
2521 struct gendisk *disk = inode->i_bdev->bd_disk;
2522 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 ide_tape_put(tape);
2525
2526 return 0;
2527}
2528
2529static int idetape_ioctl(struct inode *inode, struct file *file,
2530 unsigned int cmd, unsigned long arg)
2531{
2532 struct block_device *bdev = inode->i_bdev;
2533 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2534 ide_drive_t *drive = tape->drive;
2535 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2536 if (err == -EINVAL)
2537 err = idetape_blkdev_ioctl(drive, cmd, arg);
2538 return err;
2539}
2540
2541static struct block_device_operations idetape_block_ops = {
2542 .owner = THIS_MODULE,
2543 .open = idetape_open,
2544 .release = idetape_release,
2545 .ioctl = idetape_ioctl,
2546};
2547
Russell King4031bbe2006-01-06 11:41:00 +00002548static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 idetape_tape_t *tape;
2551 struct gendisk *g;
2552 int minor;
2553
2554 if (!strstr("ide-tape", drive->driver_req))
2555 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (drive->media != ide_tape)
2558 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002559
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002560 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002561 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2562 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 goto failed;
2564 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002565 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002567 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2568 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 goto failed;
2570 }
2571
2572 g = alloc_disk(1 << PARTN_BITS);
2573 if (!g)
2574 goto out_free_tape;
2575
2576 ide_init_disk(g, drive);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 kref_init(&tape->kref);
2579
2580 tape->drive = drive;
2581 tape->driver = &idetape_driver;
2582 tape->disk = g;
2583
2584 g->private_data = &tape->driver;
2585
2586 drive->driver_data = tape;
2587
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002588 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 for (minor = 0; idetape_devs[minor]; minor++)
2590 ;
2591 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002592 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 idetape_setup(drive, tape, minor);
2595
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002596 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2597 MKDEV(IDETAPE_MAJOR, minor), NULL,
2598 "%s", tape->name);
2599 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2600 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2601 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 g->fops = &idetape_block_ops;
2604 ide_register_region(g);
2605
2606 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608out_free_tape:
2609 kfree(tape);
2610failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002611 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002614static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002616 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002617 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 unregister_chrdev(IDETAPE_MAJOR, "ht");
2619}
2620
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002621static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Will Dysond5dee802005-09-16 02:55:07 -07002623 int error = 1;
2624 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2625 if (IS_ERR(idetape_sysfs_class)) {
2626 idetape_sysfs_class = NULL;
2627 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2628 error = -EBUSY;
2629 goto out;
2630 }
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002633 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2634 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002635 error = -EBUSY;
2636 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Will Dysond5dee802005-09-16 02:55:07 -07002638
2639 error = driver_register(&idetape_driver.gen_driver);
2640 if (error)
2641 goto out_free_driver;
2642
2643 return 0;
2644
2645out_free_driver:
2646 driver_unregister(&idetape_driver.gen_driver);
2647out_free_class:
2648 class_destroy(idetape_sysfs_class);
2649out:
2650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
Kay Sievers263756e2005-12-12 18:03:44 +01002653MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654module_init(idetape_init);
2655module_exit(idetape_exit);
2656MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002657MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2658MODULE_LICENSE("GPL");