blob: 3468ece98496cf6ce5068f5f127082c7c7a754ed [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 +010050/* define to see debug info */
Borislav Petkove972d702009-07-17 23:55:16 +000051#undef IDETAPE_DEBUG_LOG
Borislav Petkov8004a8c2008-02-06 02:57:51 +010052
Borislav Petkove972d702009-07-17 23:55:16 +000053#ifdef IDETAPE_DEBUG_LOG
54#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010055#else
Borislav Petkove972d702009-07-17 23:55:16 +000056#define ide_debug_log(lvl, fmt, args...) do {} while (0)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010057#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010061 * After each failed packet command we issue a request sense command and retry
62 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010064 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66#define IDETAPE_MAX_PC_RETRIES 3
67
68/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010069 * The following parameter is used to select the point in the internal tape fifo
70 * in which we will start to refill the buffer. Decreasing the following
71 * parameter will improve the system's latency and interactive response, while
72 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010074#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010077 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Polling for DSC (a single bit in the status register) is a very important
80 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010082 * 1. Before a read/write packet command, to ensure that we can transfer data
83 * from/to the tape's data buffers, without causing an actual media access.
84 * In case the tape is not ready yet, we take out our request from the device
85 * request queue, so that ide.c could service requests from the other device
86 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010088 * 2. After the successful initialization of a "media access packet command",
89 * which is a command that can take a long time to complete (the interval can
90 * range from several seconds to even an hour). Again, we postpone our request
91 * in the middle to free the bus for the other device. The polling frequency
92 * here should be lower than the read/write frequency since those media access
93 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
94 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
95 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * We also set a timeout for the timer, in case something goes wrong. The
98 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100100
101/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
103#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
104#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
105#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
106#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
107#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
108#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
109
110/*************************** End of tunable parameters ***********************/
111
Borislav Petkov54abf372008-02-06 02:57:52 +0100112/* tape directions */
113enum {
114 IDETAPE_DIR_NONE = (1 << 0),
115 IDETAPE_DIR_READ = (1 << 1),
116 IDETAPE_DIR_WRITE = (1 << 2),
117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Borislav Petkov03056b92008-04-18 00:46:26 +0200119/* Tape door status */
120#define DOOR_UNLOCKED 0
121#define DOOR_LOCKED 1
122#define DOOR_EXPLICITLY_LOCKED 2
123
124/* Some defines for the SPACE command */
125#define IDETAPE_SPACE_OVER_FILEMARK 1
126#define IDETAPE_SPACE_TO_EOD 3
127
128/* Some defines for the LOAD UNLOAD command */
129#define IDETAPE_LU_LOAD_MASK 1
130#define IDETAPE_LU_RETENSION_MASK 2
131#define IDETAPE_LU_EOT_MASK 4
132
Borislav Petkov03056b92008-04-18 00:46:26 +0200133/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
134#define IDETAPE_BLOCK_DESCRIPTOR 0
135#define IDETAPE_CAPABILITIES_PAGE 0x2a
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100138 * Most of our global data which we need to save even as we leave the driver due
139 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100142 ide_drive_t *drive;
143 struct ide_driver *driver;
144 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100145 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200147 /* used by REQ_IDETAPE_{READ,WRITE} requests */
148 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100151 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100153 * While polling for DSC we use postponed_rq to postpone the current
154 * request so that ide.c will be able to service pending requests on the
155 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200156 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158 struct request *postponed_rq;
159 /* The time in which we started polling for DSC */
160 unsigned long dsc_polling_start;
161 /* Timer used to poll for dsc */
162 struct timer_list dsc_timer;
163 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100164 unsigned long best_dsc_rw_freq;
165 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned long dsc_timeout;
167
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100168 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 u8 partition;
170 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100171 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100173 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 u8 sense_key, asc, ascq;
175
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100176 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int minor;
178 /* device name */
179 char name[4];
180 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100181 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Borislav Petkov54bb2072008-02-06 02:57:52 +0100183 /* tape block size, usually 512 or 1024 bytes */
184 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100188 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100191 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100193 * At most, there is only one ide-tape originated data transfer request
194 * in the device request queue. This allows ide.c to easily service
195 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200197
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100198 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200199 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900200 /* Staging buffer of buffer_size bytes */
201 void *buf;
202 /* The read/write cursor */
203 void *cur;
204 /* The number of valid bytes in buf */
205 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100206
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100207 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long avg_time;
209 int avg_size;
210 int avg_speed;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* the door is currently locked */
213 int door_locked;
214 /* the tape hardware is write protected */
215 char drv_write_prot;
216 /* the tape is write protected (hardware or opened as read-only) */
217 char write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218} idetape_tape_t;
219
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800220static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Will Dysond5dee802005-09-16 02:55:07 -0700222static struct class *idetape_sysfs_class;
223
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100224static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200225
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200226static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
227
228static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
229 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct ide_tape_obj *tape = NULL;
232
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800233 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200234
235 if (cdev)
236 tape = idetape_devs[i];
237 else
238 tape = ide_drv_g(disk, ide_tape_obj);
239
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200240 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200241 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200242 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200243 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100244 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200245 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200246
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800247 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return tape;
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static void ide_tape_put(struct ide_tape_obj *tape)
252{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200253 ide_drive_t *drive = tape->drive;
254
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100256 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200257 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800258 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100262 * called on each failed packet command retry to analyze the request sense. We
263 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Borislav Petkovae3a8382009-05-02 10:58:17 +0200265static void idetape_analyze_error(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100268 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200269 struct request *rq = drive->hwif->rq;
Borislav Petkovae3a8382009-05-02 10:58:17 +0200270 u8 *sense = bio_data(rq->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Borislav Petkov1b5db432008-02-02 19:56:48 +0100272 tape->sense_key = sense[2] & 0xF;
273 tape->asc = sense[12];
274 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100275
Borislav Petkove972d702009-07-17 23:55:16 +0000276 ide_debug_log(IDE_DBG_FUNC,
277 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
278 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Borislav Petkov077e6db2009-05-01 21:21:02 +0200280 /* correct remaining bytes to transfer */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900281 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200282 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /*
285 * If error was the result of a zero-length read or write command,
286 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
287 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
288 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100289 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100290 /* length == 0 */
291 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
292 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* don't report an error, everything's ok */
294 pc->error = 0;
295 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200296 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100299 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100300 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200301 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100303 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100304 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
305 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100306 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200307 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100310 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100311 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100312 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200313 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200315 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200316 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
318 }
319}
320
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200321static void ide_tape_handle_dsc(ide_drive_t *);
322
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100323static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200326 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100327 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200328 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100329 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Borislav Petkove972d702009-07-17 23:55:16 +0000331 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
332 dsc, err);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100333
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200334 if (dsc)
335 ide_tape_handle_dsc(drive);
336
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100337 if (drive->failed_pc == pc)
338 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200339
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200340 if (pc->c[0] == REQUEST_SENSE) {
341 if (uptodate)
Borislav Petkovae3a8382009-05-02 10:58:17 +0200342 idetape_analyze_error(drive);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200343 else
344 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
345 "itself - Aborting request!\n");
346 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200347 unsigned int blocks =
348 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200349
350 tape->avg_size += blocks * tape->blk_size;
351
352 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
353 tape->avg_speed = tape->avg_size * HZ /
354 (jiffies - tape->avg_time) / 1024;
355 tape->avg_size = 0;
356 tape->avg_time = jiffies;
357 }
358
359 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200360
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100361 if (pc->error) {
362 uptodate = 0;
363 err = pc->error;
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100366 rq->errors = err;
367
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100368 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100372 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100373 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100375static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 idetape_tape_t *tape = drive->driver_data;
Borislav Petkove972d702009-07-17 23:55:16 +0000378 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Borislav Petkove972d702009-07-17 23:55:16 +0000380 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
381 rq->cmd[0], tape->dsc_poll_freq);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100382
Borislav Petkove972d702009-07-17 23:55:16 +0000383 tape->postponed_rq = rq;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100384
Borislav Petkov54bb2072008-02-06 02:57:52 +0100385 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200388static void ide_tape_handle_dsc(ide_drive_t *drive)
389{
390 idetape_tape_t *tape = drive->driver_data;
391
392 /* Media access command */
393 tape->dsc_polling_start = jiffies;
394 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
395 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
396 /* Allow ide.c to handle other requests */
397 idetape_postpone_request(drive);
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100401 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200403 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100404 * until we finish handling it. Each packet command is associated with a
405 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100407 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100409 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200410 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200412 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100413 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100415 * 3. ATAPI Tape media access commands have immediate status with a delayed
416 * process. In case of a successful initiation of a media access packet command,
417 * the DSC bit will be set when the actual execution of the command is finished.
418 * Since the tape drive will not issue an interrupt, we have to poll for this
419 * event. In this case, we define the request as "low priority request" by
420 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
421 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100423 * ide.c will then give higher priority to requests which originate from the
424 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100426 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100428 * 5. In case an error was found, we queue a request sense packet command in
429 * front of the request queue and retry the operation up to
430 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100432 * 6. In case no error was found, or we decided to give up and not to retry
433 * again, the callback function will be called and then we will handle the next
434 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100437static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
438 struct ide_cmd *cmd,
439 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200442 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100444 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
445 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200448 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200451 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100454 * We will "abort" retrying a packet command in case legitimate
455 * error code was received (crossing a filemark, or end of the
456 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200458 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100459 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 tape->sense_key == 2 && tape->asc == 4 &&
461 (tape->ascq == 1 || tape->ascq == 8))) {
462 printk(KERN_ERR "ide-tape: %s: I/O error, "
463 "pc = %2x, key = %2x, "
464 "asc = %2x, ascq = %2x\n",
465 tape->name, pc->c[0],
466 tape->sense_key, tape->asc,
467 tape->ascq);
468 }
469 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100470 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Tejun Heob3071d12009-04-19 08:46:02 +0900472
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100473 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200474 drive->pc_callback(drive, 0);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200475 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200476 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Borislav Petkove972d702009-07-17 23:55:16 +0000478 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
479 pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100483 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100486/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200487static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200489 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100490 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100492 /* DBD = 1 - Don't return block descriptors */
493 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 pc->c[2] = page_code;
495 /*
496 * Changed pc->c[3] to 0 (255 will at best return unused info).
497 *
498 * For SCSI this byte is defined as subpage instead of high byte
499 * of length and some IDE drives seem to interpret it this way
500 * and return an error when 255 is used.
501 */
502 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100503 /* We will just discard data in that case */
504 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200506 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200508 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200510 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100513static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200515 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200517 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100518 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200520 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100521
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200522 if (stat & ATA_DSC) {
523 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100525 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 printk(KERN_ERR "ide-tape: %s: I/O error, ",
527 tape->name);
528 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900529 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200530 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100534 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100535 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200537 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return ide_stopped;
539}
540
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200541static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200542 struct ide_atapi_pc *pc, struct request *rq,
543 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Borislav Petkov10c0b342009-05-01 20:29:53 +0200545 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
Borislav Petkov0014c752008-07-23 19:56:00 +0200546
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200547 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100548 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 pc->c[1] = 1;
Borislav Petkov19f52a72009-05-04 09:53:03 +0200550
551 if (blk_rq_bytes(rq) == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200552 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Tejun Heo963da552009-04-19 08:46:02 +0900554 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200555 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900556 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200557 pc->c[0] = WRITE_6;
558 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200559 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200560
561 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static ide_startstop_t idetape_do_request(ide_drive_t *drive,
565 struct request *rq, sector_t block)
566{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200567 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200569 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100571 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100572 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Borislav Petkove972d702009-07-17 23:55:16 +0000574 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
575 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
576 blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Bartlomiej Zolnierkiewicz2c7eaa42009-06-15 22:16:10 +0200578 BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100580 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100581 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
582 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200583 goto out;
584 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (postponed_rq != NULL)
587 if (rq != postponed_rq) {
588 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
589 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100590 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100591 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100592 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return ide_stopped;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 tape->postponed_rq = NULL;
597
598 /*
599 * If the tape is still busy, postpone our request and service
600 * the other device meanwhile.
601 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200602 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200604 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
605 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200606 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200608 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200609 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200610 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Borislav Petkov626542c2009-06-07 15:37:05 +0200613 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
614 !(stat & ATA_DSC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (postponed_rq == NULL) {
616 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100617 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
619 } else if (time_after(jiffies, tape->dsc_timeout)) {
620 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
621 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200622 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 idetape_media_access_finished(drive);
624 return ide_stopped;
625 } else {
626 return ide_do_reset(drive);
627 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100628 } else if (time_after(jiffies,
629 tape->dsc_polling_start +
630 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100631 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 idetape_postpone_request(drive);
633 return ide_stopped;
Borislav Petkov626542c2009-06-07 15:37:05 +0200634 } else
635 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
636
Borislav Petkov83dd5732008-07-23 19:56:00 +0200637 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200638 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200639 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto out;
641 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200642 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200643 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200644 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto out;
646 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200647 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900648 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200649 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
650 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto out;
652 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200653 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 idetape_media_access_finished(drive);
655 return ide_stopped;
656 }
657 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200658
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200659out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900660 /* prepare sense request for this command */
661 ide_prep_sense(drive, rq);
662
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100663 memset(&cmd, 0, sizeof(cmd));
664
665 if (rq_data_dir(rq))
666 cmd.tf_flags |= IDE_TFLAG_WRITE;
667
668 cmd.rq = rq;
669
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200670 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo5c4be572009-04-19 07:00:42 +0900671 ide_map_sg(drive, &cmd);
672
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100673 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100677 * Write a filemark if write_filemark=1. Flush the device buffers without
678 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100680static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200681 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200683 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100684 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200686 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
690{
691 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200692 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int load_attempted = 0;
694
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100695 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200696 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 timeout += jiffies;
698 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200699 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100702 || (tape->asc == 0x3A)) {
703 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (load_attempted)
705 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200706 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 load_attempted = 1;
708 /* not about to be ready */
709 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
710 (tape->ascq == 1 || tape->ascq == 8)))
711 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700712 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 return -EIO;
715}
716
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100717static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200719 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200720 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int rc;
722
723 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200724 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100725 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return rc;
727 idetape_wait_ready(drive, 60 * 5 * HZ);
728 return 0;
729}
730
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200731static int ide_tape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200734 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200735 u8 buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Borislav Petkove972d702009-07-17 23:55:16 +0000737 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200739 /* prep cmd */
740 ide_init_pc(&pc);
741 pc.c[0] = READ_POSITION;
742 pc.req_xfer = 20;
743
744 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -1;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200746
747 if (!pc.error) {
Borislav Petkove972d702009-07-17 23:55:16 +0000748 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200749 (buf[0] & 0x80) ? "Yes" : "No");
Borislav Petkove972d702009-07-17 23:55:16 +0000750 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200751 (buf[0] & 0x40) ? "Yes" : "No");
752
753 if (buf[0] & 0x4) {
754 printk(KERN_INFO "ide-tape: Block location is unknown"
755 "to the tape\n");
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200756 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
757 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200758 return -1;
759 } else {
Borislav Petkove972d702009-07-17 23:55:16 +0000760 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
761 be32_to_cpup((__be32 *)&buf[4]));
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200762
763 tape->partition = buf[1];
764 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200765 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
766 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200767 }
768 }
769
770 return tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Borislav Petkovd236d742008-04-18 00:46:27 +0200773static void idetape_create_locate_cmd(ide_drive_t *drive,
774 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100775 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200777 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100778 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100780 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200782 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200785static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Borislav Petkov54abf372008-02-06 02:57:52 +0100789 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200790 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Borislav Petkov49d80782009-06-07 15:37:06 +0200792 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900793 tape->valid = 0;
794 if (tape->buf != NULL) {
795 kfree(tape->buf);
796 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Borislav Petkov54abf372008-02-06 02:57:52 +0100799 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100803 * Position the tape to the requested block using the LOCATE packet command.
804 * A READ POSITION command is then issued to check where we are positioned. Like
805 * all higher level operations, we queue the commands at the tail of the request
806 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100808static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
809 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200812 struct gendisk *disk = tape->disk;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200813 int ret;
Borislav Petkovd236d742008-04-18 00:46:27 +0200814 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Borislav Petkov54abf372008-02-06 02:57:52 +0100816 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200817 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 idetape_wait_ready(drive, 60 * 5 * HZ);
819 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200820 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
821 if (ret)
822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200824 ret = ide_tape_read_position(drive);
825 if (ret < 0)
826 return ret;
827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200830static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100831 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int seek, position;
835
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200836 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (restore_position) {
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200838 position = ide_tape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200839 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100841 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200842 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return;
844 }
845 }
846}
847
848/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100849 * Generate a read/write request for the block device interface and wait for it
850 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900852static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200855 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900856 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Borislav Petkove972d702009-07-17 23:55:16 +0000858 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
859
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900860 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900861 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100862
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200863 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
864 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200865 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200866 rq->rq_disk = tape->disk;
Linus Torvaldsc9059592009-06-11 10:52:27 -0700867 rq->__sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900868
869 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900870 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900871 __GFP_WAIT);
872 if (ret)
873 goto out_put;
874 }
875
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200876 blk_execute_rq(drive->queue, tape->disk, rq, 0);
877
Tejun Heo963da552009-04-19 08:46:02 +0900878 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900879 size -= rq->resid_len;
Tejun Heo963da552009-04-19 08:46:02 +0900880 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900881 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900882 tape->valid = size;
883 else
884 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900886 ret = size;
887 if (rq->errors == IDE_DRV_ERROR_GENERAL)
888 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900889out_put:
890 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200891 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Borislav Petkovd236d742008-04-18 00:46:27 +0200894static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200896 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100897 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100898 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200899 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Borislav Petkovd236d742008-04-18 00:46:27 +0200902static void idetape_create_rewind_cmd(ide_drive_t *drive,
903 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200905 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100906 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200907 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Borislav Petkovd236d742008-04-18 00:46:27 +0200910static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200912 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100913 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200915 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Borislav Petkovd236d742008-04-18 00:46:27 +0200918static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200920 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100921 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100922 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200924 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Borislav Petkovd9df9372008-04-27 15:38:34 +0200927static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100930
Borislav Petkov54abf372008-02-06 02:57:52 +0100931 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200932 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100933 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return;
935 }
Tejun Heo963da552009-04-19 08:46:02 +0900936 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900937 size_t aligned = roundup(tape->valid, tape->blk_size);
938
939 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900940 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900941 kfree(tape->buf);
942 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100944 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Tejun Heo88f1b942009-04-19 08:46:02 +0900947static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900950 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Tejun Heo88f1b942009-04-19 08:46:02 +0900952 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Tejun Heo88f1b942009-04-19 08:46:02 +0900954 if (tape->chrdev_dir == dir)
955 return 0;
956
957 if (tape->chrdev_dir == IDETAPE_DIR_READ)
958 ide_tape_discard_merge_buffer(drive, 1);
959 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
960 ide_tape_flush_merge_buffer(drive);
961 idetape_flush_tape_buffers(drive);
962 }
963
964 if (tape->buf || tape->valid) {
965 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
966 tape->valid = 0;
967 }
968
969 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
970 if (!tape->buf)
971 return -ENOMEM;
972 tape->chrdev_dir = dir;
973 tape->cur = tape->buf;
974
975 /*
976 * Issue a 0 rw command to ensure that DSC handshake is
977 * switched from completion mode to buffer available mode. No
978 * point in issuing this if DSC overlap isn't supported, some
979 * drives (Seagate STT3401A) will return an error.
980 */
981 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
982 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
983 : REQ_IDETAPE_WRITE;
984
985 rc = idetape_queue_rw_tail(drive, cmd, 0);
986 if (rc < 0) {
987 kfree(tape->buf);
988 tape->buf = NULL;
989 tape->chrdev_dir = IDETAPE_DIR_NONE;
990 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +0200993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100997static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +09001000
1001 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +09001004 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Tejun Heo6bb11dd2009-04-19 08:46:03 +09001006 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001012 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1013 * currently support only one partition.
1014 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001015static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001017 struct ide_tape_obj *tape = drive->driver_data;
1018 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001019 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001020 int ret;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001021
Borislav Petkove972d702009-07-17 23:55:16 +00001022 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001025 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1026 if (ret)
1027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001029 ret = ide_tape_read_position(drive);
1030 if (ret < 0)
1031 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033}
1034
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001035/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001036static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1037 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 void __user *argp = (void __user *)arg;
1041
Borislav Petkovd59823f2008-02-02 19:56:51 +01001042 struct idetape_config {
1043 int dsc_rw_frequency;
1044 int dsc_media_access_frequency;
1045 int nr_stages;
1046 } config;
1047
Borislav Petkove972d702009-07-17 23:55:16 +00001048 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001051 case 0x0340:
1052 if (copy_from_user(&config, argp, sizeof(config)))
1053 return -EFAULT;
1054 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001055 break;
1056 case 0x0350:
Michael Buesch2fc21112009-07-19 09:15:19 +00001057 memset(&config, 0, sizeof(config));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001058 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001059 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001060 if (copy_to_user(argp, &config, sizeof(config)))
1061 return -EFAULT;
1062 break;
1063 default:
1064 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 return 0;
1067}
1068
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001069static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1070 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001073 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001074 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001075 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001076 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Borislav Petkove972d702009-07-17 23:55:16 +00001078
1079 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (mt_count == 0)
1082 return 0;
1083 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001084 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001086 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Borislav Petkov54abf372008-02-06 02:57:52 +01001089 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001090 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001091 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1092 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001094 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001098 case MTFSF:
1099 case MTBSF:
1100 idetape_create_space_cmd(&pc, mt_count - count,
1101 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001102 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001103 case MTFSFM:
1104 case MTBSFM:
1105 if (!sprev)
1106 return -EIO;
1107 retval = idetape_space_over_filemarks(drive, MTFSF,
1108 mt_count - count);
1109 if (retval)
1110 return retval;
1111 count = (MTBSFM == mt_op ? 1 : -1);
1112 return idetape_space_over_filemarks(drive, MTFSF, count);
1113 default:
1114 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1115 mt_op);
1116 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001121 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001123 * The tape is optimized to maximize throughput when it is transferring an
1124 * integral number of the "continuous transfer limit", which is a parameter of
1125 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001127 * As of version 1.3 of the driver, the character device provides an abstract
1128 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1129 * same backup/restore procedure is supported. The driver will internally
1130 * convert the requests to the recommended transfer unit, so that an unmatch
1131 * between the user's block size to the recommended size will only result in a
1132 * (slightly) increased driver overhead, but will no longer hit performance.
1133 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001135static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1136 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001138 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001140 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001141 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001142 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Borislav Petkove972d702009-07-17 23:55:16 +00001144 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Borislav Petkov54abf372008-02-06 02:57:52 +01001146 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001147 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001148 if (count > tape->blk_size &&
1149 (count % tape->blk_size) == 0)
1150 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001152
Tejun Heo88f1b942009-04-19 08:46:02 +09001153 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001154 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001156
1157 while (done < count) {
1158 size_t todo;
1159
1160 /* refill if staging buffer is empty */
1161 if (!tape->valid) {
1162 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001163 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1164 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001165 break;
1166 /* read */
1167 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1168 tape->buffer_size) <= 0)
1169 break;
1170 }
1171
1172 /* copy out */
1173 todo = min_t(size_t, count - done, tape->valid);
1174 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001175 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001176
1177 tape->cur += todo;
1178 tape->valid -= todo;
1179 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001181
Borislav Petkov49d80782009-06-07 15:37:06 +02001182 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 idetape_space_over_filemarks(drive, MTFSF, 1);
1184 return 0;
1185 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001186
Tejun Heo07bd9682009-04-19 08:46:03 +09001187 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001190static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 size_t count, loff_t *ppos)
1192{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001193 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001195 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001196 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001197 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /* The drive is write protected. */
1200 if (tape->write_prot)
1201 return -EACCES;
1202
Borislav Petkove972d702009-07-17 23:55:16 +00001203 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001206 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1207 if (rc < 0)
1208 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Tejun Heo07bd9682009-04-19 08:46:03 +09001210 while (done < count) {
1211 size_t todo;
1212
1213 /* flush if staging buffer is full */
1214 if (tape->valid == tape->buffer_size &&
1215 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1216 tape->buffer_size) <= 0)
1217 return rc;
1218
1219 /* copy in */
1220 todo = min_t(size_t, count - done,
1221 tape->buffer_size - tape->valid);
1222 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001223 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001224
1225 tape->cur += todo;
1226 tape->valid += todo;
1227 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001229
1230 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001233static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001235 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001236 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* Write a filemark */
1239 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001240 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1242 return -EIO;
1243 }
1244 return 0;
1245}
1246
1247/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001248 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1249 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001251 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1252 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001253 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001255 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001257 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1258 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001260static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001263 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001264 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001265 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Borislav Petkove972d702009-07-17 23:55:16 +00001267 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1268 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001271 case MTFSF:
1272 case MTFSFM:
1273 case MTBSF:
1274 case MTBSFM:
1275 if (!mt_count)
1276 return 0;
1277 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1278 default:
1279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001283 case MTWEOF:
1284 if (tape->write_prot)
1285 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001286 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001287 for (i = 0; i < mt_count; i++) {
1288 retval = idetape_write_filemark(drive);
1289 if (retval)
1290 return retval;
1291 }
1292 return 0;
1293 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001294 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001295 if (idetape_rewind_tape(drive))
1296 return -EIO;
1297 return 0;
1298 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001299 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001300 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001301 case MTUNLOAD:
1302 case MTOFFL:
1303 /*
1304 * If door is locked, attempt to unlock before
1305 * attempting to eject.
1306 */
1307 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001308 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001309 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001310 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001311 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001312 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001313 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001314 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1315 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001316 return retval;
1317 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001318 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001319 return idetape_flush_tape_buffers(drive);
1320 case MTRETEN:
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,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001323 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001324 case MTEOM:
1325 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001326 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001327 case MTERASE:
1328 (void)idetape_rewind_tape(drive);
1329 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001330 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001331 case MTSETBLK:
1332 if (mt_count) {
1333 if (mt_count < tape->blk_size ||
1334 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001336 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001337 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1338 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001340 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1341 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001342 return 0;
1343 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001344 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001345 return idetape_position_tape(drive,
1346 mt_count * tape->user_bs_factor, tape->partition, 0);
1347 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001348 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349 return idetape_position_tape(drive, 0, mt_count, 0);
1350 case MTFSR:
1351 case MTBSR:
1352 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001353 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001354 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001356 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1357 return 0;
1358 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001359 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001360 if (retval)
1361 return retval;
1362 tape->door_locked = DOOR_UNLOCKED;
1363 return 0;
1364 default:
1365 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1366 mt_op);
1367 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369}
1370
1371/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001372 * Our character device ioctls. General mtio.h magnetic io commands are
1373 * supported here, and not in the corresponding block interface. Our own
1374 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001376static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1377 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001379 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ide_drive_t *drive = tape->drive;
1381 struct mtop mtop;
1382 struct mtget mtget;
1383 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001384 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 void __user *argp = (void __user *)arg;
1386
Borislav Petkove972d702009-07-17 23:55:16 +00001387 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Borislav Petkov54abf372008-02-06 02:57:52 +01001389 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001390 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 idetape_flush_tape_buffers(drive);
1392 }
1393 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001394 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001395 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001396 position = ide_tape_read_position(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001397 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -EIO;
1399 }
1400 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001401 case MTIOCTOP:
1402 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1403 return -EFAULT;
1404 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1405 case MTIOCGET:
1406 memset(&mtget, 0, sizeof(struct mtget));
1407 mtget.mt_type = MT_ISSCSI2;
1408 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1409 mtget.mt_dsreg =
1410 ((tape->blk_size * tape->user_bs_factor)
1411 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001412
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001413 if (tape->drv_write_prot)
1414 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1415
1416 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1417 return -EFAULT;
1418 return 0;
1419 case MTIOCPOS:
1420 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1421 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1422 return -EFAULT;
1423 return 0;
1424 default:
1425 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001426 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001427 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429}
1430
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001431/*
1432 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1433 * block size with the reported value.
1434 */
1435static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1436{
1437 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001438 struct ide_atapi_pc pc;
Borislav Petkov837272b2009-05-04 09:48:57 +02001439 u8 buf[12];
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001440
1441 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkov837272b2009-05-04 09:48:57 +02001442 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001443 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001444 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001445 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1446 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001447 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001448 }
1449 return;
1450 }
Borislav Petkov837272b2009-05-04 09:48:57 +02001451 tape->blk_size = (buf[4 + 5] << 16) +
1452 (buf[4 + 6] << 8) +
1453 buf[4 + 7];
1454 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
Borislav Petkove972d702009-07-17 23:55:16 +00001455
1456 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1457 tape->blk_size, tape->drv_write_prot);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001460static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1463 ide_drive_t *drive;
1464 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int retval;
1466
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001467 if (i >= MAX_HWIFS * MAX_DRIVES)
1468 return -ENXIO;
1469
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001470 lock_kernel();
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001471 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001472 if (!tape) {
1473 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001474 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001475 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001476
Borislav Petkove972d702009-07-17 23:55:16 +00001477 drive = tape->drive;
1478 filp->private_data = tape;
1479
1480 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /*
1483 * We really want to do nonseekable_open(inode, filp); here, but some
1484 * versions of tar incorrectly call lseek on tapes and bail out if that
1485 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1486 */
1487 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Borislav Petkov49d80782009-06-07 15:37:06 +02001490 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 retval = -EBUSY;
1492 goto out_put_tape;
1493 }
1494
1495 retval = idetape_wait_ready(drive, 60 * HZ);
1496 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001497 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1499 goto out_put_tape;
1500 }
1501
Borislav Petkov79ca7432009-06-15 07:32:04 +02001502 ide_tape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001503 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 (void)idetape_rewind_tape(drive);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001507 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /* Set write protect flag if device is opened as read-only. */
1510 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1511 tape->write_prot = 1;
1512 else
1513 tape->write_prot = tape->drv_write_prot;
1514
1515 /* Make sure drive isn't write protected if user wants to write. */
1516 if (tape->write_prot) {
1517 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1518 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001519 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 retval = -EROFS;
1521 goto out_put_tape;
1522 }
1523 }
1524
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001525 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001526 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001527 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001528 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1529 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001532 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return 0;
1534
1535out_put_tape:
1536 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001537 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return retval;
1539}
1540
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001541static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 idetape_tape_t *tape = drive->driver_data;
1544
Borislav Petkovd9df9372008-04-27 15:38:34 +02001545 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001546 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1547 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001548 idetape_pad_zeros(drive, tape->blk_size *
1549 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001550 kfree(tape->buf);
1551 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 idetape_write_filemark(drive);
1554 idetape_flush_tape_buffers(drive);
1555 idetape_flush_tape_buffers(drive);
1556}
1557
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001558static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001560 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 unsigned int minor = iminor(inode);
1563
1564 lock_kernel();
1565 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001566
Borislav Petkove972d702009-07-17 23:55:16 +00001567 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Borislav Petkov54abf372008-02-06 02:57:52 +01001569 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001571 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001573 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001575
Borislav Petkov49d80782009-06-07 15:37:06 +02001576 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1577 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001579
Borislav Petkov54abf372008-02-06 02:57:52 +01001580 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001582 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001583 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001586 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 ide_tape_put(tape);
1588 unlock_kernel();
1589 return 0;
1590}
1591
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001592static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001595 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001596 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001597 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 idetape_create_inquiry_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001600 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001601 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1602 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return;
1604 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001605 memcpy(vendor_id, &pc_buf[8], 8);
1606 memcpy(product_id, &pc_buf[16], 16);
1607 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001608
Borislav Petkov801bd322008-09-27 19:32:17 +02001609 ide_fixstring(vendor_id, 8, 0);
1610 ide_fixstring(product_id, 16, 0);
1611 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001612
Borislav Petkov801bd322008-09-27 19:32:17 +02001613 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001614 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001618 * Ask the tape about its various parameters. In particular, we will adjust our
1619 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001624 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001625 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001626 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001629 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001630 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1631 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001632 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001633 put_unaligned(52, (u16 *)&tape->caps[12]);
1634 put_unaligned(540, (u16 *)&tape->caps[14]);
1635 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return;
1637 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001638 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Borislav Petkovb6422012008-02-02 19:56:49 +01001640 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001641 speed = be16_to_cpup((__be16 *)&caps[14]);
1642 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001644 *(u16 *)&caps[8] = max_speed;
1645 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1646 *(u16 *)&caps[14] = speed;
1647 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001648
1649 if (!speed) {
1650 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1651 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001652 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001654 if (!max_speed) {
1655 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1656 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001657 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Borislav Petkovb6422012008-02-02 19:56:49 +01001660 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001661
1662 /* device lacks locking support according to capabilities page */
1663 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001664 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001665
Borislav Petkovb6422012008-02-02 19:56:49 +01001666 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001667 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001668 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001669 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001672#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001673#define ide_tape_devset_get(name, field) \
1674static int get_##name(ide_drive_t *drive) \
1675{ \
1676 idetape_tape_t *tape = drive->driver_data; \
1677 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001679
1680#define ide_tape_devset_set(name, field) \
1681static int set_##name(ide_drive_t *drive, int arg) \
1682{ \
1683 idetape_tape_t *tape = drive->driver_data; \
1684 tape->field = arg; \
1685 return 0; \
1686}
1687
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001688#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001689ide_tape_devset_get(_name, _field) \
1690ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001691IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001692
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001693#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001694ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001695IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001696
1697static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1698static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1699static int divf_buffer(ide_drive_t *drive) { return 2; }
1700static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1701
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001702ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001703
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001704ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001705
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001706ide_tape_devset_r_field(avg_speed, avg_speed);
1707ide_tape_devset_r_field(speed, caps[14]);
1708ide_tape_devset_r_field(buffer, caps[16]);
1709ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001710
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001711static const struct ide_proc_devset idetape_settings[] = {
1712 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1713 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1714 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001715 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1716 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1717 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1718 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001719 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001720};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001721#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001724 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001726 * 1. Initialize our various state variables.
1727 * 2. Ask the tape for its capabilities.
1728 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1729 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001731 * Note that at this point ide.c already assigned us an irq, so that we can
1732 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001734static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Borislav Petkov83042b22008-04-27 15:38:27 +02001736 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001738 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001739 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Borislav Petkove972d702009-07-17 23:55:16 +00001741 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1742
1743 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001744
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001745 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1746
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001747 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1748 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1749 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001750 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001754 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001755 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 tape->minor = minor;
1758 tape->name[0] = 'h';
1759 tape->name[1] = 't';
1760 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001761 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 idetape_get_inquiry_results(drive);
1764 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001765 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001767 tape->buffer_size = *ctl * tape->blk_size;
1768 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001770 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001771 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001773 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Borislav Petkov83042b22008-04-27 15:38:27 +02001775 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001776 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Borislav Petkovf73850a2008-04-27 15:38:33 +02001778 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001781 * Ensure that the number we got makes sense; limit it within
1782 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001784 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1785 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001787 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001788 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001789 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1790 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001791 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001792 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001794 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Russell King4031bbe2006-01-06 11:41:00 +00001797static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001801 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001802 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 ide_unregister_region(tape->disk);
1804
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001805 mutex_lock(&idetape_ref_mutex);
1806 put_device(&tape->dev);
1807 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001810static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001812 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ide_drive_t *drive = tape->drive;
1814 struct gendisk *g = tape->disk;
1815
Tejun Heo963da552009-04-19 08:46:02 +09001816 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001817
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001818 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001820 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001821 device_destroy(idetape_sysfs_class,
1822 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 idetape_devs[tape->minor] = NULL;
1824 g->private_data = NULL;
1825 put_disk(g);
1826 kfree(tape);
1827}
1828
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001829#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830static int proc_idetape_read_name
1831 (char *page, char **start, off_t off, int count, int *eof, void *data)
1832{
1833 ide_drive_t *drive = (ide_drive_t *) data;
1834 idetape_tape_t *tape = drive->driver_data;
1835 char *out = page;
1836 int len;
1837
1838 len = sprintf(out, "%s\n", tape->name);
1839 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1840}
1841
1842static ide_proc_entry_t idetape_proc[] = {
1843 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1844 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1845 { NULL, 0, NULL, NULL }
1846};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001847
1848static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1849{
1850 return idetape_proc;
1851}
1852
1853static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1854{
1855 return idetape_settings;
1856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#endif
1858
Russell King4031bbe2006-01-06 11:41:00 +00001859static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001861static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001862 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001863 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001864 .name = "ide-tape",
1865 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001866 },
Russell King4031bbe2006-01-06 11:41:00 +00001867 .probe = ide_tape_probe,
1868 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001871#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001872 .proc_entries = ide_tape_proc_entries,
1873 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875};
1876
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001877/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001878static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .owner = THIS_MODULE,
1880 .read = idetape_chrdev_read,
1881 .write = idetape_chrdev_write,
1882 .ioctl = idetape_chrdev_ioctl,
1883 .open = idetape_chrdev_open,
1884 .release = idetape_chrdev_release,
1885};
1886
Al Viroa4600f82008-03-02 10:27:58 -05001887static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001889 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001891 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return -ENXIO;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return 0;
1895}
1896
Al Viroa4600f82008-03-02 10:27:58 -05001897static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001899 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 0;
1903}
1904
Al Viroa4600f82008-03-02 10:27:58 -05001905static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 unsigned int cmd, unsigned long arg)
1907{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001908 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04001910 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (err == -EINVAL)
1912 err = idetape_blkdev_ioctl(drive, cmd, arg);
1913 return err;
1914}
1915
1916static struct block_device_operations idetape_block_ops = {
1917 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001918 .open = idetape_open,
1919 .release = idetape_release,
1920 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921};
1922
Russell King4031bbe2006-01-06 11:41:00 +00001923static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 idetape_tape_t *tape;
1926 struct gendisk *g;
1927 int minor;
1928
Borislav Petkove972d702009-07-17 23:55:16 +00001929 ide_debug_log(IDE_DBG_FUNC, "enter");
1930
1931 if (!strstr(DRV_NAME, drive->driver_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (drive->media != ide_tape)
1935 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001936
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001937 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1938 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001939 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1940 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto failed;
1942 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001943 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001945 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1946 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto failed;
1948 }
1949
1950 g = alloc_disk(1 << PARTN_BITS);
1951 if (!g)
1952 goto out_free_tape;
1953
1954 ide_init_disk(g, drive);
1955
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001956 tape->dev.parent = &drive->gendev;
1957 tape->dev.release = ide_tape_release;
1958 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1959
1960 if (device_register(&tape->dev))
1961 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 tape->drive = drive;
1964 tape->driver = &idetape_driver;
1965 tape->disk = g;
1966
1967 g->private_data = &tape->driver;
1968
1969 drive->driver_data = tape;
1970
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001971 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 for (minor = 0; idetape_devs[minor]; minor++)
1973 ;
1974 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001975 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 idetape_setup(drive, tape, minor);
1978
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07001979 device_create(idetape_sysfs_class, &drive->gendev,
1980 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1981 device_create(idetape_sysfs_class, &drive->gendev,
1982 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
1983 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 g->fops = &idetape_block_ops;
1986 ide_register_region(g);
1987
1988 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001989
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001990out_free_disk:
1991 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992out_free_tape:
1993 kfree(tape);
1994failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001995 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001998static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002000 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002001 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 unregister_chrdev(IDETAPE_MAJOR, "ht");
2003}
2004
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002005static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Will Dysond5dee802005-09-16 02:55:07 -07002007 int error = 1;
2008 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2009 if (IS_ERR(idetape_sysfs_class)) {
2010 idetape_sysfs_class = NULL;
2011 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2012 error = -EBUSY;
2013 goto out;
2014 }
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002017 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2018 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002019 error = -EBUSY;
2020 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Will Dysond5dee802005-09-16 02:55:07 -07002022
2023 error = driver_register(&idetape_driver.gen_driver);
2024 if (error)
2025 goto out_free_driver;
2026
2027 return 0;
2028
2029out_free_driver:
2030 driver_unregister(&idetape_driver.gen_driver);
2031out_free_class:
2032 class_destroy(idetape_sysfs_class);
2033out:
2034 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Kay Sievers263756e2005-12-12 18:03:44 +01002037MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038module_init(idetape_init);
2039module_exit(idetape_exit);
2040MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002041MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2042MODULE_LICENSE("GPL");