blob: 58fc920d5c32094c4bddb1be206ebbdf5b2f30c5 [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>
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070034#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/ide.h>
38#include <linux/smp_lock.h>
39#include <linux/completion.h>
40#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080041#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010042#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010045#include <linux/irq.h>
46#include <linux/uaccess.h>
47#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/mtio.h>
50
Borislav Petkov8004a8c2008-02-06 02:57:51 +010051/* define to see debug info */
Borislav Petkove972d702009-07-17 23:55:16 +000052#undef IDETAPE_DEBUG_LOG
Borislav Petkov8004a8c2008-02-06 02:57:51 +010053
Borislav Petkove972d702009-07-17 23:55:16 +000054#ifdef IDETAPE_DEBUG_LOG
55#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010056#else
Borislav Petkove972d702009-07-17 23:55:16 +000057#define ide_debug_log(lvl, fmt, args...) do {} while (0)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010058#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010062 * After each failed packet command we issue a request sense command and retry
63 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010065 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67#define IDETAPE_MAX_PC_RETRIES 3
68
69/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010070 * The following parameter is used to select the point in the internal tape fifo
71 * in which we will start to refill the buffer. Decreasing the following
72 * parameter will improve the system's latency and interactive response, while
73 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010075#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010080 * Polling for DSC (a single bit in the status register) is a very important
81 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010083 * 1. Before a read/write packet command, to ensure that we can transfer data
84 * from/to the tape's data buffers, without causing an actual media access.
85 * In case the tape is not ready yet, we take out our request from the device
86 * request queue, so that ide.c could service requests from the other device
87 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010089 * 2. After the successful initialization of a "media access packet command",
90 * which is a command that can take a long time to complete (the interval can
91 * range from several seconds to even an hour). Again, we postpone our request
92 * in the middle to free the bus for the other device. The polling frequency
93 * here should be lower than the read/write frequency since those media access
94 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
95 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
96 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010098 * We also set a timeout for the timer, in case something goes wrong. The
99 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100101
102/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
104#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
105#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
106#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
107#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
108#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
109#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
110
111/*************************** End of tunable parameters ***********************/
112
Borislav Petkov54abf372008-02-06 02:57:52 +0100113/* tape directions */
114enum {
115 IDETAPE_DIR_NONE = (1 << 0),
116 IDETAPE_DIR_READ = (1 << 1),
117 IDETAPE_DIR_WRITE = (1 << 2),
118};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Borislav Petkov03056b92008-04-18 00:46:26 +0200120/* Tape door status */
121#define DOOR_UNLOCKED 0
122#define DOOR_LOCKED 1
123#define DOOR_EXPLICITLY_LOCKED 2
124
125/* Some defines for the SPACE command */
126#define IDETAPE_SPACE_OVER_FILEMARK 1
127#define IDETAPE_SPACE_TO_EOD 3
128
129/* Some defines for the LOAD UNLOAD command */
130#define IDETAPE_LU_LOAD_MASK 1
131#define IDETAPE_LU_RETENSION_MASK 2
132#define IDETAPE_LU_EOT_MASK 4
133
Borislav Petkov03056b92008-04-18 00:46:26 +0200134/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
135#define IDETAPE_BLOCK_DESCRIPTOR 0
136#define IDETAPE_CAPABILITIES_PAGE 0x2a
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100139 * Most of our global data which we need to save even as we leave the driver due
140 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100143 ide_drive_t *drive;
144 struct ide_driver *driver;
145 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100146 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200148 /* used by REQ_IDETAPE_{READ,WRITE} requests */
149 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100154 * While polling for DSC we use postponed_rq to postpone the current
155 * request so that ide.c will be able to service pending requests on the
156 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200157 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700159 bool postponed_rq;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* The time in which we started polling for DSC */
162 unsigned long dsc_polling_start;
163 /* Timer used to poll for dsc */
164 struct timer_list dsc_timer;
165 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100166 unsigned long best_dsc_rw_freq;
167 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned long dsc_timeout;
169
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100170 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u8 partition;
172 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100173 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100175 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 u8 sense_key, asc, ascq;
177
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100178 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 unsigned int minor;
180 /* device name */
181 char name[4];
182 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100183 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Borislav Petkov54bb2072008-02-06 02:57:52 +0100185 /* tape block size, usually 512 or 1024 bytes */
186 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100190 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100193 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100195 * At most, there is only one ide-tape originated data transfer request
196 * in the device request queue. This allows ide.c to easily service
197 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200199
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100200 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200201 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900202 /* Staging buffer of buffer_size bytes */
203 void *buf;
204 /* The read/write cursor */
205 void *cur;
206 /* The number of valid bytes in buf */
207 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100209 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long avg_time;
211 int avg_size;
212 int avg_speed;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* the door is currently locked */
215 int door_locked;
216 /* the tape hardware is write protected */
217 char drv_write_prot;
218 /* the tape is write protected (hardware or opened as read-only) */
219 char write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220} idetape_tape_t;
221
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800222static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Will Dysond5dee802005-09-16 02:55:07 -0700224static struct class *idetape_sysfs_class;
225
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100226static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200227
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200228static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
229
230static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
231 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct ide_tape_obj *tape = NULL;
234
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800235 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200236
237 if (cdev)
238 tape = idetape_devs[i];
239 else
240 tape = ide_drv_g(disk, ide_tape_obj);
241
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200242 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200243 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200244 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200245 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100246 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200247 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200248
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800249 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return tape;
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static void ide_tape_put(struct ide_tape_obj *tape)
254{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200255 ide_drive_t *drive = tape->drive;
256
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800257 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100258 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200259 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800260 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100264 * called on each failed packet command retry to analyze the request sense. We
265 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Borislav Petkovae3a8382009-05-02 10:58:17 +0200267static void idetape_analyze_error(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100270 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200271 struct request *rq = drive->hwif->rq;
Borislav Petkovae3a8382009-05-02 10:58:17 +0200272 u8 *sense = bio_data(rq->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Borislav Petkov1b5db432008-02-02 19:56:48 +0100274 tape->sense_key = sense[2] & 0xF;
275 tape->asc = sense[12];
276 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100277
Borislav Petkove972d702009-07-17 23:55:16 +0000278 ide_debug_log(IDE_DBG_FUNC,
279 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
280 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Borislav Petkov077e6db2009-05-01 21:21:02 +0200282 /* correct remaining bytes to transfer */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900283 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200284 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /*
287 * If error was the result of a zero-length read or write command,
288 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
289 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
290 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100291 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100292 /* length == 0 */
293 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
294 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* don't report an error, everything's ok */
296 pc->error = 0;
297 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200298 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100301 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100302 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200303 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100305 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100306 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
307 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100308 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200309 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100312 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100313 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100314 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200315 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200317 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200318 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
320 }
321}
322
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200323static void ide_tape_handle_dsc(ide_drive_t *);
324
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100325static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200328 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100329 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200330 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100331 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Borislav Petkove972d702009-07-17 23:55:16 +0000333 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
334 dsc, err);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100335
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200336 if (dsc)
337 ide_tape_handle_dsc(drive);
338
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100339 if (drive->failed_pc == pc)
340 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200341
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200342 if (pc->c[0] == REQUEST_SENSE) {
343 if (uptodate)
Borislav Petkovae3a8382009-05-02 10:58:17 +0200344 idetape_analyze_error(drive);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200345 else
346 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
347 "itself - Aborting request!\n");
348 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200349 unsigned int blocks =
350 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200351
352 tape->avg_size += blocks * tape->blk_size;
353
354 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
355 tape->avg_speed = tape->avg_size * HZ /
356 (jiffies - tape->avg_time) / 1024;
357 tape->avg_size = 0;
358 tape->avg_time = jiffies;
359 }
360
361 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200362
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100363 if (pc->error) {
364 uptodate = 0;
365 err = pc->error;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100368 rq->errors = err;
369
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100370 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100374 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100375 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700377static void ide_tape_stall_queue(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 idetape_tape_t *tape = drive->driver_data;
380
Borislav Petkove972d702009-07-17 23:55:16 +0000381 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700382 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100383
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700384 tape->postponed_rq = true;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100385
Borislav Petkov54bb2072008-02-06 02:57:52 +0100386 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200389static void ide_tape_handle_dsc(ide_drive_t *drive)
390{
391 idetape_tape_t *tape = drive->driver_data;
392
393 /* Media access command */
394 tape->dsc_polling_start = jiffies;
395 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
396 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
397 /* Allow ide.c to handle other requests */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700398 ide_tape_stall_queue(drive);
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100402 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200404 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100405 * until we finish handling it. Each packet command is associated with a
406 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100408 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100410 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200411 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200413 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100414 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100416 * 3. ATAPI Tape media access commands have immediate status with a delayed
417 * process. In case of a successful initiation of a media access packet command,
418 * the DSC bit will be set when the actual execution of the command is finished.
419 * Since the tape drive will not issue an interrupt, we have to poll for this
420 * event. In this case, we define the request as "low priority request" by
421 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
422 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100424 * ide.c will then give higher priority to requests which originate from the
425 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100427 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100429 * 5. In case an error was found, we queue a request sense packet command in
430 * front of the request queue and retry the operation up to
431 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100433 * 6. In case no error was found, or we decided to give up and not to retry
434 * again, the callback function will be called and then we will handle the next
435 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100438static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
439 struct ide_cmd *cmd,
440 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200443 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100445 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
446 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200449 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200452 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100455 * We will "abort" retrying a packet command in case legitimate
456 * error code was received (crossing a filemark, or end of the
457 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200459 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100460 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 tape->sense_key == 2 && tape->asc == 4 &&
462 (tape->ascq == 1 || tape->ascq == 8))) {
463 printk(KERN_ERR "ide-tape: %s: I/O error, "
464 "pc = %2x, key = %2x, "
465 "asc = %2x, ascq = %2x\n",
466 tape->name, pc->c[0],
467 tape->sense_key, tape->asc,
468 tape->ascq);
469 }
470 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100471 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Tejun Heob3071d12009-04-19 08:46:02 +0900473
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100474 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200475 drive->pc_callback(drive, 0);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200476 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200477 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Borislav Petkove972d702009-07-17 23:55:16 +0000479 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
480 pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100484 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100487/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200488static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200490 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100491 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100493 /* DBD = 1 - Don't return block descriptors */
494 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 pc->c[2] = page_code;
496 /*
497 * Changed pc->c[3] to 0 (255 will at best return unused info).
498 *
499 * For SCSI this byte is defined as subpage instead of high byte
500 * of length and some IDE drives seem to interpret it this way
501 * and return an error when 255 is used.
502 */
503 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100504 /* We will just discard data in that case */
505 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200507 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200509 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200511 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100514static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200516 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200518 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100519 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200521 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100522
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200523 if (stat & ATA_DSC) {
524 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100526 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 printk(KERN_ERR "ide-tape: %s: I/O error, ",
528 tape->name);
529 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900530 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200531 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100535 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100536 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200538 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return ide_stopped;
540}
541
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200542static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200543 struct ide_atapi_pc *pc, struct request *rq,
544 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Borislav Petkov10c0b342009-05-01 20:29:53 +0200546 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
Borislav Petkov0014c752008-07-23 19:56:00 +0200547
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200548 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100549 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 pc->c[1] = 1;
Borislav Petkov19f52a72009-05-04 09:53:03 +0200551
552 if (blk_rq_bytes(rq) == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200553 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Tejun Heo963da552009-04-19 08:46:02 +0900555 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200556 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900557 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200558 pc->c[0] = WRITE_6;
559 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200560 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200561
562 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static ide_startstop_t idetape_do_request(ide_drive_t *drive,
566 struct request *rq, sector_t block)
567{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200568 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200570 struct ide_atapi_pc *pc = NULL;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100571 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100572 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Borislav Petkove972d702009-07-17 23:55:16 +0000574 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
575 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
576 blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Bartlomiej Zolnierkiewicz2c7eaa42009-06-15 22:16:10 +0200578 BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100580 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100581 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
582 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200583 goto out;
584 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
587 * If the tape is still busy, postpone our request and service
588 * the other device meanwhile.
589 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200590 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200592 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
593 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200594 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200596 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200597 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200598 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
Borislav Petkov626542c2009-06-07 15:37:05 +0200601 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
602 !(stat & ATA_DSC)) {
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700603 if (!tape->postponed_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100605 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
607 } else if (time_after(jiffies, tape->dsc_timeout)) {
608 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
609 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200610 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 idetape_media_access_finished(drive);
612 return ide_stopped;
613 } else {
614 return ide_do_reset(drive);
615 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100616 } else if (time_after(jiffies,
617 tape->dsc_polling_start +
618 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100619 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700620 ide_tape_stall_queue(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return ide_stopped;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700622 } else {
Borislav Petkov626542c2009-06-07 15:37:05 +0200623 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700624 tape->postponed_rq = false;
625 }
Borislav Petkov626542c2009-06-07 15:37:05 +0200626
Borislav Petkov83dd5732008-07-23 19:56:00 +0200627 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200628 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200629 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto out;
631 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200632 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200633 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200634 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto out;
636 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200637 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900638 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200639 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
640 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto out;
642 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200643 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 idetape_media_access_finished(drive);
645 return ide_stopped;
646 }
647 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200648
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200649out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900650 /* prepare sense request for this command */
651 ide_prep_sense(drive, rq);
652
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100653 memset(&cmd, 0, sizeof(cmd));
654
655 if (rq_data_dir(rq))
656 cmd.tf_flags |= IDE_TFLAG_WRITE;
657
658 cmd.rq = rq;
659
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200660 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo5c4be572009-04-19 07:00:42 +0900661 ide_map_sg(drive, &cmd);
662
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100663 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100667 * Write a filemark if write_filemark=1. Flush the device buffers without
668 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100670static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200671 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200673 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100674 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200676 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
680{
681 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200682 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int load_attempted = 0;
684
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100685 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200686 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 timeout += jiffies;
688 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200689 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100692 || (tape->asc == 0x3A)) {
693 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (load_attempted)
695 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200696 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 load_attempted = 1;
698 /* not about to be ready */
699 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
700 (tape->ascq == 1 || tape->ascq == 8)))
701 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700702 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 return -EIO;
705}
706
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100707static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200709 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200710 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int rc;
712
713 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200714 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100715 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return rc;
717 idetape_wait_ready(drive, 60 * 5 * HZ);
718 return 0;
719}
720
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200721static int ide_tape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200724 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200725 u8 buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Borislav Petkove972d702009-07-17 23:55:16 +0000727 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200729 /* prep cmd */
730 ide_init_pc(&pc);
731 pc.c[0] = READ_POSITION;
732 pc.req_xfer = 20;
733
734 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -1;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200736
737 if (!pc.error) {
Borislav Petkove972d702009-07-17 23:55:16 +0000738 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200739 (buf[0] & 0x80) ? "Yes" : "No");
Borislav Petkove972d702009-07-17 23:55:16 +0000740 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200741 (buf[0] & 0x40) ? "Yes" : "No");
742
743 if (buf[0] & 0x4) {
744 printk(KERN_INFO "ide-tape: Block location is unknown"
745 "to the tape\n");
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200746 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
747 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200748 return -1;
749 } else {
Borislav Petkove972d702009-07-17 23:55:16 +0000750 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
751 be32_to_cpup((__be32 *)&buf[4]));
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200752
753 tape->partition = buf[1];
754 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200755 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
756 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200757 }
758 }
759
760 return tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Borislav Petkovd236d742008-04-18 00:46:27 +0200763static void idetape_create_locate_cmd(ide_drive_t *drive,
764 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100765 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200767 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100768 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100770 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200772 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200775static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Borislav Petkov54abf372008-02-06 02:57:52 +0100779 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200780 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Borislav Petkov49d80782009-06-07 15:37:06 +0200782 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900783 tape->valid = 0;
784 if (tape->buf != NULL) {
785 kfree(tape->buf);
786 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Borislav Petkov54abf372008-02-06 02:57:52 +0100789 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100793 * Position the tape to the requested block using the LOCATE packet command.
794 * A READ POSITION command is then issued to check where we are positioned. Like
795 * all higher level operations, we queue the commands at the tail of the request
796 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100798static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
799 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200802 struct gendisk *disk = tape->disk;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200803 int ret;
Borislav Petkovd236d742008-04-18 00:46:27 +0200804 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Borislav Petkov54abf372008-02-06 02:57:52 +0100806 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200807 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 idetape_wait_ready(drive, 60 * 5 * HZ);
809 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200810 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
811 if (ret)
812 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200814 ret = ide_tape_read_position(drive);
815 if (ret < 0)
816 return ret;
817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200820static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100821 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int seek, position;
825
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200826 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (restore_position) {
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200828 position = ide_tape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200829 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100831 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200832 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return;
834 }
835 }
836}
837
838/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100839 * Generate a read/write request for the block device interface and wait for it
840 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900842static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200845 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900846 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Borislav Petkove972d702009-07-17 23:55:16 +0000848 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
849
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900850 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900851 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100852
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200853 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
854 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200855 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200856 rq->rq_disk = tape->disk;
Linus Torvaldsc9059592009-06-11 10:52:27 -0700857 rq->__sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900858
859 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900860 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900861 __GFP_WAIT);
862 if (ret)
863 goto out_put;
864 }
865
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200866 blk_execute_rq(drive->queue, tape->disk, rq, 0);
867
Tejun Heo963da552009-04-19 08:46:02 +0900868 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900869 size -= rq->resid_len;
Tejun Heo963da552009-04-19 08:46:02 +0900870 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900871 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900872 tape->valid = size;
873 else
874 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900876 ret = size;
877 if (rq->errors == IDE_DRV_ERROR_GENERAL)
878 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900879out_put:
880 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Borislav Petkovd236d742008-04-18 00:46:27 +0200884static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200886 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100887 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100888 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200889 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Borislav Petkovd236d742008-04-18 00:46:27 +0200892static void idetape_create_rewind_cmd(ide_drive_t *drive,
893 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200895 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100896 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200897 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Borislav Petkovd236d742008-04-18 00:46:27 +0200900static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200902 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100903 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200905 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Borislav Petkovd236d742008-04-18 00:46:27 +0200908static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200910 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100911 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100912 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200914 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Borislav Petkovd9df9372008-04-27 15:38:34 +0200917static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100920
Borislav Petkov54abf372008-02-06 02:57:52 +0100921 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200922 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100923 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return;
925 }
Tejun Heo963da552009-04-19 08:46:02 +0900926 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900927 size_t aligned = roundup(tape->valid, tape->blk_size);
928
929 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900930 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900931 kfree(tape->buf);
932 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100934 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Tejun Heo88f1b942009-04-19 08:46:02 +0900937static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900940 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Tejun Heo88f1b942009-04-19 08:46:02 +0900942 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Tejun Heo88f1b942009-04-19 08:46:02 +0900944 if (tape->chrdev_dir == dir)
945 return 0;
946
947 if (tape->chrdev_dir == IDETAPE_DIR_READ)
948 ide_tape_discard_merge_buffer(drive, 1);
949 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
950 ide_tape_flush_merge_buffer(drive);
951 idetape_flush_tape_buffers(drive);
952 }
953
954 if (tape->buf || tape->valid) {
955 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
956 tape->valid = 0;
957 }
958
959 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
960 if (!tape->buf)
961 return -ENOMEM;
962 tape->chrdev_dir = dir;
963 tape->cur = tape->buf;
964
965 /*
966 * Issue a 0 rw command to ensure that DSC handshake is
967 * switched from completion mode to buffer available mode. No
968 * point in issuing this if DSC overlap isn't supported, some
969 * drives (Seagate STT3401A) will return an error.
970 */
971 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
972 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
973 : REQ_IDETAPE_WRITE;
974
975 rc = idetape_queue_rw_tail(drive, cmd, 0);
976 if (rc < 0) {
977 kfree(tape->buf);
978 tape->buf = NULL;
979 tape->chrdev_dir = IDETAPE_DIR_NONE;
980 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +0200983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
985}
986
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100987static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +0900990
991 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +0900994 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900996 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001002 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1003 * currently support only one partition.
1004 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001005static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001007 struct ide_tape_obj *tape = drive->driver_data;
1008 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001009 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001010 int ret;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001011
Borislav Petkove972d702009-07-17 23:55:16 +00001012 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001015 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1016 if (ret)
1017 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001019 ret = ide_tape_read_position(drive);
1020 if (ret < 0)
1021 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001025/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001026static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1027 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 void __user *argp = (void __user *)arg;
1031
Borislav Petkovd59823f2008-02-02 19:56:51 +01001032 struct idetape_config {
1033 int dsc_rw_frequency;
1034 int dsc_media_access_frequency;
1035 int nr_stages;
1036 } config;
1037
Borislav Petkove972d702009-07-17 23:55:16 +00001038 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001041 case 0x0340:
1042 if (copy_from_user(&config, argp, sizeof(config)))
1043 return -EFAULT;
1044 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001045 break;
1046 case 0x0350:
Michael Buesch2fc21112009-07-19 09:15:19 +00001047 memset(&config, 0, sizeof(config));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001048 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001049 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001050 if (copy_to_user(argp, &config, sizeof(config)))
1051 return -EFAULT;
1052 break;
1053 default:
1054 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 return 0;
1057}
1058
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001059static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1060 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001063 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001064 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001065 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001066 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Borislav Petkove972d702009-07-17 23:55:16 +00001068
1069 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (mt_count == 0)
1072 return 0;
1073 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001074 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001076 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Borislav Petkov54abf372008-02-06 02:57:52 +01001079 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001080 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001081 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1082 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001084 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001088 case MTFSF:
1089 case MTBSF:
1090 idetape_create_space_cmd(&pc, mt_count - count,
1091 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001092 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001093 case MTFSFM:
1094 case MTBSFM:
1095 if (!sprev)
1096 return -EIO;
1097 retval = idetape_space_over_filemarks(drive, MTFSF,
1098 mt_count - count);
1099 if (retval)
1100 return retval;
1101 count = (MTBSFM == mt_op ? 1 : -1);
1102 return idetape_space_over_filemarks(drive, MTFSF, count);
1103 default:
1104 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1105 mt_op);
1106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001111 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001113 * The tape is optimized to maximize throughput when it is transferring an
1114 * integral number of the "continuous transfer limit", which is a parameter of
1115 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001117 * As of version 1.3 of the driver, the character device provides an abstract
1118 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1119 * same backup/restore procedure is supported. The driver will internally
1120 * convert the requests to the recommended transfer unit, so that an unmatch
1121 * between the user's block size to the recommended size will only result in a
1122 * (slightly) increased driver overhead, but will no longer hit performance.
1123 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001125static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1126 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001128 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001130 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001131 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001132 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Borislav Petkove972d702009-07-17 23:55:16 +00001134 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Borislav Petkov54abf372008-02-06 02:57:52 +01001136 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001137 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001138 if (count > tape->blk_size &&
1139 (count % tape->blk_size) == 0)
1140 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001142
Tejun Heo88f1b942009-04-19 08:46:02 +09001143 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001144 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001146
1147 while (done < count) {
1148 size_t todo;
1149
1150 /* refill if staging buffer is empty */
1151 if (!tape->valid) {
1152 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001153 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1154 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001155 break;
1156 /* read */
1157 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1158 tape->buffer_size) <= 0)
1159 break;
1160 }
1161
1162 /* copy out */
1163 todo = min_t(size_t, count - done, tape->valid);
1164 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001165 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001166
1167 tape->cur += todo;
1168 tape->valid -= todo;
1169 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001171
Borislav Petkov49d80782009-06-07 15:37:06 +02001172 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 idetape_space_over_filemarks(drive, MTFSF, 1);
1174 return 0;
1175 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001176
Tejun Heo07bd9682009-04-19 08:46:03 +09001177 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001180static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 size_t count, loff_t *ppos)
1182{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001183 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001185 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001186 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001187 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* The drive is write protected. */
1190 if (tape->write_prot)
1191 return -EACCES;
1192
Borislav Petkove972d702009-07-17 23:55:16 +00001193 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001196 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1197 if (rc < 0)
1198 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tejun Heo07bd9682009-04-19 08:46:03 +09001200 while (done < count) {
1201 size_t todo;
1202
1203 /* flush if staging buffer is full */
1204 if (tape->valid == tape->buffer_size &&
1205 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1206 tape->buffer_size) <= 0)
1207 return rc;
1208
1209 /* copy in */
1210 todo = min_t(size_t, count - done,
1211 tape->buffer_size - tape->valid);
1212 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001213 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001214
1215 tape->cur += todo;
1216 tape->valid += todo;
1217 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001219
1220 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001223static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001225 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001226 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* Write a filemark */
1229 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001230 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1232 return -EIO;
1233 }
1234 return 0;
1235}
1236
1237/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001238 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1239 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001241 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1242 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001243 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001245 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001247 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1248 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001250static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001253 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001254 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001255 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Borislav Petkove972d702009-07-17 23:55:16 +00001257 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1258 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001261 case MTFSF:
1262 case MTFSFM:
1263 case MTBSF:
1264 case MTBSFM:
1265 if (!mt_count)
1266 return 0;
1267 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1268 default:
1269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001273 case MTWEOF:
1274 if (tape->write_prot)
1275 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001276 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001277 for (i = 0; i < mt_count; i++) {
1278 retval = idetape_write_filemark(drive);
1279 if (retval)
1280 return retval;
1281 }
1282 return 0;
1283 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001284 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001285 if (idetape_rewind_tape(drive))
1286 return -EIO;
1287 return 0;
1288 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001289 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001290 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001291 case MTUNLOAD:
1292 case MTOFFL:
1293 /*
1294 * If door is locked, attempt to unlock before
1295 * attempting to eject.
1296 */
1297 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001298 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001299 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001300 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001301 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001302 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001303 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001304 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1305 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001306 return retval;
1307 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001308 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001309 return idetape_flush_tape_buffers(drive);
1310 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001311 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001312 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001313 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001314 case MTEOM:
1315 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001316 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317 case MTERASE:
1318 (void)idetape_rewind_tape(drive);
1319 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001320 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001321 case MTSETBLK:
1322 if (mt_count) {
1323 if (mt_count < tape->blk_size ||
1324 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001326 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001327 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1328 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001329 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001330 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1331 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332 return 0;
1333 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001334 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001335 return idetape_position_tape(drive,
1336 mt_count * tape->user_bs_factor, tape->partition, 0);
1337 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001338 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339 return idetape_position_tape(drive, 0, mt_count, 0);
1340 case MTFSR:
1341 case MTBSR:
1342 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001343 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001344 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001346 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1347 return 0;
1348 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001349 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001350 if (retval)
1351 return retval;
1352 tape->door_locked = DOOR_UNLOCKED;
1353 return 0;
1354 default:
1355 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1356 mt_op);
1357 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359}
1360
1361/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001362 * Our character device ioctls. General mtio.h magnetic io commands are
1363 * supported here, and not in the corresponding block interface. Our own
1364 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001366static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1367 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001369 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ide_drive_t *drive = tape->drive;
1371 struct mtop mtop;
1372 struct mtget mtget;
1373 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001374 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 void __user *argp = (void __user *)arg;
1376
Borislav Petkove972d702009-07-17 23:55:16 +00001377 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Borislav Petkov54abf372008-02-06 02:57:52 +01001379 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001380 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 idetape_flush_tape_buffers(drive);
1382 }
1383 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001384 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001385 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001386 position = ide_tape_read_position(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001387 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -EIO;
1389 }
1390 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001391 case MTIOCTOP:
1392 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1393 return -EFAULT;
1394 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1395 case MTIOCGET:
1396 memset(&mtget, 0, sizeof(struct mtget));
1397 mtget.mt_type = MT_ISSCSI2;
1398 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1399 mtget.mt_dsreg =
1400 ((tape->blk_size * tape->user_bs_factor)
1401 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001402
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001403 if (tape->drv_write_prot)
1404 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1405
1406 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1407 return -EFAULT;
1408 return 0;
1409 case MTIOCPOS:
1410 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1411 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1412 return -EFAULT;
1413 return 0;
1414 default:
1415 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001416 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001417 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419}
1420
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001421/*
1422 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1423 * block size with the reported value.
1424 */
1425static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1426{
1427 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001428 struct ide_atapi_pc pc;
Borislav Petkov837272b2009-05-04 09:48:57 +02001429 u8 buf[12];
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001430
1431 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkov837272b2009-05-04 09:48:57 +02001432 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001433 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001434 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001435 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1436 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001437 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001438 }
1439 return;
1440 }
Borislav Petkov837272b2009-05-04 09:48:57 +02001441 tape->blk_size = (buf[4 + 5] << 16) +
1442 (buf[4 + 6] << 8) +
1443 buf[4 + 7];
1444 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
Borislav Petkove972d702009-07-17 23:55:16 +00001445
1446 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1447 tape->blk_size, tape->drv_write_prot);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001450static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1453 ide_drive_t *drive;
1454 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int retval;
1456
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001457 if (i >= MAX_HWIFS * MAX_DRIVES)
1458 return -ENXIO;
1459
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001460 lock_kernel();
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001461 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001462 if (!tape) {
1463 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001464 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001465 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001466
Borislav Petkove972d702009-07-17 23:55:16 +00001467 drive = tape->drive;
1468 filp->private_data = tape;
1469
1470 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /*
1473 * We really want to do nonseekable_open(inode, filp); here, but some
1474 * versions of tar incorrectly call lseek on tapes and bail out if that
1475 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1476 */
1477 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Borislav Petkov49d80782009-06-07 15:37:06 +02001480 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 retval = -EBUSY;
1482 goto out_put_tape;
1483 }
1484
1485 retval = idetape_wait_ready(drive, 60 * HZ);
1486 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001487 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1489 goto out_put_tape;
1490 }
1491
Borislav Petkov79ca7432009-06-15 07:32:04 +02001492 ide_tape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001493 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 (void)idetape_rewind_tape(drive);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001497 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /* Set write protect flag if device is opened as read-only. */
1500 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1501 tape->write_prot = 1;
1502 else
1503 tape->write_prot = tape->drv_write_prot;
1504
1505 /* Make sure drive isn't write protected if user wants to write. */
1506 if (tape->write_prot) {
1507 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1508 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001509 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 retval = -EROFS;
1511 goto out_put_tape;
1512 }
1513 }
1514
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001515 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001516 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001517 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001518 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1519 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001522 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
1524
1525out_put_tape:
1526 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001527 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return retval;
1529}
1530
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001531static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 idetape_tape_t *tape = drive->driver_data;
1534
Borislav Petkovd9df9372008-04-27 15:38:34 +02001535 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001536 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1537 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001538 idetape_pad_zeros(drive, tape->blk_size *
1539 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001540 kfree(tape->buf);
1541 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 idetape_write_filemark(drive);
1544 idetape_flush_tape_buffers(drive);
1545 idetape_flush_tape_buffers(drive);
1546}
1547
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001548static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001550 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 unsigned int minor = iminor(inode);
1553
1554 lock_kernel();
1555 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001556
Borislav Petkove972d702009-07-17 23:55:16 +00001557 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Borislav Petkov54abf372008-02-06 02:57:52 +01001559 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001561 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001563 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001565
Borislav Petkov49d80782009-06-07 15:37:06 +02001566 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1567 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001569
Borislav Petkov54abf372008-02-06 02:57:52 +01001570 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001572 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001573 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001576 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ide_tape_put(tape);
1578 unlock_kernel();
1579 return 0;
1580}
1581
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001582static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001585 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001586 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001587 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 idetape_create_inquiry_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001590 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001591 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1592 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return;
1594 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001595 memcpy(vendor_id, &pc_buf[8], 8);
1596 memcpy(product_id, &pc_buf[16], 16);
1597 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001598
Borislav Petkov801bd322008-09-27 19:32:17 +02001599 ide_fixstring(vendor_id, 8, 0);
1600 ide_fixstring(product_id, 16, 0);
1601 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001602
Borislav Petkov801bd322008-09-27 19:32:17 +02001603 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001604 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
1607/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001608 * Ask the tape about its various parameters. In particular, we will adjust our
1609 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001611static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001614 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001615 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001616 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001619 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001620 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1621 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001622 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001623 put_unaligned(52, (u16 *)&tape->caps[12]);
1624 put_unaligned(540, (u16 *)&tape->caps[14]);
1625 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return;
1627 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001628 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Borislav Petkovb6422012008-02-02 19:56:49 +01001630 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001631 speed = be16_to_cpup((__be16 *)&caps[14]);
1632 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001634 *(u16 *)&caps[8] = max_speed;
1635 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1636 *(u16 *)&caps[14] = speed;
1637 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001638
1639 if (!speed) {
1640 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1641 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001642 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001644 if (!max_speed) {
1645 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1646 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001647 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Borislav Petkovb6422012008-02-02 19:56:49 +01001650 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001651
1652 /* device lacks locking support according to capabilities page */
1653 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001654 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001655
Borislav Petkovb6422012008-02-02 19:56:49 +01001656 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001657 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001658 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001659 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001662#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001663#define ide_tape_devset_get(name, field) \
1664static int get_##name(ide_drive_t *drive) \
1665{ \
1666 idetape_tape_t *tape = drive->driver_data; \
1667 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001669
1670#define ide_tape_devset_set(name, field) \
1671static int set_##name(ide_drive_t *drive, int arg) \
1672{ \
1673 idetape_tape_t *tape = drive->driver_data; \
1674 tape->field = arg; \
1675 return 0; \
1676}
1677
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001678#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001679ide_tape_devset_get(_name, _field) \
1680ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001681IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001682
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001683#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001684ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001685IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001686
1687static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1688static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1689static int divf_buffer(ide_drive_t *drive) { return 2; }
1690static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1691
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001692ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001693
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001694ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001695
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001696ide_tape_devset_r_field(avg_speed, avg_speed);
1697ide_tape_devset_r_field(speed, caps[14]);
1698ide_tape_devset_r_field(buffer, caps[16]);
1699ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001700
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001701static const struct ide_proc_devset idetape_settings[] = {
1702 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1703 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1704 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001705 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1706 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1707 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1708 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001709 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001710};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001711#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001714 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001716 * 1. Initialize our various state variables.
1717 * 2. Ask the tape for its capabilities.
1718 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1719 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001721 * Note that at this point ide.c already assigned us an irq, so that we can
1722 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001724static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Borislav Petkov83042b22008-04-27 15:38:27 +02001726 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001728 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001729 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Borislav Petkove972d702009-07-17 23:55:16 +00001731 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1732
1733 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001734
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001735 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1736
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001737 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1738 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1739 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001740 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001744 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001745 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 tape->minor = minor;
1748 tape->name[0] = 'h';
1749 tape->name[1] = 't';
1750 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001751 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 idetape_get_inquiry_results(drive);
1754 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001755 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001757 tape->buffer_size = *ctl * tape->blk_size;
1758 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001760 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001761 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001763 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Borislav Petkov83042b22008-04-27 15:38:27 +02001765 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001766 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Borislav Petkovf73850a2008-04-27 15:38:33 +02001768 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001771 * Ensure that the number we got makes sense; limit it within
1772 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001774 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1775 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001777 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001778 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001779 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1780 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001781 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001782 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001784 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Russell King4031bbe2006-01-06 11:41:00 +00001787static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001791 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001792 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 ide_unregister_region(tape->disk);
1794
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001795 mutex_lock(&idetape_ref_mutex);
1796 put_device(&tape->dev);
1797 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001800static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001802 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 ide_drive_t *drive = tape->drive;
1804 struct gendisk *g = tape->disk;
1805
Tejun Heo963da552009-04-19 08:46:02 +09001806 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001807
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001808 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001810 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001811 device_destroy(idetape_sysfs_class,
1812 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 idetape_devs[tape->minor] = NULL;
1814 g->private_data = NULL;
1815 put_disk(g);
1816 kfree(tape);
1817}
1818
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001819#ifdef CONFIG_IDE_PROC_FS
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001820static int idetape_name_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001822 ide_drive_t *drive = (ide_drive_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001825 seq_printf(m, "%s\n", tape->name);
1826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001829static int idetape_name_proc_open(struct inode *inode, struct file *file)
1830{
1831 return single_open(file, idetape_name_proc_show, PDE(inode)->data);
1832}
1833
1834static const struct file_operations idetape_name_proc_fops = {
1835 .owner = THIS_MODULE,
1836 .open = idetape_name_proc_open,
1837 .read = seq_read,
1838 .llseek = seq_lseek,
1839 .release = single_release,
1840};
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842static ide_proc_entry_t idetape_proc[] = {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001843 { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops },
1844 { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops },
1845 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001847
1848static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1849{
1850 return idetape_proc;
1851}
1852
1853static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1854{
1855 return idetape_settings;
1856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#endif
1858
Russell King4031bbe2006-01-06 11:41:00 +00001859static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001861static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001862 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001863 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001864 .name = "ide-tape",
1865 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001866 },
Russell King4031bbe2006-01-06 11:41:00 +00001867 .probe = ide_tape_probe,
1868 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001871#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001872 .proc_entries = ide_tape_proc_entries,
1873 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875};
1876
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001877/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001878static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .owner = THIS_MODULE,
1880 .read = idetape_chrdev_read,
1881 .write = idetape_chrdev_write,
1882 .ioctl = idetape_chrdev_ioctl,
1883 .open = idetape_chrdev_open,
1884 .release = idetape_chrdev_release,
1885};
1886
Al Viroa4600f82008-03-02 10:27:58 -05001887static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001889 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001891 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return -ENXIO;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return 0;
1895}
1896
Al Viroa4600f82008-03-02 10:27:58 -05001897static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001899 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 0;
1903}
1904
Al Viroa4600f82008-03-02 10:27:58 -05001905static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 unsigned int cmd, unsigned long arg)
1907{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001908 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04001910 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (err == -EINVAL)
1912 err = idetape_blkdev_ioctl(drive, cmd, arg);
1913 return err;
1914}
1915
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001916static const struct block_device_operations idetape_block_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001918 .open = idetape_open,
1919 .release = idetape_release,
1920 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921};
1922
Russell King4031bbe2006-01-06 11:41:00 +00001923static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 idetape_tape_t *tape;
1926 struct gendisk *g;
1927 int minor;
1928
Borislav Petkove972d702009-07-17 23:55:16 +00001929 ide_debug_log(IDE_DBG_FUNC, "enter");
1930
1931 if (!strstr(DRV_NAME, drive->driver_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (drive->media != ide_tape)
1935 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001936
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001937 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1938 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001939 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1940 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto failed;
1942 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001943 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001945 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1946 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto failed;
1948 }
1949
1950 g = alloc_disk(1 << PARTN_BITS);
1951 if (!g)
1952 goto out_free_tape;
1953
1954 ide_init_disk(g, drive);
1955
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001956 tape->dev.parent = &drive->gendev;
1957 tape->dev.release = ide_tape_release;
1958 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1959
1960 if (device_register(&tape->dev))
1961 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 tape->drive = drive;
1964 tape->driver = &idetape_driver;
1965 tape->disk = g;
1966
1967 g->private_data = &tape->driver;
1968
1969 drive->driver_data = tape;
1970
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001971 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 for (minor = 0; idetape_devs[minor]; minor++)
1973 ;
1974 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001975 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 idetape_setup(drive, tape, minor);
1978
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07001979 device_create(idetape_sysfs_class, &drive->gendev,
1980 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1981 device_create(idetape_sysfs_class, &drive->gendev,
1982 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
1983 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 g->fops = &idetape_block_ops;
1986 ide_register_region(g);
1987
1988 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001989
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001990out_free_disk:
1991 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992out_free_tape:
1993 kfree(tape);
1994failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001995 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001998static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002000 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002001 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 unregister_chrdev(IDETAPE_MAJOR, "ht");
2003}
2004
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002005static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Will Dysond5dee802005-09-16 02:55:07 -07002007 int error = 1;
2008 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2009 if (IS_ERR(idetape_sysfs_class)) {
2010 idetape_sysfs_class = NULL;
2011 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2012 error = -EBUSY;
2013 goto out;
2014 }
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002017 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2018 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002019 error = -EBUSY;
2020 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Will Dysond5dee802005-09-16 02:55:07 -07002022
2023 error = driver_register(&idetape_driver.gen_driver);
2024 if (error)
2025 goto out_free_driver;
2026
2027 return 0;
2028
2029out_free_driver:
2030 driver_unregister(&idetape_driver.gen_driver);
2031out_free_class:
2032 class_destroy(idetape_sysfs_class);
2033out:
2034 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Kay Sievers263756e2005-12-12 18:03:44 +01002037MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038module_init(idetape_init);
2039module_exit(idetape_exit);
2040MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002041MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2042MODULE_LICENSE("GPL");