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