blob: 1f7f50473a4f5009d9f4294b201bd0869f89c7e5 [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
Borislav Petkov03056b92008-04-18 00:46:26 +0200134/* Tape door status */
135#define DOOR_UNLOCKED 0
136#define DOOR_LOCKED 1
137#define DOOR_EXPLICITLY_LOCKED 2
138
139/* Some defines for the SPACE command */
140#define IDETAPE_SPACE_OVER_FILEMARK 1
141#define IDETAPE_SPACE_TO_EOD 3
142
143/* Some defines for the LOAD UNLOAD command */
144#define IDETAPE_LU_LOAD_MASK 1
145#define IDETAPE_LU_RETENSION_MASK 2
146#define IDETAPE_LU_EOT_MASK 4
147
Borislav Petkov03056b92008-04-18 00:46:26 +0200148/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
149#define IDETAPE_BLOCK_DESCRIPTOR 0
150#define IDETAPE_CAPABILITIES_PAGE 0x2a
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100153 * Most of our global data which we need to save even as we leave the driver due
154 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
156typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100157 ide_drive_t *drive;
158 struct ide_driver *driver;
159 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100160 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200162 /* used by REQ_IDETAPE_{READ,WRITE} requests */
163 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100166 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100168 * While polling for DSC we use postponed_rq to postpone the current
169 * request so that ide.c will be able to service pending requests on the
170 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200171 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173 struct request *postponed_rq;
174 /* The time in which we started polling for DSC */
175 unsigned long dsc_polling_start;
176 /* Timer used to poll for dsc */
177 struct timer_list dsc_timer;
178 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100179 unsigned long best_dsc_rw_freq;
180 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned long dsc_timeout;
182
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100183 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 partition;
185 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100186 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100188 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u8 sense_key, asc, ascq;
190
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100191 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned int minor;
193 /* device name */
194 char name[4];
195 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100196 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Borislav Petkov54bb2072008-02-06 02:57:52 +0100198 /* tape block size, usually 512 or 1024 bytes */
199 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100203 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100206 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208 * At most, there is only one ide-tape originated data transfer request
209 * in the device request queue. This allows ide.c to easily service
210 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200212
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100213 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200214 int buffer_size;
Tejun Heo6cf3d542009-04-19 08:46:02 +0900215 /* Staging buffer of buffer_size bytes */
216 void *buf;
217 /* The read/write cursor */
218 void *cur;
219 /* The number of valid bytes in buf */
220 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100221
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100222 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unsigned long avg_time;
224 int avg_size;
225 int avg_speed;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* the door is currently locked */
228 int door_locked;
229 /* the tape hardware is write protected */
230 char drv_write_prot;
231 /* the tape is write protected (hardware or opened as read-only) */
232 char write_prot;
233
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100234 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235} idetape_tape_t;
236
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800237static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Will Dysond5dee802005-09-16 02:55:07 -0700239static struct class *idetape_sysfs_class;
240
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100241static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
244{
245 struct ide_tape_obj *tape = NULL;
246
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800247 mutex_lock(&idetape_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +0200248 tape = ide_drv_g(disk, ide_tape_obj);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200249 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200250 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200251 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200252 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100253 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200254 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return tape;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static void ide_tape_put(struct ide_tape_obj *tape)
260{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200261 ide_drive_t *drive = tape->drive;
262
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800263 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100264 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200265 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800266 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100270 * The variables below are used for the character device interface. Additional
271 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100273static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
276{
277 struct ide_tape_obj *tape = NULL;
278
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800279 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 tape = idetape_devs[i];
281 if (tape)
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100282 get_device(&tape->dev);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800283 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return tape;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100288 * called on each failed packet command retry to analyze the request sense. We
289 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100291static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100294 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200295 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Borislav Petkov1b5db432008-02-02 19:56:48 +0100297 tape->sense_key = sense[2] & 0xF;
298 tape->asc = sense[12];
299 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100300
301 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
302 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Borislav Petkov077e6db2009-05-01 21:21:02 +0200304 /* correct remaining bytes to transfer */
Tejun Heoe998f302009-04-19 08:46:02 +0900305 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200306 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
309 * If error was the result of a zero-length read or write command,
310 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
311 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
312 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100313 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100314 /* length == 0 */
315 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
316 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* don't report an error, everything's ok */
318 pc->error = 0;
319 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200320 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100323 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100324 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200325 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100327 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100328 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
329 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100330 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200331 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100334 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100335 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100336 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200337 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200339 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200340 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
342 }
343}
344
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200345static void ide_tape_handle_dsc(ide_drive_t *);
346
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100347static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200350 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100351 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200352 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100353 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100355 debug_log(DBG_PROCS, "Enter %s\n", __func__);
356
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200357 if (dsc)
358 ide_tape_handle_dsc(drive);
359
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100360 if (drive->failed_pc == pc)
361 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200362
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200363 if (pc->c[0] == REQUEST_SENSE) {
364 if (uptodate)
365 idetape_analyze_error(drive, pc->buf);
366 else
367 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
368 "itself - Aborting request!\n");
369 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200370 unsigned int blocks =
371 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200372
373 tape->avg_size += blocks * tape->blk_size;
374
375 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
376 tape->avg_speed = tape->avg_size * HZ /
377 (jiffies - tape->avg_time) / 1024;
378 tape->avg_size = 0;
379 tape->avg_time = jiffies;
380 }
381
382 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200383
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100384 if (pc->error) {
385 uptodate = 0;
386 err = pc->error;
387 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200388 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200389 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200390
391 debug_log(DBG_SENSE, "BOP - %s\n",
392 (readpos[0] & 0x80) ? "Yes" : "No");
393 debug_log(DBG_SENSE, "EOP - %s\n",
394 (readpos[0] & 0x40) ? "Yes" : "No");
395
396 if (readpos[0] & 0x4) {
397 printk(KERN_INFO "ide-tape: Block location is unknown"
398 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200399 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200400 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100401 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200402 } else {
403 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200404 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200405
406 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200407 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200408 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200411
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100412 rq->errors = err;
413
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100414 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100418 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100419 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100421static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 idetape_tape_t *tape = drive->driver_data;
424
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100425 debug_log(DBG_PROCS, "Enter %s\n", __func__);
426
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100427 tape->postponed_rq = drive->hwif->rq;
428
Borislav Petkov54bb2072008-02-06 02:57:52 +0100429 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200432static void ide_tape_handle_dsc(ide_drive_t *drive)
433{
434 idetape_tape_t *tape = drive->driver_data;
435
436 /* Media access command */
437 tape->dsc_polling_start = jiffies;
438 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
439 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
440 /* Allow ide.c to handle other requests */
441 idetape_postpone_request(drive);
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100445 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200447 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100448 * until we finish handling it. Each packet command is associated with a
449 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100451 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100453 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200454 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200456 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100457 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100459 * 3. ATAPI Tape media access commands have immediate status with a delayed
460 * process. In case of a successful initiation of a media access packet command,
461 * the DSC bit will be set when the actual execution of the command is finished.
462 * Since the tape drive will not issue an interrupt, we have to poll for this
463 * event. In this case, we define the request as "low priority request" by
464 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
465 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100467 * ide.c will then give higher priority to requests which originate from the
468 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100470 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100472 * 5. In case an error was found, we queue a request sense packet command in
473 * front of the request queue and retry the operation up to
474 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100476 * 6. In case no error was found, or we decided to give up and not to retry
477 * again, the callback function will be called and then we will handle the next
478 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100481static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
482 struct ide_cmd *cmd,
483 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200486 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100488 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
489 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200492 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200495 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heoeb6a61b2009-04-19 08:46:02 +0900496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100498 * We will "abort" retrying a packet command in case legitimate
499 * error code was received (crossing a filemark, or end of the
500 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200502 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100503 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 tape->sense_key == 2 && tape->asc == 4 &&
505 (tape->ascq == 1 || tape->ascq == 8))) {
506 printk(KERN_ERR "ide-tape: %s: I/O error, "
507 "pc = %2x, key = %2x, "
508 "asc = %2x, ascq = %2x\n",
509 tape->name, pc->c[0],
510 tape->sense_key, tape->asc,
511 tape->ascq);
512 }
513 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100514 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Tejun Heoeb6a61b2009-04-19 08:46:02 +0900516
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100517 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200518 drive->pc_callback(drive, 0);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200519 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200520 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100522 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100526 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100529/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200530static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200532 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100533 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100535 /* DBD = 1 - Don't return block descriptors */
536 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 pc->c[2] = page_code;
538 /*
539 * Changed pc->c[3] to 0 (255 will at best return unused info).
540 *
541 * For SCSI this byte is defined as subpage instead of high byte
542 * of length and some IDE drives seem to interpret it this way
543 * and return an error when 255 is used.
544 */
545 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100546 /* We will just discard data in that case */
547 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200549 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200551 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200553 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100556static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200558 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200560 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100561 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200563 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100564
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200565 if (stat & ATA_DSC) {
566 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100568 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 printk(KERN_ERR "ide-tape: %s: I/O error, ",
570 tape->name);
571 /* Retry operation */
Borislav Petkov06875322009-04-19 07:00:42 +0900572 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200573 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100577 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100578 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200580 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return ide_stopped;
582}
583
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200584static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200585 struct ide_atapi_pc *pc, struct request *rq,
586 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Borislav Petkov10c0b342009-05-01 20:29:53 +0200588 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
Borislav Petkov0014c752008-07-23 19:56:00 +0200589
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200590 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100591 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +0200593 pc->buf = NULL;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200594 pc->buf_size = blk_rq_bytes(rq);
595 if (pc->buf_size == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200596 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Tejun Heo6cf3d542009-04-19 08:46:02 +0900598 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200599 pc->c[0] = READ_6;
Tejun Heo6cf3d542009-04-19 08:46:02 +0900600 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200601 pc->c[0] = WRITE_6;
602 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200603 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200604
605 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static ide_startstop_t idetape_do_request(ide_drive_t *drive,
609 struct request *rq, sector_t block)
610{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200611 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200613 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100615 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100616 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Tejun Heo9780e2d2009-05-07 22:24:40 +0900618 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %u\n"
619 (unsigned long long)blk_rq_pos(rq), blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Borislav Petkov06875322009-04-19 07:00:42 +0900621 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100622 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200624 "request queue (%d)\n", drive->name, rq->cmd_type);
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100625 if (blk_fs_request(rq) == 0 && rq->errors == 0)
626 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100627 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return ide_stopped;
629 }
630
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100631 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100632 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
633 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200634 goto out;
635 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (postponed_rq != NULL)
638 if (rq != postponed_rq) {
639 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
640 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100641 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100642 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100643 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return ide_stopped;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 tape->postponed_rq = NULL;
648
649 /*
650 * If the tape is still busy, postpone our request and service
651 * the other device meanwhile.
652 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200653 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200655 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
656 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200657 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200659 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200660 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200661 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200664 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200665 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (postponed_rq == NULL) {
667 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100668 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
670 } else if (time_after(jiffies, tape->dsc_timeout)) {
671 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
672 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200673 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 idetape_media_access_finished(drive);
675 return ide_stopped;
676 } else {
677 return ide_do_reset(drive);
678 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100679 } else if (time_after(jiffies,
680 tape->dsc_polling_start +
681 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100682 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 idetape_postpone_request(drive);
684 return ide_stopped;
685 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200686 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200687 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200688 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto out;
690 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200691 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200692 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200693 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto out;
695 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200696 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoac0b0112009-04-19 07:00:42 +0900697 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200698 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
699 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto out;
701 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200702 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 idetape_media_access_finished(drive);
704 return ide_stopped;
705 }
706 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200707
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200708out:
Borislav Petkov06875322009-04-19 07:00:42 +0900709 /* prepare sense request for this command */
710 ide_prep_sense(drive, rq);
711
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100712 memset(&cmd, 0, sizeof(cmd));
713
714 if (rq_data_dir(rq))
715 cmd.tf_flags |= IDE_TFLAG_WRITE;
716
717 cmd.rq = rq;
718
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200719 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo02e7cf82009-04-19 07:00:42 +0900720 ide_map_sg(drive, &cmd);
721
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100722 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100726 * Write a filemark if write_filemark=1. Flush the device buffers without
727 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100729static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200730 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200732 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100733 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200735 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
739{
740 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200741 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int load_attempted = 0;
743
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100744 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200745 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 timeout += jiffies;
747 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200748 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100751 || (tape->asc == 0x3A)) {
752 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (load_attempted)
754 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200755 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 load_attempted = 1;
757 /* not about to be ready */
758 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
759 (tape->ascq == 1 || tape->ascq == 8)))
760 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700761 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 return -EIO;
764}
765
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100766static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200768 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200769 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int rc;
771
772 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200773 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100774 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return rc;
776 idetape_wait_ready(drive, 60 * 5 * HZ);
777 return 0;
778}
779
Borislav Petkovd236d742008-04-18 00:46:27 +0200780static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200782 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100783 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +0200784 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100787static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200790 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int position;
792
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100793 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 idetape_create_read_position_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200796 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc.buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100798 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return position;
800}
801
Borislav Petkovd236d742008-04-18 00:46:27 +0200802static void idetape_create_locate_cmd(ide_drive_t *drive,
803 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100804 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200806 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100807 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100809 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200811 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200814static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Borislav Petkov54abf372008-02-06 02:57:52 +0100818 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200821 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Tejun Heo6cf3d542009-04-19 08:46:02 +0900822 tape->valid = 0;
823 if (tape->buf != NULL) {
824 kfree(tape->buf);
825 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Borislav Petkov54abf372008-02-06 02:57:52 +0100828 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100832 * Position the tape to the requested block using the LOCATE packet command.
833 * A READ POSITION command is then issued to check where we are positioned. Like
834 * all higher level operations, we queue the commands at the tail of the request
835 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100837static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
838 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200841 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +0200843 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Borislav Petkov54abf372008-02-06 02:57:52 +0100845 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200846 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 idetape_wait_ready(drive, 60 * 5 * HZ);
848 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200849 retval = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (retval)
851 return (retval);
852
853 idetape_create_read_position_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200854 return ide_queue_pc_tail(drive, disk, &pc, pc.buf, pc.req_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200857static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100858 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 int seek, position;
862
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200863 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (restore_position) {
865 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200866 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100868 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200869 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return;
871 }
872 }
873}
874
875/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100876 * Generate a read/write request for the block device interface and wait for it
877 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Tejun Heo71294cf2009-04-19 08:46:03 +0900879static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200882 struct request *rq;
Tejun Heoe998f302009-04-19 08:46:02 +0900883 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100885 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
Tejun Heoe998f302009-04-19 08:46:02 +0900886 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo71294cf2009-04-19 08:46:03 +0900887 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100888
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200889 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
890 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200891 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200892 rq->rq_disk = tape->disk;
Tejun Heoe998f302009-04-19 08:46:02 +0900893
894 if (size) {
Tejun Heo6cf3d542009-04-19 08:46:02 +0900895 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heoe998f302009-04-19 08:46:02 +0900896 __GFP_WAIT);
897 if (ret)
898 goto out_put;
899 }
900
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200901 blk_execute_rq(drive->queue, tape->disk, rq, 0);
902
Tejun Heo6cf3d542009-04-19 08:46:02 +0900903 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900904 size -= rq->resid_len;
Tejun Heo6cf3d542009-04-19 08:46:02 +0900905 tape->cur = tape->buf;
Tejun Heoe998f302009-04-19 08:46:02 +0900906 if (cmd == REQ_IDETAPE_READ)
Tejun Heo6cf3d542009-04-19 08:46:02 +0900907 tape->valid = size;
908 else
909 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Tejun Heoe998f302009-04-19 08:46:02 +0900911 ret = size;
912 if (rq->errors == IDE_DRV_ERROR_GENERAL)
913 ret = -EIO;
Tejun Heoe998f302009-04-19 08:46:02 +0900914out_put:
915 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200916 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Borislav Petkovd236d742008-04-18 00:46:27 +0200919static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200921 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100922 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100923 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200924 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Borislav Petkovd236d742008-04-18 00:46:27 +0200927static void idetape_create_rewind_cmd(ide_drive_t *drive,
928 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200930 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100931 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200932 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Borislav Petkovd236d742008-04-18 00:46:27 +0200935static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200937 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100938 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200940 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Borislav Petkovd236d742008-04-18 00:46:27 +0200943static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200945 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100946 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100947 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200949 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Borislav Petkovd9df9372008-04-27 15:38:34 +0200952static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100955
Borislav Petkov54abf372008-02-06 02:57:52 +0100956 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200957 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100958 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return;
960 }
Tejun Heo6cf3d542009-04-19 08:46:02 +0900961 if (tape->buf) {
Tejun Heo71294cf2009-04-19 08:46:03 +0900962 size_t aligned = roundup(tape->valid, tape->blk_size);
963
964 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo4344d072009-04-19 08:46:03 +0900965 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo6cf3d542009-04-19 08:46:02 +0900966 kfree(tape->buf);
967 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100969 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Tejun Heo3596b662009-04-19 08:46:02 +0900972static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 idetape_tape_t *tape = drive->driver_data;
Tejun Heo3596b662009-04-19 08:46:02 +0900975 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Tejun Heo3596b662009-04-19 08:46:02 +0900977 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Tejun Heo3596b662009-04-19 08:46:02 +0900979 if (tape->chrdev_dir == dir)
980 return 0;
981
982 if (tape->chrdev_dir == IDETAPE_DIR_READ)
983 ide_tape_discard_merge_buffer(drive, 1);
984 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
985 ide_tape_flush_merge_buffer(drive);
986 idetape_flush_tape_buffers(drive);
987 }
988
989 if (tape->buf || tape->valid) {
990 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
991 tape->valid = 0;
992 }
993
994 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
995 if (!tape->buf)
996 return -ENOMEM;
997 tape->chrdev_dir = dir;
998 tape->cur = tape->buf;
999
1000 /*
1001 * Issue a 0 rw command to ensure that DSC handshake is
1002 * switched from completion mode to buffer available mode. No
1003 * point in issuing this if DSC overlap isn't supported, some
1004 * drives (Seagate STT3401A) will return an error.
1005 */
1006 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1007 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
1008 : REQ_IDETAPE_WRITE;
1009
1010 rc = idetape_queue_rw_tail(drive, cmd, 0);
1011 if (rc < 0) {
1012 kfree(tape->buf);
1013 tape->buf = NULL;
1014 tape->chrdev_dir = IDETAPE_DIR_NONE;
1015 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001022static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 idetape_tape_t *tape = drive->driver_data;
Tejun Heo6cf3d542009-04-19 08:46:02 +09001025
1026 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 while (bcount) {
Tejun Heo6cf3d542009-04-19 08:46:02 +09001029 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Tejun Heo71294cf2009-04-19 08:46:03 +09001031 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001037 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1038 * currently support only one partition.
1039 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001040static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001042 struct ide_tape_obj *tape = drive->driver_data;
1043 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001045 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001046
1047 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001050 retval = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (retval)
1052 return retval;
1053
1054 idetape_create_read_position_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001055 retval = ide_queue_pc_tail(drive, disk, &pc, pc.buf, pc.req_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (retval)
1057 return retval;
1058 return 0;
1059}
1060
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001061/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001062static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1063 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 void __user *argp = (void __user *)arg;
1067
Borislav Petkovd59823f2008-02-02 19:56:51 +01001068 struct idetape_config {
1069 int dsc_rw_frequency;
1070 int dsc_media_access_frequency;
1071 int nr_stages;
1072 } config;
1073
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001074 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001077 case 0x0340:
1078 if (copy_from_user(&config, argp, sizeof(config)))
1079 return -EFAULT;
1080 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001081 break;
1082 case 0x0350:
1083 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001084 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001085 if (copy_to_user(argp, &config, sizeof(config)))
1086 return -EFAULT;
1087 break;
1088 default:
1089 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 return 0;
1092}
1093
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001094static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1095 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001098 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001099 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001100 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001101 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (mt_count == 0)
1104 return 0;
1105 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001106 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001108 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Borislav Petkov54abf372008-02-06 02:57:52 +01001111 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo6cf3d542009-04-19 08:46:02 +09001112 tape->valid = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001113 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001115 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001119 case MTFSF:
1120 case MTBSF:
1121 idetape_create_space_cmd(&pc, mt_count - count,
1122 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001123 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001124 case MTFSFM:
1125 case MTBSFM:
1126 if (!sprev)
1127 return -EIO;
1128 retval = idetape_space_over_filemarks(drive, MTFSF,
1129 mt_count - count);
1130 if (retval)
1131 return retval;
1132 count = (MTBSFM == mt_op ? 1 : -1);
1133 return idetape_space_over_filemarks(drive, MTFSF, count);
1134 default:
1135 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1136 mt_op);
1137 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001142 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001144 * The tape is optimized to maximize throughput when it is transferring an
1145 * integral number of the "continuous transfer limit", which is a parameter of
1146 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001148 * As of version 1.3 of the driver, the character device provides an abstract
1149 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1150 * same backup/restore procedure is supported. The driver will internally
1151 * convert the requests to the recommended transfer unit, so that an unmatch
1152 * between the user's block size to the recommended size will only result in a
1153 * (slightly) increased driver overhead, but will no longer hit performance.
1154 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001156static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1157 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001159 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ide_drive_t *drive = tape->drive;
Tejun Heo4344d072009-04-19 08:46:03 +09001161 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001162 ssize_t ret = 0;
Tejun Heo4344d072009-04-19 08:46:03 +09001163 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001165 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Borislav Petkov54abf372008-02-06 02:57:52 +01001167 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001168 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001169 if (count > tape->blk_size &&
1170 (count % tape->blk_size) == 0)
1171 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Tejun Heo4344d072009-04-19 08:46:03 +09001173
Tejun Heo3596b662009-04-19 08:46:02 +09001174 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001175 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return rc;
Tejun Heo4344d072009-04-19 08:46:03 +09001177
1178 while (done < count) {
1179 size_t todo;
1180
1181 /* refill if staging buffer is empty */
1182 if (!tape->valid) {
1183 /* If we are at a filemark, nothing more to read */
1184 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1185 break;
1186 /* read */
1187 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1188 tape->buffer_size) <= 0)
1189 break;
1190 }
1191
1192 /* copy out */
1193 todo = min_t(size_t, count - done, tape->valid);
1194 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001195 ret = -EFAULT;
Tejun Heo4344d072009-04-19 08:46:03 +09001196
1197 tape->cur += todo;
1198 tape->valid -= todo;
1199 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Tejun Heo4344d072009-04-19 08:46:03 +09001201
1202 if (!done && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001203 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 idetape_space_over_filemarks(drive, MTFSF, 1);
1206 return 0;
1207 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001208
Tejun Heo4344d072009-04-19 08:46:03 +09001209 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001212static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 size_t count, loff_t *ppos)
1214{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001215 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ide_drive_t *drive = tape->drive;
Tejun Heo4344d072009-04-19 08:46:03 +09001217 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001218 ssize_t ret = 0;
Tejun Heo3596b662009-04-19 08:46:02 +09001219 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* The drive is write protected. */
1222 if (tape->write_prot)
1223 return -EACCES;
1224
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001225 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* Initialize write operation */
Tejun Heo3596b662009-04-19 08:46:02 +09001228 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1229 if (rc < 0)
1230 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Tejun Heo4344d072009-04-19 08:46:03 +09001232 while (done < count) {
1233 size_t todo;
1234
1235 /* flush if staging buffer is full */
1236 if (tape->valid == tape->buffer_size &&
1237 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1238 tape->buffer_size) <= 0)
1239 return rc;
1240
1241 /* copy in */
1242 todo = min_t(size_t, count - done,
1243 tape->buffer_size - tape->valid);
1244 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001245 ret = -EFAULT;
Tejun Heo4344d072009-04-19 08:46:03 +09001246
1247 tape->cur += todo;
1248 tape->valid += todo;
1249 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Tejun Heo4344d072009-04-19 08:46:03 +09001251
1252 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001255static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001257 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001258 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /* Write a filemark */
1261 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001262 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1264 return -EIO;
1265 }
1266 return 0;
1267}
1268
1269/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001270 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1271 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001273 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1274 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001275 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001277 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001279 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1280 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001282static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001285 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001286 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001287 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001289 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1290 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001293 case MTFSF:
1294 case MTFSFM:
1295 case MTBSF:
1296 case MTBSFM:
1297 if (!mt_count)
1298 return 0;
1299 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1300 default:
1301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001305 case MTWEOF:
1306 if (tape->write_prot)
1307 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001308 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001309 for (i = 0; i < mt_count; i++) {
1310 retval = idetape_write_filemark(drive);
1311 if (retval)
1312 return retval;
1313 }
1314 return 0;
1315 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001316 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317 if (idetape_rewind_tape(drive))
1318 return -EIO;
1319 return 0;
1320 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001321 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001322 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001323 case MTUNLOAD:
1324 case MTOFFL:
1325 /*
1326 * If door is locked, attempt to unlock before
1327 * attempting to eject.
1328 */
1329 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001330 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001331 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001333 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001334 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001335 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001336 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001337 return retval;
1338 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001339 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001340 return idetape_flush_tape_buffers(drive);
1341 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001342 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001343 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001344 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001345 case MTEOM:
1346 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001347 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001348 case MTERASE:
1349 (void)idetape_rewind_tape(drive);
1350 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001351 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001352 case MTSETBLK:
1353 if (mt_count) {
1354 if (mt_count < tape->blk_size ||
1355 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001357 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001358 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001359 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001360 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001361 return 0;
1362 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001363 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001364 return idetape_position_tape(drive,
1365 mt_count * tape->user_bs_factor, tape->partition, 0);
1366 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001367 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001368 return idetape_position_tape(drive, 0, mt_count, 0);
1369 case MTFSR:
1370 case MTBSR:
1371 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001372 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001373 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001375 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1376 return 0;
1377 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001378 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001379 if (retval)
1380 return retval;
1381 tape->door_locked = DOOR_UNLOCKED;
1382 return 0;
1383 default:
1384 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1385 mt_op);
1386 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388}
1389
1390/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001391 * Our character device ioctls. General mtio.h magnetic io commands are
1392 * supported here, and not in the corresponding block interface. Our own
1393 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001395static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1396 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001398 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ide_drive_t *drive = tape->drive;
1400 struct mtop mtop;
1401 struct mtget mtget;
1402 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001403 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 void __user *argp = (void __user *)arg;
1405
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001406 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Borislav Petkov54abf372008-02-06 02:57:52 +01001408 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001409 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 idetape_flush_tape_buffers(drive);
1411 }
1412 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo6cf3d542009-04-19 08:46:02 +09001413 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001414 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001415 position = idetape_read_position(drive);
1416 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EIO;
1418 }
1419 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001420 case MTIOCTOP:
1421 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1422 return -EFAULT;
1423 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1424 case MTIOCGET:
1425 memset(&mtget, 0, sizeof(struct mtget));
1426 mtget.mt_type = MT_ISSCSI2;
1427 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1428 mtget.mt_dsreg =
1429 ((tape->blk_size * tape->user_bs_factor)
1430 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001431
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001432 if (tape->drv_write_prot)
1433 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1434
1435 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1436 return -EFAULT;
1437 return 0;
1438 case MTIOCPOS:
1439 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1440 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1441 return -EFAULT;
1442 return 0;
1443 default:
1444 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001445 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001446 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448}
1449
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001450/*
1451 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1452 * block size with the reported value.
1453 */
1454static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1455{
1456 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001457 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001458
1459 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001460 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc.buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001461 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001462 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001463 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1464 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001465 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001466 }
1467 return;
1468 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001469 tape->blk_size = (pc.buf[4 + 5] << 16) +
1470 (pc.buf[4 + 6] << 8) +
1471 pc.buf[4 + 7];
1472 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001475static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1478 ide_drive_t *drive;
1479 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int retval;
1481
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001482 if (i >= MAX_HWIFS * MAX_DRIVES)
1483 return -ENXIO;
1484
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001485 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001486 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001487 if (!tape) {
1488 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001489 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001490 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001491
1492 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /*
1495 * We really want to do nonseekable_open(inode, filp); here, but some
1496 * versions of tar incorrectly call lseek on tapes and bail out if that
1497 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1498 */
1499 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 drive = tape->drive;
1502
1503 filp->private_data = tape;
1504
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001505 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 retval = -EBUSY;
1507 goto out_put_tape;
1508 }
1509
1510 retval = idetape_wait_ready(drive, 60 * HZ);
1511 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001512 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1514 goto out_put_tape;
1515 }
1516
1517 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001518 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 (void)idetape_rewind_tape(drive);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001522 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* Set write protect flag if device is opened as read-only. */
1525 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1526 tape->write_prot = 1;
1527 else
1528 tape->write_prot = tape->drv_write_prot;
1529
1530 /* Make sure drive isn't write protected if user wants to write. */
1531 if (tape->write_prot) {
1532 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1533 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001534 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 retval = -EROFS;
1536 goto out_put_tape;
1537 }
1538 }
1539
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001540 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001541 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001542 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001543 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1544 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001547 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return 0;
1549
1550out_put_tape:
1551 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001552 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return retval;
1554}
1555
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001556static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 idetape_tape_t *tape = drive->driver_data;
1559
Borislav Petkovd9df9372008-04-27 15:38:34 +02001560 ide_tape_flush_merge_buffer(drive);
Tejun Heo6cf3d542009-04-19 08:46:02 +09001561 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1562 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001563 idetape_pad_zeros(drive, tape->blk_size *
1564 (tape->user_bs_factor - 1));
Tejun Heo6cf3d542009-04-19 08:46:02 +09001565 kfree(tape->buf);
1566 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 idetape_write_filemark(drive);
1569 idetape_flush_tape_buffers(drive);
1570 idetape_flush_tape_buffers(drive);
1571}
1572
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001573static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001575 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 unsigned int minor = iminor(inode);
1578
1579 lock_kernel();
1580 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001581
1582 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Borislav Petkov54abf372008-02-06 02:57:52 +01001584 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001586 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001588 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001590
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001591 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01001593 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001595 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001596 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001599 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ide_tape_put(tape);
1601 unlock_kernel();
1602 return 0;
1603}
1604
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001605static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001608 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001609 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001610 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001613 pc.buf_size = sizeof(pc_buf);
1614
Borislav Petkovb13345f2009-05-02 10:26:12 +02001615 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001616 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1617 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return;
1619 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001620 memcpy(vendor_id, &pc_buf[8], 8);
1621 memcpy(product_id, &pc_buf[16], 16);
1622 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001623
Borislav Petkov801bd322008-09-27 19:32:17 +02001624 ide_fixstring(vendor_id, 8, 0);
1625 ide_fixstring(product_id, 16, 0);
1626 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001627
Borislav Petkov801bd322008-09-27 19:32:17 +02001628 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001629 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
1632/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001633 * Ask the tape about its various parameters. In particular, we will adjust our
1634 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001636static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001639 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001640 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001641 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001644 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001645 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1646 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001647 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001648 put_unaligned(52, (u16 *)&tape->caps[12]);
1649 put_unaligned(540, (u16 *)&tape->caps[14]);
1650 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return;
1652 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001653 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Borislav Petkovb6422012008-02-02 19:56:49 +01001655 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001656 speed = be16_to_cpup((__be16 *)&caps[14]);
1657 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001659 *(u16 *)&caps[8] = max_speed;
1660 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1661 *(u16 *)&caps[14] = speed;
1662 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001663
1664 if (!speed) {
1665 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1666 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001667 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001669 if (!max_speed) {
1670 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1671 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001672 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
Borislav Petkovb6422012008-02-02 19:56:49 +01001675 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001676
1677 /* device lacks locking support according to capabilities page */
1678 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001679 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001680
Borislav Petkovb6422012008-02-02 19:56:49 +01001681 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001682 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001683 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001684 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001687#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001688#define ide_tape_devset_get(name, field) \
1689static int get_##name(ide_drive_t *drive) \
1690{ \
1691 idetape_tape_t *tape = drive->driver_data; \
1692 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001694
1695#define ide_tape_devset_set(name, field) \
1696static int set_##name(ide_drive_t *drive, int arg) \
1697{ \
1698 idetape_tape_t *tape = drive->driver_data; \
1699 tape->field = arg; \
1700 return 0; \
1701}
1702
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001703#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001704ide_tape_devset_get(_name, _field) \
1705ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001706IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001707
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001708#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001709ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001710IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001711
1712static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1713static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1714static int divf_buffer(ide_drive_t *drive) { return 2; }
1715static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1716
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001717ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001718
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001719ide_tape_devset_rw_field(debug_mask, debug_mask);
1720ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001721
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001722ide_tape_devset_r_field(avg_speed, avg_speed);
1723ide_tape_devset_r_field(speed, caps[14]);
1724ide_tape_devset_r_field(buffer, caps[16]);
1725ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001726
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001727static const struct ide_proc_devset idetape_settings[] = {
1728 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1729 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1730 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1731 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
1732 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1733 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1734 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1735 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001736 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001737};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001741 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001743 * 1. Initialize our various state variables.
1744 * 2. Ask the tape for its capabilities.
1745 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1746 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001748 * Note that at this point ide.c already assigned us an irq, so that we can
1749 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001751static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Borislav Petkov83042b22008-04-27 15:38:27 +02001753 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001755 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001756 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02001758 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001759
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001760 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1761
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001762 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1763 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1764 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001765 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001769 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001770 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 tape->minor = minor;
1773 tape->name[0] = 'h';
1774 tape->name[1] = 't';
1775 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001776 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 idetape_get_inquiry_results(drive);
1779 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001780 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001782 tape->buffer_size = *ctl * tape->blk_size;
1783 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001785 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001786 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001788 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Borislav Petkov83042b22008-04-27 15:38:27 +02001790 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001791 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Borislav Petkovf73850a2008-04-27 15:38:33 +02001793 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001796 * Ensure that the number we got makes sense; limit it within
1797 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001799 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1800 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001802 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001803 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001804 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1805 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001806 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001807 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001809 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Russell King4031bbe2006-01-06 11:41:00 +00001812static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001816 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001817 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ide_unregister_region(tape->disk);
1819
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001820 mutex_lock(&idetape_ref_mutex);
1821 put_device(&tape->dev);
1822 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001825static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001827 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 ide_drive_t *drive = tape->drive;
1829 struct gendisk *g = tape->disk;
1830
Tejun Heo6cf3d542009-04-19 08:46:02 +09001831 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001832
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001833 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001835 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001836 device_destroy(idetape_sysfs_class,
1837 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 idetape_devs[tape->minor] = NULL;
1839 g->private_data = NULL;
1840 put_disk(g);
1841 kfree(tape);
1842}
1843
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001844#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845static int proc_idetape_read_name
1846 (char *page, char **start, off_t off, int count, int *eof, void *data)
1847{
1848 ide_drive_t *drive = (ide_drive_t *) data;
1849 idetape_tape_t *tape = drive->driver_data;
1850 char *out = page;
1851 int len;
1852
1853 len = sprintf(out, "%s\n", tape->name);
1854 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1855}
1856
1857static ide_proc_entry_t idetape_proc[] = {
1858 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1859 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1860 { NULL, 0, NULL, NULL }
1861};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001862
1863static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1864{
1865 return idetape_proc;
1866}
1867
1868static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1869{
1870 return idetape_settings;
1871}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872#endif
1873
Russell King4031bbe2006-01-06 11:41:00 +00001874static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001876static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001877 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001878 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001879 .name = "ide-tape",
1880 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001881 },
Russell King4031bbe2006-01-06 11:41:00 +00001882 .probe = ide_tape_probe,
1883 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001886#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001887 .proc_entries = ide_tape_proc_entries,
1888 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890};
1891
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001892/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001893static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 .owner = THIS_MODULE,
1895 .read = idetape_chrdev_read,
1896 .write = idetape_chrdev_write,
1897 .ioctl = idetape_chrdev_ioctl,
1898 .open = idetape_chrdev_open,
1899 .release = idetape_chrdev_release,
1900};
1901
Al Viroa4600f82008-03-02 10:27:58 -05001902static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Al Viroa4600f82008-03-02 10:27:58 -05001904 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001906 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return -ENXIO;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return 0;
1910}
1911
Al Viroa4600f82008-03-02 10:27:58 -05001912static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001914 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return 0;
1918}
1919
Al Viroa4600f82008-03-02 10:27:58 -05001920static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned int cmd, unsigned long arg)
1922{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001923 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04001925 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (err == -EINVAL)
1927 err = idetape_blkdev_ioctl(drive, cmd, arg);
1928 return err;
1929}
1930
1931static struct block_device_operations idetape_block_ops = {
1932 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001933 .open = idetape_open,
1934 .release = idetape_release,
1935 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936};
1937
Russell King4031bbe2006-01-06 11:41:00 +00001938static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 idetape_tape_t *tape;
1941 struct gendisk *g;
1942 int minor;
1943
1944 if (!strstr("ide-tape", drive->driver_req))
1945 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (drive->media != ide_tape)
1948 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001949
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001950 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1951 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001952 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1953 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto failed;
1955 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001956 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001958 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1959 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto failed;
1961 }
1962
1963 g = alloc_disk(1 << PARTN_BITS);
1964 if (!g)
1965 goto out_free_tape;
1966
1967 ide_init_disk(g, drive);
1968
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001969 tape->dev.parent = &drive->gendev;
1970 tape->dev.release = ide_tape_release;
1971 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1972
1973 if (device_register(&tape->dev))
1974 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 tape->drive = drive;
1977 tape->driver = &idetape_driver;
1978 tape->disk = g;
1979
1980 g->private_data = &tape->driver;
1981
1982 drive->driver_data = tape;
1983
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001984 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 for (minor = 0; idetape_devs[minor]; minor++)
1986 ;
1987 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001988 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 idetape_setup(drive, tape, minor);
1991
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07001992 device_create(idetape_sysfs_class, &drive->gendev,
1993 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1994 device_create(idetape_sysfs_class, &drive->gendev,
1995 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
1996 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 g->fops = &idetape_block_ops;
1999 ide_register_region(g);
2000
2001 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002002
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002003out_free_disk:
2004 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005out_free_tape:
2006 kfree(tape);
2007failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002008 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002011static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002013 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002014 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unregister_chrdev(IDETAPE_MAJOR, "ht");
2016}
2017
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002018static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Will Dysond5dee802005-09-16 02:55:07 -07002020 int error = 1;
2021 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2022 if (IS_ERR(idetape_sysfs_class)) {
2023 idetape_sysfs_class = NULL;
2024 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2025 error = -EBUSY;
2026 goto out;
2027 }
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002030 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2031 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002032 error = -EBUSY;
2033 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Will Dysond5dee802005-09-16 02:55:07 -07002035
2036 error = driver_register(&idetape_driver.gen_driver);
2037 if (error)
2038 goto out_free_driver;
2039
2040 return 0;
2041
2042out_free_driver:
2043 driver_unregister(&idetape_driver.gen_driver);
2044out_free_class:
2045 class_destroy(idetape_sysfs_class);
2046out:
2047 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Kay Sievers263756e2005-12-12 18:03:44 +01002050MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051module_init(idetape_init);
2052module_exit(idetape_exit);
2053MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002054MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2055MODULE_LICENSE("GPL");