blob: 70b499a617d8022ca4c56d5dd2a22b8aad4ad909 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-tape"
19
Borislav Petkovdfe79932008-02-06 02:57:55 +010020#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/genhd.h>
34#include <linux/slab.h>
35#include <linux/pci.h>
36#include <linux/ide.h>
37#include <linux/smp_lock.h>
38#include <linux/completion.h>
39#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtio.h>
49
Borislav Petkov8004a8c2008-02-06 02:57:51 +010050enum {
51 /* output errors only */
52 DBG_ERR = (1 << 0),
53 /* output all sense key/asc */
54 DBG_SENSE = (1 << 1),
55 /* info regarding all chrdev-related procedures */
56 DBG_CHRDEV = (1 << 2),
57 /* all remaining procedures */
58 DBG_PROCS = (1 << 3),
Borislav Petkov8004a8c2008-02-06 02:57:51 +010059};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * The following parameter is used to select the point in the internal tape fifo
85 * in which we will start to refill the buffer. Decreasing the following
86 * parameter will improve the system's latency and interactive response, while
87 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010089#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010092 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010094 * Polling for DSC (a single bit in the status register) is a very important
95 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * 1. Before a read/write packet command, to ensure that we can transfer data
98 * from/to the tape's data buffers, without causing an actual media access.
99 * In case the tape is not ready yet, we take out our request from the device
100 * request queue, so that ide.c could service requests from the other device
101 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100103 * 2. After the successful initialization of a "media access packet command",
104 * which is a command that can take a long time to complete (the interval can
105 * range from several seconds to even an hour). Again, we postpone our request
106 * in the middle to free the bus for the other device. The polling frequency
107 * here should be lower than the read/write frequency since those media access
108 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
109 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
110 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * We also set a timeout for the timer, in case something goes wrong. The
113 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100115
116/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
118#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
119#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
120#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
121#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
122#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
123#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
124
125/*************************** End of tunable parameters ***********************/
126
Borislav Petkov54abf372008-02-06 02:57:52 +0100127/* tape directions */
128enum {
129 IDETAPE_DIR_NONE = (1 << 0),
130 IDETAPE_DIR_READ = (1 << 1),
131 IDETAPE_DIR_WRITE = (1 << 2),
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200135 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 atomic_t b_count;
137 struct idetape_bh *b_reqnext;
138 char *b_data;
139};
140
Borislav Petkov03056b92008-04-18 00:46:26 +0200141/* Tape door status */
142#define DOOR_UNLOCKED 0
143#define DOOR_LOCKED 1
144#define DOOR_EXPLICITLY_LOCKED 2
145
146/* Some defines for the SPACE command */
147#define IDETAPE_SPACE_OVER_FILEMARK 1
148#define IDETAPE_SPACE_TO_EOD 3
149
150/* Some defines for the LOAD UNLOAD command */
151#define IDETAPE_LU_LOAD_MASK 1
152#define IDETAPE_LU_RETENSION_MASK 2
153#define IDETAPE_LU_EOT_MASK 4
154
Borislav Petkov03056b92008-04-18 00:46:26 +0200155/* Error codes returned in rq->errors to the higher part of the driver. */
156#define IDETAPE_ERROR_GENERAL 101
157#define IDETAPE_ERROR_FILEMARK 102
158#define IDETAPE_ERROR_EOD 103
159
160/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
161#define IDETAPE_BLOCK_DESCRIPTOR 0
162#define IDETAPE_CAPABILITIES_PAGE 0x2a
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100165 * Most of our global data which we need to save even as we leave the driver due
166 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168typedef struct ide_tape_obj {
169 ide_drive_t *drive;
170 ide_driver_t *driver;
171 struct gendisk *disk;
172 struct kref kref;
173
174 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * pc points to the current processed packet command.
176 *
177 * failed_pc points to the last failed packet command, or contains
178 * NULL if we do not need to retry any packet command. This is
179 * required since an additional packet command is needed before the
180 * retry, to get detailed information on what went wrong.
181 */
182 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200183 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200185 struct ide_atapi_pc *failed_pc;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200186 /* used by REQ_IDETAPE_{READ,WRITE} requests */
187 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200188
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200189 struct ide_atapi_pc request_sense_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200190 struct request request_sense_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100193 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100195 * While polling for DSC we use postponed_rq to postpone the current
196 * request so that ide.c will be able to service pending requests on the
197 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200198 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
200 struct request *postponed_rq;
201 /* The time in which we started polling for DSC */
202 unsigned long dsc_polling_start;
203 /* Timer used to poll for dsc */
204 struct timer_list dsc_timer;
205 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100206 unsigned long best_dsc_rw_freq;
207 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long dsc_timeout;
209
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100210 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 u8 partition;
212 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100213 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100215 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 u8 sense_key, asc, ascq;
217
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100218 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned int minor;
220 /* device name */
221 char name[4];
222 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100223 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Borislav Petkov54bb2072008-02-06 02:57:52 +0100225 /* tape block size, usually 512 or 1024 bytes */
226 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100230 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100233 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100235 * At most, there is only one ide-tape originated data transfer request
236 * in the device request queue. This allows ide.c to easily service
237 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200239
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100240 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200241 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200242 /* merge buffer */
243 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200244 /* size of the merge buffer */
245 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200246 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 struct idetape_bh *bh;
248 char *b_data;
249 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100250
Borislav Petkova997a432008-04-27 15:38:33 +0200251 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Wasted space in each stage */
253 int excess_bh_size;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100256 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100258 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long avg_time;
260 int avg_size;
261 int avg_speed;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* the door is currently locked */
264 int door_locked;
265 /* the tape hardware is write protected */
266 char drv_write_prot;
267 /* the tape is write protected (hardware or opened as read-only) */
268 char write_prot;
269
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100270 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271} idetape_tape_t;
272
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800273static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Will Dysond5dee802005-09-16 02:55:07 -0700275static struct class *idetape_sysfs_class;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
278
279#define ide_tape_g(disk) \
280 container_of((disk)->private_data, struct ide_tape_obj, driver)
281
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200282static void ide_tape_release(struct kref *);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
285{
286 struct ide_tape_obj *tape = NULL;
287
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800288 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 tape = ide_tape_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200290 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200291 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200292 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200293 else
294 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200295 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800296 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return tape;
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static void ide_tape_put(struct ide_tape_obj *tape)
301{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200302 ide_drive_t *drive = tape->drive;
303
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800304 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 kref_put(&tape->kref, ide_tape_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200306 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800307 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100311 * The variables below are used for the character device interface. Additional
312 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100314static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316#define ide_tape_f(file) ((file)->private_data)
317
318static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
319{
320 struct ide_tape_obj *tape = NULL;
321
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800322 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 tape = idetape_devs[i];
324 if (tape)
325 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800326 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return tape;
328}
329
Borislav Petkovd236d742008-04-18 00:46:27 +0200330static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100331 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct idetape_bh *bh = pc->bh;
334 int count;
335
336 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (bh == NULL) {
338 printk(KERN_ERR "ide-tape: bh == NULL in "
339 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200340 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
342 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100343 count = min(
344 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
345 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200346 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100347 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 bcount -= count;
349 atomic_add(count, &bh->b_count);
350 if (atomic_read(&bh->b_count) == bh->b_size) {
351 bh = bh->b_reqnext;
352 if (bh)
353 atomic_set(&bh->b_count, 0);
354 }
355 }
356 pc->bh = bh;
357}
358
Borislav Petkovd236d742008-04-18 00:46:27 +0200359static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100360 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct idetape_bh *bh = pc->bh;
363 int count;
364
365 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100367 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
368 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200372 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 bcount -= count;
374 pc->b_data += count;
375 pc->b_count -= count;
376 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100377 bh = bh->b_reqnext;
378 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (bh) {
380 pc->b_data = bh->b_data;
381 pc->b_count = atomic_read(&bh->b_count);
382 }
383 }
384 }
385}
386
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200387static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct idetape_bh *bh = pc->bh;
390 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200391 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Borislav Petkov346331f2008-04-18 00:46:26 +0200393 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return;
395 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100397 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
398 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
402 atomic_set(&bh->b_count, count);
403 if (atomic_read(&bh->b_count) == bh->b_size)
404 bh = bh->b_reqnext;
405 bcount -= count;
406 }
407 pc->bh = bh;
408}
409
410/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100411 * called on each failed packet command retry to analyze the request sense. We
412 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100414static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200417 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Borislav Petkov1b5db432008-02-02 19:56:48 +0100419 tape->sense_key = sense[2] & 0xF;
420 tape->asc = sense[12];
421 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100422
423 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
424 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Borislav Petkovd236d742008-04-18 00:46:27 +0200426 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200427 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200428 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100429 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200430 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200431 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 /*
435 * If error was the result of a zero-length read or write command,
436 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
437 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
438 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100439 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100440 /* length == 0 */
441 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
442 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* don't report an error, everything's ok */
444 pc->error = 0;
445 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200446 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100449 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200451 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100453 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100454 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
455 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200457 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100460 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100461 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200463 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200465 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200466 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
468 }
469}
470
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200471/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200472static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200474 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200476 while (bh) {
477 u32 size = bh->b_size;
478
479 while (size) {
480 unsigned int order = fls(size >> PAGE_SHIFT)-1;
481
482 if (bh->b_data)
483 free_pages((unsigned long)bh->b_data, order);
484
485 size &= (order-1);
486 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 prev_bh = bh;
489 bh = bh->b_reqnext;
490 kfree(prev_bh);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
495{
496 struct request *rq = HWGROUP(drive)->rq;
497 idetape_tape_t *tape = drive->driver_data;
498 unsigned long flags;
499 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100501 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100504 case 0: error = IDETAPE_ERROR_GENERAL; break;
505 case 1: error = 0; break;
506 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 rq->errors = error;
509 if (error)
510 tape->failed_pc = NULL;
511
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100512 if (!blk_special_request(rq)) {
513 ide_end_request(drive, uptodate, nr_sects);
514 return 0;
515 }
516
Borislav Petkov54bb2072008-02-06 02:57:52 +0100517 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Borislav Petkov54bb2072008-02-06 02:57:52 +0100521 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 0;
523}
524
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200525static void ide_tape_handle_dsc(ide_drive_t *);
526
527static void ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200530 struct ide_atapi_pc *pc = tape->pc;
531 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100533 debug_log(DBG_PROCS, "Enter %s\n", __func__);
534
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200535 if (dsc)
536 ide_tape_handle_dsc(drive);
537
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200538 if (tape->failed_pc == pc)
539 tape->failed_pc = NULL;
540
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200541 if (pc->c[0] == REQUEST_SENSE) {
542 if (uptodate)
543 idetape_analyze_error(drive, pc->buf);
544 else
545 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
546 "itself - Aborting request!\n");
547 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
548 struct request *rq = drive->hwif->hwgroup->rq;
549 int blocks = pc->xferred / tape->blk_size;
550
551 tape->avg_size += blocks * tape->blk_size;
552
553 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
554 tape->avg_speed = tape->avg_size * HZ /
555 (jiffies - tape->avg_time) / 1024;
556 tape->avg_size = 0;
557 tape->avg_time = jiffies;
558 }
559
560 tape->first_frame += blocks;
561 rq->current_nr_sectors -= blocks;
562
563 if (pc->error)
564 uptodate = pc->error;
565 } else if (pc->c[0] == READ_POSITION && uptodate) {
566 u8 *readpos = tape->pc->buf;
567
568 debug_log(DBG_SENSE, "BOP - %s\n",
569 (readpos[0] & 0x80) ? "Yes" : "No");
570 debug_log(DBG_SENSE, "EOP - %s\n",
571 (readpos[0] & 0x40) ? "Yes" : "No");
572
573 if (readpos[0] & 0x4) {
574 printk(KERN_INFO "ide-tape: Block location is unknown"
575 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200576 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200577 uptodate = 0;
578 } else {
579 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200580 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200581
582 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200583 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200584 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200587
588 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Borislav Petkovd236d742008-04-18 00:46:27 +0200591static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200593 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100594 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200596 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * idetape_retry_pc is called when an error was detected during the
601 * last packet command. We queue a request sense packet command in
602 * the head of the request list.
603 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200604static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200606 struct ide_tape_obj *tape = drive->driver_data;
607 struct request *rq = &tape->request_sense_rq;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200608 struct ide_atapi_pc *pc = &tape->request_sense_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100610 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200612 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200613 ide_queue_pc_head(drive, tape->disk, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100617 * Postpone the current request so that ide.c will be able to service requests
618 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100620static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 idetape_tape_t *tape = drive->driver_data;
623
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100624 debug_log(DBG_PROCS, "Enter %s\n", __func__);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100627 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200630static void ide_tape_handle_dsc(ide_drive_t *drive)
631{
632 idetape_tape_t *tape = drive->driver_data;
633
634 /* Media access command */
635 tape->dsc_polling_start = jiffies;
636 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
637 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
638 /* Allow ide.c to handle other requests */
639 idetape_postpone_request(drive);
640}
641
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200642static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200643 unsigned int bcount, int write)
644{
645 if (write)
646 idetape_output_buffers(drive, pc, bcount);
647 else
648 idetape_input_buffers(drive, pc, bcount);
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200649
650 return bcount;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200651}
Borislav Petkova1efc852008-02-06 02:57:52 +0100652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100654 * This is the usual interrupt handler which will be called during a packet
655 * command. We will transfer some of the data (as requested by the drive) and
656 * will re-point interrupt handler to us. When data transfer is finished, we
657 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100658 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100660static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Bartlomiej Zolnierkiewiczd6e29552008-10-10 22:39:39 +0200664 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, WAIT_TAPE_CMD,
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200665 NULL, idetape_update_buffers, idetape_retry_pc,
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200666 ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100670 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100672 * The current Packet Command is available in tape->pc, and will not change
673 * until we finish handling it. Each packet command is associated with a
674 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100676 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100678 * 1. idetape_issue_pc will send the packet command to the drive, and will set
679 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100681 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
682 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100684 * 3. ATAPI Tape media access commands have immediate status with a delayed
685 * process. In case of a successful initiation of a media access packet command,
686 * the DSC bit will be set when the actual execution of the command is finished.
687 * Since the tape drive will not issue an interrupt, we have to poll for this
688 * event. In this case, we define the request as "low priority request" by
689 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
690 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100692 * ide.c will then give higher priority to requests which originate from the
693 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100695 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100697 * 5. In case an error was found, we queue a request sense packet command in
698 * front of the request queue and retry the operation up to
699 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100701 * 6. In case no error was found, or we decided to give up and not to retry
702 * again, the callback function will be called and then we will handle the next
703 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
705static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
706{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200709 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
Bartlomiej Zolnierkiewiczd6e29552008-10-10 22:39:39 +0200710 WAIT_TAPE_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Borislav Petkovd236d742008-04-18 00:46:27 +0200713static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
714 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Borislav Petkov90699ce2008-02-02 19:56:50 +0100718 if (tape->pc->c[0] == REQUEST_SENSE &&
719 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
721 "Two request sense in serial were issued\n");
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Borislav Petkov90699ce2008-02-02 19:56:50 +0100724 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 tape->failed_pc = pc;
726 /* Set the current packet command */
727 tape->pc = pc;
728
729 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200730 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100732 * We will "abort" retrying a packet command in case legitimate
733 * error code was received (crossing a filemark, or end of the
734 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200736 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100737 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 tape->sense_key == 2 && tape->asc == 4 &&
739 (tape->ascq == 1 || tape->ascq == 8))) {
740 printk(KERN_ERR "ide-tape: %s: I/O error, "
741 "pc = %2x, key = %2x, "
742 "asc = %2x, ascq = %2x\n",
743 tape->name, pc->c[0],
744 tape->sense_key, tape->asc,
745 tape->ascq);
746 }
747 /* Giving up */
748 pc->error = IDETAPE_ERROR_GENERAL;
749 }
750 tape->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200751 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200752 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100754 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200758 return ide_issue_pc(drive, pc, idetape_transfer_pc,
Bartlomiej Zolnierkiewiczd6e29552008-10-10 22:39:39 +0200759 WAIT_TAPE_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100762/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200763static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200765 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100766 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100768 /* DBD = 1 - Don't return block descriptors */
769 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 pc->c[2] = page_code;
771 /*
772 * Changed pc->c[3] to 0 (255 will at best return unused info).
773 *
774 * For SCSI this byte is defined as subpage instead of high byte
775 * of length and some IDE drives seem to interpret it this way
776 * and return an error when 255 is used.
777 */
778 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100779 /* We will just discard data in that case */
780 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200782 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200784 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200786 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100789static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200791 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200793 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100794 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200796 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100797
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200798 if (stat & ATA_DSC) {
799 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100801 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 printk(KERN_ERR "ide-tape: %s: I/O error, ",
803 tape->name);
804 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200805 idetape_retry_pc(drive);
806 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 } else {
810 pc->error = IDETAPE_ERROR_GENERAL;
811 tape->failed_pc = NULL;
812 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200813 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return ide_stopped;
815}
816
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200817static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200818 struct ide_atapi_pc *pc, struct request *rq,
819 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Borislav Petkov0014c752008-07-23 19:56:00 +0200821 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
822 unsigned int length = rq->current_nr_sectors;
823
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200824 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100825 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200828 pc->buf = NULL;
829 pc->buf_size = length * tape->blk_size;
830 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200831 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200832 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200834 if (opcode == READ_6) {
835 pc->c[0] = READ_6;
836 atomic_set(&bh->b_count, 0);
837 } else if (opcode == WRITE_6) {
838 pc->c[0] = WRITE_6;
839 pc->flags |= PC_FLAG_WRITING;
840 pc->b_data = bh->b_data;
841 pc->b_count = atomic_read(&bh->b_count);
842 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200843
844 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static ide_startstop_t idetape_do_request(ide_drive_t *drive,
848 struct request *rq, sector_t block)
849{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200850 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200852 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100854 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Mark de Wever730616b2008-10-10 22:39:17 +0200856 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
857 " current_nr_sectors: %u\n",
858 (unsigned long long)rq->sector, rq->nr_sectors,
859 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Jens Axboe4aff5e22006-08-10 08:44:47 +0200861 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100862 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200864 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ide_end_request(drive, 0, 0);
866 return ide_stopped;
867 }
868
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100869 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200870 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
871 pc = tape->failed_pc;
872 goto out;
873 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (postponed_rq != NULL)
876 if (rq != postponed_rq) {
877 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
878 "Two DSC requests were queued\n");
879 idetape_end_request(drive, 0, 0);
880 return ide_stopped;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 tape->postponed_rq = NULL;
884
885 /*
886 * If the tape is still busy, postpone our request and service
887 * the other device meanwhile.
888 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200889 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Borislav Petkov83dd5732008-07-23 19:56:00 +0200891 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200892 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200895 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 drive->post_reset = 0;
897 }
898
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200899 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200900 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (postponed_rq == NULL) {
902 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100903 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
905 } else if (time_after(jiffies, tape->dsc_timeout)) {
906 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
907 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200908 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 idetape_media_access_finished(drive);
910 return ide_stopped;
911 } else {
912 return ide_do_reset(drive);
913 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100914 } else if (time_after(jiffies,
915 tape->dsc_polling_start +
916 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100917 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 idetape_postpone_request(drive);
919 return ide_stopped;
920 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200921 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200922 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200923 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto out;
925 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200926 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200927 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200928 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
930 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200931 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200932 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200933 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
934 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto out;
936 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200937 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 idetape_media_access_finished(drive);
939 return ide_stopped;
940 }
941 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200942
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200943out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100944 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
Borislav Petkov41aa1702008-04-27 15:38:32 +0200948 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +0200949 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100950 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Borislav Petkov41aa1702008-04-27 15:38:32 +0200952 * It returns a pointer to the newly allocated buffer, or NULL in case of
953 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200955static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
956 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200958 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +0200959 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200960 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 char *b_data = NULL;
962
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200963 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
964 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (bh == NULL)
966 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200967
968 order = fls(pages) - 1;
969 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100970 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200972 b_allocd = (1 << order) * PAGE_SIZE;
973 pages &= (order-1);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200976 memset(bh->b_data, 0, b_allocd);
977 bh->b_reqnext = NULL;
978 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 atomic_set(&bh->b_count, full ? bh->b_size : 0);
980
Borislav Petkov41aa1702008-04-27 15:38:32 +0200981 while (pages) {
982 order = fls(pages) - 1;
983 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100984 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200986 b_allocd = (1 << order) * PAGE_SIZE;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200989 memset(b_data, 0, b_allocd);
990
991 /* newly allocated page frames below buffer header or ...*/
992 if (bh->b_data == b_data + b_allocd) {
993 bh->b_size += b_allocd;
994 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200996 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 continue;
998 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200999 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001001 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001003 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 continue;
1005 }
1006 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001007 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1008 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001009 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto abort;
1011 }
1012 bh->b_reqnext = NULL;
1013 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001014 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1016 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001017
1018 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 bh->b_size -= tape->excess_bh_size;
1022 if (full)
1023 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001024 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001026 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return NULL;
1028}
1029
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001030static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001031 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 struct idetape_bh *bh = tape->bh;
1034 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001035 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001039 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1040 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001041 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001043 count = min((unsigned int)
1044 (bh->b_size - atomic_read(&bh->b_count)),
1045 (unsigned int)n);
1046 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1047 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001048 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 n -= count;
1050 atomic_add(count, &bh->b_count);
1051 buf += count;
1052 if (atomic_read(&bh->b_count) == bh->b_size) {
1053 bh = bh->b_reqnext;
1054 if (bh)
1055 atomic_set(&bh->b_count, 0);
1056 }
1057 }
1058 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001059 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001062static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001063 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct idetape_bh *bh = tape->bh;
1066 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001067 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001071 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1072 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001073 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001076 if (copy_to_user(buf, tape->b_data, count))
1077 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 n -= count;
1079 tape->b_data += count;
1080 tape->b_count -= count;
1081 buf += count;
1082 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001083 bh = bh->b_reqnext;
1084 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (bh) {
1086 tape->b_data = bh->b_data;
1087 tape->b_count = atomic_read(&bh->b_count);
1088 }
1089 }
1090 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001094static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001096 struct idetape_bh *bh = tape->merge_bh;
1097 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001098
Borislav Petkov54abf372008-02-06 02:57:52 +01001099 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 atomic_set(&bh->b_count, 0);
1101 else {
1102 tape->b_data = bh->b_data;
1103 tape->b_count = atomic_read(&bh->b_count);
1104 }
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001108 * Write a filemark if write_filemark=1. Flush the device buffers without
1109 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001111static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001112 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001114 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001115 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001117 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1121{
1122 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001123 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int load_attempted = 0;
1125
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001126 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001127 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 timeout += jiffies;
1129 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +02001130 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return 0;
1132 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001133 || (tape->asc == 0x3A)) {
1134 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (load_attempted)
1136 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001137 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 load_attempted = 1;
1139 /* not about to be ready */
1140 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1141 (tape->ascq == 1 || tape->ascq == 8)))
1142 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001143 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 return -EIO;
1146}
1147
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001148static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001150 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001151 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int rc;
1153
1154 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001155 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001156 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return rc;
1158 idetape_wait_ready(drive, 60 * 5 * HZ);
1159 return 0;
1160}
1161
Borislav Petkovd236d742008-04-18 00:46:27 +02001162static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001164 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001165 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001166 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001169static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001172 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int position;
1174
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001175 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001178 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001180 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return position;
1182}
1183
Borislav Petkovd236d742008-04-18 00:46:27 +02001184static void idetape_create_locate_cmd(ide_drive_t *drive,
1185 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001186 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001188 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001189 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001191 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001193 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001196static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Borislav Petkov54abf372008-02-06 02:57:52 +01001200 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001203 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001204 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001205 if (tape->merge_bh != NULL) {
1206 ide_tape_kfree_buffer(tape);
1207 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Borislav Petkov54abf372008-02-06 02:57:52 +01001210 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001214 * Position the tape to the requested block using the LOCATE packet command.
1215 * A READ POSITION command is then issued to check where we are positioned. Like
1216 * all higher level operations, we queue the commands at the tail of the request
1217 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001219static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1220 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001223 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001225 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Borislav Petkov54abf372008-02-06 02:57:52 +01001227 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001228 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 idetape_wait_ready(drive, 60 * 5 * HZ);
1230 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001231 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (retval)
1233 return (retval);
1234
1235 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001236 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001239static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001240 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int seek, position;
1244
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001245 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (restore_position) {
1247 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001248 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001250 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001251 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return;
1253 }
1254 }
1255}
1256
1257/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001258 * Generate a read/write request for the block device interface and wait for it
1259 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001261static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1262 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001265 struct request *rq;
1266 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001268 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1269
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001270 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1271 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001272 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001273 rq->rq_disk = tape->disk;
1274 rq->special = (void *)bh;
1275 rq->sector = tape->first_frame;
1276 rq->nr_sectors = blocks;
1277 rq->current_nr_sectors = blocks;
1278 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1279
1280 errors = rq->errors;
1281 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1282 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1285 return 0;
1286
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001287 if (tape->merge_bh)
1288 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001289 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Borislav Petkovd236d742008-04-18 00:46:27 +02001294static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001296 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001297 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001298 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001299 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Borislav Petkovd236d742008-04-18 00:46:27 +02001302static void idetape_create_rewind_cmd(ide_drive_t *drive,
1303 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001305 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001306 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001307 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Borislav Petkovd236d742008-04-18 00:46:27 +02001310static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001312 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001313 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001315 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Borislav Petkovd236d742008-04-18 00:46:27 +02001318static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001320 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001321 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001322 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001324 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Borislav Petkov97c566c2008-04-27 15:38:25 +02001327/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001328static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001332 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001334 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001335 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Borislav Petkovd9df9372008-04-27 15:38:34 +02001338static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 idetape_tape_t *tape = drive->driver_data;
1341 int blocks, min;
1342 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001343
Borislav Petkov54abf372008-02-06 02:57:52 +01001344 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001345 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001346 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return;
1348 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001349 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001351 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001353 if (tape->merge_bh_size) {
1354 blocks = tape->merge_bh_size / tape->blk_size;
1355 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 unsigned int i;
1357
1358 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001359 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001360 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 bh = tape->bh->b_reqnext;
1362 while (bh) {
1363 atomic_set(&bh->b_count, 0);
1364 bh = bh->b_reqnext;
1365 }
1366 bh = tape->bh;
1367 while (i) {
1368 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001369 printk(KERN_INFO "ide-tape: bug,"
1370 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001373 min = min(i, (unsigned int)(bh->b_size -
1374 atomic_read(&bh->b_count)));
1375 memset(bh->b_data + atomic_read(&bh->b_count),
1376 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 atomic_add(min, &bh->b_count);
1378 i -= min;
1379 bh = bh->b_reqnext;
1380 }
1381 }
1382 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001383 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001385 if (tape->merge_bh != NULL) {
1386 ide_tape_kfree_buffer(tape);
1387 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001389 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Borislav Petkov83042b22008-04-27 15:38:27 +02001392static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001398 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1399 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001400 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 idetape_flush_tape_buffers(drive);
1402 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001403 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001404 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001405 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001406 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001408 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1409 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001411 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001414 * Issue a read 0 command to ensure that DSC handshake is
1415 * switched from completion mode to buffer available mode.
1416 * No point in issuing this if DSC overlap isn't supported, some
1417 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001420 bytes_read = idetape_queue_rw_tail(drive,
1421 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001422 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001424 ide_tape_kfree_buffer(tape);
1425 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001426 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return bytes_read;
1428 }
1429 }
1430 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
1433}
1434
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001435/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001436static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001440 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001442 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001443 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return 0;
1445
Borislav Petkov83042b22008-04-27 15:38:27 +02001446 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001448 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001449 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001452static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 idetape_tape_t *tape = drive->driver_data;
1455 struct idetape_bh *bh;
1456 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 while (bcount) {
1459 unsigned int count;
1460
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001461 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001462 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001464 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001466 atomic_set(&bh->b_count,
1467 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1469 count -= atomic_read(&bh->b_count);
1470 bh = bh->b_reqnext;
1471 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001472 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001473 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001478 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1479 * currently support only one partition.
1480 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001481static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001483 struct ide_tape_obj *tape = drive->driver_data;
1484 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001486 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001487
1488 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001491 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (retval)
1493 return retval;
1494
1495 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001496 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (retval)
1498 return retval;
1499 return 0;
1500}
1501
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001502/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001503static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1504 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 void __user *argp = (void __user *)arg;
1508
Borislav Petkovd59823f2008-02-02 19:56:51 +01001509 struct idetape_config {
1510 int dsc_rw_frequency;
1511 int dsc_media_access_frequency;
1512 int nr_stages;
1513 } config;
1514
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001515 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001518 case 0x0340:
1519 if (copy_from_user(&config, argp, sizeof(config)))
1520 return -EFAULT;
1521 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001522 break;
1523 case 0x0350:
1524 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001525 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001526 if (copy_to_user(argp, &config, sizeof(config)))
1527 return -EFAULT;
1528 break;
1529 default:
1530 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 return 0;
1533}
1534
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001535static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1536 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001539 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001540 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001541 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001542 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (mt_count == 0)
1545 return 0;
1546 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001547 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001549 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
Borislav Petkov54abf372008-02-06 02:57:52 +01001552 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001553 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001554 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001556 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001560 case MTFSF:
1561 case MTBSF:
1562 idetape_create_space_cmd(&pc, mt_count - count,
1563 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001564 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001565 case MTFSFM:
1566 case MTBSFM:
1567 if (!sprev)
1568 return -EIO;
1569 retval = idetape_space_over_filemarks(drive, MTFSF,
1570 mt_count - count);
1571 if (retval)
1572 return retval;
1573 count = (MTBSFM == mt_op ? 1 : -1);
1574 return idetape_space_over_filemarks(drive, MTFSF, count);
1575 default:
1576 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1577 mt_op);
1578 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001583 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001585 * The tape is optimized to maximize throughput when it is transferring an
1586 * integral number of the "continuous transfer limit", which is a parameter of
1587 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001589 * As of version 1.3 of the driver, the character device provides an abstract
1590 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1591 * same backup/restore procedure is supported. The driver will internally
1592 * convert the requests to the recommended transfer unit, so that an unmatch
1593 * between the user's block size to the recommended size will only result in a
1594 * (slightly) increased driver overhead, but will no longer hit performance.
1595 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001597static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1598 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct ide_tape_obj *tape = ide_tape_f(file);
1601 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001602 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001603 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001604 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001606 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Borislav Petkov54abf372008-02-06 02:57:52 +01001608 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001609 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001610 if (count > tape->blk_size &&
1611 (count % tape->blk_size) == 0)
1612 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001614 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001615 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return rc;
1617 if (count == 0)
1618 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001619 if (tape->merge_bh_size) {
1620 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001622 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001623 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001625 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 count -= actually_read;
1627 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001628 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001629 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (bytes_read <= 0)
1631 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001632 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001633 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 buf += bytes_read;
1635 count -= bytes_read;
1636 actually_read += bytes_read;
1637 }
1638 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001639 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (bytes_read <= 0)
1641 goto finish;
1642 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001643 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001644 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001646 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001649 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001650 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 idetape_space_over_filemarks(drive, MTFSF, 1);
1653 return 0;
1654 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001655
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001656 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001659static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 size_t count, loff_t *ppos)
1661{
1662 struct ide_tape_obj *tape = ide_tape_f(file);
1663 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001664 ssize_t actually_written = 0;
1665 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001666 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /* The drive is write protected. */
1669 if (tape->write_prot)
1670 return -EACCES;
1671
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001672 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001675 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1676 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001677 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001678 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001679 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001681 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001683 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1684 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001686 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001687 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001690 * Issue a write 0 command to ensure that DSC handshake is
1691 * switched from completion mode to buffer available mode. No
1692 * point in issuing this if DSC overlap isn't supported, some
1693 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
1695 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001696 ssize_t retval = idetape_queue_rw_tail(drive,
1697 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001698 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001700 ide_tape_kfree_buffer(tape);
1701 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001702 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return retval;
1704 }
1705 }
1706 }
1707 if (count == 0)
1708 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001709 if (tape->merge_bh_size) {
1710 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001711 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001712 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001714 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001715 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001716 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001717 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001718 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001720 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 count -= actually_written;
1722
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001723 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001724 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001725 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001726 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (retval <= 0)
1728 return (retval);
1729 }
1730 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001731 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001732 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001733 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001734 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001735 buf += tape->buffer_size;
1736 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001737 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001738 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (retval <= 0)
1740 return (retval);
1741 }
1742 if (count) {
1743 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001744 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001745 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001746 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001748 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001751static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001753 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001754 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /* Write a filemark */
1757 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001758 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1760 return -EIO;
1761 }
1762 return 0;
1763}
1764
1765/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001766 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1767 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001769 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1770 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001771 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001773 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001775 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1776 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001778static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001781 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001782 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001783 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001785 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1786 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001789 case MTFSF:
1790 case MTFSFM:
1791 case MTBSF:
1792 case MTBSFM:
1793 if (!mt_count)
1794 return 0;
1795 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1796 default:
1797 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001801 case MTWEOF:
1802 if (tape->write_prot)
1803 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001804 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001805 for (i = 0; i < mt_count; i++) {
1806 retval = idetape_write_filemark(drive);
1807 if (retval)
1808 return retval;
1809 }
1810 return 0;
1811 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001812 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001813 if (idetape_rewind_tape(drive))
1814 return -EIO;
1815 return 0;
1816 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001817 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001818 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001819 case MTUNLOAD:
1820 case MTOFFL:
1821 /*
1822 * If door is locked, attempt to unlock before
1823 * attempting to eject.
1824 */
1825 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001826 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001827 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001828 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001829 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001830 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001831 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001832 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001833 return retval;
1834 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001835 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001836 return idetape_flush_tape_buffers(drive);
1837 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001838 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001839 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001840 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001841 case MTEOM:
1842 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001843 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001844 case MTERASE:
1845 (void)idetape_rewind_tape(drive);
1846 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001847 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001848 case MTSETBLK:
1849 if (mt_count) {
1850 if (mt_count < tape->blk_size ||
1851 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001853 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001854 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001855 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001856 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001857 return 0;
1858 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001859 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001860 return idetape_position_tape(drive,
1861 mt_count * tape->user_bs_factor, tape->partition, 0);
1862 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001863 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001864 return idetape_position_tape(drive, 0, mt_count, 0);
1865 case MTFSR:
1866 case MTBSR:
1867 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001868 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001869 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001871 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1872 return 0;
1873 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001874 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001875 if (retval)
1876 return retval;
1877 tape->door_locked = DOOR_UNLOCKED;
1878 return 0;
1879 default:
1880 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1881 mt_op);
1882 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884}
1885
1886/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001887 * Our character device ioctls. General mtio.h magnetic io commands are
1888 * supported here, and not in the corresponding block interface. Our own
1889 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001891static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1892 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 struct ide_tape_obj *tape = ide_tape_f(file);
1895 ide_drive_t *drive = tape->drive;
1896 struct mtop mtop;
1897 struct mtget mtget;
1898 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001899 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 void __user *argp = (void __user *)arg;
1901
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001902 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Borislav Petkov54abf372008-02-06 02:57:52 +01001904 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001905 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 idetape_flush_tape_buffers(drive);
1907 }
1908 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001909 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001910 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001911 position = idetape_read_position(drive);
1912 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return -EIO;
1914 }
1915 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001916 case MTIOCTOP:
1917 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1918 return -EFAULT;
1919 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1920 case MTIOCGET:
1921 memset(&mtget, 0, sizeof(struct mtget));
1922 mtget.mt_type = MT_ISSCSI2;
1923 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1924 mtget.mt_dsreg =
1925 ((tape->blk_size * tape->user_bs_factor)
1926 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001927
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001928 if (tape->drv_write_prot)
1929 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1930
1931 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1932 return -EFAULT;
1933 return 0;
1934 case MTIOCPOS:
1935 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1936 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1937 return -EFAULT;
1938 return 0;
1939 default:
1940 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001941 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001942 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944}
1945
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001946/*
1947 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1948 * block size with the reported value.
1949 */
1950static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1951{
1952 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001953 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001954
1955 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001956 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001957 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001958 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001959 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1960 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001961 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001962 }
1963 return;
1964 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001965 tape->blk_size = (pc.buf[4 + 5] << 16) +
1966 (pc.buf[4 + 6] << 8) +
1967 pc.buf[4 + 7];
1968 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001971static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1974 ide_drive_t *drive;
1975 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int retval;
1977
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001978 if (i >= MAX_HWIFS * MAX_DRIVES)
1979 return -ENXIO;
1980
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001981 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001982 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001983 if (!tape) {
1984 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001985 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001986 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001987
1988 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /*
1991 * We really want to do nonseekable_open(inode, filp); here, but some
1992 * versions of tar incorrectly call lseek on tapes and bail out if that
1993 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1994 */
1995 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 drive = tape->drive;
1998
1999 filp->private_data = tape;
2000
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002001 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 retval = -EBUSY;
2003 goto out_put_tape;
2004 }
2005
2006 retval = idetape_wait_ready(drive, 60 * HZ);
2007 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002008 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2010 goto out_put_tape;
2011 }
2012
2013 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002014 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 (void)idetape_rewind_tape(drive);
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002018 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /* Set write protect flag if device is opened as read-only. */
2021 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2022 tape->write_prot = 1;
2023 else
2024 tape->write_prot = tape->drv_write_prot;
2025
2026 /* Make sure drive isn't write protected if user wants to write. */
2027 if (tape->write_prot) {
2028 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2029 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002030 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 retval = -EROFS;
2032 goto out_put_tape;
2033 }
2034 }
2035
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002036 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002037 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002038 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002039 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2040 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002043 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return 0;
2045
2046out_put_tape:
2047 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002048 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return retval;
2050}
2051
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002052static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 idetape_tape_t *tape = drive->driver_data;
2055
Borislav Petkovd9df9372008-04-27 15:38:34 +02002056 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002057 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2058 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002059 idetape_pad_zeros(drive, tape->blk_size *
2060 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002061 ide_tape_kfree_buffer(tape);
2062 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 idetape_write_filemark(drive);
2065 idetape_flush_tape_buffers(drive);
2066 idetape_flush_tape_buffers(drive);
2067}
2068
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002069static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 struct ide_tape_obj *tape = ide_tape_f(filp);
2072 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 unsigned int minor = iminor(inode);
2074
2075 lock_kernel();
2076 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002077
2078 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Borislav Petkov54abf372008-02-06 02:57:52 +01002080 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002082 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002084 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002086
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002087 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002089 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002091 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002092 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002095 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 ide_tape_put(tape);
2097 unlock_kernel();
2098 return 0;
2099}
2100
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002101static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002104 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002105 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002108 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002109 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2110 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return;
2112 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002113 memcpy(vendor_id, &pc.buf[8], 8);
2114 memcpy(product_id, &pc.buf[16], 16);
2115 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002116
Borislav Petkov801bd322008-09-27 19:32:17 +02002117 ide_fixstring(vendor_id, 8, 0);
2118 ide_fixstring(product_id, 16, 0);
2119 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002120
Borislav Petkov801bd322008-09-27 19:32:17 +02002121 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002122 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002126 * Ask the tape about its various parameters. In particular, we will adjust our
2127 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002129static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002132 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002133 u8 *caps;
2134 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002137 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002138 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2139 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002140 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002141 put_unaligned(52, (u16 *)&tape->caps[12]);
2142 put_unaligned(540, (u16 *)&tape->caps[14]);
2143 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return;
2145 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002146 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Borislav Petkovb6422012008-02-02 19:56:49 +01002148 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002149 speed = be16_to_cpup((__be16 *)&caps[14]);
2150 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002152 *(u16 *)&caps[8] = max_speed;
2153 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2154 *(u16 *)&caps[14] = speed;
2155 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002156
2157 if (!speed) {
2158 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2159 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002160 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002162 if (!max_speed) {
2163 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2164 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002165 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167
Borislav Petkovb6422012008-02-02 19:56:49 +01002168 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002169
2170 /* device lacks locking support according to capabilities page */
2171 if ((caps[6] & 1) == 0)
2172 drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
2173
Borislav Petkovb6422012008-02-02 19:56:49 +01002174 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002175 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002176 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002177 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002180#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002181#define ide_tape_devset_get(name, field) \
2182static int get_##name(ide_drive_t *drive) \
2183{ \
2184 idetape_tape_t *tape = drive->driver_data; \
2185 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002187
2188#define ide_tape_devset_set(name, field) \
2189static int set_##name(ide_drive_t *drive, int arg) \
2190{ \
2191 idetape_tape_t *tape = drive->driver_data; \
2192 tape->field = arg; \
2193 return 0; \
2194}
2195
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002196#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002197ide_tape_devset_get(_name, _field) \
2198ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002199IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002200
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002201#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002202ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002203IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002204
2205static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2206static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2207static int divf_buffer(ide_drive_t *drive) { return 2; }
2208static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2209
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002210ide_devset_rw_field(dsc_overlap, dsc_overlap);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002211
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002212ide_tape_devset_rw_field(debug_mask, debug_mask);
2213ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002214
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002215ide_tape_devset_r_field(avg_speed, avg_speed);
2216ide_tape_devset_r_field(speed, caps[14]);
2217ide_tape_devset_r_field(buffer, caps[16]);
2218ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002219
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002220static const struct ide_proc_devset idetape_settings[] = {
2221 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2222 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2223 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2224 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2225 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2226 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2227 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2228 mulf_tdsc, divf_tdsc),
2229 { 0 },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002230};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002234 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002236 * 1. Initialize our various state variables.
2237 * 2. Ask the tape for its capabilities.
2238 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2239 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002241 * Note that at this point ide.c already assigned us an irq, so that we can
2242 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002244static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Borislav Petkov83042b22008-04-27 15:38:27 +02002246 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002248 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002249 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002250 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Borislav Petkov776bb022008-07-23 19:55:59 +02002252 drive->pc_callback = ide_tape_callback;
2253
Borislav Petkov54bb2072008-02-06 02:57:52 +01002254 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002256 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2257 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2258 tape->name);
2259 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002262 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 drive->dsc_overlap = 0;
2264 tape->minor = minor;
2265 tape->name[0] = 'h';
2266 tape->name[1] = 't';
2267 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002268 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002269
2270 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002271
2272 /* Command packet DRQ type */
2273 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002274 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 idetape_get_inquiry_results(drive);
2277 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002278 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002280 tape->buffer_size = *ctl * tape->blk_size;
2281 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002283 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002284 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002286 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002287 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002288 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002289 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002290 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292
Borislav Petkov83042b22008-04-27 15:38:27 +02002293 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002294 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Borislav Petkovf73850a2008-04-27 15:38:33 +02002296 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002299 * Ensure that the number we got makes sense; limit it within
2300 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002302 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2303 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002305 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002306 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002307 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2308 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002309 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 drive->using_dma ? ", DMA":"");
2311
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002312 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Russell King4031bbe2006-01-06 11:41:00 +00002315static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002319 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 ide_unregister_region(tape->disk);
2322
2323 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
2326static void ide_tape_release(struct kref *kref)
2327{
2328 struct ide_tape_obj *tape = to_ide_tape(kref);
2329 ide_drive_t *drive = tape->drive;
2330 struct gendisk *g = tape->disk;
2331
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002332 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 drive->dsc_overlap = 0;
2335 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002336 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002337 device_destroy(idetape_sysfs_class,
2338 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 idetape_devs[tape->minor] = NULL;
2340 g->private_data = NULL;
2341 put_disk(g);
2342 kfree(tape);
2343}
2344
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002345#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346static int proc_idetape_read_name
2347 (char *page, char **start, off_t off, int count, int *eof, void *data)
2348{
2349 ide_drive_t *drive = (ide_drive_t *) data;
2350 idetape_tape_t *tape = drive->driver_data;
2351 char *out = page;
2352 int len;
2353
2354 len = sprintf(out, "%s\n", tape->name);
2355 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2356}
2357
2358static ide_proc_entry_t idetape_proc[] = {
2359 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2360 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2361 { NULL, 0, NULL, NULL }
2362};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363#endif
2364
Russell King4031bbe2006-01-06 11:41:00 +00002365static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002368 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002369 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002370 .name = "ide-tape",
2371 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002372 },
Russell King4031bbe2006-01-06 11:41:00 +00002373 .probe = ide_tape_probe,
2374 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 .version = IDETAPE_VERSION,
2376 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 .do_request = idetape_do_request,
2378 .end_request = idetape_end_request,
2379 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002380#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002382 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384};
2385
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002386/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002387static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 .owner = THIS_MODULE,
2389 .read = idetape_chrdev_read,
2390 .write = idetape_chrdev_write,
2391 .ioctl = idetape_chrdev_ioctl,
2392 .open = idetape_chrdev_open,
2393 .release = idetape_chrdev_release,
2394};
2395
2396static int idetape_open(struct inode *inode, struct file *filp)
2397{
2398 struct gendisk *disk = inode->i_bdev->bd_disk;
2399 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002401 tape = ide_tape_get(disk);
2402 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return -ENXIO;
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return 0;
2406}
2407
2408static int idetape_release(struct inode *inode, struct file *filp)
2409{
2410 struct gendisk *disk = inode->i_bdev->bd_disk;
2411 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 ide_tape_put(tape);
2414
2415 return 0;
2416}
2417
2418static int idetape_ioctl(struct inode *inode, struct file *file,
2419 unsigned int cmd, unsigned long arg)
2420{
2421 struct block_device *bdev = inode->i_bdev;
2422 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2423 ide_drive_t *drive = tape->drive;
2424 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2425 if (err == -EINVAL)
2426 err = idetape_blkdev_ioctl(drive, cmd, arg);
2427 return err;
2428}
2429
2430static struct block_device_operations idetape_block_ops = {
2431 .owner = THIS_MODULE,
2432 .open = idetape_open,
2433 .release = idetape_release,
2434 .ioctl = idetape_ioctl,
2435};
2436
Russell King4031bbe2006-01-06 11:41:00 +00002437static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 idetape_tape_t *tape;
2440 struct gendisk *g;
2441 int minor;
2442
2443 if (!strstr("ide-tape", drive->driver_req))
2444 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (drive->media != ide_tape)
2447 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002448
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002449 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002450 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2451 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 goto failed;
2453 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002454 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002456 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2457 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto failed;
2459 }
2460
2461 g = alloc_disk(1 << PARTN_BITS);
2462 if (!g)
2463 goto out_free_tape;
2464
2465 ide_init_disk(g, drive);
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 kref_init(&tape->kref);
2468
2469 tape->drive = drive;
2470 tape->driver = &idetape_driver;
2471 tape->disk = g;
2472
2473 g->private_data = &tape->driver;
2474
2475 drive->driver_data = tape;
2476
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002477 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 for (minor = 0; idetape_devs[minor]; minor++)
2479 ;
2480 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002481 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 idetape_setup(drive, tape, minor);
2484
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002485 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2486 MKDEV(IDETAPE_MAJOR, minor), NULL,
2487 "%s", tape->name);
2488 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2489 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2490 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 g->fops = &idetape_block_ops;
2493 ide_register_region(g);
2494
2495 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497out_free_tape:
2498 kfree(tape);
2499failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002503static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002505 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002506 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 unregister_chrdev(IDETAPE_MAJOR, "ht");
2508}
2509
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002510static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Will Dysond5dee802005-09-16 02:55:07 -07002512 int error = 1;
2513 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2514 if (IS_ERR(idetape_sysfs_class)) {
2515 idetape_sysfs_class = NULL;
2516 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2517 error = -EBUSY;
2518 goto out;
2519 }
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002522 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2523 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002524 error = -EBUSY;
2525 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
Will Dysond5dee802005-09-16 02:55:07 -07002527
2528 error = driver_register(&idetape_driver.gen_driver);
2529 if (error)
2530 goto out_free_driver;
2531
2532 return 0;
2533
2534out_free_driver:
2535 driver_unregister(&idetape_driver.gen_driver);
2536out_free_class:
2537 class_destroy(idetape_sysfs_class);
2538out:
2539 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Kay Sievers263756e2005-12-12 18:03:44 +01002542MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543module_init(idetape_init);
2544module_exit(idetape_exit);
2545MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002546MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2547MODULE_LICENSE("GPL");