blob: 2b9a13671c5fc4d75f75a5f2e73a0cca2866a1c4 [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
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200300static int 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) {
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200307 if (bh == NULL)
308 break;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100309 count = min(
310 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
311 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200312 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100313 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 bcount -= count;
315 atomic_add(count, &bh->b_count);
316 if (atomic_read(&bh->b_count) == bh->b_size) {
317 bh = bh->b_reqnext;
318 if (bh)
319 atomic_set(&bh->b_count, 0);
320 }
321 }
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 pc->bh = bh;
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200324
325 return bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200328static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100329 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct idetape_bh *bh = pc->bh;
332 int count;
333
334 while (bcount) {
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200335 if (bh == NULL)
336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200338 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 bcount -= count;
340 pc->b_data += count;
341 pc->b_count -= count;
342 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100343 bh = bh->b_reqnext;
344 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (bh) {
346 pc->b_data = bh->b_data;
347 pc->b_count = atomic_read(&bh->b_count);
348 }
349 }
350 }
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200351
352 return bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200355static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct idetape_bh *bh = pc->bh;
358 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200359 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Borislav Petkov346331f2008-04-18 00:46:26 +0200361 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return;
363 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100365 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
366 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
370 atomic_set(&bh->b_count, count);
371 if (atomic_read(&bh->b_count) == bh->b_size)
372 bh = bh->b_reqnext;
373 bcount -= count;
374 }
375 pc->bh = bh;
376}
377
378/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100379 * called on each failed packet command retry to analyze the request sense. We
380 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100382static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100385 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Borislav Petkov1b5db432008-02-02 19:56:48 +0100387 tape->sense_key = sense[2] & 0xF;
388 tape->asc = sense[12];
389 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100390
391 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
392 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Borislav Petkovd236d742008-04-18 00:46:27 +0200394 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200395 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200396 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100397 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200398 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200399 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 /*
403 * If error was the result of a zero-length read or write command,
404 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
405 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
406 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100407 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100408 /* length == 0 */
409 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
410 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* don't report an error, everything's ok */
412 pc->error = 0;
413 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200414 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100417 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100418 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200419 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100421 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100422 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
423 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100424 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200425 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100428 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100429 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100430 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200431 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200433 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200434 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
436 }
437}
438
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200439/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200440static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200442 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200444 while (bh) {
445 u32 size = bh->b_size;
446
447 while (size) {
448 unsigned int order = fls(size >> PAGE_SHIFT)-1;
449
450 if (bh->b_data)
451 free_pages((unsigned long)bh->b_data, order);
452
453 size &= (order-1);
454 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 prev_bh = bh;
457 bh = bh->b_reqnext;
458 kfree(prev_bh);
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200462static void ide_tape_handle_dsc(ide_drive_t *);
463
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100464static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200467 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100468 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200469 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100470 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100472 debug_log(DBG_PROCS, "Enter %s\n", __func__);
473
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200474 if (dsc)
475 ide_tape_handle_dsc(drive);
476
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100477 if (drive->failed_pc == pc)
478 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200479
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200480 if (pc->c[0] == REQUEST_SENSE) {
481 if (uptodate)
482 idetape_analyze_error(drive, pc->buf);
483 else
484 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
485 "itself - Aborting request!\n");
486 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200487 int blocks = pc->xferred / tape->blk_size;
488
489 tape->avg_size += blocks * tape->blk_size;
490
491 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
492 tape->avg_speed = tape->avg_size * HZ /
493 (jiffies - tape->avg_time) / 1024;
494 tape->avg_size = 0;
495 tape->avg_time = jiffies;
496 }
497
498 tape->first_frame += blocks;
499 rq->current_nr_sectors -= blocks;
500
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100501 if (pc->error) {
502 uptodate = 0;
503 err = pc->error;
504 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200505 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200506 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200507
508 debug_log(DBG_SENSE, "BOP - %s\n",
509 (readpos[0] & 0x80) ? "Yes" : "No");
510 debug_log(DBG_SENSE, "EOP - %s\n",
511 (readpos[0] & 0x40) ? "Yes" : "No");
512
513 if (readpos[0] & 0x4) {
514 printk(KERN_INFO "ide-tape: Block location is unknown"
515 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200516 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200517 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100518 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200519 } else {
520 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200521 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200522
523 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200524 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200525 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200528
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100529 rq->errors = err;
530
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100531 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100535 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100536 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100538static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 idetape_tape_t *tape = drive->driver_data;
541
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100542 debug_log(DBG_PROCS, "Enter %s\n", __func__);
543
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100544 tape->postponed_rq = drive->hwif->rq;
545
Borislav Petkov54bb2072008-02-06 02:57:52 +0100546 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200549static void ide_tape_handle_dsc(ide_drive_t *drive)
550{
551 idetape_tape_t *tape = drive->driver_data;
552
553 /* Media access command */
554 tape->dsc_polling_start = jiffies;
555 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
556 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
557 /* Allow ide.c to handle other requests */
558 idetape_postpone_request(drive);
559}
560
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200561static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200562 unsigned int bcount, int write)
563{
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200564 unsigned int bleft;
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200565
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200566 if (write)
567 bleft = idetape_output_buffers(drive, pc, bcount);
568 else
569 bleft = idetape_input_buffers(drive, pc, bcount);
570
571 return bcount - bleft;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200572}
Borislav Petkova1efc852008-02-06 02:57:52 +0100573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100575 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200577 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100578 * until we finish handling it. Each packet command is associated with a
579 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100581 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100583 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200584 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200586 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100587 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100589 * 3. ATAPI Tape media access commands have immediate status with a delayed
590 * process. In case of a successful initiation of a media access packet command,
591 * the DSC bit will be set when the actual execution of the command is finished.
592 * Since the tape drive will not issue an interrupt, we have to poll for this
593 * event. In this case, we define the request as "low priority request" by
594 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
595 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100597 * ide.c will then give higher priority to requests which originate from the
598 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100600 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100602 * 5. In case an error was found, we queue a request sense packet command in
603 * front of the request queue and retry the operation up to
604 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100606 * 6. In case no error was found, or we decided to give up and not to retry
607 * again, the callback function will be called and then we will handle the next
608 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100611static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
612 struct ide_cmd *cmd,
613 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100617 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
618 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200621 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200624 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100626 * We will "abort" retrying a packet command in case legitimate
627 * error code was received (crossing a filemark, or end of the
628 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200630 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100631 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 tape->sense_key == 2 && tape->asc == 4 &&
633 (tape->ascq == 1 || tape->ascq == 8))) {
634 printk(KERN_ERR "ide-tape: %s: I/O error, "
635 "pc = %2x, key = %2x, "
636 "asc = %2x, ascq = %2x\n",
637 tape->name, pc->c[0],
638 tape->sense_key, tape->asc,
639 tape->ascq);
640 }
641 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100642 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100644 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200645 drive->pc_callback(drive, 0);
Tejun Heoea7066a2009-04-19 08:46:02 +0900646 ide_complete_rq(drive, -EIO, blk_rq_bytes(drive->hwif->rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200647 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100649 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100653 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100656/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200657static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200659 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100660 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100662 /* DBD = 1 - Don't return block descriptors */
663 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 pc->c[2] = page_code;
665 /*
666 * Changed pc->c[3] to 0 (255 will at best return unused info).
667 *
668 * For SCSI this byte is defined as subpage instead of high byte
669 * of length and some IDE drives seem to interpret it this way
670 * and return an error when 255 is used.
671 */
672 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100673 /* We will just discard data in that case */
674 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200676 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200678 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200680 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100683static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200685 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200687 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100688 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200690 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100691
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200692 if (stat & ATA_DSC) {
693 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100695 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 printk(KERN_ERR "ide-tape: %s: I/O error, ",
697 tape->name);
698 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900699 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200700 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100704 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100705 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200707 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return ide_stopped;
709}
710
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200711static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200712 struct ide_atapi_pc *pc, struct request *rq,
713 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Borislav Petkov0014c752008-07-23 19:56:00 +0200715 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
716 unsigned int length = rq->current_nr_sectors;
717
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200718 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100719 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200722 pc->buf = NULL;
723 pc->buf_size = length * tape->blk_size;
724 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200725 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200726 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200728 if (opcode == READ_6) {
729 pc->c[0] = READ_6;
730 atomic_set(&bh->b_count, 0);
731 } else if (opcode == WRITE_6) {
732 pc->c[0] = WRITE_6;
733 pc->flags |= PC_FLAG_WRITING;
734 pc->b_data = bh->b_data;
735 pc->b_count = atomic_read(&bh->b_count);
736 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200737
738 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static ide_startstop_t idetape_do_request(ide_drive_t *drive,
742 struct request *rq, sector_t block)
743{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200744 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200746 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100748 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100749 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Mark de Wever730616b2008-10-10 22:39:17 +0200751 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
752 " current_nr_sectors: %u\n",
753 (unsigned long long)rq->sector, rq->nr_sectors,
754 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900756 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100757 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200759 "request queue (%d)\n", drive->name, rq->cmd_type);
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100760 if (blk_fs_request(rq) == 0 && rq->errors == 0)
761 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100762 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return ide_stopped;
764 }
765
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100766 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100767 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
768 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200769 goto out;
770 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (postponed_rq != NULL)
773 if (rq != postponed_rq) {
774 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
775 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100776 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100777 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100778 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ide_stopped;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 tape->postponed_rq = NULL;
783
784 /*
785 * If the tape is still busy, postpone our request and service
786 * the other device meanwhile.
787 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200788 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200790 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
791 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200792 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200794 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200795 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200796 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200799 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200800 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (postponed_rq == NULL) {
802 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100803 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
805 } else if (time_after(jiffies, tape->dsc_timeout)) {
806 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
807 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200808 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 idetape_media_access_finished(drive);
810 return ide_stopped;
811 } else {
812 return ide_do_reset(drive);
813 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100814 } else if (time_after(jiffies,
815 tape->dsc_polling_start +
816 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100817 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 idetape_postpone_request(drive);
819 return ide_stopped;
820 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200821 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200822 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200823 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto out;
825 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200826 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200827 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200828 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 goto out;
830 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200831 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900832 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200833 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
834 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto out;
836 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200837 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 idetape_media_access_finished(drive);
839 return ide_stopped;
840 }
841 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200842
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200843out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900844 /* prepare sense request for this command */
845 ide_prep_sense(drive, rq);
846
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100847 memset(&cmd, 0, sizeof(cmd));
848
849 if (rq_data_dir(rq))
850 cmd.tf_flags |= IDE_TFLAG_WRITE;
851
852 cmd.rq = rq;
853
Tejun Heo5c4be572009-04-19 07:00:42 +0900854 ide_init_sg_cmd(&cmd, pc->req_xfer);
855 ide_map_sg(drive, &cmd);
856
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100857 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
Borislav Petkov41aa1702008-04-27 15:38:32 +0200861 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +0200862 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100863 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
Borislav Petkov41aa1702008-04-27 15:38:32 +0200865 * It returns a pointer to the newly allocated buffer, or NULL in case of
866 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200868static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
869 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200871 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +0200872 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200873 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 char *b_data = NULL;
875
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200876 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
877 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (bh == NULL)
879 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200880
881 order = fls(pages) - 1;
882 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100883 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200885 b_allocd = (1 << order) * PAGE_SIZE;
886 pages &= (order-1);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200889 memset(bh->b_data, 0, b_allocd);
890 bh->b_reqnext = NULL;
891 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 atomic_set(&bh->b_count, full ? bh->b_size : 0);
893
Borislav Petkov41aa1702008-04-27 15:38:32 +0200894 while (pages) {
895 order = fls(pages) - 1;
896 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100897 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200899 b_allocd = (1 << order) * PAGE_SIZE;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200902 memset(b_data, 0, b_allocd);
903
904 /* newly allocated page frames below buffer header or ...*/
905 if (bh->b_data == b_data + b_allocd) {
906 bh->b_size += b_allocd;
907 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200909 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 continue;
911 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200912 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200914 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200916 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 continue;
918 }
919 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100920 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
921 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200922 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto abort;
924 }
925 bh->b_reqnext = NULL;
926 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200927 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 atomic_set(&bh->b_count, full ? bh->b_size : 0);
929 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200930
931 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 bh->b_size -= tape->excess_bh_size;
935 if (full)
936 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200937 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200939 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return NULL;
941}
942
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100943static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +0200944 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct idetape_bh *bh = tape->bh;
947 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700948 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100952 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
953 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700954 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100956 count = min((unsigned int)
957 (bh->b_size - atomic_read(&bh->b_count)),
958 (unsigned int)n);
959 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
960 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700961 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 n -= count;
963 atomic_add(count, &bh->b_count);
964 buf += count;
965 if (atomic_read(&bh->b_count) == bh->b_size) {
966 bh = bh->b_reqnext;
967 if (bh)
968 atomic_set(&bh->b_count, 0);
969 }
970 }
971 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100975static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +0200976 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct idetape_bh *bh = tape->bh;
979 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700980 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100984 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
985 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700986 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700989 if (copy_to_user(buf, tape->b_data, count))
990 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 n -= count;
992 tape->b_data += count;
993 tape->b_count -= count;
994 buf += count;
995 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100996 bh = bh->b_reqnext;
997 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (bh) {
999 tape->b_data = bh->b_data;
1000 tape->b_count = atomic_read(&bh->b_count);
1001 }
1002 }
1003 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001004 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001007static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001009 struct idetape_bh *bh = tape->merge_bh;
1010 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001011
Borislav Petkov54abf372008-02-06 02:57:52 +01001012 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 atomic_set(&bh->b_count, 0);
1014 else {
1015 tape->b_data = bh->b_data;
1016 tape->b_count = atomic_read(&bh->b_count);
1017 }
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001021 * Write a filemark if write_filemark=1. Flush the device buffers without
1022 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001024static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001025 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001027 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001028 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001030 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1034{
1035 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001036 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int load_attempted = 0;
1038
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001039 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001040 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 timeout += jiffies;
1042 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +02001043 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
1045 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001046 || (tape->asc == 0x3A)) {
1047 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (load_attempted)
1049 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001050 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 load_attempted = 1;
1052 /* not about to be ready */
1053 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1054 (tape->ascq == 1 || tape->ascq == 8)))
1055 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001056 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 return -EIO;
1059}
1060
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001061static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001063 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001064 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 int rc;
1066
1067 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001068 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001069 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return rc;
1071 idetape_wait_ready(drive, 60 * 5 * HZ);
1072 return 0;
1073}
1074
Borislav Petkovd236d742008-04-18 00:46:27 +02001075static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001077 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001078 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001079 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001082static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001085 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int position;
1087
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001088 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001091 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001093 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return position;
1095}
1096
Borislav Petkovd236d742008-04-18 00:46:27 +02001097static void idetape_create_locate_cmd(ide_drive_t *drive,
1098 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001099 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001101 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001102 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001104 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001106 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001109static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Borislav Petkov54abf372008-02-06 02:57:52 +01001113 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001114 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001116 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001117 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001118 if (tape->merge_bh != NULL) {
1119 ide_tape_kfree_buffer(tape);
1120 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
Borislav Petkov54abf372008-02-06 02:57:52 +01001123 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001127 * Position the tape to the requested block using the LOCATE packet command.
1128 * A READ POSITION command is then issued to check where we are positioned. Like
1129 * all higher level operations, we queue the commands at the tail of the request
1130 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001132static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1133 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001136 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001138 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Borislav Petkov54abf372008-02-06 02:57:52 +01001140 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001141 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 idetape_wait_ready(drive, 60 * 5 * HZ);
1143 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001144 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (retval)
1146 return (retval);
1147
1148 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001149 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001152static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001153 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int seek, position;
1157
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001158 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (restore_position) {
1160 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001161 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001163 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001164 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return;
1166 }
1167 }
1168}
1169
1170/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001171 * Generate a read/write request for the block device interface and wait for it
1172 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001174static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1175 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001178 struct request *rq;
1179 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001181 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1182
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001183 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1184 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001185 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001186 rq->rq_disk = tape->disk;
1187 rq->special = (void *)bh;
1188 rq->sector = tape->first_frame;
1189 rq->nr_sectors = blocks;
1190 rq->current_nr_sectors = blocks;
1191 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1192
1193 errors = rq->errors;
1194 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1195 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1198 return 0;
1199
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001200 if (tape->merge_bh)
1201 idetape_init_merge_buffer(tape);
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +01001202 if (errors == IDE_DRV_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001204 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Borislav Petkovd236d742008-04-18 00:46:27 +02001207static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001209 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001210 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001211 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001212 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Borislav Petkovd236d742008-04-18 00:46:27 +02001215static void idetape_create_rewind_cmd(ide_drive_t *drive,
1216 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001218 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001219 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001220 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Borislav Petkovd236d742008-04-18 00:46:27 +02001223static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001225 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001226 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001228 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Borislav Petkovd236d742008-04-18 00:46:27 +02001231static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001233 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001234 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001235 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001237 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Borislav Petkov97c566c2008-04-27 15:38:25 +02001240/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001241static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001245 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001247 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001248 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Borislav Petkovd9df9372008-04-27 15:38:34 +02001251static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 idetape_tape_t *tape = drive->driver_data;
1254 int blocks, min;
1255 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001256
Borislav Petkov54abf372008-02-06 02:57:52 +01001257 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001258 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001259 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return;
1261 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001262 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001264 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001266 if (tape->merge_bh_size) {
1267 blocks = tape->merge_bh_size / tape->blk_size;
1268 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 unsigned int i;
1270
1271 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001272 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001273 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 bh = tape->bh->b_reqnext;
1275 while (bh) {
1276 atomic_set(&bh->b_count, 0);
1277 bh = bh->b_reqnext;
1278 }
1279 bh = tape->bh;
1280 while (i) {
1281 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001282 printk(KERN_INFO "ide-tape: bug,"
1283 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
1285 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001286 min = min(i, (unsigned int)(bh->b_size -
1287 atomic_read(&bh->b_count)));
1288 memset(bh->b_data + atomic_read(&bh->b_count),
1289 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 atomic_add(min, &bh->b_count);
1291 i -= min;
1292 bh = bh->b_reqnext;
1293 }
1294 }
1295 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001296 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001298 if (tape->merge_bh != NULL) {
1299 ide_tape_kfree_buffer(tape);
1300 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001302 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Borislav Petkov83042b22008-04-27 15:38:27 +02001305static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001311 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1312 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001313 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 idetape_flush_tape_buffers(drive);
1315 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001316 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001317 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001318 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001319 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001321 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1322 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001324 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001327 * Issue a read 0 command to ensure that DSC handshake is
1328 * switched from completion mode to buffer available mode.
1329 * No point in issuing this if DSC overlap isn't supported, some
1330 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001332 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001333 bytes_read = idetape_queue_rw_tail(drive,
1334 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001335 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001337 ide_tape_kfree_buffer(tape);
1338 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001339 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return bytes_read;
1341 }
1342 }
1343 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return 0;
1346}
1347
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001348/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001353 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001355 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001356 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
1358
Borislav Petkov83042b22008-04-27 15:38:27 +02001359 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001361 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001362 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001365static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 idetape_tape_t *tape = drive->driver_data;
1368 struct idetape_bh *bh;
1369 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 while (bcount) {
1372 unsigned int count;
1373
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001374 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001375 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001377 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001379 atomic_set(&bh->b_count,
1380 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1382 count -= atomic_read(&bh->b_count);
1383 bh = bh->b_reqnext;
1384 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001385 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001386 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001391 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1392 * currently support only one partition.
1393 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001394static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001396 struct ide_tape_obj *tape = drive->driver_data;
1397 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001399 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001400
1401 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001404 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (retval)
1406 return retval;
1407
1408 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001409 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (retval)
1411 return retval;
1412 return 0;
1413}
1414
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001415/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001416static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1417 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 void __user *argp = (void __user *)arg;
1421
Borislav Petkovd59823f2008-02-02 19:56:51 +01001422 struct idetape_config {
1423 int dsc_rw_frequency;
1424 int dsc_media_access_frequency;
1425 int nr_stages;
1426 } config;
1427
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001428 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001431 case 0x0340:
1432 if (copy_from_user(&config, argp, sizeof(config)))
1433 return -EFAULT;
1434 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001435 break;
1436 case 0x0350:
1437 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001438 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001439 if (copy_to_user(argp, &config, sizeof(config)))
1440 return -EFAULT;
1441 break;
1442 default:
1443 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 return 0;
1446}
1447
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001448static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1449 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001452 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001453 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001454 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001455 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (mt_count == 0)
1458 return 0;
1459 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001460 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001462 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
Borislav Petkov54abf372008-02-06 02:57:52 +01001465 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001466 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001467 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001469 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001473 case MTFSF:
1474 case MTBSF:
1475 idetape_create_space_cmd(&pc, mt_count - count,
1476 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001477 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001478 case MTFSFM:
1479 case MTBSFM:
1480 if (!sprev)
1481 return -EIO;
1482 retval = idetape_space_over_filemarks(drive, MTFSF,
1483 mt_count - count);
1484 if (retval)
1485 return retval;
1486 count = (MTBSFM == mt_op ? 1 : -1);
1487 return idetape_space_over_filemarks(drive, MTFSF, count);
1488 default:
1489 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1490 mt_op);
1491 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001496 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001498 * The tape is optimized to maximize throughput when it is transferring an
1499 * integral number of the "continuous transfer limit", which is a parameter of
1500 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001502 * As of version 1.3 of the driver, the character device provides an abstract
1503 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1504 * same backup/restore procedure is supported. The driver will internally
1505 * convert the requests to the recommended transfer unit, so that an unmatch
1506 * between the user's block size to the recommended size will only result in a
1507 * (slightly) increased driver overhead, but will no longer hit performance.
1508 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001510static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1511 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001513 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001515 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001516 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001517 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001519 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Borislav Petkov54abf372008-02-06 02:57:52 +01001521 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001522 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001523 if (count > tape->blk_size &&
1524 (count % tape->blk_size) == 0)
1525 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001527 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001528 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return rc;
1530 if (count == 0)
1531 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001532 if (tape->merge_bh_size) {
1533 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001534 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001535 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001536 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001538 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 count -= actually_read;
1540 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001541 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001542 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (bytes_read <= 0)
1544 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001545 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001546 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 buf += bytes_read;
1548 count -= bytes_read;
1549 actually_read += bytes_read;
1550 }
1551 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001552 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (bytes_read <= 0)
1554 goto finish;
1555 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001556 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001557 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001559 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001562 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001563 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 idetape_space_over_filemarks(drive, MTFSF, 1);
1566 return 0;
1567 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001568
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001569 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001572static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 size_t count, loff_t *ppos)
1574{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001575 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001577 ssize_t actually_written = 0;
1578 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001579 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* The drive is write protected. */
1582 if (tape->write_prot)
1583 return -EACCES;
1584
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001585 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001588 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1589 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001590 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001591 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001592 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001594 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001596 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1597 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001599 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001600 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001603 * Issue a write 0 command to ensure that DSC handshake is
1604 * switched from completion mode to buffer available mode. No
1605 * point in issuing this if DSC overlap isn't supported, some
1606 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001608 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001609 ssize_t retval = idetape_queue_rw_tail(drive,
1610 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001611 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001613 ide_tape_kfree_buffer(tape);
1614 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001615 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return retval;
1617 }
1618 }
1619 }
1620 if (count == 0)
1621 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001622 if (tape->merge_bh_size) {
1623 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001624 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001625 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001627 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001628 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001630 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001631 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001633 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 count -= actually_written;
1635
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001636 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001637 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001638 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001639 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (retval <= 0)
1641 return (retval);
1642 }
1643 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001644 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001645 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001646 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001647 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001648 buf += tape->buffer_size;
1649 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001650 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001651 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (retval <= 0)
1653 return (retval);
1654 }
1655 if (count) {
1656 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001657 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001658 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001659 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001661 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001664static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001666 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001667 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 /* Write a filemark */
1670 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001671 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1673 return -EIO;
1674 }
1675 return 0;
1676}
1677
1678/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001679 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1680 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001682 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1683 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001684 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001686 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001688 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1689 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001691static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001694 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001695 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001696 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001698 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1699 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001702 case MTFSF:
1703 case MTFSFM:
1704 case MTBSF:
1705 case MTBSFM:
1706 if (!mt_count)
1707 return 0;
1708 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1709 default:
1710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001714 case MTWEOF:
1715 if (tape->write_prot)
1716 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001717 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001718 for (i = 0; i < mt_count; i++) {
1719 retval = idetape_write_filemark(drive);
1720 if (retval)
1721 return retval;
1722 }
1723 return 0;
1724 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001725 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001726 if (idetape_rewind_tape(drive))
1727 return -EIO;
1728 return 0;
1729 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001730 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001731 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001732 case MTUNLOAD:
1733 case MTOFFL:
1734 /*
1735 * If door is locked, attempt to unlock before
1736 * attempting to eject.
1737 */
1738 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001739 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001740 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001741 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001742 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001743 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001744 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001745 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001746 return retval;
1747 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001748 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001749 return idetape_flush_tape_buffers(drive);
1750 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001751 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001752 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001753 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001754 case MTEOM:
1755 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001756 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001757 case MTERASE:
1758 (void)idetape_rewind_tape(drive);
1759 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001760 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001761 case MTSETBLK:
1762 if (mt_count) {
1763 if (mt_count < tape->blk_size ||
1764 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001766 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001767 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001768 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001769 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001770 return 0;
1771 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001772 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001773 return idetape_position_tape(drive,
1774 mt_count * tape->user_bs_factor, tape->partition, 0);
1775 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001776 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001777 return idetape_position_tape(drive, 0, mt_count, 0);
1778 case MTFSR:
1779 case MTBSR:
1780 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001781 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001782 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001784 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1785 return 0;
1786 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001787 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001788 if (retval)
1789 return retval;
1790 tape->door_locked = DOOR_UNLOCKED;
1791 return 0;
1792 default:
1793 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1794 mt_op);
1795 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797}
1798
1799/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001800 * Our character device ioctls. General mtio.h magnetic io commands are
1801 * supported here, and not in the corresponding block interface. Our own
1802 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001804static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1805 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001807 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 ide_drive_t *drive = tape->drive;
1809 struct mtop mtop;
1810 struct mtget mtget;
1811 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001812 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 void __user *argp = (void __user *)arg;
1814
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001815 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Borislav Petkov54abf372008-02-06 02:57:52 +01001817 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001818 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 idetape_flush_tape_buffers(drive);
1820 }
1821 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001822 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001823 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001824 position = idetape_read_position(drive);
1825 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return -EIO;
1827 }
1828 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001829 case MTIOCTOP:
1830 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1831 return -EFAULT;
1832 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1833 case MTIOCGET:
1834 memset(&mtget, 0, sizeof(struct mtget));
1835 mtget.mt_type = MT_ISSCSI2;
1836 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1837 mtget.mt_dsreg =
1838 ((tape->blk_size * tape->user_bs_factor)
1839 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001840
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001841 if (tape->drv_write_prot)
1842 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1843
1844 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1845 return -EFAULT;
1846 return 0;
1847 case MTIOCPOS:
1848 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1849 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1850 return -EFAULT;
1851 return 0;
1852 default:
1853 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001854 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001855 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857}
1858
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001859/*
1860 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1861 * block size with the reported value.
1862 */
1863static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1864{
1865 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001866 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001867
1868 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001869 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001870 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001871 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001872 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1873 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001874 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001875 }
1876 return;
1877 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001878 tape->blk_size = (pc.buf[4 + 5] << 16) +
1879 (pc.buf[4 + 6] << 8) +
1880 pc.buf[4 + 7];
1881 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001884static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1887 ide_drive_t *drive;
1888 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int retval;
1890
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001891 if (i >= MAX_HWIFS * MAX_DRIVES)
1892 return -ENXIO;
1893
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001894 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001895 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001896 if (!tape) {
1897 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001898 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001899 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001900
1901 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /*
1904 * We really want to do nonseekable_open(inode, filp); here, but some
1905 * versions of tar incorrectly call lseek on tapes and bail out if that
1906 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1907 */
1908 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 drive = tape->drive;
1911
1912 filp->private_data = tape;
1913
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001914 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 retval = -EBUSY;
1916 goto out_put_tape;
1917 }
1918
1919 retval = idetape_wait_ready(drive, 60 * HZ);
1920 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001921 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1923 goto out_put_tape;
1924 }
1925
1926 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001927 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 (void)idetape_rewind_tape(drive);
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001931 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* Set write protect flag if device is opened as read-only. */
1934 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1935 tape->write_prot = 1;
1936 else
1937 tape->write_prot = tape->drv_write_prot;
1938
1939 /* Make sure drive isn't write protected if user wants to write. */
1940 if (tape->write_prot) {
1941 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1942 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001943 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 retval = -EROFS;
1945 goto out_put_tape;
1946 }
1947 }
1948
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001949 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001950 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001951 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001952 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1953 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001956 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
1958
1959out_put_tape:
1960 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001961 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return retval;
1963}
1964
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001965static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 idetape_tape_t *tape = drive->driver_data;
1968
Borislav Petkovd9df9372008-04-27 15:38:34 +02001969 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001970 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
1971 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001972 idetape_pad_zeros(drive, tape->blk_size *
1973 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001974 ide_tape_kfree_buffer(tape);
1975 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977 idetape_write_filemark(drive);
1978 idetape_flush_tape_buffers(drive);
1979 idetape_flush_tape_buffers(drive);
1980}
1981
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001982static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001984 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned int minor = iminor(inode);
1987
1988 lock_kernel();
1989 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001990
1991 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Borislav Petkov54abf372008-02-06 02:57:52 +01001993 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001995 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001997 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001999
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002000 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002002 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002004 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002005 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002008 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 ide_tape_put(tape);
2010 unlock_kernel();
2011 return 0;
2012}
2013
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002014static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002017 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02002018 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02002019 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02002022 pc.buf = &pc_buf[0];
2023 pc.buf_size = sizeof(pc_buf);
2024
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002025 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002026 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2027 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return;
2029 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002030 memcpy(vendor_id, &pc.buf[8], 8);
2031 memcpy(product_id, &pc.buf[16], 16);
2032 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002033
Borislav Petkov801bd322008-09-27 19:32:17 +02002034 ide_fixstring(vendor_id, 8, 0);
2035 ide_fixstring(product_id, 16, 0);
2036 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002037
Borislav Petkov801bd322008-09-27 19:32:17 +02002038 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002039 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002043 * Ask the tape about its various parameters. In particular, we will adjust our
2044 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002046static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002049 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002050 u8 *caps;
2051 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002054 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002055 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2056 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002057 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002058 put_unaligned(52, (u16 *)&tape->caps[12]);
2059 put_unaligned(540, (u16 *)&tape->caps[14]);
2060 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return;
2062 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002063 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Borislav Petkovb6422012008-02-02 19:56:49 +01002065 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002066 speed = be16_to_cpup((__be16 *)&caps[14]);
2067 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002069 *(u16 *)&caps[8] = max_speed;
2070 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2071 *(u16 *)&caps[14] = speed;
2072 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002073
2074 if (!speed) {
2075 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2076 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002077 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002079 if (!max_speed) {
2080 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2081 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002082 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084
Borislav Petkovb6422012008-02-02 19:56:49 +01002085 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002086
2087 /* device lacks locking support according to capabilities page */
2088 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02002089 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002090
Borislav Petkovb6422012008-02-02 19:56:49 +01002091 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002092 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002093 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002094 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002097#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002098#define ide_tape_devset_get(name, field) \
2099static int get_##name(ide_drive_t *drive) \
2100{ \
2101 idetape_tape_t *tape = drive->driver_data; \
2102 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002104
2105#define ide_tape_devset_set(name, field) \
2106static int set_##name(ide_drive_t *drive, int arg) \
2107{ \
2108 idetape_tape_t *tape = drive->driver_data; \
2109 tape->field = arg; \
2110 return 0; \
2111}
2112
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002113#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002114ide_tape_devset_get(_name, _field) \
2115ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002116IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002117
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002118#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002119ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002120IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002121
2122static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2123static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2124static int divf_buffer(ide_drive_t *drive) { return 2; }
2125static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2126
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002127ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002128
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002129ide_tape_devset_rw_field(debug_mask, debug_mask);
2130ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002131
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002132ide_tape_devset_r_field(avg_speed, avg_speed);
2133ide_tape_devset_r_field(speed, caps[14]);
2134ide_tape_devset_r_field(buffer, caps[16]);
2135ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002136
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002137static const struct ide_proc_devset idetape_settings[] = {
2138 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2139 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2140 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2141 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2142 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2143 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2144 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2145 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01002146 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002147};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002151 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002153 * 1. Initialize our various state variables.
2154 * 2. Ask the tape for its capabilities.
2155 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2156 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002158 * Note that at this point ide.c already assigned us an irq, so that we can
2159 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002161static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Borislav Petkov83042b22008-04-27 15:38:27 +02002163 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002165 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002166 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02002168 drive->pc_callback = ide_tape_callback;
2169 drive->pc_update_buffers = idetape_update_buffers;
2170 drive->pc_io_buffers = ide_tape_io_buffers;
Borislav Petkov776bb022008-07-23 19:55:59 +02002171
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002172 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2173
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002174 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2175 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2176 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002177 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002181 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002182 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 tape->minor = minor;
2185 tape->name[0] = 'h';
2186 tape->name[1] = 't';
2187 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002188 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 idetape_get_inquiry_results(drive);
2191 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002192 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002194 tape->buffer_size = *ctl * tape->blk_size;
2195 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002197 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002198 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002200 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002201 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002202 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002203 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002204 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206
Borislav Petkov83042b22008-04-27 15:38:27 +02002207 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002208 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Borislav Petkovf73850a2008-04-27 15:38:33 +02002210 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002213 * Ensure that the number we got makes sense; limit it within
2214 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002216 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2217 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002219 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002220 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002221 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2222 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002223 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002224 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002226 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
Russell King4031bbe2006-01-06 11:41:00 +00002229static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002233 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002234 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 ide_unregister_region(tape->disk);
2236
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002237 mutex_lock(&idetape_ref_mutex);
2238 put_device(&tape->dev);
2239 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002242static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002244 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 ide_drive_t *drive = tape->drive;
2246 struct gendisk *g = tape->disk;
2247
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002248 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002249
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002250 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002252 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002253 device_destroy(idetape_sysfs_class,
2254 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 idetape_devs[tape->minor] = NULL;
2256 g->private_data = NULL;
2257 put_disk(g);
2258 kfree(tape);
2259}
2260
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002261#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262static int proc_idetape_read_name
2263 (char *page, char **start, off_t off, int count, int *eof, void *data)
2264{
2265 ide_drive_t *drive = (ide_drive_t *) data;
2266 idetape_tape_t *tape = drive->driver_data;
2267 char *out = page;
2268 int len;
2269
2270 len = sprintf(out, "%s\n", tape->name);
2271 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2272}
2273
2274static ide_proc_entry_t idetape_proc[] = {
2275 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2276 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2277 { NULL, 0, NULL, NULL }
2278};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002279
2280static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2281{
2282 return idetape_proc;
2283}
2284
2285static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2286{
2287 return idetape_settings;
2288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289#endif
2290
Russell King4031bbe2006-01-06 11:41:00 +00002291static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01002293static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002294 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002295 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002296 .name = "ide-tape",
2297 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002298 },
Russell King4031bbe2006-01-06 11:41:00 +00002299 .probe = ide_tape_probe,
2300 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002303#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002304 .proc_entries = ide_tape_proc_entries,
2305 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002309/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002310static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 .owner = THIS_MODULE,
2312 .read = idetape_chrdev_read,
2313 .write = idetape_chrdev_write,
2314 .ioctl = idetape_chrdev_ioctl,
2315 .open = idetape_chrdev_open,
2316 .release = idetape_chrdev_release,
2317};
2318
Al Viroa4600f82008-03-02 10:27:58 -05002319static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Al Viroa4600f82008-03-02 10:27:58 -05002321 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002323 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return -ENXIO;
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return 0;
2327}
2328
Al Viroa4600f82008-03-02 10:27:58 -05002329static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002331 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return 0;
2335}
2336
Al Viroa4600f82008-03-02 10:27:58 -05002337static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 unsigned int cmd, unsigned long arg)
2339{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002340 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04002342 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (err == -EINVAL)
2344 err = idetape_blkdev_ioctl(drive, cmd, arg);
2345 return err;
2346}
2347
2348static struct block_device_operations idetape_block_ops = {
2349 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05002350 .open = idetape_open,
2351 .release = idetape_release,
2352 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353};
2354
Russell King4031bbe2006-01-06 11:41:00 +00002355static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 idetape_tape_t *tape;
2358 struct gendisk *g;
2359 int minor;
2360
2361 if (!strstr("ide-tape", drive->driver_req))
2362 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (drive->media != ide_tape)
2365 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002366
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002367 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2368 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002369 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2370 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto failed;
2372 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002373 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002375 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2376 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto failed;
2378 }
2379
2380 g = alloc_disk(1 << PARTN_BITS);
2381 if (!g)
2382 goto out_free_tape;
2383
2384 ide_init_disk(g, drive);
2385
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002386 tape->dev.parent = &drive->gendev;
2387 tape->dev.release = ide_tape_release;
2388 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2389
2390 if (device_register(&tape->dev))
2391 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 tape->drive = drive;
2394 tape->driver = &idetape_driver;
2395 tape->disk = g;
2396
2397 g->private_data = &tape->driver;
2398
2399 drive->driver_data = tape;
2400
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002401 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 for (minor = 0; idetape_devs[minor]; minor++)
2403 ;
2404 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002405 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 idetape_setup(drive, tape, minor);
2408
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002409 device_create(idetape_sysfs_class, &drive->gendev,
2410 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2411 device_create(idetape_sysfs_class, &drive->gendev,
2412 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2413 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 g->fops = &idetape_block_ops;
2416 ide_register_region(g);
2417
2418 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002419
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002420out_free_disk:
2421 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422out_free_tape:
2423 kfree(tape);
2424failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002425 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002428static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002430 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002431 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 unregister_chrdev(IDETAPE_MAJOR, "ht");
2433}
2434
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002435static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Will Dysond5dee802005-09-16 02:55:07 -07002437 int error = 1;
2438 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2439 if (IS_ERR(idetape_sysfs_class)) {
2440 idetape_sysfs_class = NULL;
2441 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2442 error = -EBUSY;
2443 goto out;
2444 }
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002447 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2448 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002449 error = -EBUSY;
2450 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Will Dysond5dee802005-09-16 02:55:07 -07002452
2453 error = driver_register(&idetape_driver.gen_driver);
2454 if (error)
2455 goto out_free_driver;
2456
2457 return 0;
2458
2459out_free_driver:
2460 driver_unregister(&idetape_driver.gen_driver);
2461out_free_class:
2462 class_destroy(idetape_sysfs_class);
2463out:
2464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Kay Sievers263756e2005-12-12 18:03:44 +01002467MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468module_init(idetape_init);
2469module_exit(idetape_exit);
2470MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002471MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2472MODULE_LICENSE("GPL");