blob: fc61bbef3bb940a78e088eaf75e31240ea4f7a15 [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/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
156#define IDETAPE_BLOCK_DESCRIPTOR 0
157#define IDETAPE_CAPABILITIES_PAGE 0x2a
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100160 * Most of our global data which we need to save even as we leave the driver due
161 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
163typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100164 ide_drive_t *drive;
165 struct ide_driver *driver;
166 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100167 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200169 /* used by REQ_IDETAPE_{READ,WRITE} requests */
170 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100173 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100175 * While polling for DSC we use postponed_rq to postpone the current
176 * request so that ide.c will be able to service pending requests on the
177 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200178 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180 struct request *postponed_rq;
181 /* The time in which we started polling for DSC */
182 unsigned long dsc_polling_start;
183 /* Timer used to poll for dsc */
184 struct timer_list dsc_timer;
185 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100186 unsigned long best_dsc_rw_freq;
187 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned long dsc_timeout;
189
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100190 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 u8 partition;
192 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100193 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100195 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 u8 sense_key, asc, ascq;
197
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100198 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned int minor;
200 /* device name */
201 char name[4];
202 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100203 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Borislav Petkov54bb2072008-02-06 02:57:52 +0100205 /* tape block size, usually 512 or 1024 bytes */
206 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100210 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100213 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100215 * At most, there is only one ide-tape originated data transfer request
216 * in the device request queue. This allows ide.c to easily service
217 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200219
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100220 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200221 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200222 /* merge buffer */
223 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200224 /* size of the merge buffer */
225 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200226 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct idetape_bh *bh;
228 char *b_data;
229 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100230
Borislav Petkova997a432008-04-27 15:38:33 +0200231 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Wasted space in each stage */
233 int excess_bh_size;
234
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100235 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 unsigned long avg_time;
237 int avg_size;
238 int avg_speed;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* the door is currently locked */
241 int door_locked;
242 /* the tape hardware is write protected */
243 char drv_write_prot;
244 /* the tape is write protected (hardware or opened as read-only) */
245 char write_prot;
246
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100247 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248} idetape_tape_t;
249
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800250static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Will Dysond5dee802005-09-16 02:55:07 -0700252static struct class *idetape_sysfs_class;
253
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100254static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
257{
258 struct ide_tape_obj *tape = NULL;
259
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800260 mutex_lock(&idetape_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +0200261 tape = ide_drv_g(disk, ide_tape_obj);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200262 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200263 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200264 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200265 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100266 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200267 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800268 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return tape;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void ide_tape_put(struct ide_tape_obj *tape)
273{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200274 ide_drive_t *drive = tape->drive;
275
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800276 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100277 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200278 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800279 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100283 * The variables below are used for the character device interface. Additional
284 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100286static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
289{
290 struct ide_tape_obj *tape = NULL;
291
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800292 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 tape = idetape_devs[i];
294 if (tape)
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100295 get_device(&tape->dev);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800296 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return tape;
298}
299
Borislav Petkovd236d742008-04-18 00:46:27 +0200300static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100301 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct idetape_bh *bh = pc->bh;
304 int count;
305
306 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (bh == NULL) {
308 printk(KERN_ERR "ide-tape: bh == NULL in "
309 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200310 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return;
312 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100313 count = min(
314 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
315 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200316 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100317 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 bcount -= count;
319 atomic_add(count, &bh->b_count);
320 if (atomic_read(&bh->b_count) == bh->b_size) {
321 bh = bh->b_reqnext;
322 if (bh)
323 atomic_set(&bh->b_count, 0);
324 }
325 }
326 pc->bh = bh;
327}
328
Borislav Petkovd236d742008-04-18 00:46:27 +0200329static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100330 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct idetape_bh *bh = pc->bh;
333 int count;
334
335 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100337 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
338 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200342 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 bcount -= count;
344 pc->b_data += count;
345 pc->b_count -= count;
346 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100347 bh = bh->b_reqnext;
348 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (bh) {
350 pc->b_data = bh->b_data;
351 pc->b_count = atomic_read(&bh->b_count);
352 }
353 }
354 }
355}
356
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200357static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct idetape_bh *bh = pc->bh;
360 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200361 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Petkov346331f2008-04-18 00:46:26 +0200363 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return;
365 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100367 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
368 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
372 atomic_set(&bh->b_count, count);
373 if (atomic_read(&bh->b_count) == bh->b_size)
374 bh = bh->b_reqnext;
375 bcount -= count;
376 }
377 pc->bh = bh;
378}
379
380/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100381 * called on each failed packet command retry to analyze the request sense. We
382 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100384static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100387 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Borislav Petkov1b5db432008-02-02 19:56:48 +0100389 tape->sense_key = sense[2] & 0xF;
390 tape->asc = sense[12];
391 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100392
393 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
394 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Borislav Petkovd236d742008-04-18 00:46:27 +0200396 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200397 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200398 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100399 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200400 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200401 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 /*
405 * If error was the result of a zero-length read or write command,
406 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
407 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
408 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100409 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100410 /* length == 0 */
411 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
412 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* don't report an error, everything's ok */
414 pc->error = 0;
415 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200416 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100419 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100420 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200421 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100423 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100424 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
425 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100426 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200427 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100430 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100431 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100432 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200433 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200435 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200436 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
438 }
439}
440
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200441/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200442static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200444 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200446 while (bh) {
447 u32 size = bh->b_size;
448
449 while (size) {
450 unsigned int order = fls(size >> PAGE_SHIFT)-1;
451
452 if (bh->b_data)
453 free_pages((unsigned long)bh->b_data, order);
454
455 size &= (order-1);
456 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 prev_bh = bh;
459 bh = bh->b_reqnext;
460 kfree(prev_bh);
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200464static void ide_tape_handle_dsc(ide_drive_t *);
465
466static void ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200469 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100470 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200471 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100472 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100474 debug_log(DBG_PROCS, "Enter %s\n", __func__);
475
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200476 if (dsc)
477 ide_tape_handle_dsc(drive);
478
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100479 if (drive->failed_pc == pc)
480 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200481
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200482 if (pc->c[0] == REQUEST_SENSE) {
483 if (uptodate)
484 idetape_analyze_error(drive, pc->buf);
485 else
486 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
487 "itself - Aborting request!\n");
488 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200489 int blocks = pc->xferred / tape->blk_size;
490
491 tape->avg_size += blocks * tape->blk_size;
492
493 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
494 tape->avg_speed = tape->avg_size * HZ /
495 (jiffies - tape->avg_time) / 1024;
496 tape->avg_size = 0;
497 tape->avg_time = jiffies;
498 }
499
500 tape->first_frame += blocks;
501 rq->current_nr_sectors -= blocks;
502
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100503 if (pc->error) {
504 uptodate = 0;
505 err = pc->error;
506 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200507 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200508 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200509
510 debug_log(DBG_SENSE, "BOP - %s\n",
511 (readpos[0] & 0x80) ? "Yes" : "No");
512 debug_log(DBG_SENSE, "EOP - %s\n",
513 (readpos[0] & 0x40) ? "Yes" : "No");
514
515 if (readpos[0] & 0x4) {
516 printk(KERN_INFO "ide-tape: Block location is unknown"
517 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200518 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200519 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100520 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200521 } else {
522 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200523 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200524
525 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200526 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200527 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200530
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100531 rq->errors = err;
532
533 if (uptodate == 0)
534 drive->failed_pc = NULL;
535
536 if (blk_special_request(rq))
537 ide_complete_rq(drive, 0);
538 else
539 ide_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100543 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100544 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100546static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 idetape_tape_t *tape = drive->driver_data;
549
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100550 debug_log(DBG_PROCS, "Enter %s\n", __func__);
551
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100552 tape->postponed_rq = drive->hwif->rq;
553
Borislav Petkov54bb2072008-02-06 02:57:52 +0100554 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200557static void ide_tape_handle_dsc(ide_drive_t *drive)
558{
559 idetape_tape_t *tape = drive->driver_data;
560
561 /* Media access command */
562 tape->dsc_polling_start = jiffies;
563 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
564 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
565 /* Allow ide.c to handle other requests */
566 idetape_postpone_request(drive);
567}
568
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200569static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200570 unsigned int bcount, int write)
571{
572 if (write)
573 idetape_output_buffers(drive, pc, bcount);
574 else
575 idetape_input_buffers(drive, pc, bcount);
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200576
577 return bcount;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200578}
Borislav Petkova1efc852008-02-06 02:57:52 +0100579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100581 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200583 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100584 * until we finish handling it. Each packet command is associated with a
585 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100587 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100589 * 1. idetape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200590 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200592 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100593 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100595 * 3. ATAPI Tape media access commands have immediate status with a delayed
596 * process. In case of a successful initiation of a media access packet command,
597 * the DSC bit will be set when the actual execution of the command is finished.
598 * Since the tape drive will not issue an interrupt, we have to poll for this
599 * event. In this case, we define the request as "low priority request" by
600 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
601 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100603 * ide.c will then give higher priority to requests which originate from the
604 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100606 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100608 * 5. In case an error was found, we queue a request sense packet command in
609 * front of the request queue and retry the operation up to
610 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100612 * 6. In case no error was found, or we decided to give up and not to retry
613 * again, the callback function will be called and then we will handle the next
614 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Borislav Petkovd236d742008-04-18 00:46:27 +0200617static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
618 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200622 if (drive->pc->c[0] == REQUEST_SENSE &&
Borislav Petkov90699ce2008-02-02 19:56:50 +0100623 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
625 "Two request sense in serial were issued\n");
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100628 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
629 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200632 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200635 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100637 * We will "abort" retrying a packet command in case legitimate
638 * error code was received (crossing a filemark, or end of the
639 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200641 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100642 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 tape->sense_key == 2 && tape->asc == 4 &&
644 (tape->ascq == 1 || tape->ascq == 8))) {
645 printk(KERN_ERR "ide-tape: %s: I/O error, "
646 "pc = %2x, key = %2x, "
647 "asc = %2x, ascq = %2x\n",
648 tape->name, pc->c[0],
649 tape->sense_key, tape->asc,
650 tape->ascq);
651 }
652 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100653 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100655 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200656 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200657 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100659 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100663 return ide_issue_pc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100666/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200667static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200669 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100670 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100672 /* DBD = 1 - Don't return block descriptors */
673 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 pc->c[2] = page_code;
675 /*
676 * Changed pc->c[3] to 0 (255 will at best return unused info).
677 *
678 * For SCSI this byte is defined as subpage instead of high byte
679 * of length and some IDE drives seem to interpret it this way
680 * and return an error when 255 is used.
681 */
682 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100683 /* We will just discard data in that case */
684 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200686 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200688 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200690 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100693static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200695 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200697 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100698 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200700 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100701
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200702 if (stat & ATA_DSC) {
703 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100705 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_ERR "ide-tape: %s: I/O error, ",
707 tape->name);
708 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200709 ide_retry_pc(drive, tape->disk);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200710 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100714 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100715 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200717 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return ide_stopped;
719}
720
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200721static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200722 struct ide_atapi_pc *pc, struct request *rq,
723 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Borislav Petkov0014c752008-07-23 19:56:00 +0200725 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
726 unsigned int length = rq->current_nr_sectors;
727
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200728 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100729 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200732 pc->buf = NULL;
733 pc->buf_size = length * tape->blk_size;
734 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200735 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200736 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200738 if (opcode == READ_6) {
739 pc->c[0] = READ_6;
740 atomic_set(&bh->b_count, 0);
741 } else if (opcode == WRITE_6) {
742 pc->c[0] = WRITE_6;
743 pc->flags |= PC_FLAG_WRITING;
744 pc->b_data = bh->b_data;
745 pc->b_count = atomic_read(&bh->b_count);
746 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200747
748 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static ide_startstop_t idetape_do_request(ide_drive_t *drive,
752 struct request *rq, sector_t block)
753{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200754 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200756 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100758 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Mark de Wever730616b2008-10-10 22:39:17 +0200760 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
761 " current_nr_sectors: %u\n",
762 (unsigned long long)rq->sector, rq->nr_sectors,
763 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Jens Axboe4aff5e22006-08-10 08:44:47 +0200765 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100766 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200768 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ide_end_request(drive, 0, 0);
770 return ide_stopped;
771 }
772
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100773 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100774 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
775 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200776 goto out;
777 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (postponed_rq != NULL)
780 if (rq != postponed_rq) {
781 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
782 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100783 rq->errors = IDE_DRV_ERROR_GENERAL;
784 drive->failed_pc = NULL;
785 ide_complete_rq(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return ide_stopped;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 tape->postponed_rq = NULL;
790
791 /*
792 * If the tape is still busy, postpone our request and service
793 * the other device meanwhile.
794 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200795 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200797 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
798 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200799 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200801 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200802 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200803 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200806 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200807 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (postponed_rq == NULL) {
809 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100810 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
812 } else if (time_after(jiffies, tape->dsc_timeout)) {
813 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
814 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200815 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 idetape_media_access_finished(drive);
817 return ide_stopped;
818 } else {
819 return ide_do_reset(drive);
820 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100821 } else if (time_after(jiffies,
822 tape->dsc_polling_start +
823 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100824 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 idetape_postpone_request(drive);
826 return ide_stopped;
827 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200828 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200829 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200830 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto out;
832 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200833 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200834 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200835 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto out;
837 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200838 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200839 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200840 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
841 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto out;
843 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200844 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 idetape_media_access_finished(drive);
846 return ide_stopped;
847 }
848 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200849
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200850out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100851 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/*
Borislav Petkov41aa1702008-04-27 15:38:32 +0200855 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +0200856 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100857 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
Borislav Petkov41aa1702008-04-27 15:38:32 +0200859 * It returns a pointer to the newly allocated buffer, or NULL in case of
860 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200862static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
863 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200865 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +0200866 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200867 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 char *b_data = NULL;
869
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200870 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
871 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (bh == NULL)
873 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200874
875 order = fls(pages) - 1;
876 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100877 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200879 b_allocd = (1 << order) * PAGE_SIZE;
880 pages &= (order-1);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200883 memset(bh->b_data, 0, b_allocd);
884 bh->b_reqnext = NULL;
885 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 atomic_set(&bh->b_count, full ? bh->b_size : 0);
887
Borislav Petkov41aa1702008-04-27 15:38:32 +0200888 while (pages) {
889 order = fls(pages) - 1;
890 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100891 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200893 b_allocd = (1 << order) * PAGE_SIZE;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200896 memset(b_data, 0, b_allocd);
897
898 /* newly allocated page frames below buffer header or ...*/
899 if (bh->b_data == b_data + b_allocd) {
900 bh->b_size += b_allocd;
901 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200903 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 continue;
905 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200906 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200908 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200910 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 continue;
912 }
913 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100914 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
915 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200916 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto abort;
918 }
919 bh->b_reqnext = NULL;
920 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200921 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 atomic_set(&bh->b_count, full ? bh->b_size : 0);
923 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200924
925 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 bh->b_size -= tape->excess_bh_size;
929 if (full)
930 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200931 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200933 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return NULL;
935}
936
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100937static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +0200938 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct idetape_bh *bh = tape->bh;
941 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700942 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100946 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
947 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700948 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100950 count = min((unsigned int)
951 (bh->b_size - atomic_read(&bh->b_count)),
952 (unsigned int)n);
953 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
954 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700955 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 n -= count;
957 atomic_add(count, &bh->b_count);
958 buf += count;
959 if (atomic_read(&bh->b_count) == bh->b_size) {
960 bh = bh->b_reqnext;
961 if (bh)
962 atomic_set(&bh->b_count, 0);
963 }
964 }
965 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700966 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100969static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +0200970 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 struct idetape_bh *bh = tape->bh;
973 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700974 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100978 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
979 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700980 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700983 if (copy_to_user(buf, tape->b_data, count))
984 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 n -= count;
986 tape->b_data += count;
987 tape->b_count -= count;
988 buf += count;
989 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100990 bh = bh->b_reqnext;
991 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (bh) {
993 tape->b_data = bh->b_data;
994 tape->b_count = atomic_read(&bh->b_count);
995 }
996 }
997 }
Daniel Walkerdcd96372006-06-25 05:47:37 -0700998 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001001static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001003 struct idetape_bh *bh = tape->merge_bh;
1004 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001005
Borislav Petkov54abf372008-02-06 02:57:52 +01001006 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 atomic_set(&bh->b_count, 0);
1008 else {
1009 tape->b_data = bh->b_data;
1010 tape->b_count = atomic_read(&bh->b_count);
1011 }
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001015 * Write a filemark if write_filemark=1. Flush the device buffers without
1016 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001018static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001019 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001021 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001022 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001024 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1028{
1029 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001030 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int load_attempted = 0;
1032
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001033 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001034 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 timeout += jiffies;
1036 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +02001037 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001040 || (tape->asc == 0x3A)) {
1041 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (load_attempted)
1043 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001044 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 load_attempted = 1;
1046 /* not about to be ready */
1047 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1048 (tape->ascq == 1 || tape->ascq == 8)))
1049 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001050 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052 return -EIO;
1053}
1054
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001055static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001057 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001058 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int rc;
1060
1061 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001062 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001063 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return rc;
1065 idetape_wait_ready(drive, 60 * 5 * HZ);
1066 return 0;
1067}
1068
Borislav Petkovd236d742008-04-18 00:46:27 +02001069static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001071 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001072 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001073 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001076static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001079 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int position;
1081
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001082 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001085 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001087 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return position;
1089}
1090
Borislav Petkovd236d742008-04-18 00:46:27 +02001091static void idetape_create_locate_cmd(ide_drive_t *drive,
1092 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001093 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001095 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001096 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001098 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001100 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001103static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Borislav Petkov54abf372008-02-06 02:57:52 +01001107 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001110 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001111 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001112 if (tape->merge_bh != NULL) {
1113 ide_tape_kfree_buffer(tape);
1114 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
Borislav Petkov54abf372008-02-06 02:57:52 +01001117 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001121 * Position the tape to the requested block using the LOCATE packet command.
1122 * A READ POSITION command is then issued to check where we are positioned. Like
1123 * all higher level operations, we queue the commands at the tail of the request
1124 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001126static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1127 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001130 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001132 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Borislav Petkov54abf372008-02-06 02:57:52 +01001134 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001135 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 idetape_wait_ready(drive, 60 * 5 * HZ);
1137 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001138 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (retval)
1140 return (retval);
1141
1142 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001143 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001146static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001147 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int seek, position;
1151
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001152 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (restore_position) {
1154 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001155 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001157 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001158 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return;
1160 }
1161 }
1162}
1163
1164/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001165 * Generate a read/write request for the block device interface and wait for it
1166 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001168static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1169 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001172 struct request *rq;
1173 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001175 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1176
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001177 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1178 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001179 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001180 rq->rq_disk = tape->disk;
1181 rq->special = (void *)bh;
1182 rq->sector = tape->first_frame;
1183 rq->nr_sectors = blocks;
1184 rq->current_nr_sectors = blocks;
1185 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1186
1187 errors = rq->errors;
1188 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1189 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1192 return 0;
1193
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001194 if (tape->merge_bh)
1195 idetape_init_merge_buffer(tape);
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +01001196 if (errors == IDE_DRV_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Borislav Petkovd236d742008-04-18 00:46:27 +02001201static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001203 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001204 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001205 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001206 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Borislav Petkovd236d742008-04-18 00:46:27 +02001209static void idetape_create_rewind_cmd(ide_drive_t *drive,
1210 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001212 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001213 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001214 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Borislav Petkovd236d742008-04-18 00:46:27 +02001217static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001219 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001220 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001222 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Borislav Petkovd236d742008-04-18 00:46:27 +02001225static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001227 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001228 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001229 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001231 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Borislav Petkov97c566c2008-04-27 15:38:25 +02001234/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001235static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001239 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001241 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001242 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Borislav Petkovd9df9372008-04-27 15:38:34 +02001245static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 idetape_tape_t *tape = drive->driver_data;
1248 int blocks, min;
1249 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001250
Borislav Petkov54abf372008-02-06 02:57:52 +01001251 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001252 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001253 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return;
1255 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001256 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001258 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001260 if (tape->merge_bh_size) {
1261 blocks = tape->merge_bh_size / tape->blk_size;
1262 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 unsigned int i;
1264
1265 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001266 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001267 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 bh = tape->bh->b_reqnext;
1269 while (bh) {
1270 atomic_set(&bh->b_count, 0);
1271 bh = bh->b_reqnext;
1272 }
1273 bh = tape->bh;
1274 while (i) {
1275 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001276 printk(KERN_INFO "ide-tape: bug,"
1277 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001280 min = min(i, (unsigned int)(bh->b_size -
1281 atomic_read(&bh->b_count)));
1282 memset(bh->b_data + atomic_read(&bh->b_count),
1283 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 atomic_add(min, &bh->b_count);
1285 i -= min;
1286 bh = bh->b_reqnext;
1287 }
1288 }
1289 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001290 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001292 if (tape->merge_bh != NULL) {
1293 ide_tape_kfree_buffer(tape);
1294 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001296 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Borislav Petkov83042b22008-04-27 15:38:27 +02001299static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001305 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1306 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001307 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 idetape_flush_tape_buffers(drive);
1309 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001310 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001311 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001313 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001315 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1316 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001318 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001321 * Issue a read 0 command to ensure that DSC handshake is
1322 * switched from completion mode to buffer available mode.
1323 * No point in issuing this if DSC overlap isn't supported, some
1324 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001326 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001327 bytes_read = idetape_queue_rw_tail(drive,
1328 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001329 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001331 ide_tape_kfree_buffer(tape);
1332 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001333 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return bytes_read;
1335 }
1336 }
1337 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
1340}
1341
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001342/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001343static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001347 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001349 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001350 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352
Borislav Petkov83042b22008-04-27 15:38:27 +02001353 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001355 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001356 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001359static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 idetape_tape_t *tape = drive->driver_data;
1362 struct idetape_bh *bh;
1363 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 while (bcount) {
1366 unsigned int count;
1367
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001368 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001369 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001371 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001373 atomic_set(&bh->b_count,
1374 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1376 count -= atomic_read(&bh->b_count);
1377 bh = bh->b_reqnext;
1378 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001379 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001380 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001385 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1386 * currently support only one partition.
1387 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001388static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001390 struct ide_tape_obj *tape = drive->driver_data;
1391 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001393 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001394
1395 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001398 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (retval)
1400 return retval;
1401
1402 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001403 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (retval)
1405 return retval;
1406 return 0;
1407}
1408
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001409/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001410static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1411 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 void __user *argp = (void __user *)arg;
1415
Borislav Petkovd59823f2008-02-02 19:56:51 +01001416 struct idetape_config {
1417 int dsc_rw_frequency;
1418 int dsc_media_access_frequency;
1419 int nr_stages;
1420 } config;
1421
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001422 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001425 case 0x0340:
1426 if (copy_from_user(&config, argp, sizeof(config)))
1427 return -EFAULT;
1428 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001429 break;
1430 case 0x0350:
1431 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001432 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001433 if (copy_to_user(argp, &config, sizeof(config)))
1434 return -EFAULT;
1435 break;
1436 default:
1437 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 return 0;
1440}
1441
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001442static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1443 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001446 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001447 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001448 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001449 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (mt_count == 0)
1452 return 0;
1453 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001454 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001456 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
Borislav Petkov54abf372008-02-06 02:57:52 +01001459 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001460 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001461 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001463 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001467 case MTFSF:
1468 case MTBSF:
1469 idetape_create_space_cmd(&pc, mt_count - count,
1470 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001471 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001472 case MTFSFM:
1473 case MTBSFM:
1474 if (!sprev)
1475 return -EIO;
1476 retval = idetape_space_over_filemarks(drive, MTFSF,
1477 mt_count - count);
1478 if (retval)
1479 return retval;
1480 count = (MTBSFM == mt_op ? 1 : -1);
1481 return idetape_space_over_filemarks(drive, MTFSF, count);
1482 default:
1483 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1484 mt_op);
1485 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001490 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001492 * The tape is optimized to maximize throughput when it is transferring an
1493 * integral number of the "continuous transfer limit", which is a parameter of
1494 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001496 * As of version 1.3 of the driver, the character device provides an abstract
1497 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1498 * same backup/restore procedure is supported. The driver will internally
1499 * convert the requests to the recommended transfer unit, so that an unmatch
1500 * between the user's block size to the recommended size will only result in a
1501 * (slightly) increased driver overhead, but will no longer hit performance.
1502 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001504static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1505 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001507 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001509 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001510 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001511 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001513 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Borislav Petkov54abf372008-02-06 02:57:52 +01001515 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001516 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001517 if (count > tape->blk_size &&
1518 (count % tape->blk_size) == 0)
1519 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001521 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001522 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return rc;
1524 if (count == 0)
1525 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001526 if (tape->merge_bh_size) {
1527 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001528 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001529 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001530 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001532 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 count -= actually_read;
1534 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001535 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001536 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (bytes_read <= 0)
1538 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001539 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001540 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 buf += bytes_read;
1542 count -= bytes_read;
1543 actually_read += bytes_read;
1544 }
1545 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001546 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (bytes_read <= 0)
1548 goto finish;
1549 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001550 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001551 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001553 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001556 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001557 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 idetape_space_over_filemarks(drive, MTFSF, 1);
1560 return 0;
1561 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001562
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001563 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001566static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 size_t count, loff_t *ppos)
1568{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001569 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001571 ssize_t actually_written = 0;
1572 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001573 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /* The drive is write protected. */
1576 if (tape->write_prot)
1577 return -EACCES;
1578
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001579 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001582 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1583 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001584 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001585 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001586 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001588 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001590 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1591 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001593 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001594 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001597 * Issue a write 0 command to ensure that DSC handshake is
1598 * switched from completion mode to buffer available mode. No
1599 * point in issuing this if DSC overlap isn't supported, some
1600 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001602 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001603 ssize_t retval = idetape_queue_rw_tail(drive,
1604 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001605 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001607 ide_tape_kfree_buffer(tape);
1608 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001609 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return retval;
1611 }
1612 }
1613 }
1614 if (count == 0)
1615 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001616 if (tape->merge_bh_size) {
1617 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001618 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001619 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001622 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001623 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001624 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001625 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001627 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 count -= actually_written;
1629
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001630 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001631 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001632 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001633 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (retval <= 0)
1635 return (retval);
1636 }
1637 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001638 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001639 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001640 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001641 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001642 buf += tape->buffer_size;
1643 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001644 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001645 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (retval <= 0)
1647 return (retval);
1648 }
1649 if (count) {
1650 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001651 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001652 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001653 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001655 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001658static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001660 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001661 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* Write a filemark */
1664 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001665 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1667 return -EIO;
1668 }
1669 return 0;
1670}
1671
1672/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001673 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1674 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001676 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1677 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001678 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001680 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001682 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1683 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001685static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001688 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001689 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001690 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001692 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1693 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001696 case MTFSF:
1697 case MTFSFM:
1698 case MTBSF:
1699 case MTBSFM:
1700 if (!mt_count)
1701 return 0;
1702 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1703 default:
1704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001708 case MTWEOF:
1709 if (tape->write_prot)
1710 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001711 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001712 for (i = 0; i < mt_count; i++) {
1713 retval = idetape_write_filemark(drive);
1714 if (retval)
1715 return retval;
1716 }
1717 return 0;
1718 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001719 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001720 if (idetape_rewind_tape(drive))
1721 return -EIO;
1722 return 0;
1723 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001724 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001725 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001726 case MTUNLOAD:
1727 case MTOFFL:
1728 /*
1729 * If door is locked, attempt to unlock before
1730 * attempting to eject.
1731 */
1732 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001733 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001734 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001735 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001736 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001737 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001738 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001739 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001740 return retval;
1741 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001742 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001743 return idetape_flush_tape_buffers(drive);
1744 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001745 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001746 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001747 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001748 case MTEOM:
1749 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001750 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001751 case MTERASE:
1752 (void)idetape_rewind_tape(drive);
1753 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001754 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001755 case MTSETBLK:
1756 if (mt_count) {
1757 if (mt_count < tape->blk_size ||
1758 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001760 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001761 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001762 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001763 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001764 return 0;
1765 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001766 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001767 return idetape_position_tape(drive,
1768 mt_count * tape->user_bs_factor, tape->partition, 0);
1769 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001770 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001771 return idetape_position_tape(drive, 0, mt_count, 0);
1772 case MTFSR:
1773 case MTBSR:
1774 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001775 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001776 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001778 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1779 return 0;
1780 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001781 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001782 if (retval)
1783 return retval;
1784 tape->door_locked = DOOR_UNLOCKED;
1785 return 0;
1786 default:
1787 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1788 mt_op);
1789 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791}
1792
1793/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001794 * Our character device ioctls. General mtio.h magnetic io commands are
1795 * supported here, and not in the corresponding block interface. Our own
1796 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001798static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1799 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001801 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 ide_drive_t *drive = tape->drive;
1803 struct mtop mtop;
1804 struct mtget mtget;
1805 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001806 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 void __user *argp = (void __user *)arg;
1808
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001809 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Borislav Petkov54abf372008-02-06 02:57:52 +01001811 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001812 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 idetape_flush_tape_buffers(drive);
1814 }
1815 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001816 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001817 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001818 position = idetape_read_position(drive);
1819 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EIO;
1821 }
1822 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001823 case MTIOCTOP:
1824 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1825 return -EFAULT;
1826 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1827 case MTIOCGET:
1828 memset(&mtget, 0, sizeof(struct mtget));
1829 mtget.mt_type = MT_ISSCSI2;
1830 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1831 mtget.mt_dsreg =
1832 ((tape->blk_size * tape->user_bs_factor)
1833 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001834
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001835 if (tape->drv_write_prot)
1836 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1837
1838 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1839 return -EFAULT;
1840 return 0;
1841 case MTIOCPOS:
1842 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1843 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1844 return -EFAULT;
1845 return 0;
1846 default:
1847 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001848 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001849 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851}
1852
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001853/*
1854 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1855 * block size with the reported value.
1856 */
1857static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1858{
1859 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001860 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001861
1862 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001863 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001864 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001865 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001866 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1867 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001868 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001869 }
1870 return;
1871 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001872 tape->blk_size = (pc.buf[4 + 5] << 16) +
1873 (pc.buf[4 + 6] << 8) +
1874 pc.buf[4 + 7];
1875 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001878static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1881 ide_drive_t *drive;
1882 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int retval;
1884
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001885 if (i >= MAX_HWIFS * MAX_DRIVES)
1886 return -ENXIO;
1887
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001888 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001889 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001890 if (!tape) {
1891 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001892 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001893 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001894
1895 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * We really want to do nonseekable_open(inode, filp); here, but some
1899 * versions of tar incorrectly call lseek on tapes and bail out if that
1900 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1901 */
1902 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 drive = tape->drive;
1905
1906 filp->private_data = tape;
1907
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001908 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 retval = -EBUSY;
1910 goto out_put_tape;
1911 }
1912
1913 retval = idetape_wait_ready(drive, 60 * HZ);
1914 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001915 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1917 goto out_put_tape;
1918 }
1919
1920 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001921 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 (void)idetape_rewind_tape(drive);
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001925 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 /* Set write protect flag if device is opened as read-only. */
1928 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1929 tape->write_prot = 1;
1930 else
1931 tape->write_prot = tape->drv_write_prot;
1932
1933 /* Make sure drive isn't write protected if user wants to write. */
1934 if (tape->write_prot) {
1935 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1936 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001937 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 retval = -EROFS;
1939 goto out_put_tape;
1940 }
1941 }
1942
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001943 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001944 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001945 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001946 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1947 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001950 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 0;
1952
1953out_put_tape:
1954 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001955 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return retval;
1957}
1958
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001959static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 idetape_tape_t *tape = drive->driver_data;
1962
Borislav Petkovd9df9372008-04-27 15:38:34 +02001963 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001964 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
1965 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001966 idetape_pad_zeros(drive, tape->blk_size *
1967 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001968 ide_tape_kfree_buffer(tape);
1969 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 idetape_write_filemark(drive);
1972 idetape_flush_tape_buffers(drive);
1973 idetape_flush_tape_buffers(drive);
1974}
1975
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001976static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001978 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 unsigned int minor = iminor(inode);
1981
1982 lock_kernel();
1983 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001984
1985 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Borislav Petkov54abf372008-02-06 02:57:52 +01001987 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001989 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001991 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001993
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001994 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01001996 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001998 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001999 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002002 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 ide_tape_put(tape);
2004 unlock_kernel();
2005 return 0;
2006}
2007
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002008static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002011 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002012 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002015 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002016 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2017 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return;
2019 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002020 memcpy(vendor_id, &pc.buf[8], 8);
2021 memcpy(product_id, &pc.buf[16], 16);
2022 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002023
Borislav Petkov801bd322008-09-27 19:32:17 +02002024 ide_fixstring(vendor_id, 8, 0);
2025 ide_fixstring(product_id, 16, 0);
2026 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002027
Borislav Petkov801bd322008-09-27 19:32:17 +02002028 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002029 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002033 * Ask the tape about its various parameters. In particular, we will adjust our
2034 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002036static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002039 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002040 u8 *caps;
2041 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002044 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002045 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2046 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002047 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002048 put_unaligned(52, (u16 *)&tape->caps[12]);
2049 put_unaligned(540, (u16 *)&tape->caps[14]);
2050 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return;
2052 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002053 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Borislav Petkovb6422012008-02-02 19:56:49 +01002055 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002056 speed = be16_to_cpup((__be16 *)&caps[14]);
2057 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002059 *(u16 *)&caps[8] = max_speed;
2060 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2061 *(u16 *)&caps[14] = speed;
2062 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002063
2064 if (!speed) {
2065 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2066 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002067 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002069 if (!max_speed) {
2070 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2071 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002072 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074
Borislav Petkovb6422012008-02-02 19:56:49 +01002075 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002076
2077 /* device lacks locking support according to capabilities page */
2078 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02002079 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002080
Borislav Petkovb6422012008-02-02 19:56:49 +01002081 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002082 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002083 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002084 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002087#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002088#define ide_tape_devset_get(name, field) \
2089static int get_##name(ide_drive_t *drive) \
2090{ \
2091 idetape_tape_t *tape = drive->driver_data; \
2092 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002094
2095#define ide_tape_devset_set(name, field) \
2096static int set_##name(ide_drive_t *drive, int arg) \
2097{ \
2098 idetape_tape_t *tape = drive->driver_data; \
2099 tape->field = arg; \
2100 return 0; \
2101}
2102
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002103#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002104ide_tape_devset_get(_name, _field) \
2105ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002106IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002107
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002108#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002109ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002110IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002111
2112static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2113static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2114static int divf_buffer(ide_drive_t *drive) { return 2; }
2115static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2116
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002117ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002118
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002119ide_tape_devset_rw_field(debug_mask, debug_mask);
2120ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002121
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002122ide_tape_devset_r_field(avg_speed, avg_speed);
2123ide_tape_devset_r_field(speed, caps[14]);
2124ide_tape_devset_r_field(buffer, caps[16]);
2125ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002126
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002127static const struct ide_proc_devset idetape_settings[] = {
2128 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2129 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2130 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2131 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2132 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2133 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2134 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2135 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01002136 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002137};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002141 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002143 * 1. Initialize our various state variables.
2144 * 2. Ask the tape for its capabilities.
2145 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2146 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002148 * Note that at this point ide.c already assigned us an irq, so that we can
2149 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002151static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Borislav Petkov83042b22008-04-27 15:38:27 +02002153 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002155 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002156 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02002158 drive->pc_callback = ide_tape_callback;
2159 drive->pc_update_buffers = idetape_update_buffers;
2160 drive->pc_io_buffers = ide_tape_io_buffers;
Borislav Petkov776bb022008-07-23 19:55:59 +02002161
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002162 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2163
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002164 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2165 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2166 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002167 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002171 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002172 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 tape->minor = minor;
2175 tape->name[0] = 'h';
2176 tape->name[1] = 't';
2177 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002178 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 idetape_get_inquiry_results(drive);
2181 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002182 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002184 tape->buffer_size = *ctl * tape->blk_size;
2185 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002187 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002188 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002190 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002191 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002192 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002193 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002194 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
Borislav Petkov83042b22008-04-27 15:38:27 +02002197 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002198 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Borislav Petkovf73850a2008-04-27 15:38:33 +02002200 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002203 * Ensure that the number we got makes sense; limit it within
2204 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002206 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2207 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002209 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002210 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002211 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2212 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002213 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002214 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002216 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Russell King4031bbe2006-01-06 11:41:00 +00002219static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002223 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002224 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 ide_unregister_region(tape->disk);
2226
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002227 mutex_lock(&idetape_ref_mutex);
2228 put_device(&tape->dev);
2229 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002232static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002234 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 ide_drive_t *drive = tape->drive;
2236 struct gendisk *g = tape->disk;
2237
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002238 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002239
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002240 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002242 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002243 device_destroy(idetape_sysfs_class,
2244 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 idetape_devs[tape->minor] = NULL;
2246 g->private_data = NULL;
2247 put_disk(g);
2248 kfree(tape);
2249}
2250
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002251#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252static int proc_idetape_read_name
2253 (char *page, char **start, off_t off, int count, int *eof, void *data)
2254{
2255 ide_drive_t *drive = (ide_drive_t *) data;
2256 idetape_tape_t *tape = drive->driver_data;
2257 char *out = page;
2258 int len;
2259
2260 len = sprintf(out, "%s\n", tape->name);
2261 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2262}
2263
2264static ide_proc_entry_t idetape_proc[] = {
2265 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2266 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2267 { NULL, 0, NULL, NULL }
2268};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002269
2270static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2271{
2272 return idetape_proc;
2273}
2274
2275static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2276{
2277 return idetape_settings;
2278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279#endif
2280
Russell King4031bbe2006-01-06 11:41:00 +00002281static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01002283static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002284 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002285 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002286 .name = "ide-tape",
2287 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002288 },
Russell King4031bbe2006-01-06 11:41:00 +00002289 .probe = ide_tape_probe,
2290 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002293#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002294 .proc_entries = ide_tape_proc_entries,
2295 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297};
2298
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002299/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002300static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 .owner = THIS_MODULE,
2302 .read = idetape_chrdev_read,
2303 .write = idetape_chrdev_write,
2304 .ioctl = idetape_chrdev_ioctl,
2305 .open = idetape_chrdev_open,
2306 .release = idetape_chrdev_release,
2307};
2308
Al Viroa4600f82008-03-02 10:27:58 -05002309static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Al Viroa4600f82008-03-02 10:27:58 -05002311 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002313 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return -ENXIO;
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return 0;
2317}
2318
Al Viroa4600f82008-03-02 10:27:58 -05002319static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002321 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return 0;
2325}
2326
Al Viroa4600f82008-03-02 10:27:58 -05002327static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 unsigned int cmd, unsigned long arg)
2329{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002330 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04002332 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (err == -EINVAL)
2334 err = idetape_blkdev_ioctl(drive, cmd, arg);
2335 return err;
2336}
2337
2338static struct block_device_operations idetape_block_ops = {
2339 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05002340 .open = idetape_open,
2341 .release = idetape_release,
2342 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343};
2344
Russell King4031bbe2006-01-06 11:41:00 +00002345static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 idetape_tape_t *tape;
2348 struct gendisk *g;
2349 int minor;
2350
2351 if (!strstr("ide-tape", drive->driver_req))
2352 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (drive->media != ide_tape)
2355 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002356
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002357 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2358 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002359 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2360 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto failed;
2362 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002363 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002365 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2366 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto failed;
2368 }
2369
2370 g = alloc_disk(1 << PARTN_BITS);
2371 if (!g)
2372 goto out_free_tape;
2373
2374 ide_init_disk(g, drive);
2375
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002376 tape->dev.parent = &drive->gendev;
2377 tape->dev.release = ide_tape_release;
2378 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2379
2380 if (device_register(&tape->dev))
2381 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 tape->drive = drive;
2384 tape->driver = &idetape_driver;
2385 tape->disk = g;
2386
2387 g->private_data = &tape->driver;
2388
2389 drive->driver_data = tape;
2390
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002391 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 for (minor = 0; idetape_devs[minor]; minor++)
2393 ;
2394 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002395 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 idetape_setup(drive, tape, minor);
2398
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002399 device_create(idetape_sysfs_class, &drive->gendev,
2400 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2401 device_create(idetape_sysfs_class, &drive->gendev,
2402 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2403 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 g->fops = &idetape_block_ops;
2406 ide_register_region(g);
2407
2408 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002409
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002410out_free_disk:
2411 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412out_free_tape:
2413 kfree(tape);
2414failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002415 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002418static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002420 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002421 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 unregister_chrdev(IDETAPE_MAJOR, "ht");
2423}
2424
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002425static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Will Dysond5dee802005-09-16 02:55:07 -07002427 int error = 1;
2428 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2429 if (IS_ERR(idetape_sysfs_class)) {
2430 idetape_sysfs_class = NULL;
2431 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2432 error = -EBUSY;
2433 goto out;
2434 }
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002437 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2438 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002439 error = -EBUSY;
2440 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Will Dysond5dee802005-09-16 02:55:07 -07002442
2443 error = driver_register(&idetape_driver.gen_driver);
2444 if (error)
2445 goto out_free_driver;
2446
2447 return 0;
2448
2449out_free_driver:
2450 driver_unregister(&idetape_driver.gen_driver);
2451out_free_class:
2452 class_destroy(idetape_sysfs_class);
2453out:
2454 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Kay Sievers263756e2005-12-12 18:03:44 +01002457MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458module_init(idetape_init);
2459module_exit(idetape_exit);
2460MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002461MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2462MODULE_LICENSE("GPL");