blob: 72b4350bfeb64f43b64cfd18d359180aac618058 [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 * The following parameter is used to select the point in the internal tape fifo
85 * in which we will start to refill the buffer. Decreasing the following
86 * parameter will improve the system's latency and interactive response, while
87 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010089#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010092 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010094 * Polling for DSC (a single bit in the status register) is a very important
95 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * 1. Before a read/write packet command, to ensure that we can transfer data
98 * from/to the tape's data buffers, without causing an actual media access.
99 * In case the tape is not ready yet, we take out our request from the device
100 * request queue, so that ide.c could service requests from the other device
101 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100103 * 2. After the successful initialization of a "media access packet command",
104 * which is a command that can take a long time to complete (the interval can
105 * range from several seconds to even an hour). Again, we postpone our request
106 * in the middle to free the bus for the other device. The polling frequency
107 * here should be lower than the read/write frequency since those media access
108 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
109 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
110 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * We also set a timeout for the timer, in case something goes wrong. The
113 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100115
116/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
118#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
119#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
120#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
121#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
122#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
123#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
124
125/*************************** End of tunable parameters ***********************/
126
Borislav Petkov54abf372008-02-06 02:57:52 +0100127/* tape directions */
128enum {
129 IDETAPE_DIR_NONE = (1 << 0),
130 IDETAPE_DIR_READ = (1 << 1),
131 IDETAPE_DIR_WRITE = (1 << 2),
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200135 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 atomic_t b_count;
137 struct idetape_bh *b_reqnext;
138 char *b_data;
139};
140
Borislav Petkov03056b92008-04-18 00:46:26 +0200141/* Tape door status */
142#define DOOR_UNLOCKED 0
143#define DOOR_LOCKED 1
144#define DOOR_EXPLICITLY_LOCKED 2
145
146/* Some defines for the SPACE command */
147#define IDETAPE_SPACE_OVER_FILEMARK 1
148#define IDETAPE_SPACE_TO_EOD 3
149
150/* Some defines for the LOAD UNLOAD command */
151#define IDETAPE_LU_LOAD_MASK 1
152#define IDETAPE_LU_RETENSION_MASK 2
153#define IDETAPE_LU_EOT_MASK 4
154
Borislav Petkov03056b92008-04-18 00:46:26 +0200155/* Error codes returned in rq->errors to the higher part of the driver. */
156#define IDETAPE_ERROR_GENERAL 101
157#define IDETAPE_ERROR_FILEMARK 102
158#define IDETAPE_ERROR_EOD 103
159
160/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
161#define IDETAPE_BLOCK_DESCRIPTOR 0
162#define IDETAPE_CAPABILITIES_PAGE 0x2a
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100165 * Most of our global data which we need to save even as we leave the driver due
166 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100169 ide_drive_t *drive;
170 struct ide_driver *driver;
171 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100172 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * failed_pc points to the last failed packet command, or contains
176 * NULL if we do not need to retry any packet command. This is
177 * required since an additional packet command is needed before the
178 * retry, to get detailed information on what went wrong.
179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200181 struct ide_atapi_pc *failed_pc;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200182 /* used by REQ_IDETAPE_{READ,WRITE} requests */
183 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100186 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100188 * While polling for DSC we use postponed_rq to postpone the current
189 * request so that ide.c will be able to service pending requests on the
190 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200191 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193 struct request *postponed_rq;
194 /* The time in which we started polling for DSC */
195 unsigned long dsc_polling_start;
196 /* Timer used to poll for dsc */
197 struct timer_list dsc_timer;
198 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100199 unsigned long best_dsc_rw_freq;
200 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned long dsc_timeout;
202
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100203 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 u8 partition;
205 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100206 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 u8 sense_key, asc, ascq;
210
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100211 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned int minor;
213 /* device name */
214 char name[4];
215 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100216 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Borislav Petkov54bb2072008-02-06 02:57:52 +0100218 /* tape block size, usually 512 or 1024 bytes */
219 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100223 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100226 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100228 * At most, there is only one ide-tape originated data transfer request
229 * in the device request queue. This allows ide.c to easily service
230 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200232
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100233 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200234 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200235 /* merge buffer */
236 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200237 /* size of the merge buffer */
238 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200239 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct idetape_bh *bh;
241 char *b_data;
242 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100243
Borislav Petkova997a432008-04-27 15:38:33 +0200244 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Wasted space in each stage */
246 int excess_bh_size;
247
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100248 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned long avg_time;
250 int avg_size;
251 int avg_speed;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* the door is currently locked */
254 int door_locked;
255 /* the tape hardware is write protected */
256 char drv_write_prot;
257 /* the tape is write protected (hardware or opened as read-only) */
258 char write_prot;
259
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100260 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261} idetape_tape_t;
262
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800263static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Will Dysond5dee802005-09-16 02:55:07 -0700265static struct class *idetape_sysfs_class;
266
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100267static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
270{
271 struct ide_tape_obj *tape = NULL;
272
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800273 mutex_lock(&idetape_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +0200274 tape = ide_drv_g(disk, ide_tape_obj);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200275 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200276 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200277 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200278 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100279 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200280 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800281 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return tape;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static void ide_tape_put(struct ide_tape_obj *tape)
286{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200287 ide_drive_t *drive = tape->drive;
288
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800289 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100290 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200291 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800292 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100296 * The variables below are used for the character device interface. Additional
297 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100299static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
302{
303 struct ide_tape_obj *tape = NULL;
304
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800305 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 tape = idetape_devs[i];
307 if (tape)
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100308 get_device(&tape->dev);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800309 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return tape;
311}
312
Borislav Petkovd236d742008-04-18 00:46:27 +0200313static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100314 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct idetape_bh *bh = pc->bh;
317 int count;
318
319 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (bh == NULL) {
321 printk(KERN_ERR "ide-tape: bh == NULL in "
322 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200323 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return;
325 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100326 count = min(
327 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
328 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200329 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100330 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 bcount -= count;
332 atomic_add(count, &bh->b_count);
333 if (atomic_read(&bh->b_count) == bh->b_size) {
334 bh = bh->b_reqnext;
335 if (bh)
336 atomic_set(&bh->b_count, 0);
337 }
338 }
339 pc->bh = bh;
340}
341
Borislav Petkovd236d742008-04-18 00:46:27 +0200342static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100343 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct idetape_bh *bh = pc->bh;
346 int count;
347
348 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100350 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
351 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200355 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 bcount -= count;
357 pc->b_data += count;
358 pc->b_count -= count;
359 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100360 bh = bh->b_reqnext;
361 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (bh) {
363 pc->b_data = bh->b_data;
364 pc->b_count = atomic_read(&bh->b_count);
365 }
366 }
367 }
368}
369
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200370static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct idetape_bh *bh = pc->bh;
373 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200374 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Borislav Petkov346331f2008-04-18 00:46:26 +0200376 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
378 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100380 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
381 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
385 atomic_set(&bh->b_count, count);
386 if (atomic_read(&bh->b_count) == bh->b_size)
387 bh = bh->b_reqnext;
388 bcount -= count;
389 }
390 pc->bh = bh;
391}
392
393/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100394 * called on each failed packet command retry to analyze the request sense. We
395 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100397static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200400 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Borislav Petkov1b5db432008-02-02 19:56:48 +0100402 tape->sense_key = sense[2] & 0xF;
403 tape->asc = sense[12];
404 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100405
406 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
407 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Borislav Petkovd236d742008-04-18 00:46:27 +0200409 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200410 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200411 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100412 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200413 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200414 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 /*
418 * If error was the result of a zero-length read or write command,
419 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
420 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
421 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100422 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100423 /* length == 0 */
424 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
425 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* don't report an error, everything's ok */
427 pc->error = 0;
428 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200429 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100432 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200434 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100436 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100437 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
438 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200440 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100443 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100444 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200446 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200448 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200449 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
451 }
452}
453
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200454/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200455static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200457 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200459 while (bh) {
460 u32 size = bh->b_size;
461
462 while (size) {
463 unsigned int order = fls(size >> PAGE_SHIFT)-1;
464
465 if (bh->b_data)
466 free_pages((unsigned long)bh->b_data, order);
467
468 size &= (order-1);
469 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 prev_bh = bh;
472 bh = bh->b_reqnext;
473 kfree(prev_bh);
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
478{
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100479 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100483 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100486 case 0: error = IDETAPE_ERROR_GENERAL; break;
487 case 1: error = 0; break;
488 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 rq->errors = error;
491 if (error)
492 tape->failed_pc = NULL;
493
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100494 if (!blk_special_request(rq)) {
495 ide_end_request(drive, uptodate, nr_sects);
496 return 0;
497 }
498
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100499 ide_complete_rq(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200504static void ide_tape_handle_dsc(ide_drive_t *);
505
506static void ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200509 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200510 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100512 debug_log(DBG_PROCS, "Enter %s\n", __func__);
513
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200514 if (dsc)
515 ide_tape_handle_dsc(drive);
516
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200517 if (tape->failed_pc == pc)
518 tape->failed_pc = NULL;
519
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200520 if (pc->c[0] == REQUEST_SENSE) {
521 if (uptodate)
522 idetape_analyze_error(drive, pc->buf);
523 else
524 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
525 "itself - Aborting request!\n");
526 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100527 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200528 int blocks = pc->xferred / tape->blk_size;
529
530 tape->avg_size += blocks * tape->blk_size;
531
532 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
533 tape->avg_speed = tape->avg_size * HZ /
534 (jiffies - tape->avg_time) / 1024;
535 tape->avg_size = 0;
536 tape->avg_time = jiffies;
537 }
538
539 tape->first_frame += blocks;
540 rq->current_nr_sectors -= blocks;
541
542 if (pc->error)
543 uptodate = pc->error;
544 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200545 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200546
547 debug_log(DBG_SENSE, "BOP - %s\n",
548 (readpos[0] & 0x80) ? "Yes" : "No");
549 debug_log(DBG_SENSE, "EOP - %s\n",
550 (readpos[0] & 0x40) ? "Yes" : "No");
551
552 if (readpos[0] & 0x4) {
553 printk(KERN_INFO "ide-tape: Block location is unknown"
554 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200555 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200556 uptodate = 0;
557 } else {
558 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200559 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200560
561 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200562 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200563 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200566
567 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100571 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100572 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100574static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 idetape_tape_t *tape = drive->driver_data;
577
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100578 debug_log(DBG_PROCS, "Enter %s\n", __func__);
579
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100580 tape->postponed_rq = drive->hwif->rq;
581
Borislav Petkov54bb2072008-02-06 02:57:52 +0100582 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200585static void ide_tape_handle_dsc(ide_drive_t *drive)
586{
587 idetape_tape_t *tape = drive->driver_data;
588
589 /* Media access command */
590 tape->dsc_polling_start = jiffies;
591 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
592 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
593 /* Allow ide.c to handle other requests */
594 idetape_postpone_request(drive);
595}
596
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200597static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200598 unsigned int bcount, int write)
599{
600 if (write)
601 idetape_output_buffers(drive, pc, bcount);
602 else
603 idetape_input_buffers(drive, pc, bcount);
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200604
605 return bcount;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200606}
Borislav Petkova1efc852008-02-06 02:57:52 +0100607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100609 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200611 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100612 * until we finish handling it. Each packet command is associated with a
613 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100615 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100617 * 1. idetape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200618 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200620 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100621 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100623 * 3. ATAPI Tape media access commands have immediate status with a delayed
624 * process. In case of a successful initiation of a media access packet command,
625 * the DSC bit will be set when the actual execution of the command is finished.
626 * Since the tape drive will not issue an interrupt, we have to poll for this
627 * event. In this case, we define the request as "low priority request" by
628 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
629 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100631 * ide.c will then give higher priority to requests which originate from the
632 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100634 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100636 * 5. In case an error was found, we queue a request sense packet command in
637 * front of the request queue and retry the operation up to
638 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100640 * 6. In case no error was found, or we decided to give up and not to retry
641 * again, the callback function will be called and then we will handle the next
642 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Borislav Petkovd236d742008-04-18 00:46:27 +0200645static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
646 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200650 if (drive->pc->c[0] == REQUEST_SENSE &&
Borislav Petkov90699ce2008-02-02 19:56:50 +0100651 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
653 "Two request sense in serial were issued\n");
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Borislav Petkov90699ce2008-02-02 19:56:50 +0100656 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 tape->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200660 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200663 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100665 * We will "abort" retrying a packet command in case legitimate
666 * error code was received (crossing a filemark, or end of the
667 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200669 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100670 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 tape->sense_key == 2 && tape->asc == 4 &&
672 (tape->ascq == 1 || tape->ascq == 8))) {
673 printk(KERN_ERR "ide-tape: %s: I/O error, "
674 "pc = %2x, key = %2x, "
675 "asc = %2x, ascq = %2x\n",
676 tape->name, pc->c[0],
677 tape->sense_key, tape->asc,
678 tape->ascq);
679 }
680 /* Giving up */
681 pc->error = IDETAPE_ERROR_GENERAL;
682 }
683 tape->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200684 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200685 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100687 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100691 return ide_issue_pc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100694/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200695static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200697 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100698 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100700 /* DBD = 1 - Don't return block descriptors */
701 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 pc->c[2] = page_code;
703 /*
704 * Changed pc->c[3] to 0 (255 will at best return unused info).
705 *
706 * For SCSI this byte is defined as subpage instead of high byte
707 * of length and some IDE drives seem to interpret it this way
708 * and return an error when 255 is used.
709 */
710 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100711 /* We will just discard data in that case */
712 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200714 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200716 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200718 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100721static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200723 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200725 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100726 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200728 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100729
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200730 if (stat & ATA_DSC) {
731 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100733 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 printk(KERN_ERR "ide-tape: %s: I/O error, ",
735 tape->name);
736 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200737 ide_retry_pc(drive, tape->disk);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200738 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } else {
742 pc->error = IDETAPE_ERROR_GENERAL;
743 tape->failed_pc = NULL;
744 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200745 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return ide_stopped;
747}
748
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200749static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200750 struct ide_atapi_pc *pc, struct request *rq,
751 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Borislav Petkov0014c752008-07-23 19:56:00 +0200753 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
754 unsigned int length = rq->current_nr_sectors;
755
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200756 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100757 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200760 pc->buf = NULL;
761 pc->buf_size = length * tape->blk_size;
762 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200763 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200764 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200766 if (opcode == READ_6) {
767 pc->c[0] = READ_6;
768 atomic_set(&bh->b_count, 0);
769 } else if (opcode == WRITE_6) {
770 pc->c[0] = WRITE_6;
771 pc->flags |= PC_FLAG_WRITING;
772 pc->b_data = bh->b_data;
773 pc->b_count = atomic_read(&bh->b_count);
774 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200775
776 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779static ide_startstop_t idetape_do_request(ide_drive_t *drive,
780 struct request *rq, sector_t block)
781{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200782 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200784 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100786 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Mark de Wever730616b2008-10-10 22:39:17 +0200788 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
789 " current_nr_sectors: %u\n",
790 (unsigned long long)rq->sector, rq->nr_sectors,
791 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jens Axboe4aff5e22006-08-10 08:44:47 +0200793 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100794 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200796 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ide_end_request(drive, 0, 0);
798 return ide_stopped;
799 }
800
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100801 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200802 if (tape->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200803 pc = tape->failed_pc;
804 goto out;
805 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (postponed_rq != NULL)
808 if (rq != postponed_rq) {
809 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
810 "Two DSC requests were queued\n");
811 idetape_end_request(drive, 0, 0);
812 return ide_stopped;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 tape->postponed_rq = NULL;
816
817 /*
818 * If the tape is still busy, postpone our request and service
819 * the other device meanwhile.
820 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200821 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200823 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
824 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200825 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200827 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200828 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200829 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200832 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200833 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (postponed_rq == NULL) {
835 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100836 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
838 } else if (time_after(jiffies, tape->dsc_timeout)) {
839 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
840 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200841 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 idetape_media_access_finished(drive);
843 return ide_stopped;
844 } else {
845 return ide_do_reset(drive);
846 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100847 } else if (time_after(jiffies,
848 tape->dsc_polling_start +
849 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100850 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 idetape_postpone_request(drive);
852 return ide_stopped;
853 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200854 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200855 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200856 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 goto out;
858 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200859 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200860 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200861 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto out;
863 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200864 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200865 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200866 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
867 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto out;
869 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200870 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 idetape_media_access_finished(drive);
872 return ide_stopped;
873 }
874 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200875
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200876out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100877 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
Borislav Petkov41aa1702008-04-27 15:38:32 +0200881 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +0200882 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100883 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 *
Borislav Petkov41aa1702008-04-27 15:38:32 +0200885 * It returns a pointer to the newly allocated buffer, or NULL in case of
886 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200888static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
889 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200891 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +0200892 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200893 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 char *b_data = NULL;
895
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200896 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
897 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (bh == NULL)
899 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200900
901 order = fls(pages) - 1;
902 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100903 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200905 b_allocd = (1 << order) * PAGE_SIZE;
906 pages &= (order-1);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200909 memset(bh->b_data, 0, b_allocd);
910 bh->b_reqnext = NULL;
911 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 atomic_set(&bh->b_count, full ? bh->b_size : 0);
913
Borislav Petkov41aa1702008-04-27 15:38:32 +0200914 while (pages) {
915 order = fls(pages) - 1;
916 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100917 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200919 b_allocd = (1 << order) * PAGE_SIZE;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200922 memset(b_data, 0, b_allocd);
923
924 /* newly allocated page frames below buffer header or ...*/
925 if (bh->b_data == b_data + b_allocd) {
926 bh->b_size += b_allocd;
927 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200929 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 continue;
931 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200932 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200934 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200936 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 continue;
938 }
939 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100940 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
941 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200942 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto abort;
944 }
945 bh->b_reqnext = NULL;
946 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200947 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 atomic_set(&bh->b_count, full ? bh->b_size : 0);
949 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200950
951 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 bh->b_size -= tape->excess_bh_size;
955 if (full)
956 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200957 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200959 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return NULL;
961}
962
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100963static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +0200964 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct idetape_bh *bh = tape->bh;
967 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700968 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100972 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
973 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700974 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100976 count = min((unsigned int)
977 (bh->b_size - atomic_read(&bh->b_count)),
978 (unsigned int)n);
979 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
980 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700981 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 n -= count;
983 atomic_add(count, &bh->b_count);
984 buf += count;
985 if (atomic_read(&bh->b_count) == bh->b_size) {
986 bh = bh->b_reqnext;
987 if (bh)
988 atomic_set(&bh->b_count, 0);
989 }
990 }
991 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700992 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100995static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +0200996 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct idetape_bh *bh = tape->bh;
999 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001000 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001004 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1005 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001006 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001009 if (copy_to_user(buf, tape->b_data, count))
1010 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 n -= count;
1012 tape->b_data += count;
1013 tape->b_count -= count;
1014 buf += count;
1015 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001016 bh = bh->b_reqnext;
1017 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (bh) {
1019 tape->b_data = bh->b_data;
1020 tape->b_count = atomic_read(&bh->b_count);
1021 }
1022 }
1023 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001027static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001029 struct idetape_bh *bh = tape->merge_bh;
1030 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001031
Borislav Petkov54abf372008-02-06 02:57:52 +01001032 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 atomic_set(&bh->b_count, 0);
1034 else {
1035 tape->b_data = bh->b_data;
1036 tape->b_count = atomic_read(&bh->b_count);
1037 }
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001041 * Write a filemark if write_filemark=1. Flush the device buffers without
1042 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001044static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001045 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001047 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001048 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001050 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1054{
1055 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001056 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int load_attempted = 0;
1058
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001059 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001060 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 timeout += jiffies;
1062 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +02001063 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001066 || (tape->asc == 0x3A)) {
1067 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (load_attempted)
1069 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001070 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 load_attempted = 1;
1072 /* not about to be ready */
1073 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1074 (tape->ascq == 1 || tape->ascq == 8)))
1075 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001076 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 return -EIO;
1079}
1080
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001081static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001083 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001084 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int rc;
1086
1087 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001088 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001089 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return rc;
1091 idetape_wait_ready(drive, 60 * 5 * HZ);
1092 return 0;
1093}
1094
Borislav Petkovd236d742008-04-18 00:46:27 +02001095static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001097 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001098 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001099 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001102static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001105 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int position;
1107
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001108 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001111 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001113 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return position;
1115}
1116
Borislav Petkovd236d742008-04-18 00:46:27 +02001117static void idetape_create_locate_cmd(ide_drive_t *drive,
1118 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001119 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001121 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001122 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001124 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001126 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001129static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Borislav Petkov54abf372008-02-06 02:57:52 +01001133 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001134 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001136 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001137 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001138 if (tape->merge_bh != NULL) {
1139 ide_tape_kfree_buffer(tape);
1140 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
Borislav Petkov54abf372008-02-06 02:57:52 +01001143 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001147 * Position the tape to the requested block using the LOCATE packet command.
1148 * A READ POSITION command is then issued to check where we are positioned. Like
1149 * all higher level operations, we queue the commands at the tail of the request
1150 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001152static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1153 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001156 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001158 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Borislav Petkov54abf372008-02-06 02:57:52 +01001160 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001161 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 idetape_wait_ready(drive, 60 * 5 * HZ);
1163 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001164 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (retval)
1166 return (retval);
1167
1168 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001169 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001172static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001173 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int seek, position;
1177
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001178 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (restore_position) {
1180 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001181 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001183 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001184 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return;
1186 }
1187 }
1188}
1189
1190/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001191 * Generate a read/write request for the block device interface and wait for it
1192 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001194static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1195 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001198 struct request *rq;
1199 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001201 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1202
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001203 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1204 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001205 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001206 rq->rq_disk = tape->disk;
1207 rq->special = (void *)bh;
1208 rq->sector = tape->first_frame;
1209 rq->nr_sectors = blocks;
1210 rq->current_nr_sectors = blocks;
1211 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1212
1213 errors = rq->errors;
1214 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1215 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1218 return 0;
1219
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001220 if (tape->merge_bh)
1221 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001222 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Borislav Petkovd236d742008-04-18 00:46:27 +02001227static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001229 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001230 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001231 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001232 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Borislav Petkovd236d742008-04-18 00:46:27 +02001235static void idetape_create_rewind_cmd(ide_drive_t *drive,
1236 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001238 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001239 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001240 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Borislav Petkovd236d742008-04-18 00:46:27 +02001243static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001245 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001246 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001248 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Borislav Petkovd236d742008-04-18 00:46:27 +02001251static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001253 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001254 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001255 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001257 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Borislav Petkov97c566c2008-04-27 15:38:25 +02001260/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001261static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001265 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001267 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001268 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Borislav Petkovd9df9372008-04-27 15:38:34 +02001271static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 idetape_tape_t *tape = drive->driver_data;
1274 int blocks, min;
1275 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001276
Borislav Petkov54abf372008-02-06 02:57:52 +01001277 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001278 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001279 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001282 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001284 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001286 if (tape->merge_bh_size) {
1287 blocks = tape->merge_bh_size / tape->blk_size;
1288 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned int i;
1290
1291 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001292 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001293 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 bh = tape->bh->b_reqnext;
1295 while (bh) {
1296 atomic_set(&bh->b_count, 0);
1297 bh = bh->b_reqnext;
1298 }
1299 bh = tape->bh;
1300 while (i) {
1301 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001302 printk(KERN_INFO "ide-tape: bug,"
1303 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
1305 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001306 min = min(i, (unsigned int)(bh->b_size -
1307 atomic_read(&bh->b_count)));
1308 memset(bh->b_data + atomic_read(&bh->b_count),
1309 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 atomic_add(min, &bh->b_count);
1311 i -= min;
1312 bh = bh->b_reqnext;
1313 }
1314 }
1315 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001316 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001318 if (tape->merge_bh != NULL) {
1319 ide_tape_kfree_buffer(tape);
1320 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001322 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Borislav Petkov83042b22008-04-27 15:38:27 +02001325static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001331 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1332 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001333 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 idetape_flush_tape_buffers(drive);
1335 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001336 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001337 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001338 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001339 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001341 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1342 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001344 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001347 * Issue a read 0 command to ensure that DSC handshake is
1348 * switched from completion mode to buffer available mode.
1349 * No point in issuing this if DSC overlap isn't supported, some
1350 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001352 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001353 bytes_read = idetape_queue_rw_tail(drive,
1354 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001355 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001357 ide_tape_kfree_buffer(tape);
1358 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001359 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return bytes_read;
1361 }
1362 }
1363 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366}
1367
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001368/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001369static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001373 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001375 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001376 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return 0;
1378
Borislav Petkov83042b22008-04-27 15:38:27 +02001379 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001381 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001382 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001385static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 idetape_tape_t *tape = drive->driver_data;
1388 struct idetape_bh *bh;
1389 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 while (bcount) {
1392 unsigned int count;
1393
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001394 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001395 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001397 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001399 atomic_set(&bh->b_count,
1400 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1402 count -= atomic_read(&bh->b_count);
1403 bh = bh->b_reqnext;
1404 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001405 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001406 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001411 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1412 * currently support only one partition.
1413 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001414static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001416 struct ide_tape_obj *tape = drive->driver_data;
1417 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001419 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001420
1421 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001424 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (retval)
1426 return retval;
1427
1428 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001429 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (retval)
1431 return retval;
1432 return 0;
1433}
1434
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001435/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001436static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1437 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 void __user *argp = (void __user *)arg;
1441
Borislav Petkovd59823f2008-02-02 19:56:51 +01001442 struct idetape_config {
1443 int dsc_rw_frequency;
1444 int dsc_media_access_frequency;
1445 int nr_stages;
1446 } config;
1447
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001448 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001451 case 0x0340:
1452 if (copy_from_user(&config, argp, sizeof(config)))
1453 return -EFAULT;
1454 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001455 break;
1456 case 0x0350:
1457 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001458 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001459 if (copy_to_user(argp, &config, sizeof(config)))
1460 return -EFAULT;
1461 break;
1462 default:
1463 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 return 0;
1466}
1467
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001468static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1469 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001472 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001473 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001474 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001475 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (mt_count == 0)
1478 return 0;
1479 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001480 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001482 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
Borislav Petkov54abf372008-02-06 02:57:52 +01001485 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001486 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001487 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001489 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001493 case MTFSF:
1494 case MTBSF:
1495 idetape_create_space_cmd(&pc, mt_count - count,
1496 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001497 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001498 case MTFSFM:
1499 case MTBSFM:
1500 if (!sprev)
1501 return -EIO;
1502 retval = idetape_space_over_filemarks(drive, MTFSF,
1503 mt_count - count);
1504 if (retval)
1505 return retval;
1506 count = (MTBSFM == mt_op ? 1 : -1);
1507 return idetape_space_over_filemarks(drive, MTFSF, count);
1508 default:
1509 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1510 mt_op);
1511 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001516 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001518 * The tape is optimized to maximize throughput when it is transferring an
1519 * integral number of the "continuous transfer limit", which is a parameter of
1520 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001522 * As of version 1.3 of the driver, the character device provides an abstract
1523 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1524 * same backup/restore procedure is supported. The driver will internally
1525 * convert the requests to the recommended transfer unit, so that an unmatch
1526 * between the user's block size to the recommended size will only result in a
1527 * (slightly) increased driver overhead, but will no longer hit performance.
1528 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001530static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1531 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001533 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001535 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001536 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001537 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001539 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Borislav Petkov54abf372008-02-06 02:57:52 +01001541 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001542 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001543 if (count > tape->blk_size &&
1544 (count % tape->blk_size) == 0)
1545 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001547 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001548 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return rc;
1550 if (count == 0)
1551 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001552 if (tape->merge_bh_size) {
1553 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001554 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001555 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001556 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001558 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 count -= actually_read;
1560 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001561 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001562 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (bytes_read <= 0)
1564 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001565 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001566 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 buf += bytes_read;
1568 count -= bytes_read;
1569 actually_read += bytes_read;
1570 }
1571 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001572 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (bytes_read <= 0)
1574 goto finish;
1575 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001576 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001577 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001579 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001582 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001583 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 idetape_space_over_filemarks(drive, MTFSF, 1);
1586 return 0;
1587 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001588
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001589 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001592static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 size_t count, loff_t *ppos)
1594{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001595 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001597 ssize_t actually_written = 0;
1598 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001599 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /* The drive is write protected. */
1602 if (tape->write_prot)
1603 return -EACCES;
1604
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001605 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001608 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1609 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001610 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001611 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001612 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001614 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001616 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1617 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001619 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001620 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001623 * Issue a write 0 command to ensure that DSC handshake is
1624 * switched from completion mode to buffer available mode. No
1625 * point in issuing this if DSC overlap isn't supported, some
1626 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001628 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629 ssize_t retval = idetape_queue_rw_tail(drive,
1630 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001631 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001633 ide_tape_kfree_buffer(tape);
1634 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001635 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return retval;
1637 }
1638 }
1639 }
1640 if (count == 0)
1641 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001642 if (tape->merge_bh_size) {
1643 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001644 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001645 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001647 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001648 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001649 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001650 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001651 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001653 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 count -= actually_written;
1655
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001656 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001657 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001658 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001659 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (retval <= 0)
1661 return (retval);
1662 }
1663 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001664 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001665 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001666 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001667 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001668 buf += tape->buffer_size;
1669 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001670 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001671 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (retval <= 0)
1673 return (retval);
1674 }
1675 if (count) {
1676 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001677 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001678 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001679 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001681 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001684static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001686 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001687 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 /* Write a filemark */
1690 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001691 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1693 return -EIO;
1694 }
1695 return 0;
1696}
1697
1698/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001699 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1700 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001702 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1703 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001704 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001706 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001708 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1709 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001711static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001714 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001715 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001716 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001718 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1719 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001722 case MTFSF:
1723 case MTFSFM:
1724 case MTBSF:
1725 case MTBSFM:
1726 if (!mt_count)
1727 return 0;
1728 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1729 default:
1730 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001734 case MTWEOF:
1735 if (tape->write_prot)
1736 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001737 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001738 for (i = 0; i < mt_count; i++) {
1739 retval = idetape_write_filemark(drive);
1740 if (retval)
1741 return retval;
1742 }
1743 return 0;
1744 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001745 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001746 if (idetape_rewind_tape(drive))
1747 return -EIO;
1748 return 0;
1749 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001750 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001751 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001752 case MTUNLOAD:
1753 case MTOFFL:
1754 /*
1755 * If door is locked, attempt to unlock before
1756 * attempting to eject.
1757 */
1758 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001759 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001760 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001761 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001762 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001763 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001764 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001765 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001766 return retval;
1767 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001768 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001769 return idetape_flush_tape_buffers(drive);
1770 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001771 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001772 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001773 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001774 case MTEOM:
1775 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001776 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001777 case MTERASE:
1778 (void)idetape_rewind_tape(drive);
1779 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001780 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001781 case MTSETBLK:
1782 if (mt_count) {
1783 if (mt_count < tape->blk_size ||
1784 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001786 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001787 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001788 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001789 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001790 return 0;
1791 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001792 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001793 return idetape_position_tape(drive,
1794 mt_count * tape->user_bs_factor, tape->partition, 0);
1795 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001796 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001797 return idetape_position_tape(drive, 0, mt_count, 0);
1798 case MTFSR:
1799 case MTBSR:
1800 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001801 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001802 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001804 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1805 return 0;
1806 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001807 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001808 if (retval)
1809 return retval;
1810 tape->door_locked = DOOR_UNLOCKED;
1811 return 0;
1812 default:
1813 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1814 mt_op);
1815 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817}
1818
1819/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001820 * Our character device ioctls. General mtio.h magnetic io commands are
1821 * supported here, and not in the corresponding block interface. Our own
1822 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001824static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1825 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001827 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 ide_drive_t *drive = tape->drive;
1829 struct mtop mtop;
1830 struct mtget mtget;
1831 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001832 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 void __user *argp = (void __user *)arg;
1834
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001835 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Borislav Petkov54abf372008-02-06 02:57:52 +01001837 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001838 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 idetape_flush_tape_buffers(drive);
1840 }
1841 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001842 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001843 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001844 position = idetape_read_position(drive);
1845 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EIO;
1847 }
1848 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001849 case MTIOCTOP:
1850 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1851 return -EFAULT;
1852 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1853 case MTIOCGET:
1854 memset(&mtget, 0, sizeof(struct mtget));
1855 mtget.mt_type = MT_ISSCSI2;
1856 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1857 mtget.mt_dsreg =
1858 ((tape->blk_size * tape->user_bs_factor)
1859 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001860
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001861 if (tape->drv_write_prot)
1862 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1863
1864 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1865 return -EFAULT;
1866 return 0;
1867 case MTIOCPOS:
1868 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1869 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1870 return -EFAULT;
1871 return 0;
1872 default:
1873 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001874 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001875 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877}
1878
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001879/*
1880 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1881 * block size with the reported value.
1882 */
1883static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1884{
1885 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001886 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001887
1888 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001889 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001890 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001891 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001892 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1893 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001894 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001895 }
1896 return;
1897 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001898 tape->blk_size = (pc.buf[4 + 5] << 16) +
1899 (pc.buf[4 + 6] << 8) +
1900 pc.buf[4 + 7];
1901 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001904static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1907 ide_drive_t *drive;
1908 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int retval;
1910
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001911 if (i >= MAX_HWIFS * MAX_DRIVES)
1912 return -ENXIO;
1913
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001914 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001915 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001916 if (!tape) {
1917 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001918 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001919 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001920
1921 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /*
1924 * We really want to do nonseekable_open(inode, filp); here, but some
1925 * versions of tar incorrectly call lseek on tapes and bail out if that
1926 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1927 */
1928 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 drive = tape->drive;
1931
1932 filp->private_data = tape;
1933
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001934 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 retval = -EBUSY;
1936 goto out_put_tape;
1937 }
1938
1939 retval = idetape_wait_ready(drive, 60 * HZ);
1940 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001941 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1943 goto out_put_tape;
1944 }
1945
1946 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001947 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 (void)idetape_rewind_tape(drive);
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001951 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /* Set write protect flag if device is opened as read-only. */
1954 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1955 tape->write_prot = 1;
1956 else
1957 tape->write_prot = tape->drv_write_prot;
1958
1959 /* Make sure drive isn't write protected if user wants to write. */
1960 if (tape->write_prot) {
1961 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1962 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001963 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 retval = -EROFS;
1965 goto out_put_tape;
1966 }
1967 }
1968
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001969 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001970 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001971 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001972 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1973 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001976 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
1978
1979out_put_tape:
1980 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001981 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return retval;
1983}
1984
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001985static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 idetape_tape_t *tape = drive->driver_data;
1988
Borislav Petkovd9df9372008-04-27 15:38:34 +02001989 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001990 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
1991 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001992 idetape_pad_zeros(drive, tape->blk_size *
1993 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001994 ide_tape_kfree_buffer(tape);
1995 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 idetape_write_filemark(drive);
1998 idetape_flush_tape_buffers(drive);
1999 idetape_flush_tape_buffers(drive);
2000}
2001
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002002static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002004 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 unsigned int minor = iminor(inode);
2007
2008 lock_kernel();
2009 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002010
2011 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Borislav Petkov54abf372008-02-06 02:57:52 +01002013 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002015 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002017 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002019
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002020 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002022 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002024 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002025 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002028 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 ide_tape_put(tape);
2030 unlock_kernel();
2031 return 0;
2032}
2033
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002034static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002037 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002038 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002041 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002042 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2043 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return;
2045 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002046 memcpy(vendor_id, &pc.buf[8], 8);
2047 memcpy(product_id, &pc.buf[16], 16);
2048 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002049
Borislav Petkov801bd322008-09-27 19:32:17 +02002050 ide_fixstring(vendor_id, 8, 0);
2051 ide_fixstring(product_id, 16, 0);
2052 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002053
Borislav Petkov801bd322008-09-27 19:32:17 +02002054 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002055 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002059 * Ask the tape about its various parameters. In particular, we will adjust our
2060 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002062static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002065 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002066 u8 *caps;
2067 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002070 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002071 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2072 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002073 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002074 put_unaligned(52, (u16 *)&tape->caps[12]);
2075 put_unaligned(540, (u16 *)&tape->caps[14]);
2076 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return;
2078 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002079 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Borislav Petkovb6422012008-02-02 19:56:49 +01002081 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002082 speed = be16_to_cpup((__be16 *)&caps[14]);
2083 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002085 *(u16 *)&caps[8] = max_speed;
2086 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2087 *(u16 *)&caps[14] = speed;
2088 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002089
2090 if (!speed) {
2091 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2092 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002093 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002095 if (!max_speed) {
2096 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2097 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002098 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100
Borislav Petkovb6422012008-02-02 19:56:49 +01002101 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002102
2103 /* device lacks locking support according to capabilities page */
2104 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02002105 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002106
Borislav Petkovb6422012008-02-02 19:56:49 +01002107 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002108 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002109 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002110 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002113#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002114#define ide_tape_devset_get(name, field) \
2115static int get_##name(ide_drive_t *drive) \
2116{ \
2117 idetape_tape_t *tape = drive->driver_data; \
2118 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002120
2121#define ide_tape_devset_set(name, field) \
2122static int set_##name(ide_drive_t *drive, int arg) \
2123{ \
2124 idetape_tape_t *tape = drive->driver_data; \
2125 tape->field = arg; \
2126 return 0; \
2127}
2128
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002129#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002130ide_tape_devset_get(_name, _field) \
2131ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002132IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002133
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002134#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002135ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002136IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002137
2138static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2139static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2140static int divf_buffer(ide_drive_t *drive) { return 2; }
2141static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2142
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002143ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002144
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002145ide_tape_devset_rw_field(debug_mask, debug_mask);
2146ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002147
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002148ide_tape_devset_r_field(avg_speed, avg_speed);
2149ide_tape_devset_r_field(speed, caps[14]);
2150ide_tape_devset_r_field(buffer, caps[16]);
2151ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002152
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002153static const struct ide_proc_devset idetape_settings[] = {
2154 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2155 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2156 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2157 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2158 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2159 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2160 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2161 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01002162 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002163};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002167 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002169 * 1. Initialize our various state variables.
2170 * 2. Ask the tape for its capabilities.
2171 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2172 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002174 * Note that at this point ide.c already assigned us an irq, so that we can
2175 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002177static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Borislav Petkov83042b22008-04-27 15:38:27 +02002179 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002181 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002182 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02002184 drive->pc_callback = ide_tape_callback;
2185 drive->pc_update_buffers = idetape_update_buffers;
2186 drive->pc_io_buffers = ide_tape_io_buffers;
Borislav Petkov776bb022008-07-23 19:55:59 +02002187
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002188 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2189
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002190 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2191 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2192 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002193 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002197 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002198 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 tape->minor = minor;
2201 tape->name[0] = 'h';
2202 tape->name[1] = 't';
2203 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002204 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 idetape_get_inquiry_results(drive);
2207 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002208 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002210 tape->buffer_size = *ctl * tape->blk_size;
2211 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002213 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002214 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002216 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002217 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002218 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002219 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002220 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222
Borislav Petkov83042b22008-04-27 15:38:27 +02002223 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002224 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Borislav Petkovf73850a2008-04-27 15:38:33 +02002226 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002229 * Ensure that the number we got makes sense; limit it within
2230 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002232 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2233 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002235 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002236 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002237 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2238 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002239 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002240 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002242 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Russell King4031bbe2006-01-06 11:41:00 +00002245static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002249 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002250 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 ide_unregister_region(tape->disk);
2252
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002253 mutex_lock(&idetape_ref_mutex);
2254 put_device(&tape->dev);
2255 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002258static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002260 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 ide_drive_t *drive = tape->drive;
2262 struct gendisk *g = tape->disk;
2263
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002264 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002265
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002266 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002268 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002269 device_destroy(idetape_sysfs_class,
2270 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 idetape_devs[tape->minor] = NULL;
2272 g->private_data = NULL;
2273 put_disk(g);
2274 kfree(tape);
2275}
2276
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002277#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278static int proc_idetape_read_name
2279 (char *page, char **start, off_t off, int count, int *eof, void *data)
2280{
2281 ide_drive_t *drive = (ide_drive_t *) data;
2282 idetape_tape_t *tape = drive->driver_data;
2283 char *out = page;
2284 int len;
2285
2286 len = sprintf(out, "%s\n", tape->name);
2287 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2288}
2289
2290static ide_proc_entry_t idetape_proc[] = {
2291 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2292 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2293 { NULL, 0, NULL, NULL }
2294};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002295
2296static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2297{
2298 return idetape_proc;
2299}
2300
2301static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2302{
2303 return idetape_settings;
2304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305#endif
2306
Russell King4031bbe2006-01-06 11:41:00 +00002307static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01002309static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002310 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002311 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002312 .name = "ide-tape",
2313 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002314 },
Russell King4031bbe2006-01-06 11:41:00 +00002315 .probe = ide_tape_probe,
2316 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 .do_request = idetape_do_request,
2319 .end_request = idetape_end_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002320#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002321 .proc_entries = ide_tape_proc_entries,
2322 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324};
2325
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002326/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002327static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 .owner = THIS_MODULE,
2329 .read = idetape_chrdev_read,
2330 .write = idetape_chrdev_write,
2331 .ioctl = idetape_chrdev_ioctl,
2332 .open = idetape_chrdev_open,
2333 .release = idetape_chrdev_release,
2334};
2335
Al Viroa4600f82008-03-02 10:27:58 -05002336static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Al Viroa4600f82008-03-02 10:27:58 -05002338 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002340 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return -ENXIO;
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344}
2345
Al Viroa4600f82008-03-02 10:27:58 -05002346static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002348 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return 0;
2352}
2353
Al Viroa4600f82008-03-02 10:27:58 -05002354static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 unsigned int cmd, unsigned long arg)
2356{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002357 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04002359 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (err == -EINVAL)
2361 err = idetape_blkdev_ioctl(drive, cmd, arg);
2362 return err;
2363}
2364
2365static struct block_device_operations idetape_block_ops = {
2366 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05002367 .open = idetape_open,
2368 .release = idetape_release,
2369 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370};
2371
Russell King4031bbe2006-01-06 11:41:00 +00002372static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 idetape_tape_t *tape;
2375 struct gendisk *g;
2376 int minor;
2377
2378 if (!strstr("ide-tape", drive->driver_req))
2379 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (drive->media != ide_tape)
2382 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002383
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002384 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2385 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002386 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2387 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 goto failed;
2389 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002390 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002392 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2393 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto failed;
2395 }
2396
2397 g = alloc_disk(1 << PARTN_BITS);
2398 if (!g)
2399 goto out_free_tape;
2400
2401 ide_init_disk(g, drive);
2402
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002403 tape->dev.parent = &drive->gendev;
2404 tape->dev.release = ide_tape_release;
2405 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2406
2407 if (device_register(&tape->dev))
2408 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 tape->drive = drive;
2411 tape->driver = &idetape_driver;
2412 tape->disk = g;
2413
2414 g->private_data = &tape->driver;
2415
2416 drive->driver_data = tape;
2417
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002418 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 for (minor = 0; idetape_devs[minor]; minor++)
2420 ;
2421 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002422 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 idetape_setup(drive, tape, minor);
2425
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002426 device_create(idetape_sysfs_class, &drive->gendev,
2427 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2428 device_create(idetape_sysfs_class, &drive->gendev,
2429 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2430 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 g->fops = &idetape_block_ops;
2433 ide_register_region(g);
2434
2435 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002436
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002437out_free_disk:
2438 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439out_free_tape:
2440 kfree(tape);
2441failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002442 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002445static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002447 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002448 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 unregister_chrdev(IDETAPE_MAJOR, "ht");
2450}
2451
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002452static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Will Dysond5dee802005-09-16 02:55:07 -07002454 int error = 1;
2455 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2456 if (IS_ERR(idetape_sysfs_class)) {
2457 idetape_sysfs_class = NULL;
2458 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2459 error = -EBUSY;
2460 goto out;
2461 }
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002464 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2465 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002466 error = -EBUSY;
2467 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
Will Dysond5dee802005-09-16 02:55:07 -07002469
2470 error = driver_register(&idetape_driver.gen_driver);
2471 if (error)
2472 goto out_free_driver;
2473
2474 return 0;
2475
2476out_free_driver:
2477 driver_unregister(&idetape_driver.gen_driver);
2478out_free_class:
2479 class_destroy(idetape_sysfs_class);
2480out:
2481 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Kay Sievers263756e2005-12-12 18:03:44 +01002484MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485module_init(idetape_init);
2486module_exit(idetape_exit);
2487MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002488MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2489MODULE_LICENSE("GPL");