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