]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
5ce78af4 BP |
2 | * IDE ATAPI streaming tape driver. |
3 | * | |
59bca8cc BZ |
4 | * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il> |
5 | * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz | |
1da177e4 | 6 | * |
1da177e4 LT |
7 | * 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). | |
1da177e4 | 13 | * |
5ce78af4 BP |
14 | * For a historical changelog see |
15 | * Documentation/ide/ChangeLog.ide-tape.1995-2002 | |
1da177e4 LT |
16 | */ |
17 | ||
51509eec BZ |
18 | #define DRV_NAME "ide-tape" |
19 | ||
dfe79936 | 20 | #define IDETAPE_VERSION "1.20" |
1da177e4 | 21 | |
1da177e4 LT |
22 | #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> | |
9bae1ff3 | 30 | #include <linux/jiffies.h> |
1da177e4 | 31 | #include <linux/major.h> |
1da177e4 LT |
32 | #include <linux/errno.h> |
33 | #include <linux/genhd.h> | |
6d703a81 | 34 | #include <linux/seq_file.h> |
1da177e4 LT |
35 | #include <linux/slab.h> |
36 | #include <linux/pci.h> | |
37 | #include <linux/ide.h> | |
1da177e4 LT |
38 | #include <linux/completion.h> |
39 | #include <linux/bitops.h> | |
cf8b8975 | 40 | #include <linux/mutex.h> |
90699ce2 | 41 | #include <scsi/scsi.h> |
1da177e4 LT |
42 | |
43 | #include <asm/byteorder.h> | |
c837cfa5 BP |
44 | #include <linux/irq.h> |
45 | #include <linux/uaccess.h> | |
46 | #include <linux/io.h> | |
1da177e4 | 47 | #include <asm/unaligned.h> |
1da177e4 LT |
48 | #include <linux/mtio.h> |
49 | ||
8004a8c9 | 50 | /* define to see debug info */ |
e972d702 | 51 | #undef IDETAPE_DEBUG_LOG |
8004a8c9 | 52 | |
e972d702 BP |
53 | #ifdef IDETAPE_DEBUG_LOG |
54 | #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args) | |
8004a8c9 | 55 | #else |
e972d702 | 56 | #define ide_debug_log(lvl, fmt, args...) do {} while (0) |
8004a8c9 BP |
57 | #endif |
58 | ||
1da177e4 | 59 | /**************************** Tunable parameters *****************************/ |
1da177e4 | 60 | /* |
3c98bf34 BP |
61 | * After each failed packet command we issue a request sense command and retry |
62 | * the packet command IDETAPE_MAX_PC_RETRIES times. | |
1da177e4 | 63 | * |
3c98bf34 | 64 | * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries. |
1da177e4 LT |
65 | */ |
66 | #define IDETAPE_MAX_PC_RETRIES 3 | |
67 | ||
1da177e4 | 68 | /* |
3c98bf34 BP |
69 | * The following parameter is used to select the point in the internal tape fifo |
70 | * in which we will start to refill the buffer. Decreasing the following | |
71 | * parameter will improve the system's latency and interactive response, while | |
72 | * using a high value might improve system throughput. | |
1da177e4 | 73 | */ |
3c98bf34 | 74 | #define IDETAPE_FIFO_THRESHOLD 2 |
1da177e4 LT |
75 | |
76 | /* | |
3c98bf34 | 77 | * DSC polling parameters. |
1da177e4 | 78 | * |
3c98bf34 BP |
79 | * Polling for DSC (a single bit in the status register) is a very important |
80 | * function in ide-tape. There are two cases in which we poll for DSC: | |
1da177e4 | 81 | * |
3c98bf34 BP |
82 | * 1. Before a read/write packet command, to ensure that we can transfer data |
83 | * from/to the tape's data buffers, without causing an actual media access. | |
84 | * In case the tape is not ready yet, we take out our request from the device | |
85 | * request queue, so that ide.c could service requests from the other device | |
86 | * on the same interface in the meantime. | |
1da177e4 | 87 | * |
3c98bf34 BP |
88 | * 2. After the successful initialization of a "media access packet command", |
89 | * which is a command that can take a long time to complete (the interval can | |
90 | * range from several seconds to even an hour). Again, we postpone our request | |
91 | * in the middle to free the bus for the other device. The polling frequency | |
92 | * here should be lower than the read/write frequency since those media access | |
93 | * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST | |
94 | * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD | |
95 | * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min). | |
1da177e4 | 96 | * |
3c98bf34 BP |
97 | * We also set a timeout for the timer, in case something goes wrong. The |
98 | * timeout should be longer then the maximum execution time of a tape operation. | |
1da177e4 | 99 | */ |
3c98bf34 BP |
100 | |
101 | /* DSC timings. */ | |
1da177e4 LT |
102 | #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */ |
103 | #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */ | |
104 | #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */ | |
105 | #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */ | |
106 | #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */ | |
107 | #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */ | |
108 | #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */ | |
109 | ||
110 | /*************************** End of tunable parameters ***********************/ | |
111 | ||
54abf37e BP |
112 | /* tape directions */ |
113 | enum { | |
114 | IDETAPE_DIR_NONE = (1 << 0), | |
115 | IDETAPE_DIR_READ = (1 << 1), | |
116 | IDETAPE_DIR_WRITE = (1 << 2), | |
117 | }; | |
1da177e4 | 118 | |
03056b90 BP |
119 | /* Tape door status */ |
120 | #define DOOR_UNLOCKED 0 | |
121 | #define DOOR_LOCKED 1 | |
122 | #define DOOR_EXPLICITLY_LOCKED 2 | |
123 | ||
124 | /* Some defines for the SPACE command */ | |
125 | #define IDETAPE_SPACE_OVER_FILEMARK 1 | |
126 | #define IDETAPE_SPACE_TO_EOD 3 | |
127 | ||
128 | /* Some defines for the LOAD UNLOAD command */ | |
129 | #define IDETAPE_LU_LOAD_MASK 1 | |
130 | #define IDETAPE_LU_RETENSION_MASK 2 | |
131 | #define IDETAPE_LU_EOT_MASK 4 | |
132 | ||
03056b90 BP |
133 | /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ |
134 | #define IDETAPE_BLOCK_DESCRIPTOR 0 | |
135 | #define IDETAPE_CAPABILITIES_PAGE 0x2a | |
136 | ||
1da177e4 | 137 | /* |
3c98bf34 BP |
138 | * Most of our global data which we need to save even as we leave the driver due |
139 | * to an interrupt or a timer event is stored in the struct defined below. | |
1da177e4 LT |
140 | */ |
141 | typedef struct ide_tape_obj { | |
7f3c868b BZ |
142 | ide_drive_t *drive; |
143 | struct ide_driver *driver; | |
144 | struct gendisk *disk; | |
8fed4368 | 145 | struct device dev; |
1da177e4 | 146 | |
2e8a6f89 BZ |
147 | /* used by REQ_IDETAPE_{READ,WRITE} requests */ |
148 | struct ide_atapi_pc queued_pc; | |
394a4c21 | 149 | |
1da177e4 | 150 | /* |
3c98bf34 | 151 | * DSC polling variables. |
1da177e4 | 152 | * |
3c98bf34 BP |
153 | * While polling for DSC we use postponed_rq to postpone the current |
154 | * request so that ide.c will be able to service pending requests on the | |
155 | * other device. Note that at most we will have only one DSC (usually | |
5bd50dc6 | 156 | * data transfer) request in the device request queue. |
1da177e4 | 157 | */ |
6f3848ac BP |
158 | bool postponed_rq; |
159 | ||
1da177e4 LT |
160 | /* The time in which we started polling for DSC */ |
161 | unsigned long dsc_polling_start; | |
162 | /* Timer used to poll for dsc */ | |
163 | struct timer_list dsc_timer; | |
164 | /* Read/Write dsc polling frequency */ | |
54bb2074 BP |
165 | unsigned long best_dsc_rw_freq; |
166 | unsigned long dsc_poll_freq; | |
1da177e4 LT |
167 | unsigned long dsc_timeout; |
168 | ||
3c98bf34 | 169 | /* Read position information */ |
1da177e4 LT |
170 | u8 partition; |
171 | /* Current block */ | |
54bb2074 | 172 | unsigned int first_frame; |
1da177e4 | 173 | |
3c98bf34 | 174 | /* Last error information */ |
1da177e4 LT |
175 | u8 sense_key, asc, ascq; |
176 | ||
3c98bf34 | 177 | /* Character device operation */ |
1da177e4 LT |
178 | unsigned int minor; |
179 | /* device name */ | |
180 | char name[4]; | |
181 | /* Current character device data transfer direction */ | |
54abf37e | 182 | u8 chrdev_dir; |
1da177e4 | 183 | |
54bb2074 BP |
184 | /* tape block size, usually 512 or 1024 bytes */ |
185 | unsigned short blk_size; | |
1da177e4 | 186 | int user_bs_factor; |
b6422013 | 187 | |
1da177e4 | 188 | /* Copy of the tape's Capabilities and Mechanical Page */ |
b6422013 | 189 | u8 caps[20]; |
1da177e4 LT |
190 | |
191 | /* | |
3c98bf34 | 192 | * Active data transfer request parameters. |
1da177e4 | 193 | * |
3c98bf34 BP |
194 | * At most, there is only one ide-tape originated data transfer request |
195 | * in the device request queue. This allows ide.c to easily service | |
196 | * requests from the other device when we postpone our active request. | |
1da177e4 | 197 | */ |
83042b24 | 198 | |
3c98bf34 | 199 | /* Data buffer size chosen based on the tape's recommendation */ |
f73850a3 | 200 | int buffer_size; |
963da55c TH |
201 | /* Staging buffer of buffer_size bytes */ |
202 | void *buf; | |
203 | /* The read/write cursor */ | |
204 | void *cur; | |
205 | /* The number of valid bytes in buf */ | |
206 | size_t valid; | |
3c98bf34 | 207 | |
3c98bf34 | 208 | /* Measures average tape speed */ |
1da177e4 LT |
209 | unsigned long avg_time; |
210 | int avg_size; | |
211 | int avg_speed; | |
212 | ||
1da177e4 LT |
213 | /* the door is currently locked */ |
214 | int door_locked; | |
215 | /* the tape hardware is write protected */ | |
216 | char drv_write_prot; | |
217 | /* the tape is write protected (hardware or opened as read-only) */ | |
218 | char write_prot; | |
1da177e4 LT |
219 | } idetape_tape_t; |
220 | ||
2a48fc0a | 221 | static DEFINE_MUTEX(ide_tape_mutex); |
cf8b8975 | 222 | static DEFINE_MUTEX(idetape_ref_mutex); |
1da177e4 | 223 | |
cbba2fa7 BP |
224 | static DEFINE_MUTEX(idetape_chrdev_mutex); |
225 | ||
d5dee80a WD |
226 | static struct class *idetape_sysfs_class; |
227 | ||
8fed4368 | 228 | static void ide_tape_release(struct device *); |
08da591e | 229 | |
9d01e4cd BP |
230 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
231 | ||
232 | static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev, | |
233 | unsigned int i) | |
1da177e4 LT |
234 | { |
235 | struct ide_tape_obj *tape = NULL; | |
236 | ||
cf8b8975 | 237 | mutex_lock(&idetape_ref_mutex); |
9d01e4cd BP |
238 | |
239 | if (cdev) | |
240 | tape = idetape_devs[i]; | |
241 | else | |
242 | tape = ide_drv_g(disk, ide_tape_obj); | |
243 | ||
08da591e | 244 | if (tape) { |
d3e33ff5 | 245 | if (ide_device_get(tape->drive)) |
08da591e | 246 | tape = NULL; |
d3e33ff5 | 247 | else |
8fed4368 | 248 | get_device(&tape->dev); |
08da591e | 249 | } |
9d01e4cd | 250 | |
cf8b8975 | 251 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
252 | return tape; |
253 | } | |
254 | ||
1da177e4 LT |
255 | static void ide_tape_put(struct ide_tape_obj *tape) |
256 | { | |
d3e33ff5 BZ |
257 | ide_drive_t *drive = tape->drive; |
258 | ||
cf8b8975 | 259 | mutex_lock(&idetape_ref_mutex); |
8fed4368 | 260 | put_device(&tape->dev); |
d3e33ff5 | 261 | ide_device_put(drive); |
cf8b8975 | 262 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
263 | } |
264 | ||
1da177e4 | 265 | /* |
1b5db434 BP |
266 | * called on each failed packet command retry to analyze the request sense. We |
267 | * currently do not utilize this information. | |
1da177e4 | 268 | */ |
ae3a8387 | 269 | static void idetape_analyze_error(ide_drive_t *drive) |
1da177e4 LT |
270 | { |
271 | idetape_tape_t *tape = drive->driver_data; | |
5e2040fd | 272 | struct ide_atapi_pc *pc = drive->failed_pc; |
dfb7e621 | 273 | struct request *rq = drive->hwif->rq; |
ae3a8387 | 274 | u8 *sense = bio_data(rq->bio); |
1da177e4 | 275 | |
1b5db434 BP |
276 | tape->sense_key = sense[2] & 0xF; |
277 | tape->asc = sense[12]; | |
278 | tape->ascq = sense[13]; | |
8004a8c9 | 279 | |
e972d702 BP |
280 | ide_debug_log(IDE_DBG_FUNC, |
281 | "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x", | |
282 | rq->cmd[0], tape->sense_key, tape->asc, tape->ascq); | |
1da177e4 | 283 | |
077e6dba | 284 | /* correct remaining bytes to transfer */ |
21d9c5d2 | 285 | if (pc->flags & PC_FLAG_DMA_ERROR) |
077e6dba | 286 | rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]); |
1da177e4 LT |
287 | |
288 | /* | |
289 | * If error was the result of a zero-length read or write command, | |
290 | * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives | |
291 | * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. | |
292 | */ | |
90699ce2 | 293 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
1b5db434 BP |
294 | /* length == 0 */ |
295 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { | |
296 | if (tape->sense_key == 5) { | |
1da177e4 LT |
297 | /* don't report an error, everything's ok */ |
298 | pc->error = 0; | |
299 | /* don't retry read/write */ | |
346331f8 | 300 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 LT |
301 | } |
302 | } | |
90699ce2 | 303 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
c152cc1a | 304 | pc->error = IDE_DRV_ERROR_FILEMARK; |
346331f8 | 305 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 | 306 | } |
90699ce2 | 307 | if (pc->c[0] == WRITE_6) { |
1b5db434 BP |
308 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
309 | && tape->asc == 0x0 && tape->ascq == 0x2)) { | |
c152cc1a | 310 | pc->error = IDE_DRV_ERROR_EOD; |
346331f8 | 311 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 LT |
312 | } |
313 | } | |
90699ce2 | 314 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
1b5db434 | 315 | if (tape->sense_key == 8) { |
c152cc1a | 316 | pc->error = IDE_DRV_ERROR_EOD; |
346331f8 | 317 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 | 318 | } |
346331f8 | 319 | if (!(pc->flags & PC_FLAG_ABORT) && |
077e6dba | 320 | (blk_rq_bytes(rq) - rq->resid_len)) |
1da177e4 LT |
321 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
322 | } | |
323 | } | |
324 | ||
b14c7212 BZ |
325 | static void ide_tape_handle_dsc(ide_drive_t *); |
326 | ||
03a2faae | 327 | static int ide_tape_callback(ide_drive_t *drive, int dsc) |
1da177e4 LT |
328 | { |
329 | idetape_tape_t *tape = drive->driver_data; | |
2b9efba4 | 330 | struct ide_atapi_pc *pc = drive->pc; |
313afea7 | 331 | struct request *rq = drive->hwif->rq; |
5985e6ab | 332 | int uptodate = pc->error ? 0 : 1; |
313afea7 | 333 | int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL; |
1da177e4 | 334 | |
e972d702 BP |
335 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0], |
336 | dsc, err); | |
8004a8c9 | 337 | |
b14c7212 BZ |
338 | if (dsc) |
339 | ide_tape_handle_dsc(drive); | |
340 | ||
5e2040fd BZ |
341 | if (drive->failed_pc == pc) |
342 | drive->failed_pc = NULL; | |
dd2e9a03 | 343 | |
5985e6ab BZ |
344 | if (pc->c[0] == REQUEST_SENSE) { |
345 | if (uptodate) | |
ae3a8387 | 346 | idetape_analyze_error(drive); |
5985e6ab BZ |
347 | else |
348 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE " | |
349 | "itself - Aborting request!\n"); | |
350 | } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { | |
077e6dba BP |
351 | unsigned int blocks = |
352 | (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size; | |
5985e6ab BZ |
353 | |
354 | tape->avg_size += blocks * tape->blk_size; | |
355 | ||
356 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { | |
357 | tape->avg_speed = tape->avg_size * HZ / | |
358 | (jiffies - tape->avg_time) / 1024; | |
359 | tape->avg_size = 0; | |
360 | tape->avg_time = jiffies; | |
361 | } | |
362 | ||
363 | tape->first_frame += blocks; | |
5985e6ab | 364 | |
313afea7 BZ |
365 | if (pc->error) { |
366 | uptodate = 0; | |
367 | err = pc->error; | |
368 | } | |
1da177e4 | 369 | } |
313afea7 BZ |
370 | rq->errors = err; |
371 | ||
03a2faae | 372 | return uptodate; |
1da177e4 LT |
373 | } |
374 | ||
1da177e4 | 375 | /* |
3c98bf34 | 376 | * Postpone the current request so that ide.c will be able to service requests |
b65fac32 | 377 | * from another device on the same port while we are polling for DSC. |
1da177e4 | 378 | */ |
6f3848ac | 379 | static void ide_tape_stall_queue(ide_drive_t *drive) |
1da177e4 LT |
380 | { |
381 | idetape_tape_t *tape = drive->driver_data; | |
382 | ||
e972d702 | 383 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu", |
6f3848ac | 384 | drive->hwif->rq->cmd[0], tape->dsc_poll_freq); |
8004a8c9 | 385 | |
6f3848ac | 386 | tape->postponed_rq = true; |
b65fac32 | 387 | |
54bb2074 | 388 | ide_stall_queue(drive, tape->dsc_poll_freq); |
1da177e4 LT |
389 | } |
390 | ||
74e63e74 BZ |
391 | static void ide_tape_handle_dsc(ide_drive_t *drive) |
392 | { | |
393 | idetape_tape_t *tape = drive->driver_data; | |
394 | ||
395 | /* Media access command */ | |
396 | tape->dsc_polling_start = jiffies; | |
397 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; | |
398 | tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; | |
399 | /* Allow ide.c to handle other requests */ | |
6f3848ac | 400 | ide_tape_stall_queue(drive); |
74e63e74 BZ |
401 | } |
402 | ||
1da177e4 | 403 | /* |
3c98bf34 | 404 | * Packet Command Interface |
1da177e4 | 405 | * |
2b9efba4 | 406 | * The current Packet Command is available in drive->pc, and will not change |
3c98bf34 BP |
407 | * until we finish handling it. Each packet command is associated with a |
408 | * callback function that will be called when the command is finished. | |
1da177e4 | 409 | * |
3c98bf34 | 410 | * The handling will be done in three stages: |
1da177e4 | 411 | * |
b788ee9c | 412 | * 1. ide_tape_issue_pc will send the packet command to the drive, and will set |
aa5d2de7 | 413 | * the interrupt handler to ide_pc_intr. |
1da177e4 | 414 | * |
aa5d2de7 | 415 | * 2. On each interrupt, ide_pc_intr will be called. This step will be |
3c98bf34 | 416 | * repeated until the device signals us that no more interrupts will be issued. |
1da177e4 | 417 | * |
3c98bf34 BP |
418 | * 3. ATAPI Tape media access commands have immediate status with a delayed |
419 | * process. In case of a successful initiation of a media access packet command, | |
420 | * the DSC bit will be set when the actual execution of the command is finished. | |
421 | * Since the tape drive will not issue an interrupt, we have to poll for this | |
422 | * event. In this case, we define the request as "low priority request" by | |
423 | * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and | |
424 | * exit the driver. | |
1da177e4 | 425 | * |
3c98bf34 BP |
426 | * ide.c will then give higher priority to requests which originate from the |
427 | * other device, until will change rq_status to RQ_ACTIVE. | |
1da177e4 | 428 | * |
3c98bf34 | 429 | * 4. When the packet command is finished, it will be checked for errors. |
1da177e4 | 430 | * |
3c98bf34 BP |
431 | * 5. In case an error was found, we queue a request sense packet command in |
432 | * front of the request queue and retry the operation up to | |
433 | * IDETAPE_MAX_PC_RETRIES times. | |
1da177e4 | 434 | * |
3c98bf34 BP |
435 | * 6. In case no error was found, or we decided to give up and not to retry |
436 | * again, the callback function will be called and then we will handle the next | |
437 | * request. | |
1da177e4 | 438 | */ |
1da177e4 | 439 | |
b788ee9c BZ |
440 | static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive, |
441 | struct ide_cmd *cmd, | |
442 | struct ide_atapi_pc *pc) | |
1da177e4 | 443 | { |
1da177e4 | 444 | idetape_tape_t *tape = drive->driver_data; |
dfb7e621 | 445 | struct request *rq = drive->hwif->rq; |
1da177e4 | 446 | |
5e2040fd BZ |
447 | if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
448 | drive->failed_pc = pc; | |
2b9efba4 | 449 | |
1da177e4 | 450 | /* Set the current packet command */ |
2b9efba4 | 451 | drive->pc = pc; |
1da177e4 LT |
452 | |
453 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || | |
346331f8 | 454 | (pc->flags & PC_FLAG_ABORT)) { |
b3071d19 | 455 | |
1da177e4 | 456 | /* |
3c98bf34 BP |
457 | * We will "abort" retrying a packet command in case legitimate |
458 | * error code was received (crossing a filemark, or end of the | |
459 | * media, for example). | |
1da177e4 | 460 | */ |
346331f8 | 461 | if (!(pc->flags & PC_FLAG_ABORT)) { |
90699ce2 | 462 | if (!(pc->c[0] == TEST_UNIT_READY && |
1da177e4 LT |
463 | tape->sense_key == 2 && tape->asc == 4 && |
464 | (tape->ascq == 1 || tape->ascq == 8))) { | |
465 | printk(KERN_ERR "ide-tape: %s: I/O error, " | |
466 | "pc = %2x, key = %2x, " | |
467 | "asc = %2x, ascq = %2x\n", | |
468 | tape->name, pc->c[0], | |
469 | tape->sense_key, tape->asc, | |
470 | tape->ascq); | |
471 | } | |
472 | /* Giving up */ | |
c152cc1a | 473 | pc->error = IDE_DRV_ERROR_GENERAL; |
1da177e4 | 474 | } |
b3071d19 | 475 | |
5e2040fd | 476 | drive->failed_pc = NULL; |
b14c7212 | 477 | drive->pc_callback(drive, 0); |
dfb7e621 | 478 | ide_complete_rq(drive, -EIO, blk_rq_bytes(rq)); |
92f5daff | 479 | return ide_stopped; |
1da177e4 | 480 | } |
e972d702 BP |
481 | ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries, |
482 | pc->c[0]); | |
1da177e4 LT |
483 | |
484 | pc->retries++; | |
1da177e4 | 485 | |
b788ee9c | 486 | return ide_issue_pc(drive, cmd); |
1da177e4 LT |
487 | } |
488 | ||
3c98bf34 | 489 | /* A mode sense command is used to "sense" tape parameters. */ |
d236d74c | 490 | static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code) |
1da177e4 | 491 | { |
7bf7420a | 492 | ide_init_pc(pc); |
90699ce2 | 493 | pc->c[0] = MODE_SENSE; |
1da177e4 | 494 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
3c98bf34 BP |
495 | /* DBD = 1 - Don't return block descriptors */ |
496 | pc->c[1] = 8; | |
1da177e4 LT |
497 | pc->c[2] = page_code; |
498 | /* | |
499 | * Changed pc->c[3] to 0 (255 will at best return unused info). | |
500 | * | |
501 | * For SCSI this byte is defined as subpage instead of high byte | |
502 | * of length and some IDE drives seem to interpret it this way | |
503 | * and return an error when 255 is used. | |
504 | */ | |
505 | pc->c[3] = 0; | |
3c98bf34 BP |
506 | /* We will just discard data in that case */ |
507 | pc->c[4] = 255; | |
1da177e4 | 508 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
d236d74c | 509 | pc->req_xfer = 12; |
1da177e4 | 510 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
d236d74c | 511 | pc->req_xfer = 24; |
1da177e4 | 512 | else |
d236d74c | 513 | pc->req_xfer = 50; |
1da177e4 LT |
514 | } |
515 | ||
5a04cfa9 | 516 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
1da177e4 | 517 | { |
b73c7ee2 | 518 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 519 | idetape_tape_t *tape = drive->driver_data; |
2b9efba4 | 520 | struct ide_atapi_pc *pc = drive->pc; |
22c525b9 | 521 | u8 stat; |
1da177e4 | 522 | |
374e042c | 523 | stat = hwif->tp_ops->read_status(hwif); |
c47137a9 | 524 | |
3a7d2484 BZ |
525 | if (stat & ATA_DSC) { |
526 | if (stat & ATA_ERR) { | |
1da177e4 | 527 | /* Error detected */ |
90699ce2 | 528 | if (pc->c[0] != TEST_UNIT_READY) |
1da177e4 LT |
529 | printk(KERN_ERR "ide-tape: %s: I/O error, ", |
530 | tape->name); | |
531 | /* Retry operation */ | |
6b544fcc | 532 | ide_retry_pc(drive); |
258ec411 | 533 | return ide_stopped; |
1da177e4 LT |
534 | } |
535 | pc->error = 0; | |
1da177e4 | 536 | } else { |
c152cc1a | 537 | pc->error = IDE_DRV_ERROR_GENERAL; |
5e2040fd | 538 | drive->failed_pc = NULL; |
1da177e4 | 539 | } |
b14c7212 | 540 | drive->pc_callback(drive, 0); |
1da177e4 LT |
541 | return ide_stopped; |
542 | } | |
543 | ||
cd2abbfe | 544 | static void ide_tape_create_rw_cmd(idetape_tape_t *tape, |
0014c75b BP |
545 | struct ide_atapi_pc *pc, struct request *rq, |
546 | u8 opcode) | |
1da177e4 | 547 | { |
10c0b343 | 548 | unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9); |
0014c75b | 549 | |
7bf7420a | 550 | ide_init_pc(pc); |
860ff5ec | 551 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
1da177e4 | 552 | pc->c[1] = 1; |
19f52a78 BP |
553 | |
554 | if (blk_rq_bytes(rq) == tape->buffer_size) | |
5e331095 | 555 | pc->flags |= PC_FLAG_DMA_OK; |
1da177e4 | 556 | |
963da55c | 557 | if (opcode == READ_6) |
cd2abbfe | 558 | pc->c[0] = READ_6; |
963da55c | 559 | else if (opcode == WRITE_6) { |
cd2abbfe BP |
560 | pc->c[0] = WRITE_6; |
561 | pc->flags |= PC_FLAG_WRITING; | |
cd2abbfe | 562 | } |
0014c75b BP |
563 | |
564 | memcpy(rq->cmd, pc->c, 12); | |
1da177e4 LT |
565 | } |
566 | ||
1da177e4 LT |
567 | static ide_startstop_t idetape_do_request(ide_drive_t *drive, |
568 | struct request *rq, sector_t block) | |
569 | { | |
b73c7ee2 | 570 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 571 | idetape_tape_t *tape = drive->driver_data; |
d236d74c | 572 | struct ide_atapi_pc *pc = NULL; |
b788ee9c | 573 | struct ide_cmd cmd; |
22c525b9 | 574 | u8 stat; |
1da177e4 | 575 | |
e972d702 BP |
576 | ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u", |
577 | rq->cmd[0], (unsigned long long)blk_rq_pos(rq), | |
578 | blk_rq_sectors(rq)); | |
1da177e4 | 579 | |
33659ebb CH |
580 | BUG_ON(!(rq->cmd_type == REQ_TYPE_SPECIAL || |
581 | rq->cmd_type == REQ_TYPE_SENSE)); | |
1da177e4 | 582 | |
3c98bf34 | 583 | /* Retry a failed packet command */ |
5e2040fd BZ |
584 | if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) { |
585 | pc = drive->failed_pc; | |
28c7214b BZ |
586 | goto out; |
587 | } | |
5a04cfa9 | 588 | |
1da177e4 LT |
589 | /* |
590 | * If the tape is still busy, postpone our request and service | |
591 | * the other device meanwhile. | |
592 | */ | |
374e042c | 593 | stat = hwif->tp_ops->read_status(hwif); |
1da177e4 | 594 | |
97100fc8 BZ |
595 | if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 && |
596 | (rq->cmd[13] & REQ_IDETAPE_PC2) == 0) | |
626542ca | 597 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
1da177e4 | 598 | |
97100fc8 | 599 | if (drive->dev_flags & IDE_DFLAG_POST_RESET) { |
626542ca | 600 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
97100fc8 | 601 | drive->dev_flags &= ~IDE_DFLAG_POST_RESET; |
1da177e4 LT |
602 | } |
603 | ||
626542ca BP |
604 | if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) && |
605 | !(stat & ATA_DSC)) { | |
6f3848ac | 606 | if (!tape->postponed_rq) { |
1da177e4 | 607 | tape->dsc_polling_start = jiffies; |
54bb2074 | 608 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
1da177e4 LT |
609 | tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; |
610 | } else if (time_after(jiffies, tape->dsc_timeout)) { | |
611 | printk(KERN_ERR "ide-tape: %s: DSC timeout\n", | |
612 | tape->name); | |
83dd5735 | 613 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
1da177e4 LT |
614 | idetape_media_access_finished(drive); |
615 | return ide_stopped; | |
616 | } else { | |
617 | return ide_do_reset(drive); | |
618 | } | |
5a04cfa9 BP |
619 | } else if (time_after(jiffies, |
620 | tape->dsc_polling_start + | |
621 | IDETAPE_DSC_MA_THRESHOLD)) | |
54bb2074 | 622 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
6f3848ac | 623 | ide_tape_stall_queue(drive); |
1da177e4 | 624 | return ide_stopped; |
6f3848ac | 625 | } else { |
626542ca | 626 | drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC; |
6f3848ac BP |
627 | tape->postponed_rq = false; |
628 | } | |
626542ca | 629 | |
83dd5735 | 630 | if (rq->cmd[13] & REQ_IDETAPE_READ) { |
2e8a6f89 | 631 | pc = &tape->queued_pc; |
0014c75b | 632 | ide_tape_create_rw_cmd(tape, pc, rq, READ_6); |
1da177e4 LT |
633 | goto out; |
634 | } | |
83dd5735 | 635 | if (rq->cmd[13] & REQ_IDETAPE_WRITE) { |
2e8a6f89 | 636 | pc = &tape->queued_pc; |
0014c75b | 637 | ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6); |
1da177e4 LT |
638 | goto out; |
639 | } | |
83dd5735 | 640 | if (rq->cmd[13] & REQ_IDETAPE_PC1) { |
c267cc1c | 641 | pc = (struct ide_atapi_pc *)rq->special; |
83dd5735 BP |
642 | rq->cmd[13] &= ~(REQ_IDETAPE_PC1); |
643 | rq->cmd[13] |= REQ_IDETAPE_PC2; | |
1da177e4 LT |
644 | goto out; |
645 | } | |
83dd5735 | 646 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
1da177e4 LT |
647 | idetape_media_access_finished(drive); |
648 | return ide_stopped; | |
649 | } | |
650 | BUG(); | |
28c7214b | 651 | |
f2e3ab52 | 652 | out: |
6b544fcc BP |
653 | /* prepare sense request for this command */ |
654 | ide_prep_sense(drive, rq); | |
655 | ||
b788ee9c BZ |
656 | memset(&cmd, 0, sizeof(cmd)); |
657 | ||
658 | if (rq_data_dir(rq)) | |
659 | cmd.tf_flags |= IDE_TFLAG_WRITE; | |
660 | ||
661 | cmd.rq = rq; | |
662 | ||
dfb7e621 | 663 | ide_init_sg_cmd(&cmd, blk_rq_bytes(rq)); |
5c4be572 TH |
664 | ide_map_sg(drive, &cmd); |
665 | ||
b788ee9c | 666 | return ide_tape_issue_pc(drive, &cmd, pc); |
1da177e4 LT |
667 | } |
668 | ||
1da177e4 | 669 | /* |
3c98bf34 BP |
670 | * Write a filemark if write_filemark=1. Flush the device buffers without |
671 | * writing a filemark otherwise. | |
1da177e4 | 672 | */ |
5a04cfa9 | 673 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
d236d74c | 674 | struct ide_atapi_pc *pc, int write_filemark) |
1da177e4 | 675 | { |
7bf7420a | 676 | ide_init_pc(pc); |
90699ce2 | 677 | pc->c[0] = WRITE_FILEMARKS; |
1da177e4 | 678 | pc->c[4] = write_filemark; |
346331f8 | 679 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
680 | } |
681 | ||
1da177e4 LT |
682 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) |
683 | { | |
684 | idetape_tape_t *tape = drive->driver_data; | |
2ac07d92 | 685 | struct gendisk *disk = tape->disk; |
1da177e4 LT |
686 | int load_attempted = 0; |
687 | ||
3c98bf34 | 688 | /* Wait for the tape to become ready */ |
49d8078a | 689 | set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags); |
1da177e4 LT |
690 | timeout += jiffies; |
691 | while (time_before(jiffies, timeout)) { | |
de699ad5 | 692 | if (ide_do_test_unit_ready(drive, disk) == 0) |
1da177e4 LT |
693 | return 0; |
694 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) | |
3c98bf34 BP |
695 | || (tape->asc == 0x3A)) { |
696 | /* no media */ | |
1da177e4 LT |
697 | if (load_attempted) |
698 | return -ENOMEDIUM; | |
0c8a6c7a | 699 | ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); |
1da177e4 LT |
700 | load_attempted = 1; |
701 | /* not about to be ready */ | |
702 | } else if (!(tape->sense_key == 2 && tape->asc == 4 && | |
703 | (tape->ascq == 1 || tape->ascq == 8))) | |
704 | return -EIO; | |
80ce45fd | 705 | msleep(100); |
1da177e4 LT |
706 | } |
707 | return -EIO; | |
708 | } | |
709 | ||
5a04cfa9 | 710 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
1da177e4 | 711 | { |
2ac07d92 | 712 | struct ide_tape_obj *tape = drive->driver_data; |
d236d74c | 713 | struct ide_atapi_pc pc; |
1da177e4 LT |
714 | int rc; |
715 | ||
716 | idetape_create_write_filemark_cmd(drive, &pc, 0); | |
b13345f3 | 717 | rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0); |
5a04cfa9 | 718 | if (rc) |
1da177e4 LT |
719 | return rc; |
720 | idetape_wait_ready(drive, 60 * 5 * HZ); | |
721 | return 0; | |
722 | } | |
723 | ||
55ce3a12 | 724 | static int ide_tape_read_position(ide_drive_t *drive) |
1da177e4 LT |
725 | { |
726 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 727 | struct ide_atapi_pc pc; |
55ce3a12 | 728 | u8 buf[20]; |
1da177e4 | 729 | |
e972d702 | 730 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
1da177e4 | 731 | |
55ce3a12 BP |
732 | /* prep cmd */ |
733 | ide_init_pc(&pc); | |
734 | pc.c[0] = READ_POSITION; | |
735 | pc.req_xfer = 20; | |
736 | ||
737 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) | |
1da177e4 | 738 | return -1; |
55ce3a12 BP |
739 | |
740 | if (!pc.error) { | |
e972d702 | 741 | ide_debug_log(IDE_DBG_FUNC, "BOP - %s", |
55ce3a12 | 742 | (buf[0] & 0x80) ? "Yes" : "No"); |
e972d702 | 743 | ide_debug_log(IDE_DBG_FUNC, "EOP - %s", |
55ce3a12 BP |
744 | (buf[0] & 0x40) ? "Yes" : "No"); |
745 | ||
746 | if (buf[0] & 0x4) { | |
747 | printk(KERN_INFO "ide-tape: Block location is unknown" | |
748 | "to the tape\n"); | |
8dcce408 BZ |
749 | clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), |
750 | &drive->atapi_flags); | |
55ce3a12 BP |
751 | return -1; |
752 | } else { | |
e972d702 BP |
753 | ide_debug_log(IDE_DBG_FUNC, "Block Location: %u", |
754 | be32_to_cpup((__be32 *)&buf[4])); | |
55ce3a12 BP |
755 | |
756 | tape->partition = buf[1]; | |
757 | tape->first_frame = be32_to_cpup((__be32 *)&buf[4]); | |
8dcce408 BZ |
758 | set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), |
759 | &drive->atapi_flags); | |
55ce3a12 BP |
760 | } |
761 | } | |
762 | ||
763 | return tape->first_frame; | |
1da177e4 LT |
764 | } |
765 | ||
d236d74c BP |
766 | static void idetape_create_locate_cmd(ide_drive_t *drive, |
767 | struct ide_atapi_pc *pc, | |
5a04cfa9 | 768 | unsigned int block, u8 partition, int skip) |
1da177e4 | 769 | { |
7bf7420a | 770 | ide_init_pc(pc); |
90699ce2 | 771 | pc->c[0] = POSITION_TO_ELEMENT; |
1da177e4 | 772 | pc->c[1] = 2; |
860ff5ec | 773 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
1da177e4 | 774 | pc->c[8] = partition; |
346331f8 | 775 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
776 | } |
777 | ||
ec0fdb01 | 778 | static void __ide_tape_discard_merge_buffer(ide_drive_t *drive) |
1da177e4 LT |
779 | { |
780 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 781 | |
54abf37e | 782 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
9798630a | 783 | return; |
1da177e4 | 784 | |
49d8078a | 785 | clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags); |
963da55c TH |
786 | tape->valid = 0; |
787 | if (tape->buf != NULL) { | |
788 | kfree(tape->buf); | |
789 | tape->buf = NULL; | |
1da177e4 LT |
790 | } |
791 | ||
54abf37e | 792 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
793 | } |
794 | ||
795 | /* | |
3c98bf34 BP |
796 | * Position the tape to the requested block using the LOCATE packet command. |
797 | * A READ POSITION command is then issued to check where we are positioned. Like | |
798 | * all higher level operations, we queue the commands at the tail of the request | |
799 | * queue and wait for their completion. | |
1da177e4 | 800 | */ |
5a04cfa9 BP |
801 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
802 | u8 partition, int skip) | |
1da177e4 LT |
803 | { |
804 | idetape_tape_t *tape = drive->driver_data; | |
2ac07d92 | 805 | struct gendisk *disk = tape->disk; |
55ce3a12 | 806 | int ret; |
d236d74c | 807 | struct ide_atapi_pc pc; |
1da177e4 | 808 | |
54abf37e | 809 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
ec0fdb01 | 810 | __ide_tape_discard_merge_buffer(drive); |
1da177e4 LT |
811 | idetape_wait_ready(drive, 60 * 5 * HZ); |
812 | idetape_create_locate_cmd(drive, &pc, block, partition, skip); | |
55ce3a12 BP |
813 | ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
814 | if (ret) | |
815 | return ret; | |
1da177e4 | 816 | |
55ce3a12 BP |
817 | ret = ide_tape_read_position(drive); |
818 | if (ret < 0) | |
819 | return ret; | |
820 | return 0; | |
1da177e4 LT |
821 | } |
822 | ||
ec0fdb01 | 823 | static void ide_tape_discard_merge_buffer(ide_drive_t *drive, |
5a04cfa9 | 824 | int restore_position) |
1da177e4 LT |
825 | { |
826 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 LT |
827 | int seek, position; |
828 | ||
ec0fdb01 | 829 | __ide_tape_discard_merge_buffer(drive); |
1da177e4 | 830 | if (restore_position) { |
55ce3a12 | 831 | position = ide_tape_read_position(drive); |
9798630a | 832 | seek = position > 0 ? position : 0; |
1da177e4 | 833 | if (idetape_position_tape(drive, seek, 0, 0)) { |
5a04cfa9 | 834 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
ec0fdb01 | 835 | " %s\n", tape->name, __func__); |
1da177e4 LT |
836 | return; |
837 | } | |
838 | } | |
839 | } | |
840 | ||
841 | /* | |
3c98bf34 BP |
842 | * Generate a read/write request for the block device interface and wait for it |
843 | * to be serviced. | |
1da177e4 | 844 | */ |
6bb11dd1 | 845 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size) |
1da177e4 LT |
846 | { |
847 | idetape_tape_t *tape = drive->driver_data; | |
64ea1b4a | 848 | struct request *rq; |
21d9c5d2 | 849 | int ret; |
1da177e4 | 850 | |
e972d702 BP |
851 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size); |
852 | ||
21d9c5d2 | 853 | BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE); |
6bb11dd1 | 854 | BUG_ON(size < 0 || size % tape->blk_size); |
8004a8c9 | 855 | |
64ea1b4a FT |
856 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
857 | rq->cmd_type = REQ_TYPE_SPECIAL; | |
83dd5735 | 858 | rq->cmd[13] = cmd; |
64ea1b4a | 859 | rq->rq_disk = tape->disk; |
c9059598 | 860 | rq->__sector = tape->first_frame; |
21d9c5d2 TH |
861 | |
862 | if (size) { | |
963da55c | 863 | ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size, |
21d9c5d2 TH |
864 | __GFP_WAIT); |
865 | if (ret) | |
866 | goto out_put; | |
867 | } | |
868 | ||
64ea1b4a FT |
869 | blk_execute_rq(drive->queue, tape->disk, rq, 0); |
870 | ||
963da55c | 871 | /* calculate the number of transferred bytes and update buffer state */ |
c3a4d78c | 872 | size -= rq->resid_len; |
963da55c | 873 | tape->cur = tape->buf; |
21d9c5d2 | 874 | if (cmd == REQ_IDETAPE_READ) |
963da55c TH |
875 | tape->valid = size; |
876 | else | |
877 | tape->valid = 0; | |
1da177e4 | 878 | |
21d9c5d2 TH |
879 | ret = size; |
880 | if (rq->errors == IDE_DRV_ERROR_GENERAL) | |
881 | ret = -EIO; | |
21d9c5d2 TH |
882 | out_put: |
883 | blk_put_request(rq); | |
64ea1b4a | 884 | return ret; |
1da177e4 LT |
885 | } |
886 | ||
d236d74c | 887 | static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) |
1da177e4 | 888 | { |
7bf7420a | 889 | ide_init_pc(pc); |
90699ce2 | 890 | pc->c[0] = INQUIRY; |
5a04cfa9 | 891 | pc->c[4] = 254; |
d236d74c | 892 | pc->req_xfer = 254; |
1da177e4 LT |
893 | } |
894 | ||
d236d74c BP |
895 | static void idetape_create_rewind_cmd(ide_drive_t *drive, |
896 | struct ide_atapi_pc *pc) | |
1da177e4 | 897 | { |
7bf7420a | 898 | ide_init_pc(pc); |
90699ce2 | 899 | pc->c[0] = REZERO_UNIT; |
346331f8 | 900 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
901 | } |
902 | ||
d236d74c | 903 | static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) |
1da177e4 | 904 | { |
7bf7420a | 905 | ide_init_pc(pc); |
90699ce2 | 906 | pc->c[0] = ERASE; |
1da177e4 | 907 | pc->c[1] = 1; |
346331f8 | 908 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
909 | } |
910 | ||
d236d74c | 911 | static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) |
1da177e4 | 912 | { |
7bf7420a | 913 | ide_init_pc(pc); |
90699ce2 | 914 | pc->c[0] = SPACE; |
860ff5ec | 915 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
1da177e4 | 916 | pc->c[1] = cmd; |
346331f8 | 917 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
918 | } |
919 | ||
d9df937a | 920 | static void ide_tape_flush_merge_buffer(ide_drive_t *drive) |
1da177e4 LT |
921 | { |
922 | idetape_tape_t *tape = drive->driver_data; | |
55a5d291 | 923 | |
54abf37e | 924 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
077e3bdb | 925 | printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" |
5a04cfa9 | 926 | " but we are not writing.\n"); |
1da177e4 LT |
927 | return; |
928 | } | |
963da55c | 929 | if (tape->buf) { |
6bb11dd1 TH |
930 | size_t aligned = roundup(tape->valid, tape->blk_size); |
931 | ||
932 | memset(tape->cur, 0, aligned - tape->valid); | |
07bd9686 | 933 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned); |
963da55c TH |
934 | kfree(tape->buf); |
935 | tape->buf = NULL; | |
1da177e4 | 936 | } |
54abf37e | 937 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
938 | } |
939 | ||
88f1b941 | 940 | static int idetape_init_rw(ide_drive_t *drive, int dir) |
1da177e4 LT |
941 | { |
942 | idetape_tape_t *tape = drive->driver_data; | |
88f1b941 | 943 | int rc; |
1da177e4 | 944 | |
88f1b941 | 945 | BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE); |
1da177e4 | 946 | |
88f1b941 TH |
947 | if (tape->chrdev_dir == dir) |
948 | return 0; | |
949 | ||
950 | if (tape->chrdev_dir == IDETAPE_DIR_READ) | |
951 | ide_tape_discard_merge_buffer(drive, 1); | |
952 | else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { | |
953 | ide_tape_flush_merge_buffer(drive); | |
954 | idetape_flush_tape_buffers(drive); | |
955 | } | |
956 | ||
957 | if (tape->buf || tape->valid) { | |
958 | printk(KERN_ERR "ide-tape: valid should be 0 now\n"); | |
959 | tape->valid = 0; | |
960 | } | |
961 | ||
962 | tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); | |
963 | if (!tape->buf) | |
964 | return -ENOMEM; | |
965 | tape->chrdev_dir = dir; | |
966 | tape->cur = tape->buf; | |
967 | ||
968 | /* | |
969 | * Issue a 0 rw command to ensure that DSC handshake is | |
970 | * switched from completion mode to buffer available mode. No | |
971 | * point in issuing this if DSC overlap isn't supported, some | |
972 | * drives (Seagate STT3401A) will return an error. | |
973 | */ | |
974 | if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) { | |
975 | int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ | |
976 | : REQ_IDETAPE_WRITE; | |
977 | ||
978 | rc = idetape_queue_rw_tail(drive, cmd, 0); | |
979 | if (rc < 0) { | |
980 | kfree(tape->buf); | |
981 | tape->buf = NULL; | |
982 | tape->chrdev_dir = IDETAPE_DIR_NONE; | |
983 | return rc; | |
1da177e4 LT |
984 | } |
985 | } | |
5e69bd95 | 986 | |
1da177e4 LT |
987 | return 0; |
988 | } | |
989 | ||
5a04cfa9 | 990 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
1da177e4 LT |
991 | { |
992 | idetape_tape_t *tape = drive->driver_data; | |
963da55c TH |
993 | |
994 | memset(tape->buf, 0, tape->buffer_size); | |
3c98bf34 | 995 | |
1da177e4 | 996 | while (bcount) { |
963da55c | 997 | unsigned int count = min(tape->buffer_size, bcount); |
1da177e4 | 998 | |
6bb11dd1 | 999 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count); |
1da177e4 | 1000 | bcount -= count; |
1da177e4 LT |
1001 | } |
1002 | } | |
1003 | ||
1da177e4 | 1004 | /* |
3c98bf34 BP |
1005 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
1006 | * currently support only one partition. | |
1007 | */ | |
5a04cfa9 | 1008 | static int idetape_rewind_tape(ide_drive_t *drive) |
1da177e4 | 1009 | { |
2ac07d92 BZ |
1010 | struct ide_tape_obj *tape = drive->driver_data; |
1011 | struct gendisk *disk = tape->disk; | |
d236d74c | 1012 | struct ide_atapi_pc pc; |
55ce3a12 | 1013 | int ret; |
8004a8c9 | 1014 | |
e972d702 | 1015 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
8004a8c9 | 1016 | |
1da177e4 | 1017 | idetape_create_rewind_cmd(drive, &pc); |
55ce3a12 BP |
1018 | ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
1019 | if (ret) | |
1020 | return ret; | |
1da177e4 | 1021 | |
55ce3a12 BP |
1022 | ret = ide_tape_read_position(drive); |
1023 | if (ret < 0) | |
1024 | return ret; | |
1da177e4 LT |
1025 | return 0; |
1026 | } | |
1027 | ||
3c98bf34 | 1028 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
5a04cfa9 BP |
1029 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
1030 | unsigned long arg) | |
1da177e4 LT |
1031 | { |
1032 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 LT |
1033 | void __user *argp = (void __user *)arg; |
1034 | ||
d59823fa BP |
1035 | struct idetape_config { |
1036 | int dsc_rw_frequency; | |
1037 | int dsc_media_access_frequency; | |
1038 | int nr_stages; | |
1039 | } config; | |
1040 | ||
e972d702 | 1041 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd); |
8004a8c9 | 1042 | |
1da177e4 | 1043 | switch (cmd) { |
5a04cfa9 BP |
1044 | case 0x0340: |
1045 | if (copy_from_user(&config, argp, sizeof(config))) | |
1046 | return -EFAULT; | |
1047 | tape->best_dsc_rw_freq = config.dsc_rw_frequency; | |
5a04cfa9 BP |
1048 | break; |
1049 | case 0x0350: | |
2fc2111c | 1050 | memset(&config, 0, sizeof(config)); |
5a04cfa9 | 1051 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; |
83042b24 | 1052 | config.nr_stages = 1; |
5a04cfa9 BP |
1053 | if (copy_to_user(argp, &config, sizeof(config))) |
1054 | return -EFAULT; | |
1055 | break; | |
1056 | default: | |
1057 | return -EIO; | |
1da177e4 LT |
1058 | } |
1059 | return 0; | |
1060 | } | |
1061 | ||
5a04cfa9 BP |
1062 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
1063 | int mt_count) | |
1da177e4 LT |
1064 | { |
1065 | idetape_tape_t *tape = drive->driver_data; | |
2ac07d92 | 1066 | struct gendisk *disk = tape->disk; |
d236d74c | 1067 | struct ide_atapi_pc pc; |
5a04cfa9 | 1068 | int retval, count = 0; |
b6422013 | 1069 | int sprev = !!(tape->caps[4] & 0x20); |
1da177e4 | 1070 | |
e972d702 BP |
1071 | |
1072 | ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count); | |
1073 | ||
1da177e4 LT |
1074 | if (mt_count == 0) |
1075 | return 0; | |
1076 | if (MTBSF == mt_op || MTBSFM == mt_op) { | |
b6422013 | 1077 | if (!sprev) |
1da177e4 | 1078 | return -EIO; |
5a04cfa9 | 1079 | mt_count = -mt_count; |
1da177e4 LT |
1080 | } |
1081 | ||
54abf37e | 1082 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
963da55c | 1083 | tape->valid = 0; |
49d8078a BP |
1084 | if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK), |
1085 | &drive->atapi_flags)) | |
1da177e4 | 1086 | ++count; |
ec0fdb01 | 1087 | ide_tape_discard_merge_buffer(drive, 0); |
1da177e4 LT |
1088 | } |
1089 | ||
1da177e4 | 1090 | switch (mt_op) { |
5a04cfa9 BP |
1091 | case MTFSF: |
1092 | case MTBSF: | |
1093 | idetape_create_space_cmd(&pc, mt_count - count, | |
1094 | IDETAPE_SPACE_OVER_FILEMARK); | |
b13345f3 | 1095 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
5a04cfa9 BP |
1096 | case MTFSFM: |
1097 | case MTBSFM: | |
1098 | if (!sprev) | |
1099 | return -EIO; | |
1100 | retval = idetape_space_over_filemarks(drive, MTFSF, | |
1101 | mt_count - count); | |
1102 | if (retval) | |
1103 | return retval; | |
1104 | count = (MTBSFM == mt_op ? 1 : -1); | |
1105 | return idetape_space_over_filemarks(drive, MTFSF, count); | |
1106 | default: | |
1107 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", | |
1108 | mt_op); | |
1109 | return -EIO; | |
1da177e4 LT |
1110 | } |
1111 | } | |
1112 | ||
1da177e4 | 1113 | /* |
3c98bf34 | 1114 | * Our character device read / write functions. |
1da177e4 | 1115 | * |
3c98bf34 BP |
1116 | * The tape is optimized to maximize throughput when it is transferring an |
1117 | * integral number of the "continuous transfer limit", which is a parameter of | |
1118 | * the specific tape (26kB on my particular tape, 32kB for Onstream). | |
1da177e4 | 1119 | * |
3c98bf34 BP |
1120 | * As of version 1.3 of the driver, the character device provides an abstract |
1121 | * continuous view of the media - any mix of block sizes (even 1 byte) on the | |
1122 | * same backup/restore procedure is supported. The driver will internally | |
1123 | * convert the requests to the recommended transfer unit, so that an unmatch | |
1124 | * between the user's block size to the recommended size will only result in a | |
1125 | * (slightly) increased driver overhead, but will no longer hit performance. | |
1126 | * This is not applicable to Onstream. | |
1da177e4 | 1127 | */ |
5a04cfa9 BP |
1128 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
1129 | size_t count, loff_t *ppos) | |
1da177e4 | 1130 | { |
5aeddf90 | 1131 | struct ide_tape_obj *tape = file->private_data; |
1da177e4 | 1132 | ide_drive_t *drive = tape->drive; |
07bd9686 | 1133 | size_t done = 0; |
dcd96379 | 1134 | ssize_t ret = 0; |
07bd9686 | 1135 | int rc; |
1da177e4 | 1136 | |
e972d702 | 1137 | ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); |
1da177e4 | 1138 | |
54abf37e | 1139 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
49d8078a | 1140 | if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags)) |
54bb2074 BP |
1141 | if (count > tape->blk_size && |
1142 | (count % tape->blk_size) == 0) | |
1143 | tape->user_bs_factor = count / tape->blk_size; | |
1da177e4 | 1144 | } |
07bd9686 | 1145 | |
88f1b941 | 1146 | rc = idetape_init_rw(drive, IDETAPE_DIR_READ); |
8d06bfad | 1147 | if (rc < 0) |
1da177e4 | 1148 | return rc; |
07bd9686 TH |
1149 | |
1150 | while (done < count) { | |
1151 | size_t todo; | |
1152 | ||
1153 | /* refill if staging buffer is empty */ | |
1154 | if (!tape->valid) { | |
1155 | /* If we are at a filemark, nothing more to read */ | |
49d8078a BP |
1156 | if (test_bit(ilog2(IDE_AFLAG_FILEMARK), |
1157 | &drive->atapi_flags)) | |
07bd9686 TH |
1158 | break; |
1159 | /* read */ | |
1160 | if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, | |
1161 | tape->buffer_size) <= 0) | |
1162 | break; | |
1163 | } | |
1164 | ||
1165 | /* copy out */ | |
1166 | todo = min_t(size_t, count - done, tape->valid); | |
1167 | if (copy_to_user(buf + done, tape->cur, todo)) | |
dcd96379 | 1168 | ret = -EFAULT; |
07bd9686 TH |
1169 | |
1170 | tape->cur += todo; | |
1171 | tape->valid -= todo; | |
1172 | done += todo; | |
1da177e4 | 1173 | } |
07bd9686 | 1174 | |
49d8078a | 1175 | if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) { |
1da177e4 LT |
1176 | idetape_space_over_filemarks(drive, MTFSF, 1); |
1177 | return 0; | |
1178 | } | |
dcd96379 | 1179 | |
07bd9686 | 1180 | return ret ? ret : done; |
1da177e4 LT |
1181 | } |
1182 | ||
5a04cfa9 | 1183 | static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, |
1da177e4 LT |
1184 | size_t count, loff_t *ppos) |
1185 | { | |
5aeddf90 | 1186 | struct ide_tape_obj *tape = file->private_data; |
1da177e4 | 1187 | ide_drive_t *drive = tape->drive; |
07bd9686 | 1188 | size_t done = 0; |
dcd96379 | 1189 | ssize_t ret = 0; |
88f1b941 | 1190 | int rc; |
1da177e4 LT |
1191 | |
1192 | /* The drive is write protected. */ | |
1193 | if (tape->write_prot) | |
1194 | return -EACCES; | |
1195 | ||
e972d702 | 1196 | ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); |
1da177e4 LT |
1197 | |
1198 | /* Initialize write operation */ | |
88f1b941 TH |
1199 | rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE); |
1200 | if (rc < 0) | |
1201 | return rc; | |
07bd9686 TH |
1202 | |
1203 | while (done < count) { | |
1204 | size_t todo; | |
1205 | ||
1206 | /* flush if staging buffer is full */ | |
1207 | if (tape->valid == tape->buffer_size && | |
1208 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, | |
1209 | tape->buffer_size) <= 0) | |
1210 | return rc; | |
1211 | ||
1212 | /* copy in */ | |
1213 | todo = min_t(size_t, count - done, | |
1214 | tape->buffer_size - tape->valid); | |
1215 | if (copy_from_user(tape->cur, buf + done, todo)) | |
dcd96379 | 1216 | ret = -EFAULT; |
07bd9686 TH |
1217 | |
1218 | tape->cur += todo; | |
1219 | tape->valid += todo; | |
1220 | done += todo; | |
1da177e4 | 1221 | } |
07bd9686 TH |
1222 | |
1223 | return ret ? ret : done; | |
1da177e4 LT |
1224 | } |
1225 | ||
5a04cfa9 | 1226 | static int idetape_write_filemark(ide_drive_t *drive) |
1da177e4 | 1227 | { |
2ac07d92 | 1228 | struct ide_tape_obj *tape = drive->driver_data; |
d236d74c | 1229 | struct ide_atapi_pc pc; |
1da177e4 LT |
1230 | |
1231 | /* Write a filemark */ | |
1232 | idetape_create_write_filemark_cmd(drive, &pc, 1); | |
b13345f3 | 1233 | if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) { |
1da177e4 LT |
1234 | printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); |
1235 | return -EIO; | |
1236 | } | |
1237 | return 0; | |
1238 | } | |
1239 | ||
1240 | /* | |
d99c9da2 BP |
1241 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
1242 | * requested. | |
1da177e4 | 1243 | * |
d99c9da2 BP |
1244 | * Note: MTBSF and MTBSFM are not supported when the tape doesn't support |
1245 | * spacing over filemarks in the reverse direction. In this case, MTFSFM is also | |
5bd50dc6 | 1246 | * usually not supported. |
1da177e4 | 1247 | * |
d99c9da2 | 1248 | * The following commands are currently not supported: |
1da177e4 | 1249 | * |
d99c9da2 BP |
1250 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
1251 | * MT_ST_WRITE_THRESHOLD. | |
1da177e4 | 1252 | */ |
d99c9da2 | 1253 | static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) |
1da177e4 LT |
1254 | { |
1255 | idetape_tape_t *tape = drive->driver_data; | |
2ac07d92 | 1256 | struct gendisk *disk = tape->disk; |
d236d74c | 1257 | struct ide_atapi_pc pc; |
5a04cfa9 | 1258 | int i, retval; |
1da177e4 | 1259 | |
e972d702 BP |
1260 | ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d", |
1261 | mt_op, mt_count); | |
5a04cfa9 | 1262 | |
1da177e4 | 1263 | switch (mt_op) { |
5a04cfa9 BP |
1264 | case MTFSF: |
1265 | case MTFSFM: | |
1266 | case MTBSF: | |
1267 | case MTBSFM: | |
1268 | if (!mt_count) | |
1269 | return 0; | |
1270 | return idetape_space_over_filemarks(drive, mt_op, mt_count); | |
1271 | default: | |
1272 | break; | |
1da177e4 | 1273 | } |
5a04cfa9 | 1274 | |
1da177e4 | 1275 | switch (mt_op) { |
5a04cfa9 BP |
1276 | case MTWEOF: |
1277 | if (tape->write_prot) | |
1278 | return -EACCES; | |
ec0fdb01 | 1279 | ide_tape_discard_merge_buffer(drive, 1); |
5a04cfa9 BP |
1280 | for (i = 0; i < mt_count; i++) { |
1281 | retval = idetape_write_filemark(drive); | |
1282 | if (retval) | |
1283 | return retval; | |
1284 | } | |
1285 | return 0; | |
1286 | case MTREW: | |
ec0fdb01 | 1287 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
1288 | if (idetape_rewind_tape(drive)) |
1289 | return -EIO; | |
1290 | return 0; | |
1291 | case MTLOAD: | |
ec0fdb01 | 1292 | ide_tape_discard_merge_buffer(drive, 0); |
0c8a6c7a | 1293 | return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); |
5a04cfa9 BP |
1294 | case MTUNLOAD: |
1295 | case MTOFFL: | |
1296 | /* | |
1297 | * If door is locked, attempt to unlock before | |
1298 | * attempting to eject. | |
1299 | */ | |
1300 | if (tape->door_locked) { | |
0578042d | 1301 | if (!ide_set_media_lock(drive, disk, 0)) |
385a4b87 | 1302 | tape->door_locked = DOOR_UNLOCKED; |
5a04cfa9 | 1303 | } |
ec0fdb01 | 1304 | ide_tape_discard_merge_buffer(drive, 0); |
0c8a6c7a | 1305 | retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK); |
5a04cfa9 | 1306 | if (!retval) |
49d8078a BP |
1307 | clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), |
1308 | &drive->atapi_flags); | |
5a04cfa9 BP |
1309 | return retval; |
1310 | case MTNOP: | |
ec0fdb01 | 1311 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
1312 | return idetape_flush_tape_buffers(drive); |
1313 | case MTRETEN: | |
ec0fdb01 | 1314 | ide_tape_discard_merge_buffer(drive, 0); |
0c8a6c7a | 1315 | return ide_do_start_stop(drive, disk, |
5a04cfa9 | 1316 | IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); |
5a04cfa9 BP |
1317 | case MTEOM: |
1318 | idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); | |
b13345f3 | 1319 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
5a04cfa9 BP |
1320 | case MTERASE: |
1321 | (void)idetape_rewind_tape(drive); | |
1322 | idetape_create_erase_cmd(&pc); | |
b13345f3 | 1323 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
5a04cfa9 BP |
1324 | case MTSETBLK: |
1325 | if (mt_count) { | |
1326 | if (mt_count < tape->blk_size || | |
1327 | mt_count % tape->blk_size) | |
1da177e4 | 1328 | return -EIO; |
5a04cfa9 | 1329 | tape->user_bs_factor = mt_count / tape->blk_size; |
49d8078a BP |
1330 | clear_bit(ilog2(IDE_AFLAG_DETECT_BS), |
1331 | &drive->atapi_flags); | |
5a04cfa9 | 1332 | } else |
49d8078a BP |
1333 | set_bit(ilog2(IDE_AFLAG_DETECT_BS), |
1334 | &drive->atapi_flags); | |
5a04cfa9 BP |
1335 | return 0; |
1336 | case MTSEEK: | |
ec0fdb01 | 1337 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
1338 | return idetape_position_tape(drive, |
1339 | mt_count * tape->user_bs_factor, tape->partition, 0); | |
1340 | case MTSETPART: | |
ec0fdb01 | 1341 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
1342 | return idetape_position_tape(drive, 0, mt_count, 0); |
1343 | case MTFSR: | |
1344 | case MTBSR: | |
1345 | case MTLOCK: | |
0578042d | 1346 | retval = ide_set_media_lock(drive, disk, 1); |
5a04cfa9 | 1347 | if (retval) |
1da177e4 | 1348 | return retval; |
5a04cfa9 BP |
1349 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
1350 | return 0; | |
1351 | case MTUNLOCK: | |
0578042d | 1352 | retval = ide_set_media_lock(drive, disk, 0); |
5a04cfa9 BP |
1353 | if (retval) |
1354 | return retval; | |
1355 | tape->door_locked = DOOR_UNLOCKED; | |
1356 | return 0; | |
1357 | default: | |
1358 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", | |
1359 | mt_op); | |
1360 | return -EIO; | |
1da177e4 LT |
1361 | } |
1362 | } | |
1363 | ||
1364 | /* | |
d99c9da2 BP |
1365 | * Our character device ioctls. General mtio.h magnetic io commands are |
1366 | * supported here, and not in the corresponding block interface. Our own | |
1367 | * ide-tape ioctls are supported on both interfaces. | |
1da177e4 | 1368 | */ |
05227adf | 1369 | static long do_idetape_chrdev_ioctl(struct file *file, |
d99c9da2 | 1370 | unsigned int cmd, unsigned long arg) |
1da177e4 | 1371 | { |
5aeddf90 | 1372 | struct ide_tape_obj *tape = file->private_data; |
1da177e4 LT |
1373 | ide_drive_t *drive = tape->drive; |
1374 | struct mtop mtop; | |
1375 | struct mtget mtget; | |
1376 | struct mtpos mtpos; | |
54bb2074 | 1377 | int block_offset = 0, position = tape->first_frame; |
1da177e4 LT |
1378 | void __user *argp = (void __user *)arg; |
1379 | ||
e972d702 | 1380 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd); |
1da177e4 | 1381 | |
54abf37e | 1382 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
d9df937a | 1383 | ide_tape_flush_merge_buffer(drive); |
1da177e4 LT |
1384 | idetape_flush_tape_buffers(drive); |
1385 | } | |
1386 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { | |
963da55c | 1387 | block_offset = tape->valid / |
54bb2074 | 1388 | (tape->blk_size * tape->user_bs_factor); |
55ce3a12 | 1389 | position = ide_tape_read_position(drive); |
5a04cfa9 | 1390 | if (position < 0) |
1da177e4 LT |
1391 | return -EIO; |
1392 | } | |
1393 | switch (cmd) { | |
5a04cfa9 BP |
1394 | case MTIOCTOP: |
1395 | if (copy_from_user(&mtop, argp, sizeof(struct mtop))) | |
1396 | return -EFAULT; | |
1397 | return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); | |
1398 | case MTIOCGET: | |
1399 | memset(&mtget, 0, sizeof(struct mtget)); | |
1400 | mtget.mt_type = MT_ISSCSI2; | |
1401 | mtget.mt_blkno = position / tape->user_bs_factor - block_offset; | |
1402 | mtget.mt_dsreg = | |
1403 | ((tape->blk_size * tape->user_bs_factor) | |
1404 | << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; | |
1405 | ||
1406 | if (tape->drv_write_prot) | |
1407 | mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); | |
1408 | ||
1409 | if (copy_to_user(argp, &mtget, sizeof(struct mtget))) | |
1410 | return -EFAULT; | |
1411 | return 0; | |
1412 | case MTIOCPOS: | |
1413 | mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; | |
1414 | if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) | |
1415 | return -EFAULT; | |
1416 | return 0; | |
1417 | default: | |
1418 | if (tape->chrdev_dir == IDETAPE_DIR_READ) | |
ec0fdb01 | 1419 | ide_tape_discard_merge_buffer(drive, 1); |
5a04cfa9 | 1420 | return idetape_blkdev_ioctl(drive, cmd, arg); |
1da177e4 LT |
1421 | } |
1422 | } | |
1423 | ||
05227adf AC |
1424 | static long idetape_chrdev_ioctl(struct file *file, |
1425 | unsigned int cmd, unsigned long arg) | |
1426 | { | |
1427 | long ret; | |
2a48fc0a | 1428 | mutex_lock(&ide_tape_mutex); |
05227adf | 1429 | ret = do_idetape_chrdev_ioctl(file, cmd, arg); |
2a48fc0a | 1430 | mutex_unlock(&ide_tape_mutex); |
05227adf AC |
1431 | return ret; |
1432 | } | |
1433 | ||
3cffb9ce BP |
1434 | /* |
1435 | * Do a mode sense page 0 with block descriptor and if it succeeds set the tape | |
1436 | * block size with the reported value. | |
1437 | */ | |
1438 | static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) | |
1439 | { | |
1440 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1441 | struct ide_atapi_pc pc; |
837272b4 | 1442 | u8 buf[12]; |
3cffb9ce BP |
1443 | |
1444 | idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); | |
837272b4 | 1445 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { |
3cffb9ce | 1446 | printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); |
54bb2074 | 1447 | if (tape->blk_size == 0) { |
3cffb9ce BP |
1448 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
1449 | "block size, assuming 32k\n"); | |
54bb2074 | 1450 | tape->blk_size = 32768; |
3cffb9ce BP |
1451 | } |
1452 | return; | |
1453 | } | |
837272b4 BP |
1454 | tape->blk_size = (buf[4 + 5] << 16) + |
1455 | (buf[4 + 6] << 8) + | |
1456 | buf[4 + 7]; | |
1457 | tape->drv_write_prot = (buf[2] & 0x80) >> 7; | |
e972d702 BP |
1458 | |
1459 | ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d", | |
1460 | tape->blk_size, tape->drv_write_prot); | |
3cffb9ce | 1461 | } |
1da177e4 | 1462 | |
5a04cfa9 | 1463 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
1da177e4 LT |
1464 | { |
1465 | unsigned int minor = iminor(inode), i = minor & ~0xc0; | |
1466 | ide_drive_t *drive; | |
1467 | idetape_tape_t *tape; | |
1da177e4 LT |
1468 | int retval; |
1469 | ||
8004a8c9 BP |
1470 | if (i >= MAX_HWIFS * MAX_DRIVES) |
1471 | return -ENXIO; | |
1472 | ||
cbba2fa7 BP |
1473 | mutex_lock(&idetape_chrdev_mutex); |
1474 | ||
9d01e4cd | 1475 | tape = ide_tape_get(NULL, true, i); |
04f4ac9d | 1476 | if (!tape) { |
cbba2fa7 | 1477 | mutex_unlock(&idetape_chrdev_mutex); |
8004a8c9 | 1478 | return -ENXIO; |
04f4ac9d | 1479 | } |
8004a8c9 | 1480 | |
e972d702 BP |
1481 | drive = tape->drive; |
1482 | filp->private_data = tape; | |
1483 | ||
1484 | ide_debug_log(IDE_DBG_FUNC, "enter"); | |
8004a8c9 | 1485 | |
1da177e4 LT |
1486 | /* |
1487 | * We really want to do nonseekable_open(inode, filp); here, but some | |
1488 | * versions of tar incorrectly call lseek on tapes and bail out if that | |
1489 | * fails. So we disallow pread() and pwrite(), but permit lseeks. | |
1490 | */ | |
1491 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); | |
1492 | ||
1da177e4 | 1493 | |
49d8078a | 1494 | if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) { |
1da177e4 LT |
1495 | retval = -EBUSY; |
1496 | goto out_put_tape; | |
1497 | } | |
1498 | ||
1499 | retval = idetape_wait_ready(drive, 60 * HZ); | |
1500 | if (retval) { | |
49d8078a | 1501 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
1da177e4 LT |
1502 | printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); |
1503 | goto out_put_tape; | |
1504 | } | |
1505 | ||
79ca743f | 1506 | ide_tape_read_position(drive); |
49d8078a | 1507 | if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags)) |
1da177e4 LT |
1508 | (void)idetape_rewind_tape(drive); |
1509 | ||
1da177e4 | 1510 | /* Read block size and write protect status from drive. */ |
3cffb9ce | 1511 | ide_tape_get_bsize_from_bdesc(drive); |
1da177e4 LT |
1512 | |
1513 | /* Set write protect flag if device is opened as read-only. */ | |
1514 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) | |
1515 | tape->write_prot = 1; | |
1516 | else | |
1517 | tape->write_prot = tape->drv_write_prot; | |
1518 | ||
1519 | /* Make sure drive isn't write protected if user wants to write. */ | |
1520 | if (tape->write_prot) { | |
1521 | if ((filp->f_flags & O_ACCMODE) == O_WRONLY || | |
1522 | (filp->f_flags & O_ACCMODE) == O_RDWR) { | |
49d8078a | 1523 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
1da177e4 LT |
1524 | retval = -EROFS; |
1525 | goto out_put_tape; | |
1526 | } | |
1527 | } | |
1528 | ||
3c98bf34 | 1529 | /* Lock the tape drive door so user can't eject. */ |
54abf37e | 1530 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
0578042d | 1531 | if (!ide_set_media_lock(drive, tape->disk, 1)) { |
385a4b87 BZ |
1532 | if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) |
1533 | tape->door_locked = DOOR_LOCKED; | |
1da177e4 LT |
1534 | } |
1535 | } | |
cbba2fa7 BP |
1536 | mutex_unlock(&idetape_chrdev_mutex); |
1537 | ||
1da177e4 LT |
1538 | return 0; |
1539 | ||
1540 | out_put_tape: | |
1541 | ide_tape_put(tape); | |
cbba2fa7 BP |
1542 | |
1543 | mutex_unlock(&idetape_chrdev_mutex); | |
1544 | ||
1da177e4 LT |
1545 | return retval; |
1546 | } | |
1547 | ||
5a04cfa9 | 1548 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
1da177e4 LT |
1549 | { |
1550 | idetape_tape_t *tape = drive->driver_data; | |
1551 | ||
d9df937a | 1552 | ide_tape_flush_merge_buffer(drive); |
963da55c TH |
1553 | tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); |
1554 | if (tape->buf != NULL) { | |
54bb2074 BP |
1555 | idetape_pad_zeros(drive, tape->blk_size * |
1556 | (tape->user_bs_factor - 1)); | |
963da55c TH |
1557 | kfree(tape->buf); |
1558 | tape->buf = NULL; | |
1da177e4 LT |
1559 | } |
1560 | idetape_write_filemark(drive); | |
1561 | idetape_flush_tape_buffers(drive); | |
1562 | idetape_flush_tape_buffers(drive); | |
1563 | } | |
1564 | ||
5a04cfa9 | 1565 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
1da177e4 | 1566 | { |
5aeddf90 | 1567 | struct ide_tape_obj *tape = filp->private_data; |
1da177e4 | 1568 | ide_drive_t *drive = tape->drive; |
1da177e4 LT |
1569 | unsigned int minor = iminor(inode); |
1570 | ||
cbba2fa7 BP |
1571 | mutex_lock(&idetape_chrdev_mutex); |
1572 | ||
1da177e4 | 1573 | tape = drive->driver_data; |
8004a8c9 | 1574 | |
e972d702 | 1575 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
1da177e4 | 1576 | |
54abf37e | 1577 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
1da177e4 | 1578 | idetape_write_release(drive, minor); |
54abf37e | 1579 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
1da177e4 | 1580 | if (minor < 128) |
ec0fdb01 | 1581 | ide_tape_discard_merge_buffer(drive, 1); |
1da177e4 | 1582 | } |
f64eee7b | 1583 | |
49d8078a BP |
1584 | if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), |
1585 | &drive->atapi_flags)) | |
1da177e4 | 1586 | (void) idetape_rewind_tape(drive); |
49d8078a | 1587 | |
54abf37e | 1588 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
1da177e4 | 1589 | if (tape->door_locked == DOOR_LOCKED) { |
0578042d | 1590 | if (!ide_set_media_lock(drive, tape->disk, 0)) |
385a4b87 | 1591 | tape->door_locked = DOOR_UNLOCKED; |
1da177e4 LT |
1592 | } |
1593 | } | |
49d8078a | 1594 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
1da177e4 | 1595 | ide_tape_put(tape); |
cbba2fa7 BP |
1596 | |
1597 | mutex_unlock(&idetape_chrdev_mutex); | |
1598 | ||
1da177e4 LT |
1599 | return 0; |
1600 | } | |
1601 | ||
6d29c8f0 | 1602 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
1da177e4 | 1603 | { |
1da177e4 | 1604 | idetape_tape_t *tape = drive->driver_data; |
d236d74c | 1605 | struct ide_atapi_pc pc; |
41fa9f86 | 1606 | u8 pc_buf[256]; |
801bd32e | 1607 | char fw_rev[4], vendor_id[8], product_id[16]; |
6d29c8f0 | 1608 | |
1da177e4 | 1609 | idetape_create_inquiry_cmd(&pc); |
b13345f3 | 1610 | if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) { |
6d29c8f0 BP |
1611 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
1612 | tape->name); | |
1da177e4 LT |
1613 | return; |
1614 | } | |
b13345f3 BP |
1615 | memcpy(vendor_id, &pc_buf[8], 8); |
1616 | memcpy(product_id, &pc_buf[16], 16); | |
1617 | memcpy(fw_rev, &pc_buf[32], 4); | |
41f81d54 | 1618 | |
801bd32e BP |
1619 | ide_fixstring(vendor_id, 8, 0); |
1620 | ide_fixstring(product_id, 16, 0); | |
1621 | ide_fixstring(fw_rev, 4, 0); | |
41f81d54 | 1622 | |
801bd32e | 1623 | printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n", |
41f81d54 | 1624 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
1da177e4 LT |
1625 | } |
1626 | ||
1627 | /* | |
b6422013 BP |
1628 | * Ask the tape about its various parameters. In particular, we will adjust our |
1629 | * data transfer buffer size to the recommended value as returned by the tape. | |
1da177e4 | 1630 | */ |
5a04cfa9 | 1631 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
1da177e4 LT |
1632 | { |
1633 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1634 | struct ide_atapi_pc pc; |
b13345f3 | 1635 | u8 buf[24], *caps; |
b6422013 | 1636 | u8 speed, max_speed; |
47314fa4 | 1637 | |
1da177e4 | 1638 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
b13345f3 | 1639 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { |
b6422013 BP |
1640 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
1641 | " some default values\n"); | |
54bb2074 | 1642 | tape->blk_size = 512; |
b6422013 BP |
1643 | put_unaligned(52, (u16 *)&tape->caps[12]); |
1644 | put_unaligned(540, (u16 *)&tape->caps[14]); | |
1645 | put_unaligned(6*52, (u16 *)&tape->caps[16]); | |
1da177e4 LT |
1646 | return; |
1647 | } | |
b13345f3 | 1648 | caps = buf + 4 + buf[3]; |
b6422013 BP |
1649 | |
1650 | /* convert to host order and save for later use */ | |
cd740ab0 HH |
1651 | speed = be16_to_cpup((__be16 *)&caps[14]); |
1652 | max_speed = be16_to_cpup((__be16 *)&caps[8]); | |
1da177e4 | 1653 | |
cd740ab0 HH |
1654 | *(u16 *)&caps[8] = max_speed; |
1655 | *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]); | |
1656 | *(u16 *)&caps[14] = speed; | |
1657 | *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]); | |
1da177e4 | 1658 | |
b6422013 BP |
1659 | if (!speed) { |
1660 | printk(KERN_INFO "ide-tape: %s: invalid tape speed " | |
1661 | "(assuming 650KB/sec)\n", drive->name); | |
cd740ab0 | 1662 | *(u16 *)&caps[14] = 650; |
1da177e4 | 1663 | } |
b6422013 BP |
1664 | if (!max_speed) { |
1665 | printk(KERN_INFO "ide-tape: %s: invalid max_speed " | |
1666 | "(assuming 650KB/sec)\n", drive->name); | |
cd740ab0 | 1667 | *(u16 *)&caps[8] = 650; |
1da177e4 LT |
1668 | } |
1669 | ||
b6422013 | 1670 | memcpy(&tape->caps, caps, 20); |
0578042d BZ |
1671 | |
1672 | /* device lacks locking support according to capabilities page */ | |
1673 | if ((caps[6] & 1) == 0) | |
42619d35 | 1674 | drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; |
0578042d | 1675 | |
b6422013 | 1676 | if (caps[7] & 0x02) |
54bb2074 | 1677 | tape->blk_size = 512; |
b6422013 | 1678 | else if (caps[7] & 0x04) |
54bb2074 | 1679 | tape->blk_size = 1024; |
1da177e4 LT |
1680 | } |
1681 | ||
7662d046 | 1682 | #ifdef CONFIG_IDE_PROC_FS |
8185d5aa BZ |
1683 | #define ide_tape_devset_get(name, field) \ |
1684 | static int get_##name(ide_drive_t *drive) \ | |
1685 | { \ | |
1686 | idetape_tape_t *tape = drive->driver_data; \ | |
1687 | return tape->field; \ | |
1688 | } | |
1689 | ||
1690 | #define ide_tape_devset_set(name, field) \ | |
1691 | static int set_##name(ide_drive_t *drive, int arg) \ | |
1692 | { \ | |
1693 | idetape_tape_t *tape = drive->driver_data; \ | |
1694 | tape->field = arg; \ | |
1695 | return 0; \ | |
1696 | } | |
1697 | ||
92f1f8fd | 1698 | #define ide_tape_devset_rw_field(_name, _field) \ |
8185d5aa BZ |
1699 | ide_tape_devset_get(_name, _field) \ |
1700 | ide_tape_devset_set(_name, _field) \ | |
92f1f8fd | 1701 | IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name) |
8185d5aa | 1702 | |
92f1f8fd | 1703 | #define ide_tape_devset_r_field(_name, _field) \ |
8185d5aa | 1704 | ide_tape_devset_get(_name, _field) \ |
92f1f8fd | 1705 | IDE_DEVSET(_name, 0, get_##_name, NULL) |
8185d5aa BZ |
1706 | |
1707 | static int mulf_tdsc(ide_drive_t *drive) { return 1000; } | |
1708 | static int divf_tdsc(ide_drive_t *drive) { return HZ; } | |
1709 | static int divf_buffer(ide_drive_t *drive) { return 2; } | |
1710 | static int divf_buffer_size(ide_drive_t *drive) { return 1024; } | |
1711 | ||
97100fc8 | 1712 | ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP); |
92f1f8fd | 1713 | |
92f1f8fd EO |
1714 | ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq); |
1715 | ||
1716 | ide_tape_devset_r_field(avg_speed, avg_speed); | |
1717 | ide_tape_devset_r_field(speed, caps[14]); | |
1718 | ide_tape_devset_r_field(buffer, caps[16]); | |
1719 | ide_tape_devset_r_field(buffer_size, buffer_size); | |
1720 | ||
1721 | static const struct ide_proc_devset idetape_settings[] = { | |
1722 | __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL), | |
1723 | __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer), | |
1724 | __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size), | |
92f1f8fd EO |
1725 | __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL), |
1726 | __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL), | |
1727 | __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, | |
1728 | mulf_tdsc, divf_tdsc), | |
71bfc7a7 | 1729 | { NULL }, |
8185d5aa | 1730 | }; |
7662d046 | 1731 | #endif |
1da177e4 LT |
1732 | |
1733 | /* | |
3c98bf34 | 1734 | * The function below is called to: |
1da177e4 | 1735 | * |
3c98bf34 BP |
1736 | * 1. Initialize our various state variables. |
1737 | * 2. Ask the tape for its capabilities. | |
1738 | * 3. Allocate a buffer which will be used for data transfer. The buffer size | |
1739 | * is chosen based on the recommendation which we received in step 2. | |
1da177e4 | 1740 | * |
3c98bf34 BP |
1741 | * Note that at this point ide.c already assigned us an irq, so that we can |
1742 | * queue requests here and wait for their completion. | |
1da177e4 | 1743 | */ |
5a04cfa9 | 1744 | static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) |
1da177e4 | 1745 | { |
83042b24 | 1746 | unsigned long t; |
1da177e4 | 1747 | int speed; |
f73850a3 | 1748 | int buffer_size; |
b6422013 | 1749 | u16 *ctl = (u16 *)&tape->caps[12]; |
1da177e4 | 1750 | |
e972d702 BP |
1751 | ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor); |
1752 | ||
1753 | drive->pc_callback = ide_tape_callback; | |
776bb027 | 1754 | |
97100fc8 BZ |
1755 | drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP; |
1756 | ||
4166c199 BZ |
1757 | if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { |
1758 | printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", | |
1759 | tape->name); | |
97100fc8 | 1760 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
1da177e4 | 1761 | } |
97100fc8 | 1762 | |
1da177e4 | 1763 | /* Seagate Travan drives do not support DSC overlap. */ |
4dde4492 | 1764 | if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401")) |
97100fc8 BZ |
1765 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
1766 | ||
1da177e4 LT |
1767 | tape->minor = minor; |
1768 | tape->name[0] = 'h'; | |
1769 | tape->name[1] = 't'; | |
1770 | tape->name[2] = '0' + minor; | |
54abf37e | 1771 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
4dde4492 | 1772 | |
1da177e4 LT |
1773 | idetape_get_inquiry_results(drive); |
1774 | idetape_get_mode_sense_results(drive); | |
3cffb9ce | 1775 | ide_tape_get_bsize_from_bdesc(drive); |
1da177e4 | 1776 | tape->user_bs_factor = 1; |
f73850a3 BP |
1777 | tape->buffer_size = *ctl * tape->blk_size; |
1778 | while (tape->buffer_size > 0xffff) { | |
1da177e4 | 1779 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); |
b6422013 | 1780 | *ctl /= 2; |
f73850a3 | 1781 | tape->buffer_size = *ctl * tape->blk_size; |
1da177e4 | 1782 | } |
f73850a3 | 1783 | buffer_size = tape->buffer_size; |
1da177e4 | 1784 | |
83042b24 | 1785 | /* select the "best" DSC read/write polling freq */ |
b6422013 | 1786 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
1da177e4 | 1787 | |
f73850a3 | 1788 | t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); |
1da177e4 LT |
1789 | |
1790 | /* | |
3c98bf34 BP |
1791 | * Ensure that the number we got makes sense; limit it within |
1792 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. | |
1da177e4 | 1793 | */ |
a792bd5a HH |
1794 | tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, |
1795 | IDETAPE_DSC_RW_MAX); | |
1da177e4 | 1796 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
83042b24 | 1797 | "%lums tDSC%s\n", |
b6422013 | 1798 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
f73850a3 BP |
1799 | (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, |
1800 | tape->buffer_size / 1024, | |
54bb2074 | 1801 | tape->best_dsc_rw_freq * 1000 / HZ, |
97100fc8 | 1802 | (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : ""); |
1da177e4 | 1803 | |
1e874f44 | 1804 | ide_proc_register_driver(drive, tape->driver); |
1da177e4 LT |
1805 | } |
1806 | ||
4031bbe4 | 1807 | static void ide_tape_remove(ide_drive_t *drive) |
1da177e4 LT |
1808 | { |
1809 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1810 | |
7662d046 | 1811 | ide_proc_unregister_driver(drive, tape->driver); |
8fed4368 | 1812 | device_del(&tape->dev); |
1da177e4 LT |
1813 | ide_unregister_region(tape->disk); |
1814 | ||
8fed4368 BZ |
1815 | mutex_lock(&idetape_ref_mutex); |
1816 | put_device(&tape->dev); | |
1817 | mutex_unlock(&idetape_ref_mutex); | |
1da177e4 LT |
1818 | } |
1819 | ||
8fed4368 | 1820 | static void ide_tape_release(struct device *dev) |
1da177e4 | 1821 | { |
8fed4368 | 1822 | struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj); |
1da177e4 LT |
1823 | ide_drive_t *drive = tape->drive; |
1824 | struct gendisk *g = tape->disk; | |
1825 | ||
963da55c | 1826 | BUG_ON(tape->valid); |
8604affd | 1827 | |
97100fc8 | 1828 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
1da177e4 | 1829 | drive->driver_data = NULL; |
dbc1272e | 1830 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
5a04cfa9 BP |
1831 | device_destroy(idetape_sysfs_class, |
1832 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); | |
1da177e4 LT |
1833 | idetape_devs[tape->minor] = NULL; |
1834 | g->private_data = NULL; | |
1835 | put_disk(g); | |
1836 | kfree(tape); | |
1837 | } | |
1838 | ||
ecfd80e4 | 1839 | #ifdef CONFIG_IDE_PROC_FS |
6d703a81 | 1840 | static int idetape_name_proc_show(struct seq_file *m, void *v) |
1da177e4 | 1841 | { |
6d703a81 | 1842 | ide_drive_t *drive = (ide_drive_t *) m->private; |
1da177e4 | 1843 | idetape_tape_t *tape = drive->driver_data; |
1da177e4 | 1844 | |
6d703a81 AD |
1845 | seq_printf(m, "%s\n", tape->name); |
1846 | return 0; | |
1847 | } | |
1848 | ||
1849 | static int idetape_name_proc_open(struct inode *inode, struct file *file) | |
1850 | { | |
1851 | return single_open(file, idetape_name_proc_show, PDE(inode)->data); | |
1da177e4 LT |
1852 | } |
1853 | ||
6d703a81 AD |
1854 | static const struct file_operations idetape_name_proc_fops = { |
1855 | .owner = THIS_MODULE, | |
1856 | .open = idetape_name_proc_open, | |
1857 | .read = seq_read, | |
1858 | .llseek = seq_lseek, | |
1859 | .release = single_release, | |
1860 | }; | |
1861 | ||
1da177e4 | 1862 | static ide_proc_entry_t idetape_proc[] = { |
6d703a81 AD |
1863 | { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops }, |
1864 | { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops }, | |
1865 | {} | |
1da177e4 | 1866 | }; |
79cb3803 BZ |
1867 | |
1868 | static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive) | |
1869 | { | |
1870 | return idetape_proc; | |
1871 | } | |
1872 | ||
1873 | static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive) | |
1874 | { | |
1875 | return idetape_settings; | |
1876 | } | |
1da177e4 LT |
1877 | #endif |
1878 | ||
4031bbe4 | 1879 | static int ide_tape_probe(ide_drive_t *); |
1da177e4 | 1880 | |
7f3c868b | 1881 | static struct ide_driver idetape_driver = { |
8604affd | 1882 | .gen_driver = { |
4ef3b8f4 | 1883 | .owner = THIS_MODULE, |
8604affd BZ |
1884 | .name = "ide-tape", |
1885 | .bus = &ide_bus_type, | |
8604affd | 1886 | }, |
4031bbe4 RK |
1887 | .probe = ide_tape_probe, |
1888 | .remove = ide_tape_remove, | |
1da177e4 | 1889 | .version = IDETAPE_VERSION, |
1da177e4 | 1890 | .do_request = idetape_do_request, |
7662d046 | 1891 | #ifdef CONFIG_IDE_PROC_FS |
79cb3803 BZ |
1892 | .proc_entries = ide_tape_proc_entries, |
1893 | .proc_devsets = ide_tape_proc_devsets, | |
7662d046 | 1894 | #endif |
1da177e4 LT |
1895 | }; |
1896 | ||
3c98bf34 | 1897 | /* Our character device supporting functions, passed to register_chrdev. */ |
2b8693c0 | 1898 | static const struct file_operations idetape_fops = { |
1da177e4 LT |
1899 | .owner = THIS_MODULE, |
1900 | .read = idetape_chrdev_read, | |
1901 | .write = idetape_chrdev_write, | |
05227adf | 1902 | .unlocked_ioctl = idetape_chrdev_ioctl, |
1da177e4 LT |
1903 | .open = idetape_chrdev_open, |
1904 | .release = idetape_chrdev_release, | |
1905 | }; | |
1906 | ||
a4600f81 | 1907 | static int idetape_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 1908 | { |
6e9624b8 AB |
1909 | struct ide_tape_obj *tape; |
1910 | ||
2a48fc0a | 1911 | mutex_lock(&ide_tape_mutex); |
6e9624b8 | 1912 | tape = ide_tape_get(bdev->bd_disk, false, 0); |
2a48fc0a | 1913 | mutex_unlock(&ide_tape_mutex); |
1da177e4 | 1914 | |
5a04cfa9 | 1915 | if (!tape) |
1da177e4 LT |
1916 | return -ENXIO; |
1917 | ||
1da177e4 LT |
1918 | return 0; |
1919 | } | |
1920 | ||
a4600f81 | 1921 | static int idetape_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 1922 | { |
5aeddf90 | 1923 | struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj); |
1da177e4 | 1924 | |
2a48fc0a | 1925 | mutex_lock(&ide_tape_mutex); |
1da177e4 | 1926 | ide_tape_put(tape); |
2a48fc0a | 1927 | mutex_unlock(&ide_tape_mutex); |
6e9624b8 | 1928 | |
1da177e4 LT |
1929 | return 0; |
1930 | } | |
1931 | ||
a4600f81 | 1932 | static int idetape_ioctl(struct block_device *bdev, fmode_t mode, |
1da177e4 LT |
1933 | unsigned int cmd, unsigned long arg) |
1934 | { | |
5aeddf90 | 1935 | struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj); |
1da177e4 | 1936 | ide_drive_t *drive = tape->drive; |
8a6cfeb6 AB |
1937 | int err; |
1938 | ||
2a48fc0a | 1939 | mutex_lock(&ide_tape_mutex); |
8a6cfeb6 | 1940 | err = generic_ide_ioctl(drive, bdev, cmd, arg); |
1da177e4 LT |
1941 | if (err == -EINVAL) |
1942 | err = idetape_blkdev_ioctl(drive, cmd, arg); | |
2a48fc0a | 1943 | mutex_unlock(&ide_tape_mutex); |
8a6cfeb6 | 1944 | |
1da177e4 LT |
1945 | return err; |
1946 | } | |
1947 | ||
83d5cde4 | 1948 | static const struct block_device_operations idetape_block_ops = { |
1da177e4 | 1949 | .owner = THIS_MODULE, |
a4600f81 AV |
1950 | .open = idetape_open, |
1951 | .release = idetape_release, | |
8a6cfeb6 | 1952 | .ioctl = idetape_ioctl, |
1da177e4 LT |
1953 | }; |
1954 | ||
4031bbe4 | 1955 | static int ide_tape_probe(ide_drive_t *drive) |
1da177e4 LT |
1956 | { |
1957 | idetape_tape_t *tape; | |
1958 | struct gendisk *g; | |
1959 | int minor; | |
1960 | ||
e972d702 BP |
1961 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
1962 | ||
1963 | if (!strstr(DRV_NAME, drive->driver_req)) | |
1da177e4 | 1964 | goto failed; |
2a924662 | 1965 | |
1da177e4 LT |
1966 | if (drive->media != ide_tape) |
1967 | goto failed; | |
2a924662 | 1968 | |
97100fc8 BZ |
1969 | if ((drive->dev_flags & IDE_DFLAG_ID_READ) && |
1970 | ide_check_atapi_device(drive, DRV_NAME) == 0) { | |
5a04cfa9 BP |
1971 | printk(KERN_ERR "ide-tape: %s: not supported by this version of" |
1972 | " the driver\n", drive->name); | |
1da177e4 LT |
1973 | goto failed; |
1974 | } | |
5a04cfa9 | 1975 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
1da177e4 | 1976 | if (tape == NULL) { |
5a04cfa9 BP |
1977 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
1978 | drive->name); | |
1da177e4 LT |
1979 | goto failed; |
1980 | } | |
1981 | ||
1982 | g = alloc_disk(1 << PARTN_BITS); | |
1983 | if (!g) | |
1984 | goto out_free_tape; | |
1985 | ||
1986 | ide_init_disk(g, drive); | |
1987 | ||
8fed4368 BZ |
1988 | tape->dev.parent = &drive->gendev; |
1989 | tape->dev.release = ide_tape_release; | |
1990 | dev_set_name(&tape->dev, dev_name(&drive->gendev)); | |
1991 | ||
1992 | if (device_register(&tape->dev)) | |
1993 | goto out_free_disk; | |
1da177e4 LT |
1994 | |
1995 | tape->drive = drive; | |
1996 | tape->driver = &idetape_driver; | |
1997 | tape->disk = g; | |
1998 | ||
1999 | g->private_data = &tape->driver; | |
2000 | ||
2001 | drive->driver_data = tape; | |
2002 | ||
cf8b8975 | 2003 | mutex_lock(&idetape_ref_mutex); |
1da177e4 LT |
2004 | for (minor = 0; idetape_devs[minor]; minor++) |
2005 | ; | |
2006 | idetape_devs[minor] = tape; | |
cf8b8975 | 2007 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
2008 | |
2009 | idetape_setup(drive, tape, minor); | |
2010 | ||
3ee074bf GKH |
2011 | device_create(idetape_sysfs_class, &drive->gendev, |
2012 | MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name); | |
2013 | device_create(idetape_sysfs_class, &drive->gendev, | |
2014 | MKDEV(IDETAPE_MAJOR, minor + 128), NULL, | |
2015 | "n%s", tape->name); | |
d5dee80a | 2016 | |
1da177e4 LT |
2017 | g->fops = &idetape_block_ops; |
2018 | ide_register_region(g); | |
2019 | ||
2020 | return 0; | |
8604affd | 2021 | |
8fed4368 BZ |
2022 | out_free_disk: |
2023 | put_disk(g); | |
1da177e4 LT |
2024 | out_free_tape: |
2025 | kfree(tape); | |
2026 | failed: | |
8604affd | 2027 | return -ENODEV; |
1da177e4 LT |
2028 | } |
2029 | ||
5a04cfa9 | 2030 | static void __exit idetape_exit(void) |
1da177e4 | 2031 | { |
8604affd | 2032 | driver_unregister(&idetape_driver.gen_driver); |
d5dee80a | 2033 | class_destroy(idetape_sysfs_class); |
1da177e4 LT |
2034 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
2035 | } | |
2036 | ||
17514e8a | 2037 | static int __init idetape_init(void) |
1da177e4 | 2038 | { |
d5dee80a WD |
2039 | int error = 1; |
2040 | idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); | |
2041 | if (IS_ERR(idetape_sysfs_class)) { | |
2042 | idetape_sysfs_class = NULL; | |
2043 | printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); | |
2044 | error = -EBUSY; | |
2045 | goto out; | |
2046 | } | |
2047 | ||
1da177e4 | 2048 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
5a04cfa9 BP |
2049 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
2050 | " interface\n"); | |
d5dee80a WD |
2051 | error = -EBUSY; |
2052 | goto out_free_class; | |
1da177e4 | 2053 | } |
d5dee80a WD |
2054 | |
2055 | error = driver_register(&idetape_driver.gen_driver); | |
2056 | if (error) | |
2057 | goto out_free_driver; | |
2058 | ||
2059 | return 0; | |
2060 | ||
2061 | out_free_driver: | |
2062 | driver_unregister(&idetape_driver.gen_driver); | |
2063 | out_free_class: | |
2064 | class_destroy(idetape_sysfs_class); | |
2065 | out: | |
2066 | return error; | |
1da177e4 LT |
2067 | } |
2068 | ||
263756ec | 2069 | MODULE_ALIAS("ide:*m-tape*"); |
1da177e4 LT |
2070 | module_init(idetape_init); |
2071 | module_exit(idetape_exit); | |
2072 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); | |
9c145768 BP |
2073 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
2074 | MODULE_LICENSE("GPL"); |