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