blob: 7b2032bc357b1f5872fc7422796681c2387d8853 [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 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700158 bool postponed_rq;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* The time in which we started polling for DSC */
161 unsigned long dsc_polling_start;
162 /* Timer used to poll for dsc */
163 struct timer_list dsc_timer;
164 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100165 unsigned long best_dsc_rw_freq;
166 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned long dsc_timeout;
168
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100169 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 u8 partition;
171 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100172 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100174 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 u8 sense_key, asc, ascq;
176
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100177 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int minor;
179 /* device name */
180 char name[4];
181 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100182 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Borislav Petkov54bb2072008-02-06 02:57:52 +0100184 /* tape block size, usually 512 or 1024 bytes */
185 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100189 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100192 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100194 * At most, there is only one ide-tape originated data transfer request
195 * in the device request queue. This allows ide.c to easily service
196 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200198
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100199 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200200 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900201 /* Staging buffer of buffer_size bytes */
202 void *buf;
203 /* The read/write cursor */
204 void *cur;
205 /* The number of valid bytes in buf */
206 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100207
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 unsigned long avg_time;
210 int avg_size;
211 int avg_speed;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* the door is currently locked */
214 int door_locked;
215 /* the tape hardware is write protected */
216 char drv_write_prot;
217 /* the tape is write protected (hardware or opened as read-only) */
218 char write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219} idetape_tape_t;
220
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800221static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Will Dysond5dee802005-09-16 02:55:07 -0700223static struct class *idetape_sysfs_class;
224
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100225static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200226
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200227static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
228
229static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
230 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct ide_tape_obj *tape = NULL;
233
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800234 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200235
236 if (cdev)
237 tape = idetape_devs[i];
238 else
239 tape = ide_drv_g(disk, ide_tape_obj);
240
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200241 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200242 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200243 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200244 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100245 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200246 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200247
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800248 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return tape;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static void ide_tape_put(struct ide_tape_obj *tape)
253{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200254 ide_drive_t *drive = tape->drive;
255
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800256 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100257 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200258 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800259 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100263 * called on each failed packet command retry to analyze the request sense. We
264 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Borislav Petkovae3a8382009-05-02 10:58:17 +0200266static void idetape_analyze_error(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100269 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200270 struct request *rq = drive->hwif->rq;
Borislav Petkovae3a8382009-05-02 10:58:17 +0200271 u8 *sense = bio_data(rq->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Borislav Petkov1b5db432008-02-02 19:56:48 +0100273 tape->sense_key = sense[2] & 0xF;
274 tape->asc = sense[12];
275 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100276
Borislav Petkove972d702009-07-17 23:55:16 +0000277 ide_debug_log(IDE_DBG_FUNC,
278 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
279 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Borislav Petkov077e6db2009-05-01 21:21:02 +0200281 /* correct remaining bytes to transfer */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900282 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200283 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * If error was the result of a zero-length read or write command,
287 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
288 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
289 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100290 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100291 /* length == 0 */
292 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
293 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* don't report an error, everything's ok */
295 pc->error = 0;
296 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200297 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100300 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100301 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200302 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100304 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100305 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
306 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100307 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200308 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100311 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100312 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100313 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200314 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200316 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200317 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
319 }
320}
321
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200322static void ide_tape_handle_dsc(ide_drive_t *);
323
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100324static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200327 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100328 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200329 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100330 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Borislav Petkove972d702009-07-17 23:55:16 +0000332 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
333 dsc, err);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100334
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200335 if (dsc)
336 ide_tape_handle_dsc(drive);
337
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100338 if (drive->failed_pc == pc)
339 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200340
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200341 if (pc->c[0] == REQUEST_SENSE) {
342 if (uptodate)
Borislav Petkovae3a8382009-05-02 10:58:17 +0200343 idetape_analyze_error(drive);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200344 else
345 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
346 "itself - Aborting request!\n");
347 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200348 unsigned int blocks =
349 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200350
351 tape->avg_size += blocks * tape->blk_size;
352
353 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
354 tape->avg_speed = tape->avg_size * HZ /
355 (jiffies - tape->avg_time) / 1024;
356 tape->avg_size = 0;
357 tape->avg_time = jiffies;
358 }
359
360 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200361
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100362 if (pc->error) {
363 uptodate = 0;
364 err = pc->error;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100367 rq->errors = err;
368
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100369 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100373 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100374 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700376static void ide_tape_stall_queue(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 idetape_tape_t *tape = drive->driver_data;
379
Borislav Petkove972d702009-07-17 23:55:16 +0000380 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700381 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100382
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700383 tape->postponed_rq = true;
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 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700397 ide_tape_stall_queue(drive);
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200398}
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;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100570 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100571 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Borislav Petkove972d702009-07-17 23:55:16 +0000573 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
574 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
575 blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Bartlomiej Zolnierkiewicz2c7eaa42009-06-15 22:16:10 +0200577 BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100579 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100580 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
581 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200582 goto out;
583 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /*
586 * If the tape is still busy, postpone our request and service
587 * the other device meanwhile.
588 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200589 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200591 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
592 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200593 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200595 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200596 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200597 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Borislav Petkov626542c2009-06-07 15:37:05 +0200600 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
601 !(stat & ATA_DSC)) {
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700602 if (!tape->postponed_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100604 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
606 } else if (time_after(jiffies, tape->dsc_timeout)) {
607 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
608 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200609 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 idetape_media_access_finished(drive);
611 return ide_stopped;
612 } else {
613 return ide_do_reset(drive);
614 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100615 } else if (time_after(jiffies,
616 tape->dsc_polling_start +
617 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100618 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700619 ide_tape_stall_queue(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return ide_stopped;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700621 } else {
Borislav Petkov626542c2009-06-07 15:37:05 +0200622 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700623 tape->postponed_rq = false;
624 }
Borislav Petkov626542c2009-06-07 15:37:05 +0200625
Borislav Petkov83dd5732008-07-23 19:56:00 +0200626 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200627 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200628 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto out;
630 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200631 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200632 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200633 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto out;
635 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200636 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900637 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200638 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
639 rq->cmd[13] |= REQ_IDETAPE_PC2;
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_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 idetape_media_access_finished(drive);
644 return ide_stopped;
645 }
646 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200647
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200648out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900649 /* prepare sense request for this command */
650 ide_prep_sense(drive, rq);
651
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100652 memset(&cmd, 0, sizeof(cmd));
653
654 if (rq_data_dir(rq))
655 cmd.tf_flags |= IDE_TFLAG_WRITE;
656
657 cmd.rq = rq;
658
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200659 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo5c4be572009-04-19 07:00:42 +0900660 ide_map_sg(drive, &cmd);
661
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100662 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100666 * Write a filemark if write_filemark=1. Flush the device buffers without
667 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100669static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200670 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200672 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100673 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200675 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
679{
680 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200681 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int load_attempted = 0;
683
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100684 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200685 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 timeout += jiffies;
687 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200688 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100691 || (tape->asc == 0x3A)) {
692 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (load_attempted)
694 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200695 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 load_attempted = 1;
697 /* not about to be ready */
698 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
699 (tape->ascq == 1 || tape->ascq == 8)))
700 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700701 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 return -EIO;
704}
705
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100706static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200708 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200709 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int rc;
711
712 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200713 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100714 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return rc;
716 idetape_wait_ready(drive, 60 * 5 * HZ);
717 return 0;
718}
719
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200720static int ide_tape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200723 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200724 u8 buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Borislav Petkove972d702009-07-17 23:55:16 +0000726 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200728 /* prep cmd */
729 ide_init_pc(&pc);
730 pc.c[0] = READ_POSITION;
731 pc.req_xfer = 20;
732
733 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -1;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200735
736 if (!pc.error) {
Borislav Petkove972d702009-07-17 23:55:16 +0000737 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200738 (buf[0] & 0x80) ? "Yes" : "No");
Borislav Petkove972d702009-07-17 23:55:16 +0000739 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200740 (buf[0] & 0x40) ? "Yes" : "No");
741
742 if (buf[0] & 0x4) {
743 printk(KERN_INFO "ide-tape: Block location is unknown"
744 "to the tape\n");
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200745 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
746 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200747 return -1;
748 } else {
Borislav Petkove972d702009-07-17 23:55:16 +0000749 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
750 be32_to_cpup((__be32 *)&buf[4]));
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200751
752 tape->partition = buf[1];
753 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200754 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
755 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200756 }
757 }
758
759 return tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Borislav Petkovd236d742008-04-18 00:46:27 +0200762static void idetape_create_locate_cmd(ide_drive_t *drive,
763 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100764 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200766 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100767 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100769 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200771 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200774static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Borislav Petkov54abf372008-02-06 02:57:52 +0100778 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200779 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Borislav Petkov49d80782009-06-07 15:37:06 +0200781 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900782 tape->valid = 0;
783 if (tape->buf != NULL) {
784 kfree(tape->buf);
785 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Borislav Petkov54abf372008-02-06 02:57:52 +0100788 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100792 * Position the tape to the requested block using the LOCATE packet command.
793 * A READ POSITION command is then issued to check where we are positioned. Like
794 * all higher level operations, we queue the commands at the tail of the request
795 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100797static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
798 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200801 struct gendisk *disk = tape->disk;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200802 int ret;
Borislav Petkovd236d742008-04-18 00:46:27 +0200803 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Borislav Petkov54abf372008-02-06 02:57:52 +0100805 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200806 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 idetape_wait_ready(drive, 60 * 5 * HZ);
808 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200809 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
810 if (ret)
811 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200813 ret = ide_tape_read_position(drive);
814 if (ret < 0)
815 return ret;
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200819static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100820 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int seek, position;
824
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200825 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (restore_position) {
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200827 position = ide_tape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200828 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100830 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200831 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return;
833 }
834 }
835}
836
837/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100838 * Generate a read/write request for the block device interface and wait for it
839 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900841static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200844 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900845 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Borislav Petkove972d702009-07-17 23:55:16 +0000847 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
848
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900849 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900850 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100851
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200852 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
853 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200854 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200855 rq->rq_disk = tape->disk;
Linus Torvaldsc9059592009-06-11 10:52:27 -0700856 rq->__sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900857
858 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900859 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900860 __GFP_WAIT);
861 if (ret)
862 goto out_put;
863 }
864
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200865 blk_execute_rq(drive->queue, tape->disk, rq, 0);
866
Tejun Heo963da552009-04-19 08:46:02 +0900867 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900868 size -= rq->resid_len;
Tejun Heo963da552009-04-19 08:46:02 +0900869 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900870 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900871 tape->valid = size;
872 else
873 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900875 ret = size;
876 if (rq->errors == IDE_DRV_ERROR_GENERAL)
877 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900878out_put:
879 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200880 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Borislav Petkovd236d742008-04-18 00:46:27 +0200883static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200885 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100886 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100887 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200888 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Borislav Petkovd236d742008-04-18 00:46:27 +0200891static void idetape_create_rewind_cmd(ide_drive_t *drive,
892 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200894 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100895 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200896 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Borislav Petkovd236d742008-04-18 00:46:27 +0200899static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200901 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100902 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200904 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Borislav Petkovd236d742008-04-18 00:46:27 +0200907static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200909 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100910 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100911 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200913 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Borislav Petkovd9df9372008-04-27 15:38:34 +0200916static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100919
Borislav Petkov54abf372008-02-06 02:57:52 +0100920 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200921 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100922 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return;
924 }
Tejun Heo963da552009-04-19 08:46:02 +0900925 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900926 size_t aligned = roundup(tape->valid, tape->blk_size);
927
928 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900929 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900930 kfree(tape->buf);
931 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100933 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Tejun Heo88f1b942009-04-19 08:46:02 +0900936static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900939 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Tejun Heo88f1b942009-04-19 08:46:02 +0900941 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Tejun Heo88f1b942009-04-19 08:46:02 +0900943 if (tape->chrdev_dir == dir)
944 return 0;
945
946 if (tape->chrdev_dir == IDETAPE_DIR_READ)
947 ide_tape_discard_merge_buffer(drive, 1);
948 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
949 ide_tape_flush_merge_buffer(drive);
950 idetape_flush_tape_buffers(drive);
951 }
952
953 if (tape->buf || tape->valid) {
954 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
955 tape->valid = 0;
956 }
957
958 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
959 if (!tape->buf)
960 return -ENOMEM;
961 tape->chrdev_dir = dir;
962 tape->cur = tape->buf;
963
964 /*
965 * Issue a 0 rw command to ensure that DSC handshake is
966 * switched from completion mode to buffer available mode. No
967 * point in issuing this if DSC overlap isn't supported, some
968 * drives (Seagate STT3401A) will return an error.
969 */
970 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
971 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
972 : REQ_IDETAPE_WRITE;
973
974 rc = idetape_queue_rw_tail(drive, cmd, 0);
975 if (rc < 0) {
976 kfree(tape->buf);
977 tape->buf = NULL;
978 tape->chrdev_dir = IDETAPE_DIR_NONE;
979 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +0200982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 0;
984}
985
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100986static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +0900989
990 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +0900993 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900995 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001001 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1002 * currently support only one partition.
1003 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001004static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001006 struct ide_tape_obj *tape = drive->driver_data;
1007 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001008 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001009 int ret;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001010
Borislav Petkove972d702009-07-17 23:55:16 +00001011 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001014 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1015 if (ret)
1016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001018 ret = ide_tape_read_position(drive);
1019 if (ret < 0)
1020 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022}
1023
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001024/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001025static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1026 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 void __user *argp = (void __user *)arg;
1030
Borislav Petkovd59823f2008-02-02 19:56:51 +01001031 struct idetape_config {
1032 int dsc_rw_frequency;
1033 int dsc_media_access_frequency;
1034 int nr_stages;
1035 } config;
1036
Borislav Petkove972d702009-07-17 23:55:16 +00001037 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001040 case 0x0340:
1041 if (copy_from_user(&config, argp, sizeof(config)))
1042 return -EFAULT;
1043 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001044 break;
1045 case 0x0350:
Michael Buesch2fc21112009-07-19 09:15:19 +00001046 memset(&config, 0, sizeof(config));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001047 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001048 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001049 if (copy_to_user(argp, &config, sizeof(config)))
1050 return -EFAULT;
1051 break;
1052 default:
1053 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055 return 0;
1056}
1057
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001058static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1059 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001062 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001063 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001064 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001065 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Borislav Petkove972d702009-07-17 23:55:16 +00001067
1068 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (mt_count == 0)
1071 return 0;
1072 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001073 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001075 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
Borislav Petkov54abf372008-02-06 02:57:52 +01001078 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001079 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001080 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1081 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001083 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001087 case MTFSF:
1088 case MTBSF:
1089 idetape_create_space_cmd(&pc, mt_count - count,
1090 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001091 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001092 case MTFSFM:
1093 case MTBSFM:
1094 if (!sprev)
1095 return -EIO;
1096 retval = idetape_space_over_filemarks(drive, MTFSF,
1097 mt_count - count);
1098 if (retval)
1099 return retval;
1100 count = (MTBSFM == mt_op ? 1 : -1);
1101 return idetape_space_over_filemarks(drive, MTFSF, count);
1102 default:
1103 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1104 mt_op);
1105 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001110 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001112 * The tape is optimized to maximize throughput when it is transferring an
1113 * integral number of the "continuous transfer limit", which is a parameter of
1114 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001116 * As of version 1.3 of the driver, the character device provides an abstract
1117 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1118 * same backup/restore procedure is supported. The driver will internally
1119 * convert the requests to the recommended transfer unit, so that an unmatch
1120 * between the user's block size to the recommended size will only result in a
1121 * (slightly) increased driver overhead, but will no longer hit performance.
1122 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001124static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1125 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001127 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001129 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001130 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001131 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Borislav Petkove972d702009-07-17 23:55:16 +00001133 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Borislav Petkov54abf372008-02-06 02:57:52 +01001135 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001136 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001137 if (count > tape->blk_size &&
1138 (count % tape->blk_size) == 0)
1139 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001141
Tejun Heo88f1b942009-04-19 08:46:02 +09001142 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001143 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001145
1146 while (done < count) {
1147 size_t todo;
1148
1149 /* refill if staging buffer is empty */
1150 if (!tape->valid) {
1151 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001152 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1153 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001154 break;
1155 /* read */
1156 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1157 tape->buffer_size) <= 0)
1158 break;
1159 }
1160
1161 /* copy out */
1162 todo = min_t(size_t, count - done, tape->valid);
1163 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001164 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001165
1166 tape->cur += todo;
1167 tape->valid -= todo;
1168 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001170
Borislav Petkov49d80782009-06-07 15:37:06 +02001171 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 idetape_space_over_filemarks(drive, MTFSF, 1);
1173 return 0;
1174 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001175
Tejun Heo07bd9682009-04-19 08:46:03 +09001176 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001179static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 size_t count, loff_t *ppos)
1181{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001182 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001184 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001185 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001186 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* The drive is write protected. */
1189 if (tape->write_prot)
1190 return -EACCES;
1191
Borislav Petkove972d702009-07-17 23:55:16 +00001192 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001195 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1196 if (rc < 0)
1197 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Tejun Heo07bd9682009-04-19 08:46:03 +09001199 while (done < count) {
1200 size_t todo;
1201
1202 /* flush if staging buffer is full */
1203 if (tape->valid == tape->buffer_size &&
1204 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1205 tape->buffer_size) <= 0)
1206 return rc;
1207
1208 /* copy in */
1209 todo = min_t(size_t, count - done,
1210 tape->buffer_size - tape->valid);
1211 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001212 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001213
1214 tape->cur += todo;
1215 tape->valid += todo;
1216 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001218
1219 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001222static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001224 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001225 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* Write a filemark */
1228 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001229 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1231 return -EIO;
1232 }
1233 return 0;
1234}
1235
1236/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001237 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1238 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001240 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1241 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001242 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001244 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001246 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1247 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001249static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001252 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001253 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001254 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Borislav Petkove972d702009-07-17 23:55:16 +00001256 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1257 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001260 case MTFSF:
1261 case MTFSFM:
1262 case MTBSF:
1263 case MTBSFM:
1264 if (!mt_count)
1265 return 0;
1266 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1267 default:
1268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001272 case MTWEOF:
1273 if (tape->write_prot)
1274 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001275 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001276 for (i = 0; i < mt_count; i++) {
1277 retval = idetape_write_filemark(drive);
1278 if (retval)
1279 return retval;
1280 }
1281 return 0;
1282 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001283 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001284 if (idetape_rewind_tape(drive))
1285 return -EIO;
1286 return 0;
1287 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001288 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001289 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001290 case MTUNLOAD:
1291 case MTOFFL:
1292 /*
1293 * If door is locked, attempt to unlock before
1294 * attempting to eject.
1295 */
1296 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001297 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001298 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001299 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001300 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001301 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001302 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001303 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1304 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001305 return retval;
1306 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001307 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001308 return idetape_flush_tape_buffers(drive);
1309 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001310 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001311 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001313 case MTEOM:
1314 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001315 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001316 case MTERASE:
1317 (void)idetape_rewind_tape(drive);
1318 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001319 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001320 case MTSETBLK:
1321 if (mt_count) {
1322 if (mt_count < tape->blk_size ||
1323 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001325 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001326 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1327 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001328 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001329 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1330 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001331 return 0;
1332 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001333 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001334 return idetape_position_tape(drive,
1335 mt_count * tape->user_bs_factor, tape->partition, 0);
1336 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001337 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001338 return idetape_position_tape(drive, 0, mt_count, 0);
1339 case MTFSR:
1340 case MTBSR:
1341 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001342 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001343 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001345 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1346 return 0;
1347 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001348 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349 if (retval)
1350 return retval;
1351 tape->door_locked = DOOR_UNLOCKED;
1352 return 0;
1353 default:
1354 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1355 mt_op);
1356 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358}
1359
1360/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001361 * Our character device ioctls. General mtio.h magnetic io commands are
1362 * supported here, and not in the corresponding block interface. Our own
1363 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001365static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1366 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001368 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ide_drive_t *drive = tape->drive;
1370 struct mtop mtop;
1371 struct mtget mtget;
1372 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001373 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 void __user *argp = (void __user *)arg;
1375
Borislav Petkove972d702009-07-17 23:55:16 +00001376 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Borislav Petkov54abf372008-02-06 02:57:52 +01001378 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001379 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 idetape_flush_tape_buffers(drive);
1381 }
1382 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001383 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001384 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001385 position = ide_tape_read_position(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001386 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EIO;
1388 }
1389 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001390 case MTIOCTOP:
1391 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1392 return -EFAULT;
1393 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1394 case MTIOCGET:
1395 memset(&mtget, 0, sizeof(struct mtget));
1396 mtget.mt_type = MT_ISSCSI2;
1397 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1398 mtget.mt_dsreg =
1399 ((tape->blk_size * tape->user_bs_factor)
1400 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001401
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001402 if (tape->drv_write_prot)
1403 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1404
1405 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1406 return -EFAULT;
1407 return 0;
1408 case MTIOCPOS:
1409 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1410 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1411 return -EFAULT;
1412 return 0;
1413 default:
1414 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001415 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001416 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418}
1419
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001420/*
1421 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1422 * block size with the reported value.
1423 */
1424static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1425{
1426 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001427 struct ide_atapi_pc pc;
Borislav Petkov837272b2009-05-04 09:48:57 +02001428 u8 buf[12];
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001429
1430 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkov837272b2009-05-04 09:48:57 +02001431 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001432 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001433 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001434 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1435 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001436 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001437 }
1438 return;
1439 }
Borislav Petkov837272b2009-05-04 09:48:57 +02001440 tape->blk_size = (buf[4 + 5] << 16) +
1441 (buf[4 + 6] << 8) +
1442 buf[4 + 7];
1443 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
Borislav Petkove972d702009-07-17 23:55:16 +00001444
1445 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1446 tape->blk_size, tape->drv_write_prot);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001449static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1452 ide_drive_t *drive;
1453 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int retval;
1455
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001456 if (i >= MAX_HWIFS * MAX_DRIVES)
1457 return -ENXIO;
1458
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001459 lock_kernel();
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001460 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001461 if (!tape) {
1462 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001463 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001464 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001465
Borislav Petkove972d702009-07-17 23:55:16 +00001466 drive = tape->drive;
1467 filp->private_data = tape;
1468
1469 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * We really want to do nonseekable_open(inode, filp); here, but some
1473 * versions of tar incorrectly call lseek on tapes and bail out if that
1474 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1475 */
1476 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Borislav Petkov49d80782009-06-07 15:37:06 +02001479 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 retval = -EBUSY;
1481 goto out_put_tape;
1482 }
1483
1484 retval = idetape_wait_ready(drive, 60 * HZ);
1485 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001486 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1488 goto out_put_tape;
1489 }
1490
Borislav Petkov79ca7432009-06-15 07:32:04 +02001491 ide_tape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001492 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 (void)idetape_rewind_tape(drive);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001496 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /* Set write protect flag if device is opened as read-only. */
1499 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1500 tape->write_prot = 1;
1501 else
1502 tape->write_prot = tape->drv_write_prot;
1503
1504 /* Make sure drive isn't write protected if user wants to write. */
1505 if (tape->write_prot) {
1506 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1507 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001508 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 retval = -EROFS;
1510 goto out_put_tape;
1511 }
1512 }
1513
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001514 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001515 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001516 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001517 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1518 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001521 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 0;
1523
1524out_put_tape:
1525 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001526 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return retval;
1528}
1529
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001530static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 idetape_tape_t *tape = drive->driver_data;
1533
Borislav Petkovd9df9372008-04-27 15:38:34 +02001534 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001535 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1536 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001537 idetape_pad_zeros(drive, tape->blk_size *
1538 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001539 kfree(tape->buf);
1540 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 idetape_write_filemark(drive);
1543 idetape_flush_tape_buffers(drive);
1544 idetape_flush_tape_buffers(drive);
1545}
1546
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001547static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001549 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 unsigned int minor = iminor(inode);
1552
1553 lock_kernel();
1554 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001555
Borislav Petkove972d702009-07-17 23:55:16 +00001556 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Borislav Petkov54abf372008-02-06 02:57:52 +01001558 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001560 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001562 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001564
Borislav Petkov49d80782009-06-07 15:37:06 +02001565 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1566 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001568
Borislav Petkov54abf372008-02-06 02:57:52 +01001569 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001571 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001572 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001575 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ide_tape_put(tape);
1577 unlock_kernel();
1578 return 0;
1579}
1580
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001581static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001584 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001585 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001586 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 idetape_create_inquiry_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001589 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001590 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1591 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return;
1593 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001594 memcpy(vendor_id, &pc_buf[8], 8);
1595 memcpy(product_id, &pc_buf[16], 16);
1596 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001597
Borislav Petkov801bd322008-09-27 19:32:17 +02001598 ide_fixstring(vendor_id, 8, 0);
1599 ide_fixstring(product_id, 16, 0);
1600 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001601
Borislav Petkov801bd322008-09-27 19:32:17 +02001602 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001603 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001607 * Ask the tape about its various parameters. In particular, we will adjust our
1608 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001610static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001613 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001614 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001615 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001618 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001619 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1620 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001621 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001622 put_unaligned(52, (u16 *)&tape->caps[12]);
1623 put_unaligned(540, (u16 *)&tape->caps[14]);
1624 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return;
1626 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001627 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Borislav Petkovb6422012008-02-02 19:56:49 +01001629 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001630 speed = be16_to_cpup((__be16 *)&caps[14]);
1631 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001633 *(u16 *)&caps[8] = max_speed;
1634 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1635 *(u16 *)&caps[14] = speed;
1636 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001637
1638 if (!speed) {
1639 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1640 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001641 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001643 if (!max_speed) {
1644 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1645 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001646 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
Borislav Petkovb6422012008-02-02 19:56:49 +01001649 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001650
1651 /* device lacks locking support according to capabilities page */
1652 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001653 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001654
Borislav Petkovb6422012008-02-02 19:56:49 +01001655 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001656 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001657 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001658 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001661#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001662#define ide_tape_devset_get(name, field) \
1663static int get_##name(ide_drive_t *drive) \
1664{ \
1665 idetape_tape_t *tape = drive->driver_data; \
1666 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001668
1669#define ide_tape_devset_set(name, field) \
1670static int set_##name(ide_drive_t *drive, int arg) \
1671{ \
1672 idetape_tape_t *tape = drive->driver_data; \
1673 tape->field = arg; \
1674 return 0; \
1675}
1676
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001677#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001678ide_tape_devset_get(_name, _field) \
1679ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001680IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001681
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001682#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001683ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001684IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001685
1686static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1687static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1688static int divf_buffer(ide_drive_t *drive) { return 2; }
1689static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1690
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001691ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001692
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001693ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001694
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001695ide_tape_devset_r_field(avg_speed, avg_speed);
1696ide_tape_devset_r_field(speed, caps[14]);
1697ide_tape_devset_r_field(buffer, caps[16]);
1698ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001699
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001700static const struct ide_proc_devset idetape_settings[] = {
1701 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1702 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1703 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001704 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1705 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1706 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1707 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001708 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001709};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001713 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001715 * 1. Initialize our various state variables.
1716 * 2. Ask the tape for its capabilities.
1717 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1718 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001720 * Note that at this point ide.c already assigned us an irq, so that we can
1721 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001723static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Borislav Petkov83042b22008-04-27 15:38:27 +02001725 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001727 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001728 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Borislav Petkove972d702009-07-17 23:55:16 +00001730 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1731
1732 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001733
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001734 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1735
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001736 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1737 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1738 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001739 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001743 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001744 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 tape->minor = minor;
1747 tape->name[0] = 'h';
1748 tape->name[1] = 't';
1749 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001750 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 idetape_get_inquiry_results(drive);
1753 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001754 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001756 tape->buffer_size = *ctl * tape->blk_size;
1757 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001759 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001760 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001762 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Borislav Petkov83042b22008-04-27 15:38:27 +02001764 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001765 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Borislav Petkovf73850a2008-04-27 15:38:33 +02001767 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001770 * Ensure that the number we got makes sense; limit it within
1771 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001773 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1774 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001776 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001777 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001778 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1779 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001780 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001781 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001783 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Russell King4031bbe2006-01-06 11:41:00 +00001786static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001790 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001791 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 ide_unregister_region(tape->disk);
1793
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001794 mutex_lock(&idetape_ref_mutex);
1795 put_device(&tape->dev);
1796 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001799static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001801 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 ide_drive_t *drive = tape->drive;
1803 struct gendisk *g = tape->disk;
1804
Tejun Heo963da552009-04-19 08:46:02 +09001805 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001806
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001807 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001809 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001810 device_destroy(idetape_sysfs_class,
1811 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 idetape_devs[tape->minor] = NULL;
1813 g->private_data = NULL;
1814 put_disk(g);
1815 kfree(tape);
1816}
1817
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001818#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819static int proc_idetape_read_name
1820 (char *page, char **start, off_t off, int count, int *eof, void *data)
1821{
1822 ide_drive_t *drive = (ide_drive_t *) data;
1823 idetape_tape_t *tape = drive->driver_data;
1824 char *out = page;
1825 int len;
1826
1827 len = sprintf(out, "%s\n", tape->name);
1828 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1829}
1830
1831static ide_proc_entry_t idetape_proc[] = {
1832 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1833 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1834 { NULL, 0, NULL, NULL }
1835};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001836
1837static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1838{
1839 return idetape_proc;
1840}
1841
1842static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1843{
1844 return idetape_settings;
1845}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846#endif
1847
Russell King4031bbe2006-01-06 11:41:00 +00001848static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001850static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001851 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001852 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001853 .name = "ide-tape",
1854 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001855 },
Russell King4031bbe2006-01-06 11:41:00 +00001856 .probe = ide_tape_probe,
1857 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001860#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001861 .proc_entries = ide_tape_proc_entries,
1862 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864};
1865
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001866/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001867static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .owner = THIS_MODULE,
1869 .read = idetape_chrdev_read,
1870 .write = idetape_chrdev_write,
1871 .ioctl = idetape_chrdev_ioctl,
1872 .open = idetape_chrdev_open,
1873 .release = idetape_chrdev_release,
1874};
1875
Al Viroa4600f82008-03-02 10:27:58 -05001876static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001878 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001880 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return -ENXIO;
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884}
1885
Al Viroa4600f82008-03-02 10:27:58 -05001886static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001888 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892}
1893
Al Viroa4600f82008-03-02 10:27:58 -05001894static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 unsigned int cmd, unsigned long arg)
1896{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001897 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04001899 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (err == -EINVAL)
1901 err = idetape_blkdev_ioctl(drive, cmd, arg);
1902 return err;
1903}
1904
1905static struct block_device_operations idetape_block_ops = {
1906 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001907 .open = idetape_open,
1908 .release = idetape_release,
1909 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910};
1911
Russell King4031bbe2006-01-06 11:41:00 +00001912static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 idetape_tape_t *tape;
1915 struct gendisk *g;
1916 int minor;
1917
Borislav Petkove972d702009-07-17 23:55:16 +00001918 ide_debug_log(IDE_DBG_FUNC, "enter");
1919
1920 if (!strstr(DRV_NAME, drive->driver_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (drive->media != ide_tape)
1924 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001925
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001926 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1927 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001928 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1929 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 goto failed;
1931 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001932 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001934 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1935 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 goto failed;
1937 }
1938
1939 g = alloc_disk(1 << PARTN_BITS);
1940 if (!g)
1941 goto out_free_tape;
1942
1943 ide_init_disk(g, drive);
1944
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001945 tape->dev.parent = &drive->gendev;
1946 tape->dev.release = ide_tape_release;
1947 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1948
1949 if (device_register(&tape->dev))
1950 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 tape->drive = drive;
1953 tape->driver = &idetape_driver;
1954 tape->disk = g;
1955
1956 g->private_data = &tape->driver;
1957
1958 drive->driver_data = tape;
1959
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001960 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 for (minor = 0; idetape_devs[minor]; minor++)
1962 ;
1963 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001964 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 idetape_setup(drive, tape, minor);
1967
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07001968 device_create(idetape_sysfs_class, &drive->gendev,
1969 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1970 device_create(idetape_sysfs_class, &drive->gendev,
1971 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
1972 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 g->fops = &idetape_block_ops;
1975 ide_register_region(g);
1976
1977 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001978
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001979out_free_disk:
1980 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981out_free_tape:
1982 kfree(tape);
1983failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001984 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001987static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001989 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07001990 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 unregister_chrdev(IDETAPE_MAJOR, "ht");
1992}
1993
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001994static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Will Dysond5dee802005-09-16 02:55:07 -07001996 int error = 1;
1997 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
1998 if (IS_ERR(idetape_sysfs_class)) {
1999 idetape_sysfs_class = NULL;
2000 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2001 error = -EBUSY;
2002 goto out;
2003 }
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002006 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2007 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002008 error = -EBUSY;
2009 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Will Dysond5dee802005-09-16 02:55:07 -07002011
2012 error = driver_register(&idetape_driver.gen_driver);
2013 if (error)
2014 goto out_free_driver;
2015
2016 return 0;
2017
2018out_free_driver:
2019 driver_unregister(&idetape_driver.gen_driver);
2020out_free_class:
2021 class_destroy(idetape_sysfs_class);
2022out:
2023 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Kay Sievers263756e2005-12-12 18:03:44 +01002026MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027module_init(idetape_init);
2028module_exit(idetape_exit);
2029MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002030MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2031MODULE_LICENSE("GPL");