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 | ||
dfe79936 | 18 | #define IDETAPE_VERSION "1.20" |
1da177e4 | 19 | |
1da177e4 LT |
20 | #include <linux/module.h> |
21 | #include <linux/types.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/timer.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/interrupt.h> | |
9bae1ff3 | 28 | #include <linux/jiffies.h> |
1da177e4 | 29 | #include <linux/major.h> |
1da177e4 LT |
30 | #include <linux/errno.h> |
31 | #include <linux/genhd.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/pci.h> | |
34 | #include <linux/ide.h> | |
35 | #include <linux/smp_lock.h> | |
36 | #include <linux/completion.h> | |
37 | #include <linux/bitops.h> | |
cf8b8975 | 38 | #include <linux/mutex.h> |
90699ce2 | 39 | #include <scsi/scsi.h> |
1da177e4 LT |
40 | |
41 | #include <asm/byteorder.h> | |
c837cfa5 BP |
42 | #include <linux/irq.h> |
43 | #include <linux/uaccess.h> | |
44 | #include <linux/io.h> | |
1da177e4 | 45 | #include <asm/unaligned.h> |
1da177e4 LT |
46 | #include <linux/mtio.h> |
47 | ||
8004a8c9 BP |
48 | enum { |
49 | /* output errors only */ | |
50 | DBG_ERR = (1 << 0), | |
51 | /* output all sense key/asc */ | |
52 | DBG_SENSE = (1 << 1), | |
53 | /* info regarding all chrdev-related procedures */ | |
54 | DBG_CHRDEV = (1 << 2), | |
55 | /* all remaining procedures */ | |
56 | DBG_PROCS = (1 << 3), | |
57 | /* buffer alloc info (pc_stack & rq_stack) */ | |
58 | DBG_PCRQ_STACK = (1 << 4), | |
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 | ||
83 | /* | |
3c98bf34 BP |
84 | * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE |
85 | * bytes. This is used for several packet commands (Not for READ/WRITE commands) | |
1da177e4 LT |
86 | */ |
87 | #define IDETAPE_PC_BUFFER_SIZE 256 | |
88 | ||
89 | /* | |
90 | * In various places in the driver, we need to allocate storage | |
91 | * for packet commands and requests, which will remain valid while | |
92 | * we leave the driver to wait for an interrupt or a timeout event. | |
93 | */ | |
94 | #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES) | |
95 | ||
96 | /* | |
97 | * Some drives (for example, Seagate STT3401A Travan) require a very long | |
98 | * timeout, because they don't return an interrupt or clear their busy bit | |
99 | * until after the command completes (even retension commands). | |
100 | */ | |
101 | #define IDETAPE_WAIT_CMD (900*HZ) | |
102 | ||
103 | /* | |
3c98bf34 BP |
104 | * The following parameter is used to select the point in the internal tape fifo |
105 | * in which we will start to refill the buffer. Decreasing the following | |
106 | * parameter will improve the system's latency and interactive response, while | |
107 | * using a high value might improve system throughput. | |
1da177e4 | 108 | */ |
3c98bf34 | 109 | #define IDETAPE_FIFO_THRESHOLD 2 |
1da177e4 LT |
110 | |
111 | /* | |
3c98bf34 | 112 | * DSC polling parameters. |
1da177e4 | 113 | * |
3c98bf34 BP |
114 | * Polling for DSC (a single bit in the status register) is a very important |
115 | * function in ide-tape. There are two cases in which we poll for DSC: | |
1da177e4 | 116 | * |
3c98bf34 BP |
117 | * 1. Before a read/write packet command, to ensure that we can transfer data |
118 | * from/to the tape's data buffers, without causing an actual media access. | |
119 | * In case the tape is not ready yet, we take out our request from the device | |
120 | * request queue, so that ide.c could service requests from the other device | |
121 | * on the same interface in the meantime. | |
1da177e4 | 122 | * |
3c98bf34 BP |
123 | * 2. After the successful initialization of a "media access packet command", |
124 | * which is a command that can take a long time to complete (the interval can | |
125 | * range from several seconds to even an hour). Again, we postpone our request | |
126 | * in the middle to free the bus for the other device. The polling frequency | |
127 | * here should be lower than the read/write frequency since those media access | |
128 | * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST | |
129 | * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD | |
130 | * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min). | |
1da177e4 | 131 | * |
3c98bf34 BP |
132 | * We also set a timeout for the timer, in case something goes wrong. The |
133 | * timeout should be longer then the maximum execution time of a tape operation. | |
1da177e4 | 134 | */ |
3c98bf34 BP |
135 | |
136 | /* DSC timings. */ | |
1da177e4 LT |
137 | #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */ |
138 | #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */ | |
139 | #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */ | |
140 | #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */ | |
141 | #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */ | |
142 | #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */ | |
143 | #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */ | |
144 | ||
145 | /*************************** End of tunable parameters ***********************/ | |
146 | ||
54abf37e BP |
147 | /* tape directions */ |
148 | enum { | |
149 | IDETAPE_DIR_NONE = (1 << 0), | |
150 | IDETAPE_DIR_READ = (1 << 1), | |
151 | IDETAPE_DIR_WRITE = (1 << 2), | |
152 | }; | |
1da177e4 LT |
153 | |
154 | struct idetape_bh { | |
ab057968 | 155 | u32 b_size; |
1da177e4 LT |
156 | atomic_t b_count; |
157 | struct idetape_bh *b_reqnext; | |
158 | char *b_data; | |
159 | }; | |
160 | ||
03056b90 BP |
161 | /* Tape door status */ |
162 | #define DOOR_UNLOCKED 0 | |
163 | #define DOOR_LOCKED 1 | |
164 | #define DOOR_EXPLICITLY_LOCKED 2 | |
165 | ||
166 | /* Some defines for the SPACE command */ | |
167 | #define IDETAPE_SPACE_OVER_FILEMARK 1 | |
168 | #define IDETAPE_SPACE_TO_EOD 3 | |
169 | ||
170 | /* Some defines for the LOAD UNLOAD command */ | |
171 | #define IDETAPE_LU_LOAD_MASK 1 | |
172 | #define IDETAPE_LU_RETENSION_MASK 2 | |
173 | #define IDETAPE_LU_EOT_MASK 4 | |
174 | ||
175 | /* | |
176 | * Special requests for our block device strategy routine. | |
177 | * | |
178 | * In order to service a character device command, we add special requests to | |
179 | * the tail of our block device request queue and wait for their completion. | |
180 | */ | |
181 | ||
182 | enum { | |
183 | REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */ | |
184 | REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */ | |
185 | REQ_IDETAPE_READ = (1 << 2), | |
186 | REQ_IDETAPE_WRITE = (1 << 3), | |
187 | }; | |
188 | ||
189 | /* Error codes returned in rq->errors to the higher part of the driver. */ | |
190 | #define IDETAPE_ERROR_GENERAL 101 | |
191 | #define IDETAPE_ERROR_FILEMARK 102 | |
192 | #define IDETAPE_ERROR_EOD 103 | |
193 | ||
194 | /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ | |
195 | #define IDETAPE_BLOCK_DESCRIPTOR 0 | |
196 | #define IDETAPE_CAPABILITIES_PAGE 0x2a | |
197 | ||
1da177e4 | 198 | /* |
3c98bf34 BP |
199 | * Most of our global data which we need to save even as we leave the driver due |
200 | * to an interrupt or a timer event is stored in the struct defined below. | |
1da177e4 LT |
201 | */ |
202 | typedef struct ide_tape_obj { | |
203 | ide_drive_t *drive; | |
204 | ide_driver_t *driver; | |
205 | struct gendisk *disk; | |
206 | struct kref kref; | |
207 | ||
208 | /* | |
209 | * Since a typical character device operation requires more | |
210 | * than one packet command, we provide here enough memory | |
211 | * for the maximum of interconnected packet commands. | |
212 | * The packet commands are stored in the circular array pc_stack. | |
213 | * pc_stack_index points to the last used entry, and warps around | |
214 | * to the start when we get to the last array entry. | |
215 | * | |
216 | * pc points to the current processed packet command. | |
217 | * | |
218 | * failed_pc points to the last failed packet command, or contains | |
219 | * NULL if we do not need to retry any packet command. This is | |
220 | * required since an additional packet command is needed before the | |
221 | * retry, to get detailed information on what went wrong. | |
222 | */ | |
223 | /* Current packet command */ | |
d236d74c | 224 | struct ide_atapi_pc *pc; |
1da177e4 | 225 | /* Last failed packet command */ |
d236d74c | 226 | struct ide_atapi_pc *failed_pc; |
1da177e4 | 227 | /* Packet command stack */ |
d236d74c | 228 | struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK]; |
1da177e4 LT |
229 | /* Next free packet command storage space */ |
230 | int pc_stack_index; | |
231 | struct request rq_stack[IDETAPE_PC_STACK]; | |
232 | /* We implement a circular array */ | |
233 | int rq_stack_index; | |
234 | ||
235 | /* | |
3c98bf34 | 236 | * DSC polling variables. |
1da177e4 | 237 | * |
3c98bf34 BP |
238 | * While polling for DSC we use postponed_rq to postpone the current |
239 | * request so that ide.c will be able to service pending requests on the | |
240 | * other device. Note that at most we will have only one DSC (usually | |
5bd50dc6 | 241 | * data transfer) request in the device request queue. |
1da177e4 LT |
242 | */ |
243 | struct request *postponed_rq; | |
244 | /* The time in which we started polling for DSC */ | |
245 | unsigned long dsc_polling_start; | |
246 | /* Timer used to poll for dsc */ | |
247 | struct timer_list dsc_timer; | |
248 | /* Read/Write dsc polling frequency */ | |
54bb2074 BP |
249 | unsigned long best_dsc_rw_freq; |
250 | unsigned long dsc_poll_freq; | |
1da177e4 LT |
251 | unsigned long dsc_timeout; |
252 | ||
3c98bf34 | 253 | /* Read position information */ |
1da177e4 LT |
254 | u8 partition; |
255 | /* Current block */ | |
54bb2074 | 256 | unsigned int first_frame; |
1da177e4 | 257 | |
3c98bf34 | 258 | /* Last error information */ |
1da177e4 LT |
259 | u8 sense_key, asc, ascq; |
260 | ||
3c98bf34 | 261 | /* Character device operation */ |
1da177e4 LT |
262 | unsigned int minor; |
263 | /* device name */ | |
264 | char name[4]; | |
265 | /* Current character device data transfer direction */ | |
54abf37e | 266 | u8 chrdev_dir; |
1da177e4 | 267 | |
54bb2074 BP |
268 | /* tape block size, usually 512 or 1024 bytes */ |
269 | unsigned short blk_size; | |
1da177e4 | 270 | int user_bs_factor; |
b6422013 | 271 | |
1da177e4 | 272 | /* Copy of the tape's Capabilities and Mechanical Page */ |
b6422013 | 273 | u8 caps[20]; |
1da177e4 LT |
274 | |
275 | /* | |
3c98bf34 | 276 | * Active data transfer request parameters. |
1da177e4 | 277 | * |
3c98bf34 BP |
278 | * At most, there is only one ide-tape originated data transfer request |
279 | * in the device request queue. This allows ide.c to easily service | |
280 | * requests from the other device when we postpone our active request. | |
1da177e4 | 281 | */ |
83042b24 | 282 | |
3c98bf34 | 283 | /* Data buffer size chosen based on the tape's recommendation */ |
f73850a3 | 284 | int buffer_size; |
077e3bdb BP |
285 | /* merge buffer */ |
286 | struct idetape_bh *merge_bh; | |
01a63aeb BP |
287 | /* size of the merge buffer */ |
288 | int merge_bh_size; | |
077e3bdb | 289 | /* pointer to current buffer head within the merge buffer */ |
1da177e4 LT |
290 | struct idetape_bh *bh; |
291 | char *b_data; | |
292 | int b_count; | |
3c98bf34 | 293 | |
a997a435 | 294 | int pages_per_buffer; |
1da177e4 LT |
295 | /* Wasted space in each stage */ |
296 | int excess_bh_size; | |
297 | ||
1da177e4 | 298 | /* protects the ide-tape queue */ |
54bb2074 | 299 | spinlock_t lock; |
1da177e4 | 300 | |
3c98bf34 | 301 | /* Measures average tape speed */ |
1da177e4 LT |
302 | unsigned long avg_time; |
303 | int avg_size; | |
304 | int avg_speed; | |
305 | ||
1da177e4 LT |
306 | /* the door is currently locked */ |
307 | int door_locked; | |
308 | /* the tape hardware is write protected */ | |
309 | char drv_write_prot; | |
310 | /* the tape is write protected (hardware or opened as read-only) */ | |
311 | char write_prot; | |
312 | ||
8004a8c9 | 313 | u32 debug_mask; |
1da177e4 LT |
314 | } idetape_tape_t; |
315 | ||
cf8b8975 | 316 | static DEFINE_MUTEX(idetape_ref_mutex); |
1da177e4 | 317 | |
d5dee80a WD |
318 | static struct class *idetape_sysfs_class; |
319 | ||
1da177e4 LT |
320 | #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref) |
321 | ||
322 | #define ide_tape_g(disk) \ | |
323 | container_of((disk)->private_data, struct ide_tape_obj, driver) | |
324 | ||
08da591e BZ |
325 | static void ide_tape_release(struct kref *); |
326 | ||
1da177e4 LT |
327 | static struct ide_tape_obj *ide_tape_get(struct gendisk *disk) |
328 | { | |
329 | struct ide_tape_obj *tape = NULL; | |
330 | ||
cf8b8975 | 331 | mutex_lock(&idetape_ref_mutex); |
1da177e4 | 332 | tape = ide_tape_g(disk); |
08da591e | 333 | if (tape) { |
d3e33ff5 | 334 | if (ide_device_get(tape->drive)) |
08da591e | 335 | tape = NULL; |
d3e33ff5 BZ |
336 | else |
337 | kref_get(&tape->kref); | |
08da591e | 338 | } |
cf8b8975 | 339 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
340 | return tape; |
341 | } | |
342 | ||
1da177e4 LT |
343 | static void ide_tape_put(struct ide_tape_obj *tape) |
344 | { | |
d3e33ff5 BZ |
345 | ide_drive_t *drive = tape->drive; |
346 | ||
cf8b8975 | 347 | mutex_lock(&idetape_ref_mutex); |
1da177e4 | 348 | kref_put(&tape->kref, ide_tape_release); |
d3e33ff5 | 349 | ide_device_put(drive); |
cf8b8975 | 350 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
351 | } |
352 | ||
1da177e4 | 353 | /* |
3c98bf34 BP |
354 | * The variables below are used for the character device interface. Additional |
355 | * state variables are defined in our ide_drive_t structure. | |
1da177e4 | 356 | */ |
5a04cfa9 | 357 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
1da177e4 LT |
358 | |
359 | #define ide_tape_f(file) ((file)->private_data) | |
360 | ||
361 | static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i) | |
362 | { | |
363 | struct ide_tape_obj *tape = NULL; | |
364 | ||
cf8b8975 | 365 | mutex_lock(&idetape_ref_mutex); |
1da177e4 LT |
366 | tape = idetape_devs[i]; |
367 | if (tape) | |
368 | kref_get(&tape->kref); | |
cf8b8975 | 369 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
370 | return tape; |
371 | } | |
372 | ||
d236d74c | 373 | static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
5a04cfa9 | 374 | unsigned int bcount) |
1da177e4 LT |
375 | { |
376 | struct idetape_bh *bh = pc->bh; | |
377 | int count; | |
378 | ||
379 | while (bcount) { | |
1da177e4 LT |
380 | if (bh == NULL) { |
381 | printk(KERN_ERR "ide-tape: bh == NULL in " | |
382 | "idetape_input_buffers\n"); | |
9f87abe8 | 383 | ide_pad_transfer(drive, 0, bcount); |
1da177e4 LT |
384 | return; |
385 | } | |
5a04cfa9 BP |
386 | count = min( |
387 | (unsigned int)(bh->b_size - atomic_read(&bh->b_count)), | |
388 | bcount); | |
374e042c | 389 | drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data + |
5a04cfa9 | 390 | atomic_read(&bh->b_count), count); |
1da177e4 LT |
391 | bcount -= count; |
392 | atomic_add(count, &bh->b_count); | |
393 | if (atomic_read(&bh->b_count) == bh->b_size) { | |
394 | bh = bh->b_reqnext; | |
395 | if (bh) | |
396 | atomic_set(&bh->b_count, 0); | |
397 | } | |
398 | } | |
399 | pc->bh = bh; | |
400 | } | |
401 | ||
d236d74c | 402 | static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
5a04cfa9 | 403 | unsigned int bcount) |
1da177e4 LT |
404 | { |
405 | struct idetape_bh *bh = pc->bh; | |
406 | int count; | |
407 | ||
408 | while (bcount) { | |
1da177e4 | 409 | if (bh == NULL) { |
5a04cfa9 BP |
410 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
411 | __func__); | |
1da177e4 LT |
412 | return; |
413 | } | |
1da177e4 | 414 | count = min((unsigned int)pc->b_count, (unsigned int)bcount); |
374e042c | 415 | drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count); |
1da177e4 LT |
416 | bcount -= count; |
417 | pc->b_data += count; | |
418 | pc->b_count -= count; | |
419 | if (!pc->b_count) { | |
5a04cfa9 BP |
420 | bh = bh->b_reqnext; |
421 | pc->bh = bh; | |
1da177e4 LT |
422 | if (bh) { |
423 | pc->b_data = bh->b_data; | |
424 | pc->b_count = atomic_read(&bh->b_count); | |
425 | } | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
646c0cb6 | 430 | static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc) |
1da177e4 LT |
431 | { |
432 | struct idetape_bh *bh = pc->bh; | |
433 | int count; | |
d236d74c | 434 | unsigned int bcount = pc->xferred; |
1da177e4 | 435 | |
346331f8 | 436 | if (pc->flags & PC_FLAG_WRITING) |
1da177e4 LT |
437 | return; |
438 | while (bcount) { | |
1da177e4 | 439 | if (bh == NULL) { |
5a04cfa9 BP |
440 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
441 | __func__); | |
1da177e4 LT |
442 | return; |
443 | } | |
1da177e4 LT |
444 | count = min((unsigned int)bh->b_size, (unsigned int)bcount); |
445 | atomic_set(&bh->b_count, count); | |
446 | if (atomic_read(&bh->b_count) == bh->b_size) | |
447 | bh = bh->b_reqnext; | |
448 | bcount -= count; | |
449 | } | |
450 | pc->bh = bh; | |
451 | } | |
452 | ||
453 | /* | |
454 | * idetape_next_pc_storage returns a pointer to a place in which we can | |
455 | * safely store a packet command, even though we intend to leave the | |
456 | * driver. A storage space for a maximum of IDETAPE_PC_STACK packet | |
457 | * commands is allocated at initialization time. | |
458 | */ | |
d236d74c | 459 | static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive) |
1da177e4 LT |
460 | { |
461 | idetape_tape_t *tape = drive->driver_data; | |
462 | ||
8004a8c9 BP |
463 | debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index); |
464 | ||
1da177e4 | 465 | if (tape->pc_stack_index == IDETAPE_PC_STACK) |
5a04cfa9 | 466 | tape->pc_stack_index = 0; |
1da177e4 LT |
467 | return (&tape->pc_stack[tape->pc_stack_index++]); |
468 | } | |
469 | ||
470 | /* | |
471 | * idetape_next_rq_storage is used along with idetape_next_pc_storage. | |
472 | * Since we queue packet commands in the request queue, we need to | |
473 | * allocate a request, along with the allocation of a packet command. | |
474 | */ | |
5a04cfa9 | 475 | |
1da177e4 LT |
476 | /************************************************************** |
477 | * * | |
478 | * This should get fixed to use kmalloc(.., GFP_ATOMIC) * | |
479 | * followed later on by kfree(). -ml * | |
480 | * * | |
481 | **************************************************************/ | |
5a04cfa9 BP |
482 | |
483 | static struct request *idetape_next_rq_storage(ide_drive_t *drive) | |
1da177e4 LT |
484 | { |
485 | idetape_tape_t *tape = drive->driver_data; | |
486 | ||
8004a8c9 BP |
487 | debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index); |
488 | ||
1da177e4 | 489 | if (tape->rq_stack_index == IDETAPE_PC_STACK) |
5a04cfa9 | 490 | tape->rq_stack_index = 0; |
1da177e4 LT |
491 | return (&tape->rq_stack[tape->rq_stack_index++]); |
492 | } | |
493 | ||
1da177e4 | 494 | /* |
1b5db434 BP |
495 | * called on each failed packet command retry to analyze the request sense. We |
496 | * currently do not utilize this information. | |
1da177e4 | 497 | */ |
1b5db434 | 498 | static void idetape_analyze_error(ide_drive_t *drive, u8 *sense) |
1da177e4 LT |
499 | { |
500 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 501 | struct ide_atapi_pc *pc = tape->failed_pc; |
1da177e4 | 502 | |
1b5db434 BP |
503 | tape->sense_key = sense[2] & 0xF; |
504 | tape->asc = sense[12]; | |
505 | tape->ascq = sense[13]; | |
8004a8c9 BP |
506 | |
507 | debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n", | |
508 | pc->c[0], tape->sense_key, tape->asc, tape->ascq); | |
1da177e4 | 509 | |
d236d74c | 510 | /* Correct pc->xferred by asking the tape. */ |
346331f8 | 511 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
d236d74c | 512 | pc->xferred = pc->req_xfer - |
54bb2074 | 513 | tape->blk_size * |
5d0cc8ae | 514 | get_unaligned_be32(&sense[3]); |
646c0cb6 | 515 | idetape_update_buffers(drive, pc); |
1da177e4 LT |
516 | } |
517 | ||
518 | /* | |
519 | * If error was the result of a zero-length read or write command, | |
520 | * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives | |
521 | * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. | |
522 | */ | |
90699ce2 | 523 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
1b5db434 BP |
524 | /* length == 0 */ |
525 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { | |
526 | if (tape->sense_key == 5) { | |
1da177e4 LT |
527 | /* don't report an error, everything's ok */ |
528 | pc->error = 0; | |
529 | /* don't retry read/write */ | |
346331f8 | 530 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 LT |
531 | } |
532 | } | |
90699ce2 | 533 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
1da177e4 | 534 | pc->error = IDETAPE_ERROR_FILEMARK; |
346331f8 | 535 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 | 536 | } |
90699ce2 | 537 | if (pc->c[0] == WRITE_6) { |
1b5db434 BP |
538 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
539 | && tape->asc == 0x0 && tape->ascq == 0x2)) { | |
1da177e4 | 540 | pc->error = IDETAPE_ERROR_EOD; |
346331f8 | 541 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 LT |
542 | } |
543 | } | |
90699ce2 | 544 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
1b5db434 | 545 | if (tape->sense_key == 8) { |
1da177e4 | 546 | pc->error = IDETAPE_ERROR_EOD; |
346331f8 | 547 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 | 548 | } |
346331f8 | 549 | if (!(pc->flags & PC_FLAG_ABORT) && |
d236d74c | 550 | pc->xferred) |
1da177e4 LT |
551 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
552 | } | |
553 | } | |
554 | ||
d01dbc3b | 555 | /* Free data buffers completely. */ |
077e3bdb | 556 | static void ide_tape_kfree_buffer(idetape_tape_t *tape) |
1da177e4 | 557 | { |
077e3bdb | 558 | struct idetape_bh *prev_bh, *bh = tape->merge_bh; |
d01dbc3b BP |
559 | |
560 | while (bh) { | |
561 | u32 size = bh->b_size; | |
562 | ||
563 | while (size) { | |
564 | unsigned int order = fls(size >> PAGE_SHIFT)-1; | |
565 | ||
566 | if (bh->b_data) | |
567 | free_pages((unsigned long)bh->b_data, order); | |
568 | ||
569 | size &= (order-1); | |
570 | bh->b_data += (1 << order) * PAGE_SIZE; | |
1da177e4 LT |
571 | } |
572 | prev_bh = bh; | |
573 | bh = bh->b_reqnext; | |
574 | kfree(prev_bh); | |
575 | } | |
1da177e4 LT |
576 | } |
577 | ||
1da177e4 LT |
578 | static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects) |
579 | { | |
580 | struct request *rq = HWGROUP(drive)->rq; | |
581 | idetape_tape_t *tape = drive->driver_data; | |
582 | unsigned long flags; | |
583 | int error; | |
1da177e4 | 584 | |
8004a8c9 | 585 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1da177e4 LT |
586 | |
587 | switch (uptodate) { | |
5a04cfa9 BP |
588 | case 0: error = IDETAPE_ERROR_GENERAL; break; |
589 | case 1: error = 0; break; | |
590 | default: error = uptodate; | |
1da177e4 LT |
591 | } |
592 | rq->errors = error; | |
593 | if (error) | |
594 | tape->failed_pc = NULL; | |
595 | ||
3687221f BZ |
596 | if (!blk_special_request(rq)) { |
597 | ide_end_request(drive, uptodate, nr_sects); | |
598 | return 0; | |
599 | } | |
600 | ||
54bb2074 | 601 | spin_lock_irqsave(&tape->lock, flags); |
1da177e4 | 602 | |
1da177e4 | 603 | ide_end_drive_cmd(drive, 0, 0); |
1da177e4 | 604 | |
54bb2074 | 605 | spin_unlock_irqrestore(&tape->lock, flags); |
1da177e4 LT |
606 | return 0; |
607 | } | |
608 | ||
92f5daff | 609 | static void ide_tape_callback(ide_drive_t *drive) |
1da177e4 LT |
610 | { |
611 | idetape_tape_t *tape = drive->driver_data; | |
5985e6ab BZ |
612 | struct ide_atapi_pc *pc = tape->pc; |
613 | int uptodate = pc->error ? 0 : 1; | |
1da177e4 | 614 | |
8004a8c9 BP |
615 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
616 | ||
dd2e9a03 BZ |
617 | if (tape->failed_pc == pc) |
618 | tape->failed_pc = NULL; | |
619 | ||
5985e6ab BZ |
620 | if (pc->c[0] == REQUEST_SENSE) { |
621 | if (uptodate) | |
622 | idetape_analyze_error(drive, pc->buf); | |
623 | else | |
624 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE " | |
625 | "itself - Aborting request!\n"); | |
626 | } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { | |
627 | struct request *rq = drive->hwif->hwgroup->rq; | |
628 | int blocks = pc->xferred / tape->blk_size; | |
629 | ||
630 | tape->avg_size += blocks * tape->blk_size; | |
631 | ||
632 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { | |
633 | tape->avg_speed = tape->avg_size * HZ / | |
634 | (jiffies - tape->avg_time) / 1024; | |
635 | tape->avg_size = 0; | |
636 | tape->avg_time = jiffies; | |
637 | } | |
638 | ||
639 | tape->first_frame += blocks; | |
640 | rq->current_nr_sectors -= blocks; | |
641 | ||
642 | if (pc->error) | |
643 | uptodate = pc->error; | |
644 | } else if (pc->c[0] == READ_POSITION && uptodate) { | |
645 | u8 *readpos = tape->pc->buf; | |
646 | ||
647 | debug_log(DBG_SENSE, "BOP - %s\n", | |
648 | (readpos[0] & 0x80) ? "Yes" : "No"); | |
649 | debug_log(DBG_SENSE, "EOP - %s\n", | |
650 | (readpos[0] & 0x40) ? "Yes" : "No"); | |
651 | ||
652 | if (readpos[0] & 0x4) { | |
653 | printk(KERN_INFO "ide-tape: Block location is unknown" | |
654 | "to the tape\n"); | |
f2e3ab52 | 655 | clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags); |
5985e6ab BZ |
656 | uptodate = 0; |
657 | } else { | |
658 | debug_log(DBG_SENSE, "Block Location - %u\n", | |
cd740ab0 | 659 | be32_to_cpup((__be32 *)&readpos[4])); |
5985e6ab BZ |
660 | |
661 | tape->partition = readpos[1]; | |
cd740ab0 | 662 | tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]); |
f2e3ab52 | 663 | set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags); |
5985e6ab | 664 | } |
1da177e4 | 665 | } |
5985e6ab BZ |
666 | |
667 | idetape_end_request(drive, uptodate, 0); | |
1da177e4 LT |
668 | } |
669 | ||
5985e6ab BZ |
670 | static void idetape_init_pc(struct ide_atapi_pc *pc) |
671 | { | |
672 | memset(pc->c, 0, 12); | |
673 | pc->retries = 0; | |
674 | pc->flags = 0; | |
675 | pc->req_xfer = 0; | |
676 | pc->buf = pc->pc_buf; | |
677 | pc->buf_size = IDETAPE_PC_BUFFER_SIZE; | |
678 | pc->bh = NULL; | |
679 | pc->b_data = NULL; | |
1da177e4 LT |
680 | } |
681 | ||
d236d74c | 682 | static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc) |
1da177e4 | 683 | { |
5a04cfa9 | 684 | idetape_init_pc(pc); |
90699ce2 | 685 | pc->c[0] = REQUEST_SENSE; |
1da177e4 | 686 | pc->c[4] = 20; |
d236d74c | 687 | pc->req_xfer = 20; |
1da177e4 LT |
688 | } |
689 | ||
690 | static void idetape_init_rq(struct request *rq, u8 cmd) | |
691 | { | |
e7b241a7 | 692 | blk_rq_init(NULL, rq); |
4aff5e23 | 693 | rq->cmd_type = REQ_TYPE_SPECIAL; |
83dd5735 | 694 | rq->cmd[13] = cmd; |
1da177e4 LT |
695 | } |
696 | ||
697 | /* | |
3c98bf34 BP |
698 | * Generate a new packet command request in front of the request queue, before |
699 | * the current request, so that it will be processed immediately, on the next | |
700 | * pass through the driver. The function below is called from the request | |
701 | * handling part of the driver (the "bottom" part). Safe storage for the request | |
702 | * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that. | |
1da177e4 | 703 | * |
3c98bf34 BP |
704 | * Memory for those requests is pre-allocated at initialization time, and is |
705 | * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for | |
706 | * the maximum possible number of inter-dependent packet commands. | |
1da177e4 | 707 | * |
3c98bf34 BP |
708 | * The higher level of the driver - The ioctl handler and the character device |
709 | * handling functions should queue request to the lower level part and wait for | |
710 | * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail. | |
1da177e4 | 711 | */ |
d236d74c | 712 | static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc, |
5a04cfa9 | 713 | struct request *rq) |
1da177e4 LT |
714 | { |
715 | struct ide_tape_obj *tape = drive->driver_data; | |
716 | ||
717 | idetape_init_rq(rq, REQ_IDETAPE_PC1); | |
e8a96aa7 | 718 | rq->cmd_flags |= REQ_PREEMPT; |
1da177e4 LT |
719 | rq->buffer = (char *) pc; |
720 | rq->rq_disk = tape->disk; | |
0014c75b | 721 | memcpy(rq->cmd, pc->c, 12); |
63f5abb0 | 722 | ide_do_drive_cmd(drive, rq); |
1da177e4 LT |
723 | } |
724 | ||
725 | /* | |
726 | * idetape_retry_pc is called when an error was detected during the | |
727 | * last packet command. We queue a request sense packet command in | |
728 | * the head of the request list. | |
729 | */ | |
258ec411 | 730 | static void idetape_retry_pc(ide_drive_t *drive) |
1da177e4 | 731 | { |
d236d74c | 732 | struct ide_atapi_pc *pc; |
1da177e4 | 733 | struct request *rq; |
1da177e4 | 734 | |
64a57fe4 | 735 | (void)ide_read_error(drive); |
1da177e4 LT |
736 | pc = idetape_next_pc_storage(drive); |
737 | rq = idetape_next_rq_storage(drive); | |
738 | idetape_create_request_sense_cmd(pc); | |
f2e3ab52 | 739 | set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); |
1da177e4 | 740 | idetape_queue_pc_head(drive, pc, rq); |
1da177e4 LT |
741 | } |
742 | ||
743 | /* | |
3c98bf34 BP |
744 | * Postpone the current request so that ide.c will be able to service requests |
745 | * from another device on the same hwgroup while we are polling for DSC. | |
1da177e4 | 746 | */ |
5a04cfa9 | 747 | static void idetape_postpone_request(ide_drive_t *drive) |
1da177e4 LT |
748 | { |
749 | idetape_tape_t *tape = drive->driver_data; | |
750 | ||
8004a8c9 BP |
751 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
752 | ||
1da177e4 | 753 | tape->postponed_rq = HWGROUP(drive)->rq; |
54bb2074 | 754 | ide_stall_queue(drive, tape->dsc_poll_freq); |
1da177e4 LT |
755 | } |
756 | ||
74e63e74 BZ |
757 | static void ide_tape_handle_dsc(ide_drive_t *drive) |
758 | { | |
759 | idetape_tape_t *tape = drive->driver_data; | |
760 | ||
761 | /* Media access command */ | |
762 | tape->dsc_polling_start = jiffies; | |
763 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; | |
764 | tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; | |
765 | /* Allow ide.c to handle other requests */ | |
766 | idetape_postpone_request(drive); | |
767 | } | |
768 | ||
08424ac2 BZ |
769 | static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
770 | unsigned int bcount, int write) | |
771 | { | |
772 | if (write) | |
773 | idetape_output_buffers(drive, pc, bcount); | |
774 | else | |
775 | idetape_input_buffers(drive, pc, bcount); | |
776 | } | |
a1efc85f | 777 | |
1da177e4 | 778 | /* |
a1efc85f BP |
779 | * This is the usual interrupt handler which will be called during a packet |
780 | * command. We will transfer some of the data (as requested by the drive) and | |
781 | * will re-point interrupt handler to us. When data transfer is finished, we | |
782 | * will act according to the algorithm described before | |
8d06bfad | 783 | * idetape_issue_pc. |
1da177e4 | 784 | */ |
a1efc85f | 785 | static ide_startstop_t idetape_pc_intr(ide_drive_t *drive) |
1da177e4 | 786 | { |
1da177e4 | 787 | idetape_tape_t *tape = drive->driver_data; |
1da177e4 | 788 | |
646c0cb6 BZ |
789 | return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD, |
790 | NULL, idetape_update_buffers, idetape_retry_pc, | |
791 | ide_tape_handle_dsc, ide_tape_io_buffers); | |
1da177e4 LT |
792 | } |
793 | ||
794 | /* | |
3c98bf34 | 795 | * Packet Command Interface |
1da177e4 | 796 | * |
3c98bf34 BP |
797 | * The current Packet Command is available in tape->pc, and will not change |
798 | * until we finish handling it. Each packet command is associated with a | |
799 | * callback function that will be called when the command is finished. | |
1da177e4 | 800 | * |
3c98bf34 | 801 | * The handling will be done in three stages: |
1da177e4 | 802 | * |
3c98bf34 BP |
803 | * 1. idetape_issue_pc will send the packet command to the drive, and will set |
804 | * the interrupt handler to idetape_pc_intr. | |
1da177e4 | 805 | * |
3c98bf34 BP |
806 | * 2. On each interrupt, idetape_pc_intr will be called. This step will be |
807 | * repeated until the device signals us that no more interrupts will be issued. | |
1da177e4 | 808 | * |
3c98bf34 BP |
809 | * 3. ATAPI Tape media access commands have immediate status with a delayed |
810 | * process. In case of a successful initiation of a media access packet command, | |
811 | * the DSC bit will be set when the actual execution of the command is finished. | |
812 | * Since the tape drive will not issue an interrupt, we have to poll for this | |
813 | * event. In this case, we define the request as "low priority request" by | |
814 | * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and | |
815 | * exit the driver. | |
1da177e4 | 816 | * |
3c98bf34 BP |
817 | * ide.c will then give higher priority to requests which originate from the |
818 | * other device, until will change rq_status to RQ_ACTIVE. | |
1da177e4 | 819 | * |
3c98bf34 | 820 | * 4. When the packet command is finished, it will be checked for errors. |
1da177e4 | 821 | * |
3c98bf34 BP |
822 | * 5. In case an error was found, we queue a request sense packet command in |
823 | * front of the request queue and retry the operation up to | |
824 | * IDETAPE_MAX_PC_RETRIES times. | |
1da177e4 | 825 | * |
3c98bf34 BP |
826 | * 6. In case no error was found, or we decided to give up and not to retry |
827 | * again, the callback function will be called and then we will handle the next | |
828 | * request. | |
1da177e4 LT |
829 | */ |
830 | static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive) | |
831 | { | |
1da177e4 | 832 | idetape_tape_t *tape = drive->driver_data; |
9567b349 | 833 | |
594c16d8 BZ |
834 | return ide_transfer_pc(drive, tape->pc, idetape_pc_intr, |
835 | IDETAPE_WAIT_CMD, NULL); | |
1da177e4 LT |
836 | } |
837 | ||
d236d74c BP |
838 | static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, |
839 | struct ide_atapi_pc *pc) | |
1da177e4 | 840 | { |
1da177e4 | 841 | idetape_tape_t *tape = drive->driver_data; |
1da177e4 | 842 | |
90699ce2 BP |
843 | if (tape->pc->c[0] == REQUEST_SENSE && |
844 | pc->c[0] == REQUEST_SENSE) { | |
1da177e4 LT |
845 | printk(KERN_ERR "ide-tape: possible ide-tape.c bug - " |
846 | "Two request sense in serial were issued\n"); | |
847 | } | |
1da177e4 | 848 | |
90699ce2 | 849 | if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
1da177e4 LT |
850 | tape->failed_pc = pc; |
851 | /* Set the current packet command */ | |
852 | tape->pc = pc; | |
853 | ||
854 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || | |
346331f8 | 855 | (pc->flags & PC_FLAG_ABORT)) { |
1da177e4 | 856 | /* |
3c98bf34 BP |
857 | * We will "abort" retrying a packet command in case legitimate |
858 | * error code was received (crossing a filemark, or end of the | |
859 | * media, for example). | |
1da177e4 | 860 | */ |
346331f8 | 861 | if (!(pc->flags & PC_FLAG_ABORT)) { |
90699ce2 | 862 | if (!(pc->c[0] == TEST_UNIT_READY && |
1da177e4 LT |
863 | tape->sense_key == 2 && tape->asc == 4 && |
864 | (tape->ascq == 1 || tape->ascq == 8))) { | |
865 | printk(KERN_ERR "ide-tape: %s: I/O error, " | |
866 | "pc = %2x, key = %2x, " | |
867 | "asc = %2x, ascq = %2x\n", | |
868 | tape->name, pc->c[0], | |
869 | tape->sense_key, tape->asc, | |
870 | tape->ascq); | |
871 | } | |
872 | /* Giving up */ | |
873 | pc->error = IDETAPE_ERROR_GENERAL; | |
874 | } | |
875 | tape->failed_pc = NULL; | |
776bb027 | 876 | drive->pc_callback(drive); |
92f5daff | 877 | return ide_stopped; |
1da177e4 | 878 | } |
8004a8c9 | 879 | debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]); |
1da177e4 LT |
880 | |
881 | pc->retries++; | |
1da177e4 | 882 | |
6bf1641c BZ |
883 | return ide_issue_pc(drive, pc, idetape_transfer_pc, |
884 | IDETAPE_WAIT_CMD, NULL); | |
1da177e4 LT |
885 | } |
886 | ||
3c98bf34 | 887 | /* A mode sense command is used to "sense" tape parameters. */ |
d236d74c | 888 | static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code) |
1da177e4 LT |
889 | { |
890 | idetape_init_pc(pc); | |
90699ce2 | 891 | pc->c[0] = MODE_SENSE; |
1da177e4 | 892 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
3c98bf34 BP |
893 | /* DBD = 1 - Don't return block descriptors */ |
894 | pc->c[1] = 8; | |
1da177e4 LT |
895 | pc->c[2] = page_code; |
896 | /* | |
897 | * Changed pc->c[3] to 0 (255 will at best return unused info). | |
898 | * | |
899 | * For SCSI this byte is defined as subpage instead of high byte | |
900 | * of length and some IDE drives seem to interpret it this way | |
901 | * and return an error when 255 is used. | |
902 | */ | |
903 | pc->c[3] = 0; | |
3c98bf34 BP |
904 | /* We will just discard data in that case */ |
905 | pc->c[4] = 255; | |
1da177e4 | 906 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
d236d74c | 907 | pc->req_xfer = 12; |
1da177e4 | 908 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
d236d74c | 909 | pc->req_xfer = 24; |
1da177e4 | 910 | else |
d236d74c | 911 | pc->req_xfer = 50; |
1da177e4 LT |
912 | } |
913 | ||
5a04cfa9 | 914 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
1da177e4 | 915 | { |
b73c7ee2 | 916 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 917 | idetape_tape_t *tape = drive->driver_data; |
d236d74c | 918 | struct ide_atapi_pc *pc = tape->pc; |
22c525b9 | 919 | u8 stat; |
1da177e4 | 920 | |
374e042c | 921 | stat = hwif->tp_ops->read_status(hwif); |
c47137a9 | 922 | |
3a7d2484 BZ |
923 | if (stat & ATA_DSC) { |
924 | if (stat & ATA_ERR) { | |
1da177e4 | 925 | /* Error detected */ |
90699ce2 | 926 | if (pc->c[0] != TEST_UNIT_READY) |
1da177e4 LT |
927 | printk(KERN_ERR "ide-tape: %s: I/O error, ", |
928 | tape->name); | |
929 | /* Retry operation */ | |
258ec411 BZ |
930 | idetape_retry_pc(drive); |
931 | return ide_stopped; | |
1da177e4 LT |
932 | } |
933 | pc->error = 0; | |
1da177e4 LT |
934 | } else { |
935 | pc->error = IDETAPE_ERROR_GENERAL; | |
936 | tape->failed_pc = NULL; | |
937 | } | |
776bb027 | 938 | drive->pc_callback(drive); |
1da177e4 LT |
939 | return ide_stopped; |
940 | } | |
941 | ||
cd2abbfe | 942 | static void ide_tape_create_rw_cmd(idetape_tape_t *tape, |
0014c75b BP |
943 | struct ide_atapi_pc *pc, struct request *rq, |
944 | u8 opcode) | |
1da177e4 | 945 | { |
0014c75b BP |
946 | struct idetape_bh *bh = (struct idetape_bh *)rq->special; |
947 | unsigned int length = rq->current_nr_sectors; | |
948 | ||
1da177e4 | 949 | idetape_init_pc(pc); |
860ff5ec | 950 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
1da177e4 | 951 | pc->c[1] = 1; |
1da177e4 | 952 | pc->bh = bh; |
d236d74c BP |
953 | pc->buf = NULL; |
954 | pc->buf_size = length * tape->blk_size; | |
955 | pc->req_xfer = pc->buf_size; | |
f73850a3 | 956 | if (pc->req_xfer == tape->buffer_size) |
5e331095 | 957 | pc->flags |= PC_FLAG_DMA_OK; |
1da177e4 | 958 | |
cd2abbfe BP |
959 | if (opcode == READ_6) { |
960 | pc->c[0] = READ_6; | |
961 | atomic_set(&bh->b_count, 0); | |
962 | } else if (opcode == WRITE_6) { | |
963 | pc->c[0] = WRITE_6; | |
964 | pc->flags |= PC_FLAG_WRITING; | |
965 | pc->b_data = bh->b_data; | |
966 | pc->b_count = atomic_read(&bh->b_count); | |
967 | } | |
0014c75b BP |
968 | |
969 | memcpy(rq->cmd, pc->c, 12); | |
1da177e4 LT |
970 | } |
971 | ||
1da177e4 LT |
972 | static ide_startstop_t idetape_do_request(ide_drive_t *drive, |
973 | struct request *rq, sector_t block) | |
974 | { | |
b73c7ee2 | 975 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 976 | idetape_tape_t *tape = drive->driver_data; |
d236d74c | 977 | struct ide_atapi_pc *pc = NULL; |
1da177e4 | 978 | struct request *postponed_rq = tape->postponed_rq; |
22c525b9 | 979 | u8 stat; |
1da177e4 | 980 | |
730616b2 MW |
981 | debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu," |
982 | " current_nr_sectors: %u\n", | |
983 | (unsigned long long)rq->sector, rq->nr_sectors, | |
984 | rq->current_nr_sectors); | |
1da177e4 | 985 | |
4aff5e23 | 986 | if (!blk_special_request(rq)) { |
3c98bf34 | 987 | /* We do not support buffer cache originated requests. */ |
1da177e4 | 988 | printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " |
4aff5e23 | 989 | "request queue (%d)\n", drive->name, rq->cmd_type); |
1da177e4 LT |
990 | ide_end_request(drive, 0, 0); |
991 | return ide_stopped; | |
992 | } | |
993 | ||
3c98bf34 | 994 | /* Retry a failed packet command */ |
28c7214b BZ |
995 | if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) { |
996 | pc = tape->failed_pc; | |
997 | goto out; | |
998 | } | |
5a04cfa9 | 999 | |
1da177e4 LT |
1000 | if (postponed_rq != NULL) |
1001 | if (rq != postponed_rq) { | |
1002 | printk(KERN_ERR "ide-tape: ide-tape.c bug - " | |
1003 | "Two DSC requests were queued\n"); | |
1004 | idetape_end_request(drive, 0, 0); | |
1005 | return ide_stopped; | |
1006 | } | |
1da177e4 LT |
1007 | |
1008 | tape->postponed_rq = NULL; | |
1009 | ||
1010 | /* | |
1011 | * If the tape is still busy, postpone our request and service | |
1012 | * the other device meanwhile. | |
1013 | */ | |
374e042c | 1014 | stat = hwif->tp_ops->read_status(hwif); |
1da177e4 | 1015 | |
83dd5735 | 1016 | if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2)) |
f2e3ab52 | 1017 | set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); |
1da177e4 LT |
1018 | |
1019 | if (drive->post_reset == 1) { | |
f2e3ab52 | 1020 | set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); |
1da177e4 LT |
1021 | drive->post_reset = 0; |
1022 | } | |
1023 | ||
f2e3ab52 | 1024 | if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) && |
3a7d2484 | 1025 | (stat & ATA_DSC) == 0) { |
1da177e4 LT |
1026 | if (postponed_rq == NULL) { |
1027 | tape->dsc_polling_start = jiffies; | |
54bb2074 | 1028 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
1da177e4 LT |
1029 | tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; |
1030 | } else if (time_after(jiffies, tape->dsc_timeout)) { | |
1031 | printk(KERN_ERR "ide-tape: %s: DSC timeout\n", | |
1032 | tape->name); | |
83dd5735 | 1033 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
1da177e4 LT |
1034 | idetape_media_access_finished(drive); |
1035 | return ide_stopped; | |
1036 | } else { | |
1037 | return ide_do_reset(drive); | |
1038 | } | |
5a04cfa9 BP |
1039 | } else if (time_after(jiffies, |
1040 | tape->dsc_polling_start + | |
1041 | IDETAPE_DSC_MA_THRESHOLD)) | |
54bb2074 | 1042 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
1da177e4 LT |
1043 | idetape_postpone_request(drive); |
1044 | return ide_stopped; | |
1045 | } | |
83dd5735 | 1046 | if (rq->cmd[13] & REQ_IDETAPE_READ) { |
1da177e4 | 1047 | pc = idetape_next_pc_storage(drive); |
0014c75b | 1048 | ide_tape_create_rw_cmd(tape, pc, rq, READ_6); |
1da177e4 LT |
1049 | goto out; |
1050 | } | |
83dd5735 | 1051 | if (rq->cmd[13] & REQ_IDETAPE_WRITE) { |
1da177e4 | 1052 | pc = idetape_next_pc_storage(drive); |
0014c75b | 1053 | ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6); |
1da177e4 LT |
1054 | goto out; |
1055 | } | |
83dd5735 | 1056 | if (rq->cmd[13] & REQ_IDETAPE_PC1) { |
d236d74c | 1057 | pc = (struct ide_atapi_pc *) rq->buffer; |
83dd5735 BP |
1058 | rq->cmd[13] &= ~(REQ_IDETAPE_PC1); |
1059 | rq->cmd[13] |= REQ_IDETAPE_PC2; | |
1da177e4 LT |
1060 | goto out; |
1061 | } | |
83dd5735 | 1062 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
1da177e4 LT |
1063 | idetape_media_access_finished(drive); |
1064 | return ide_stopped; | |
1065 | } | |
1066 | BUG(); | |
28c7214b | 1067 | |
f2e3ab52 | 1068 | out: |
8d06bfad | 1069 | return idetape_issue_pc(drive, pc); |
1da177e4 LT |
1070 | } |
1071 | ||
1da177e4 | 1072 | /* |
41aa1706 | 1073 | * The function below uses __get_free_pages to allocate a data buffer of size |
f73850a3 | 1074 | * tape->buffer_size (or a bit more). We attempt to combine sequential pages as |
3c98bf34 | 1075 | * much as possible. |
1da177e4 | 1076 | * |
41aa1706 BP |
1077 | * It returns a pointer to the newly allocated buffer, or NULL in case of |
1078 | * failure. | |
1da177e4 | 1079 | */ |
077e3bdb BP |
1080 | static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape, |
1081 | int full, int clear) | |
1da177e4 | 1082 | { |
077e3bdb | 1083 | struct idetape_bh *prev_bh, *bh, *merge_bh; |
a997a435 | 1084 | int pages = tape->pages_per_buffer; |
41aa1706 | 1085 | unsigned int order, b_allocd; |
1da177e4 LT |
1086 | char *b_data = NULL; |
1087 | ||
077e3bdb BP |
1088 | merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
1089 | bh = merge_bh; | |
1da177e4 LT |
1090 | if (bh == NULL) |
1091 | goto abort; | |
41aa1706 BP |
1092 | |
1093 | order = fls(pages) - 1; | |
1094 | bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order); | |
5a04cfa9 | 1095 | if (!bh->b_data) |
1da177e4 | 1096 | goto abort; |
41aa1706 BP |
1097 | b_allocd = (1 << order) * PAGE_SIZE; |
1098 | pages &= (order-1); | |
1099 | ||
1da177e4 | 1100 | if (clear) |
41aa1706 BP |
1101 | memset(bh->b_data, 0, b_allocd); |
1102 | bh->b_reqnext = NULL; | |
1103 | bh->b_size = b_allocd; | |
1da177e4 LT |
1104 | atomic_set(&bh->b_count, full ? bh->b_size : 0); |
1105 | ||
41aa1706 BP |
1106 | while (pages) { |
1107 | order = fls(pages) - 1; | |
1108 | b_data = (char *) __get_free_pages(GFP_KERNEL, order); | |
5a04cfa9 | 1109 | if (!b_data) |
1da177e4 | 1110 | goto abort; |
41aa1706 BP |
1111 | b_allocd = (1 << order) * PAGE_SIZE; |
1112 | ||
1da177e4 | 1113 | if (clear) |
41aa1706 BP |
1114 | memset(b_data, 0, b_allocd); |
1115 | ||
1116 | /* newly allocated page frames below buffer header or ...*/ | |
1117 | if (bh->b_data == b_data + b_allocd) { | |
1118 | bh->b_size += b_allocd; | |
1119 | bh->b_data -= b_allocd; | |
1da177e4 | 1120 | if (full) |
41aa1706 | 1121 | atomic_add(b_allocd, &bh->b_count); |
1da177e4 LT |
1122 | continue; |
1123 | } | |
41aa1706 | 1124 | /* they are above the header */ |
1da177e4 | 1125 | if (b_data == bh->b_data + bh->b_size) { |
41aa1706 | 1126 | bh->b_size += b_allocd; |
1da177e4 | 1127 | if (full) |
41aa1706 | 1128 | atomic_add(b_allocd, &bh->b_count); |
1da177e4 LT |
1129 | continue; |
1130 | } | |
1131 | prev_bh = bh; | |
5a04cfa9 BP |
1132 | bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
1133 | if (!bh) { | |
41aa1706 | 1134 | free_pages((unsigned long) b_data, order); |
1da177e4 LT |
1135 | goto abort; |
1136 | } | |
1137 | bh->b_reqnext = NULL; | |
1138 | bh->b_data = b_data; | |
41aa1706 | 1139 | bh->b_size = b_allocd; |
1da177e4 LT |
1140 | atomic_set(&bh->b_count, full ? bh->b_size : 0); |
1141 | prev_bh->b_reqnext = bh; | |
41aa1706 BP |
1142 | |
1143 | pages &= (order-1); | |
1da177e4 | 1144 | } |
41aa1706 | 1145 | |
1da177e4 LT |
1146 | bh->b_size -= tape->excess_bh_size; |
1147 | if (full) | |
1148 | atomic_sub(tape->excess_bh_size, &bh->b_count); | |
077e3bdb | 1149 | return merge_bh; |
1da177e4 | 1150 | abort: |
077e3bdb | 1151 | ide_tape_kfree_buffer(tape); |
1da177e4 LT |
1152 | return NULL; |
1153 | } | |
1154 | ||
5a04cfa9 | 1155 | static int idetape_copy_stage_from_user(idetape_tape_t *tape, |
8646c88f | 1156 | const char __user *buf, int n) |
1da177e4 LT |
1157 | { |
1158 | struct idetape_bh *bh = tape->bh; | |
1159 | int count; | |
dcd96379 | 1160 | int ret = 0; |
1da177e4 LT |
1161 | |
1162 | while (n) { | |
1da177e4 | 1163 | if (bh == NULL) { |
5a04cfa9 BP |
1164 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
1165 | __func__); | |
dcd96379 | 1166 | return 1; |
1da177e4 | 1167 | } |
5a04cfa9 BP |
1168 | count = min((unsigned int) |
1169 | (bh->b_size - atomic_read(&bh->b_count)), | |
1170 | (unsigned int)n); | |
1171 | if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, | |
1172 | count)) | |
dcd96379 | 1173 | ret = 1; |
1da177e4 LT |
1174 | n -= count; |
1175 | atomic_add(count, &bh->b_count); | |
1176 | buf += count; | |
1177 | if (atomic_read(&bh->b_count) == bh->b_size) { | |
1178 | bh = bh->b_reqnext; | |
1179 | if (bh) | |
1180 | atomic_set(&bh->b_count, 0); | |
1181 | } | |
1182 | } | |
1183 | tape->bh = bh; | |
dcd96379 | 1184 | return ret; |
1da177e4 LT |
1185 | } |
1186 | ||
5a04cfa9 | 1187 | static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf, |
99d74e61 | 1188 | int n) |
1da177e4 LT |
1189 | { |
1190 | struct idetape_bh *bh = tape->bh; | |
1191 | int count; | |
dcd96379 | 1192 | int ret = 0; |
1da177e4 LT |
1193 | |
1194 | while (n) { | |
1da177e4 | 1195 | if (bh == NULL) { |
5a04cfa9 BP |
1196 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
1197 | __func__); | |
dcd96379 | 1198 | return 1; |
1da177e4 | 1199 | } |
1da177e4 | 1200 | count = min(tape->b_count, n); |
dcd96379 DW |
1201 | if (copy_to_user(buf, tape->b_data, count)) |
1202 | ret = 1; | |
1da177e4 LT |
1203 | n -= count; |
1204 | tape->b_data += count; | |
1205 | tape->b_count -= count; | |
1206 | buf += count; | |
1207 | if (!tape->b_count) { | |
5a04cfa9 BP |
1208 | bh = bh->b_reqnext; |
1209 | tape->bh = bh; | |
1da177e4 LT |
1210 | if (bh) { |
1211 | tape->b_data = bh->b_data; | |
1212 | tape->b_count = atomic_read(&bh->b_count); | |
1213 | } | |
1214 | } | |
1215 | } | |
dcd96379 | 1216 | return ret; |
1da177e4 LT |
1217 | } |
1218 | ||
077e3bdb | 1219 | static void idetape_init_merge_buffer(idetape_tape_t *tape) |
1da177e4 | 1220 | { |
077e3bdb BP |
1221 | struct idetape_bh *bh = tape->merge_bh; |
1222 | tape->bh = tape->merge_bh; | |
5a04cfa9 | 1223 | |
54abf37e | 1224 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
1da177e4 LT |
1225 | atomic_set(&bh->b_count, 0); |
1226 | else { | |
1227 | tape->b_data = bh->b_data; | |
1228 | tape->b_count = atomic_read(&bh->b_count); | |
1229 | } | |
1230 | } | |
1231 | ||
1da177e4 | 1232 | /* |
3c98bf34 BP |
1233 | * Write a filemark if write_filemark=1. Flush the device buffers without |
1234 | * writing a filemark otherwise. | |
1da177e4 | 1235 | */ |
5a04cfa9 | 1236 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
d236d74c | 1237 | struct ide_atapi_pc *pc, int write_filemark) |
1da177e4 LT |
1238 | { |
1239 | idetape_init_pc(pc); | |
90699ce2 | 1240 | pc->c[0] = WRITE_FILEMARKS; |
1da177e4 | 1241 | pc->c[4] = write_filemark; |
346331f8 | 1242 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
1243 | } |
1244 | ||
d236d74c | 1245 | static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1246 | { |
1247 | idetape_init_pc(pc); | |
90699ce2 | 1248 | pc->c[0] = TEST_UNIT_READY; |
1da177e4 LT |
1249 | } |
1250 | ||
1251 | /* | |
3c98bf34 BP |
1252 | * We add a special packet command request to the tail of the request queue, and |
1253 | * wait for it to be serviced. This is not to be called from within the request | |
1254 | * handling part of the driver! We allocate here data on the stack and it is | |
1255 | * valid until the request is finished. This is not the case for the bottom part | |
1256 | * of the driver, where we are always leaving the functions to wait for an | |
1257 | * interrupt or a timer event. | |
1da177e4 | 1258 | * |
3c98bf34 BP |
1259 | * From the bottom part of the driver, we should allocate safe memory using |
1260 | * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request | |
1261 | * to the request list without waiting for it to be serviced! In that case, we | |
1262 | * usually use idetape_queue_pc_head(). | |
1da177e4 | 1263 | */ |
ea1ab3d3 | 1264 | static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc) |
1da177e4 LT |
1265 | { |
1266 | struct ide_tape_obj *tape = drive->driver_data; | |
64ea1b4a FT |
1267 | struct request *rq; |
1268 | int error; | |
1da177e4 | 1269 | |
64ea1b4a FT |
1270 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
1271 | rq->cmd_type = REQ_TYPE_SPECIAL; | |
83dd5735 | 1272 | rq->cmd[13] = REQ_IDETAPE_PC1; |
64ea1b4a | 1273 | rq->buffer = (char *)pc; |
0014c75b | 1274 | memcpy(rq->cmd, pc->c, 12); |
64ea1b4a FT |
1275 | error = blk_execute_rq(drive->queue, tape->disk, rq, 0); |
1276 | blk_put_request(rq); | |
1277 | return error; | |
1da177e4 LT |
1278 | } |
1279 | ||
d236d74c BP |
1280 | static void idetape_create_load_unload_cmd(ide_drive_t *drive, |
1281 | struct ide_atapi_pc *pc, int cmd) | |
1da177e4 LT |
1282 | { |
1283 | idetape_init_pc(pc); | |
90699ce2 | 1284 | pc->c[0] = START_STOP; |
1da177e4 | 1285 | pc->c[4] = cmd; |
346331f8 | 1286 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
1287 | } |
1288 | ||
1289 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) | |
1290 | { | |
1291 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1292 | struct ide_atapi_pc pc; |
1da177e4 LT |
1293 | int load_attempted = 0; |
1294 | ||
3c98bf34 | 1295 | /* Wait for the tape to become ready */ |
f2e3ab52 | 1296 | set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags); |
1da177e4 LT |
1297 | timeout += jiffies; |
1298 | while (time_before(jiffies, timeout)) { | |
1299 | idetape_create_test_unit_ready_cmd(&pc); | |
ea1ab3d3 | 1300 | if (!idetape_queue_pc_tail(drive, &pc)) |
1da177e4 LT |
1301 | return 0; |
1302 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) | |
3c98bf34 BP |
1303 | || (tape->asc == 0x3A)) { |
1304 | /* no media */ | |
1da177e4 LT |
1305 | if (load_attempted) |
1306 | return -ENOMEDIUM; | |
5a04cfa9 BP |
1307 | idetape_create_load_unload_cmd(drive, &pc, |
1308 | IDETAPE_LU_LOAD_MASK); | |
ea1ab3d3 | 1309 | idetape_queue_pc_tail(drive, &pc); |
1da177e4 LT |
1310 | load_attempted = 1; |
1311 | /* not about to be ready */ | |
1312 | } else if (!(tape->sense_key == 2 && tape->asc == 4 && | |
1313 | (tape->ascq == 1 || tape->ascq == 8))) | |
1314 | return -EIO; | |
80ce45fd | 1315 | msleep(100); |
1da177e4 LT |
1316 | } |
1317 | return -EIO; | |
1318 | } | |
1319 | ||
5a04cfa9 | 1320 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
1da177e4 | 1321 | { |
d236d74c | 1322 | struct ide_atapi_pc pc; |
1da177e4 LT |
1323 | int rc; |
1324 | ||
1325 | idetape_create_write_filemark_cmd(drive, &pc, 0); | |
5a04cfa9 BP |
1326 | rc = idetape_queue_pc_tail(drive, &pc); |
1327 | if (rc) | |
1da177e4 LT |
1328 | return rc; |
1329 | idetape_wait_ready(drive, 60 * 5 * HZ); | |
1330 | return 0; | |
1331 | } | |
1332 | ||
d236d74c | 1333 | static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1334 | { |
1335 | idetape_init_pc(pc); | |
90699ce2 | 1336 | pc->c[0] = READ_POSITION; |
d236d74c | 1337 | pc->req_xfer = 20; |
1da177e4 LT |
1338 | } |
1339 | ||
5a04cfa9 | 1340 | static int idetape_read_position(ide_drive_t *drive) |
1da177e4 LT |
1341 | { |
1342 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1343 | struct ide_atapi_pc pc; |
1da177e4 LT |
1344 | int position; |
1345 | ||
8004a8c9 | 1346 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1da177e4 LT |
1347 | |
1348 | idetape_create_read_position_cmd(&pc); | |
1349 | if (idetape_queue_pc_tail(drive, &pc)) | |
1350 | return -1; | |
54bb2074 | 1351 | position = tape->first_frame; |
1da177e4 LT |
1352 | return position; |
1353 | } | |
1354 | ||
d236d74c BP |
1355 | static void idetape_create_locate_cmd(ide_drive_t *drive, |
1356 | struct ide_atapi_pc *pc, | |
5a04cfa9 | 1357 | unsigned int block, u8 partition, int skip) |
1da177e4 LT |
1358 | { |
1359 | idetape_init_pc(pc); | |
90699ce2 | 1360 | pc->c[0] = POSITION_TO_ELEMENT; |
1da177e4 | 1361 | pc->c[1] = 2; |
860ff5ec | 1362 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
1da177e4 | 1363 | pc->c[8] = partition; |
346331f8 | 1364 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
1365 | } |
1366 | ||
d236d74c BP |
1367 | static int idetape_create_prevent_cmd(ide_drive_t *drive, |
1368 | struct ide_atapi_pc *pc, int prevent) | |
1da177e4 LT |
1369 | { |
1370 | idetape_tape_t *tape = drive->driver_data; | |
1371 | ||
b6422013 BP |
1372 | /* device supports locking according to capabilities page */ |
1373 | if (!(tape->caps[6] & 0x01)) | |
1da177e4 LT |
1374 | return 0; |
1375 | ||
1376 | idetape_init_pc(pc); | |
90699ce2 | 1377 | pc->c[0] = ALLOW_MEDIUM_REMOVAL; |
1da177e4 | 1378 | pc->c[4] = prevent; |
1da177e4 LT |
1379 | return 1; |
1380 | } | |
1381 | ||
ec0fdb01 | 1382 | static void __ide_tape_discard_merge_buffer(ide_drive_t *drive) |
1da177e4 LT |
1383 | { |
1384 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1385 | |
54abf37e | 1386 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
9798630a | 1387 | return; |
1da177e4 | 1388 | |
f2e3ab52 | 1389 | clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags); |
01a63aeb | 1390 | tape->merge_bh_size = 0; |
077e3bdb BP |
1391 | if (tape->merge_bh != NULL) { |
1392 | ide_tape_kfree_buffer(tape); | |
1393 | tape->merge_bh = NULL; | |
1da177e4 LT |
1394 | } |
1395 | ||
54abf37e | 1396 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
1397 | } |
1398 | ||
1399 | /* | |
3c98bf34 BP |
1400 | * Position the tape to the requested block using the LOCATE packet command. |
1401 | * A READ POSITION command is then issued to check where we are positioned. Like | |
1402 | * all higher level operations, we queue the commands at the tail of the request | |
1403 | * queue and wait for their completion. | |
1da177e4 | 1404 | */ |
5a04cfa9 BP |
1405 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
1406 | u8 partition, int skip) | |
1da177e4 LT |
1407 | { |
1408 | idetape_tape_t *tape = drive->driver_data; | |
1409 | int retval; | |
d236d74c | 1410 | struct ide_atapi_pc pc; |
1da177e4 | 1411 | |
54abf37e | 1412 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
ec0fdb01 | 1413 | __ide_tape_discard_merge_buffer(drive); |
1da177e4 LT |
1414 | idetape_wait_ready(drive, 60 * 5 * HZ); |
1415 | idetape_create_locate_cmd(drive, &pc, block, partition, skip); | |
1416 | retval = idetape_queue_pc_tail(drive, &pc); | |
1417 | if (retval) | |
1418 | return (retval); | |
1419 | ||
1420 | idetape_create_read_position_cmd(&pc); | |
1421 | return (idetape_queue_pc_tail(drive, &pc)); | |
1422 | } | |
1423 | ||
ec0fdb01 | 1424 | static void ide_tape_discard_merge_buffer(ide_drive_t *drive, |
5a04cfa9 | 1425 | int restore_position) |
1da177e4 LT |
1426 | { |
1427 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 LT |
1428 | int seek, position; |
1429 | ||
ec0fdb01 | 1430 | __ide_tape_discard_merge_buffer(drive); |
1da177e4 LT |
1431 | if (restore_position) { |
1432 | position = idetape_read_position(drive); | |
9798630a | 1433 | seek = position > 0 ? position : 0; |
1da177e4 | 1434 | if (idetape_position_tape(drive, seek, 0, 0)) { |
5a04cfa9 | 1435 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
ec0fdb01 | 1436 | " %s\n", tape->name, __func__); |
1da177e4 LT |
1437 | return; |
1438 | } | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | /* | |
3c98bf34 BP |
1443 | * Generate a read/write request for the block device interface and wait for it |
1444 | * to be serviced. | |
1da177e4 | 1445 | */ |
5a04cfa9 BP |
1446 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, |
1447 | struct idetape_bh *bh) | |
1da177e4 LT |
1448 | { |
1449 | idetape_tape_t *tape = drive->driver_data; | |
64ea1b4a FT |
1450 | struct request *rq; |
1451 | int ret, errors; | |
1da177e4 | 1452 | |
8004a8c9 BP |
1453 | debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd); |
1454 | ||
64ea1b4a FT |
1455 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
1456 | rq->cmd_type = REQ_TYPE_SPECIAL; | |
83dd5735 | 1457 | rq->cmd[13] = cmd; |
64ea1b4a FT |
1458 | rq->rq_disk = tape->disk; |
1459 | rq->special = (void *)bh; | |
1460 | rq->sector = tape->first_frame; | |
1461 | rq->nr_sectors = blocks; | |
1462 | rq->current_nr_sectors = blocks; | |
1463 | blk_execute_rq(drive->queue, tape->disk, rq, 0); | |
1464 | ||
1465 | errors = rq->errors; | |
1466 | ret = tape->blk_size * (blocks - rq->current_nr_sectors); | |
1467 | blk_put_request(rq); | |
1da177e4 LT |
1468 | |
1469 | if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0) | |
1470 | return 0; | |
1471 | ||
077e3bdb BP |
1472 | if (tape->merge_bh) |
1473 | idetape_init_merge_buffer(tape); | |
64ea1b4a | 1474 | if (errors == IDETAPE_ERROR_GENERAL) |
1da177e4 | 1475 | return -EIO; |
64ea1b4a | 1476 | return ret; |
1da177e4 LT |
1477 | } |
1478 | ||
d236d74c | 1479 | static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1480 | { |
1481 | idetape_init_pc(pc); | |
90699ce2 | 1482 | pc->c[0] = INQUIRY; |
5a04cfa9 | 1483 | pc->c[4] = 254; |
d236d74c | 1484 | pc->req_xfer = 254; |
1da177e4 LT |
1485 | } |
1486 | ||
d236d74c BP |
1487 | static void idetape_create_rewind_cmd(ide_drive_t *drive, |
1488 | struct ide_atapi_pc *pc) | |
1da177e4 LT |
1489 | { |
1490 | idetape_init_pc(pc); | |
90699ce2 | 1491 | pc->c[0] = REZERO_UNIT; |
346331f8 | 1492 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
1493 | } |
1494 | ||
d236d74c | 1495 | static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1496 | { |
1497 | idetape_init_pc(pc); | |
90699ce2 | 1498 | pc->c[0] = ERASE; |
1da177e4 | 1499 | pc->c[1] = 1; |
346331f8 | 1500 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
1501 | } |
1502 | ||
d236d74c | 1503 | static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) |
1da177e4 LT |
1504 | { |
1505 | idetape_init_pc(pc); | |
90699ce2 | 1506 | pc->c[0] = SPACE; |
860ff5ec | 1507 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
1da177e4 | 1508 | pc->c[1] = cmd; |
346331f8 | 1509 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
1da177e4 LT |
1510 | } |
1511 | ||
97c566ce | 1512 | /* Queue up a character device originated write request. */ |
5a04cfa9 | 1513 | static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks) |
1da177e4 LT |
1514 | { |
1515 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1516 | |
8004a8c9 | 1517 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
1da177e4 | 1518 | |
0aa4b01e | 1519 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, |
077e3bdb | 1520 | blocks, tape->merge_bh); |
1da177e4 LT |
1521 | } |
1522 | ||
d9df937a | 1523 | static void ide_tape_flush_merge_buffer(ide_drive_t *drive) |
1da177e4 LT |
1524 | { |
1525 | idetape_tape_t *tape = drive->driver_data; | |
1526 | int blocks, min; | |
1527 | struct idetape_bh *bh; | |
55a5d291 | 1528 | |
54abf37e | 1529 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
077e3bdb | 1530 | printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" |
5a04cfa9 | 1531 | " but we are not writing.\n"); |
1da177e4 LT |
1532 | return; |
1533 | } | |
01a63aeb | 1534 | if (tape->merge_bh_size > tape->buffer_size) { |
1da177e4 | 1535 | printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n"); |
01a63aeb | 1536 | tape->merge_bh_size = tape->buffer_size; |
1da177e4 | 1537 | } |
01a63aeb BP |
1538 | if (tape->merge_bh_size) { |
1539 | blocks = tape->merge_bh_size / tape->blk_size; | |
1540 | if (tape->merge_bh_size % tape->blk_size) { | |
1da177e4 LT |
1541 | unsigned int i; |
1542 | ||
1543 | blocks++; | |
01a63aeb | 1544 | i = tape->blk_size - tape->merge_bh_size % |
54bb2074 | 1545 | tape->blk_size; |
1da177e4 LT |
1546 | bh = tape->bh->b_reqnext; |
1547 | while (bh) { | |
1548 | atomic_set(&bh->b_count, 0); | |
1549 | bh = bh->b_reqnext; | |
1550 | } | |
1551 | bh = tape->bh; | |
1552 | while (i) { | |
1553 | if (bh == NULL) { | |
5a04cfa9 BP |
1554 | printk(KERN_INFO "ide-tape: bug," |
1555 | " bh NULL\n"); | |
1da177e4 LT |
1556 | break; |
1557 | } | |
5a04cfa9 BP |
1558 | min = min(i, (unsigned int)(bh->b_size - |
1559 | atomic_read(&bh->b_count))); | |
1560 | memset(bh->b_data + atomic_read(&bh->b_count), | |
1561 | 0, min); | |
1da177e4 LT |
1562 | atomic_add(min, &bh->b_count); |
1563 | i -= min; | |
1564 | bh = bh->b_reqnext; | |
1565 | } | |
1566 | } | |
1567 | (void) idetape_add_chrdev_write_request(drive, blocks); | |
01a63aeb | 1568 | tape->merge_bh_size = 0; |
1da177e4 | 1569 | } |
077e3bdb BP |
1570 | if (tape->merge_bh != NULL) { |
1571 | ide_tape_kfree_buffer(tape); | |
1572 | tape->merge_bh = NULL; | |
1da177e4 | 1573 | } |
54abf37e | 1574 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
1575 | } |
1576 | ||
83042b24 | 1577 | static int idetape_init_read(ide_drive_t *drive) |
1da177e4 LT |
1578 | { |
1579 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1580 | int bytes_read; |
1da177e4 LT |
1581 | |
1582 | /* Initialize read operation */ | |
54abf37e BP |
1583 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
1584 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { | |
d9df937a | 1585 | ide_tape_flush_merge_buffer(drive); |
1da177e4 LT |
1586 | idetape_flush_tape_buffers(drive); |
1587 | } | |
077e3bdb | 1588 | if (tape->merge_bh || tape->merge_bh_size) { |
01a63aeb | 1589 | printk(KERN_ERR "ide-tape: merge_bh_size should be" |
5a04cfa9 | 1590 | " 0 now\n"); |
01a63aeb | 1591 | tape->merge_bh_size = 0; |
1da177e4 | 1592 | } |
077e3bdb BP |
1593 | tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); |
1594 | if (!tape->merge_bh) | |
1da177e4 | 1595 | return -ENOMEM; |
54abf37e | 1596 | tape->chrdev_dir = IDETAPE_DIR_READ; |
1da177e4 LT |
1597 | |
1598 | /* | |
3c98bf34 BP |
1599 | * Issue a read 0 command to ensure that DSC handshake is |
1600 | * switched from completion mode to buffer available mode. | |
1601 | * No point in issuing this if DSC overlap isn't supported, some | |
1602 | * drives (Seagate STT3401A) will return an error. | |
1da177e4 LT |
1603 | */ |
1604 | if (drive->dsc_overlap) { | |
5a04cfa9 BP |
1605 | bytes_read = idetape_queue_rw_tail(drive, |
1606 | REQ_IDETAPE_READ, 0, | |
077e3bdb | 1607 | tape->merge_bh); |
1da177e4 | 1608 | if (bytes_read < 0) { |
077e3bdb BP |
1609 | ide_tape_kfree_buffer(tape); |
1610 | tape->merge_bh = NULL; | |
54abf37e | 1611 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
1612 | return bytes_read; |
1613 | } | |
1614 | } | |
1615 | } | |
5e69bd95 | 1616 | |
1da177e4 LT |
1617 | return 0; |
1618 | } | |
1619 | ||
5bd50dc6 | 1620 | /* called from idetape_chrdev_read() to service a chrdev read request. */ |
5a04cfa9 | 1621 | static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks) |
1da177e4 LT |
1622 | { |
1623 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1624 | |
8004a8c9 | 1625 | debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks); |
1da177e4 | 1626 | |
3c98bf34 | 1627 | /* If we are at a filemark, return a read length of 0 */ |
f2e3ab52 | 1628 | if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) |
1da177e4 LT |
1629 | return 0; |
1630 | ||
83042b24 | 1631 | idetape_init_read(drive); |
5e69bd95 | 1632 | |
5e69bd95 | 1633 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, |
077e3bdb | 1634 | tape->merge_bh); |
1da177e4 LT |
1635 | } |
1636 | ||
5a04cfa9 | 1637 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
1da177e4 LT |
1638 | { |
1639 | idetape_tape_t *tape = drive->driver_data; | |
1640 | struct idetape_bh *bh; | |
1641 | int blocks; | |
3c98bf34 | 1642 | |
1da177e4 LT |
1643 | while (bcount) { |
1644 | unsigned int count; | |
1645 | ||
077e3bdb | 1646 | bh = tape->merge_bh; |
f73850a3 | 1647 | count = min(tape->buffer_size, bcount); |
1da177e4 | 1648 | bcount -= count; |
54bb2074 | 1649 | blocks = count / tape->blk_size; |
1da177e4 | 1650 | while (count) { |
5a04cfa9 BP |
1651 | atomic_set(&bh->b_count, |
1652 | min(count, (unsigned int)bh->b_size)); | |
1da177e4 LT |
1653 | memset(bh->b_data, 0, atomic_read(&bh->b_count)); |
1654 | count -= atomic_read(&bh->b_count); | |
1655 | bh = bh->b_reqnext; | |
1656 | } | |
5a04cfa9 | 1657 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, |
077e3bdb | 1658 | tape->merge_bh); |
1da177e4 LT |
1659 | } |
1660 | } | |
1661 | ||
1da177e4 | 1662 | /* |
3c98bf34 BP |
1663 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
1664 | * currently support only one partition. | |
1665 | */ | |
5a04cfa9 | 1666 | static int idetape_rewind_tape(ide_drive_t *drive) |
1da177e4 LT |
1667 | { |
1668 | int retval; | |
d236d74c | 1669 | struct ide_atapi_pc pc; |
8004a8c9 BP |
1670 | idetape_tape_t *tape; |
1671 | tape = drive->driver_data; | |
1672 | ||
1673 | debug_log(DBG_SENSE, "Enter %s\n", __func__); | |
1674 | ||
1da177e4 LT |
1675 | idetape_create_rewind_cmd(drive, &pc); |
1676 | retval = idetape_queue_pc_tail(drive, &pc); | |
1677 | if (retval) | |
1678 | return retval; | |
1679 | ||
1680 | idetape_create_read_position_cmd(&pc); | |
1681 | retval = idetape_queue_pc_tail(drive, &pc); | |
1682 | if (retval) | |
1683 | return retval; | |
1684 | return 0; | |
1685 | } | |
1686 | ||
3c98bf34 | 1687 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
5a04cfa9 BP |
1688 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
1689 | unsigned long arg) | |
1da177e4 LT |
1690 | { |
1691 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 LT |
1692 | void __user *argp = (void __user *)arg; |
1693 | ||
d59823fa BP |
1694 | struct idetape_config { |
1695 | int dsc_rw_frequency; | |
1696 | int dsc_media_access_frequency; | |
1697 | int nr_stages; | |
1698 | } config; | |
1699 | ||
8004a8c9 BP |
1700 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1701 | ||
1da177e4 | 1702 | switch (cmd) { |
5a04cfa9 BP |
1703 | case 0x0340: |
1704 | if (copy_from_user(&config, argp, sizeof(config))) | |
1705 | return -EFAULT; | |
1706 | tape->best_dsc_rw_freq = config.dsc_rw_frequency; | |
5a04cfa9 BP |
1707 | break; |
1708 | case 0x0350: | |
1709 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; | |
83042b24 | 1710 | config.nr_stages = 1; |
5a04cfa9 BP |
1711 | if (copy_to_user(argp, &config, sizeof(config))) |
1712 | return -EFAULT; | |
1713 | break; | |
1714 | default: | |
1715 | return -EIO; | |
1da177e4 LT |
1716 | } |
1717 | return 0; | |
1718 | } | |
1719 | ||
5a04cfa9 BP |
1720 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
1721 | int mt_count) | |
1da177e4 LT |
1722 | { |
1723 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1724 | struct ide_atapi_pc pc; |
5a04cfa9 | 1725 | int retval, count = 0; |
b6422013 | 1726 | int sprev = !!(tape->caps[4] & 0x20); |
1da177e4 LT |
1727 | |
1728 | if (mt_count == 0) | |
1729 | return 0; | |
1730 | if (MTBSF == mt_op || MTBSFM == mt_op) { | |
b6422013 | 1731 | if (!sprev) |
1da177e4 | 1732 | return -EIO; |
5a04cfa9 | 1733 | mt_count = -mt_count; |
1da177e4 LT |
1734 | } |
1735 | ||
54abf37e | 1736 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
01a63aeb | 1737 | tape->merge_bh_size = 0; |
f2e3ab52 | 1738 | if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) |
1da177e4 | 1739 | ++count; |
ec0fdb01 | 1740 | ide_tape_discard_merge_buffer(drive, 0); |
1da177e4 LT |
1741 | } |
1742 | ||
1da177e4 | 1743 | switch (mt_op) { |
5a04cfa9 BP |
1744 | case MTFSF: |
1745 | case MTBSF: | |
1746 | idetape_create_space_cmd(&pc, mt_count - count, | |
1747 | IDETAPE_SPACE_OVER_FILEMARK); | |
1748 | return idetape_queue_pc_tail(drive, &pc); | |
1749 | case MTFSFM: | |
1750 | case MTBSFM: | |
1751 | if (!sprev) | |
1752 | return -EIO; | |
1753 | retval = idetape_space_over_filemarks(drive, MTFSF, | |
1754 | mt_count - count); | |
1755 | if (retval) | |
1756 | return retval; | |
1757 | count = (MTBSFM == mt_op ? 1 : -1); | |
1758 | return idetape_space_over_filemarks(drive, MTFSF, count); | |
1759 | default: | |
1760 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", | |
1761 | mt_op); | |
1762 | return -EIO; | |
1da177e4 LT |
1763 | } |
1764 | } | |
1765 | ||
1da177e4 | 1766 | /* |
3c98bf34 | 1767 | * Our character device read / write functions. |
1da177e4 | 1768 | * |
3c98bf34 BP |
1769 | * The tape is optimized to maximize throughput when it is transferring an |
1770 | * integral number of the "continuous transfer limit", which is a parameter of | |
1771 | * the specific tape (26kB on my particular tape, 32kB for Onstream). | |
1da177e4 | 1772 | * |
3c98bf34 BP |
1773 | * As of version 1.3 of the driver, the character device provides an abstract |
1774 | * continuous view of the media - any mix of block sizes (even 1 byte) on the | |
1775 | * same backup/restore procedure is supported. The driver will internally | |
1776 | * convert the requests to the recommended transfer unit, so that an unmatch | |
1777 | * between the user's block size to the recommended size will only result in a | |
1778 | * (slightly) increased driver overhead, but will no longer hit performance. | |
1779 | * This is not applicable to Onstream. | |
1da177e4 | 1780 | */ |
5a04cfa9 BP |
1781 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
1782 | size_t count, loff_t *ppos) | |
1da177e4 LT |
1783 | { |
1784 | struct ide_tape_obj *tape = ide_tape_f(file); | |
1785 | ide_drive_t *drive = tape->drive; | |
5a04cfa9 | 1786 | ssize_t bytes_read, temp, actually_read = 0, rc; |
dcd96379 | 1787 | ssize_t ret = 0; |
b6422013 | 1788 | u16 ctl = *(u16 *)&tape->caps[12]; |
1da177e4 | 1789 | |
8004a8c9 | 1790 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
1da177e4 | 1791 | |
54abf37e | 1792 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
f2e3ab52 | 1793 | if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags)) |
54bb2074 BP |
1794 | if (count > tape->blk_size && |
1795 | (count % tape->blk_size) == 0) | |
1796 | tape->user_bs_factor = count / tape->blk_size; | |
1da177e4 | 1797 | } |
83042b24 | 1798 | rc = idetape_init_read(drive); |
8d06bfad | 1799 | if (rc < 0) |
1da177e4 LT |
1800 | return rc; |
1801 | if (count == 0) | |
1802 | return (0); | |
01a63aeb BP |
1803 | if (tape->merge_bh_size) { |
1804 | actually_read = min((unsigned int)(tape->merge_bh_size), | |
5a04cfa9 | 1805 | (unsigned int)count); |
99d74e61 | 1806 | if (idetape_copy_stage_to_user(tape, buf, actually_read)) |
dcd96379 | 1807 | ret = -EFAULT; |
1da177e4 | 1808 | buf += actually_read; |
01a63aeb | 1809 | tape->merge_bh_size -= actually_read; |
1da177e4 LT |
1810 | count -= actually_read; |
1811 | } | |
f73850a3 | 1812 | while (count >= tape->buffer_size) { |
b6422013 | 1813 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
1da177e4 LT |
1814 | if (bytes_read <= 0) |
1815 | goto finish; | |
99d74e61 | 1816 | if (idetape_copy_stage_to_user(tape, buf, bytes_read)) |
dcd96379 | 1817 | ret = -EFAULT; |
1da177e4 LT |
1818 | buf += bytes_read; |
1819 | count -= bytes_read; | |
1820 | actually_read += bytes_read; | |
1821 | } | |
1822 | if (count) { | |
b6422013 | 1823 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
1da177e4 LT |
1824 | if (bytes_read <= 0) |
1825 | goto finish; | |
1826 | temp = min((unsigned long)count, (unsigned long)bytes_read); | |
99d74e61 | 1827 | if (idetape_copy_stage_to_user(tape, buf, temp)) |
dcd96379 | 1828 | ret = -EFAULT; |
1da177e4 | 1829 | actually_read += temp; |
01a63aeb | 1830 | tape->merge_bh_size = bytes_read-temp; |
1da177e4 LT |
1831 | } |
1832 | finish: | |
f2e3ab52 | 1833 | if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) { |
8004a8c9 BP |
1834 | debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); |
1835 | ||
1da177e4 LT |
1836 | idetape_space_over_filemarks(drive, MTFSF, 1); |
1837 | return 0; | |
1838 | } | |
dcd96379 | 1839 | |
5a04cfa9 | 1840 | return ret ? ret : actually_read; |
1da177e4 LT |
1841 | } |
1842 | ||
5a04cfa9 | 1843 | static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, |
1da177e4 LT |
1844 | size_t count, loff_t *ppos) |
1845 | { | |
1846 | struct ide_tape_obj *tape = ide_tape_f(file); | |
1847 | ide_drive_t *drive = tape->drive; | |
dcd96379 DW |
1848 | ssize_t actually_written = 0; |
1849 | ssize_t ret = 0; | |
b6422013 | 1850 | u16 ctl = *(u16 *)&tape->caps[12]; |
1da177e4 LT |
1851 | |
1852 | /* The drive is write protected. */ | |
1853 | if (tape->write_prot) | |
1854 | return -EACCES; | |
1855 | ||
8004a8c9 | 1856 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
1da177e4 LT |
1857 | |
1858 | /* Initialize write operation */ | |
54abf37e BP |
1859 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
1860 | if (tape->chrdev_dir == IDETAPE_DIR_READ) | |
ec0fdb01 | 1861 | ide_tape_discard_merge_buffer(drive, 1); |
077e3bdb | 1862 | if (tape->merge_bh || tape->merge_bh_size) { |
01a63aeb | 1863 | printk(KERN_ERR "ide-tape: merge_bh_size " |
1da177e4 | 1864 | "should be 0 now\n"); |
01a63aeb | 1865 | tape->merge_bh_size = 0; |
1da177e4 | 1866 | } |
077e3bdb BP |
1867 | tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); |
1868 | if (!tape->merge_bh) | |
1da177e4 | 1869 | return -ENOMEM; |
54abf37e | 1870 | tape->chrdev_dir = IDETAPE_DIR_WRITE; |
077e3bdb | 1871 | idetape_init_merge_buffer(tape); |
1da177e4 LT |
1872 | |
1873 | /* | |
3c98bf34 BP |
1874 | * Issue a write 0 command to ensure that DSC handshake is |
1875 | * switched from completion mode to buffer available mode. No | |
1876 | * point in issuing this if DSC overlap isn't supported, some | |
1877 | * drives (Seagate STT3401A) will return an error. | |
1da177e4 LT |
1878 | */ |
1879 | if (drive->dsc_overlap) { | |
5a04cfa9 BP |
1880 | ssize_t retval = idetape_queue_rw_tail(drive, |
1881 | REQ_IDETAPE_WRITE, 0, | |
077e3bdb | 1882 | tape->merge_bh); |
1da177e4 | 1883 | if (retval < 0) { |
077e3bdb BP |
1884 | ide_tape_kfree_buffer(tape); |
1885 | tape->merge_bh = NULL; | |
54abf37e | 1886 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
1887 | return retval; |
1888 | } | |
1889 | } | |
1890 | } | |
1891 | if (count == 0) | |
1892 | return (0); | |
01a63aeb BP |
1893 | if (tape->merge_bh_size) { |
1894 | if (tape->merge_bh_size >= tape->buffer_size) { | |
5a04cfa9 | 1895 | printk(KERN_ERR "ide-tape: bug: merge buf too big\n"); |
01a63aeb | 1896 | tape->merge_bh_size = 0; |
1da177e4 | 1897 | } |
5a04cfa9 | 1898 | actually_written = min((unsigned int) |
01a63aeb | 1899 | (tape->buffer_size - tape->merge_bh_size), |
5a04cfa9 | 1900 | (unsigned int)count); |
8646c88f | 1901 | if (idetape_copy_stage_from_user(tape, buf, actually_written)) |
dcd96379 | 1902 | ret = -EFAULT; |
1da177e4 | 1903 | buf += actually_written; |
01a63aeb | 1904 | tape->merge_bh_size += actually_written; |
1da177e4 LT |
1905 | count -= actually_written; |
1906 | ||
01a63aeb | 1907 | if (tape->merge_bh_size == tape->buffer_size) { |
dcd96379 | 1908 | ssize_t retval; |
01a63aeb | 1909 | tape->merge_bh_size = 0; |
b6422013 | 1910 | retval = idetape_add_chrdev_write_request(drive, ctl); |
1da177e4 LT |
1911 | if (retval <= 0) |
1912 | return (retval); | |
1913 | } | |
1914 | } | |
f73850a3 | 1915 | while (count >= tape->buffer_size) { |
dcd96379 | 1916 | ssize_t retval; |
f73850a3 | 1917 | if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size)) |
dcd96379 | 1918 | ret = -EFAULT; |
f73850a3 BP |
1919 | buf += tape->buffer_size; |
1920 | count -= tape->buffer_size; | |
b6422013 | 1921 | retval = idetape_add_chrdev_write_request(drive, ctl); |
f73850a3 | 1922 | actually_written += tape->buffer_size; |
1da177e4 LT |
1923 | if (retval <= 0) |
1924 | return (retval); | |
1925 | } | |
1926 | if (count) { | |
1927 | actually_written += count; | |
8646c88f | 1928 | if (idetape_copy_stage_from_user(tape, buf, count)) |
dcd96379 | 1929 | ret = -EFAULT; |
01a63aeb | 1930 | tape->merge_bh_size += count; |
1da177e4 | 1931 | } |
5a04cfa9 | 1932 | return ret ? ret : actually_written; |
1da177e4 LT |
1933 | } |
1934 | ||
5a04cfa9 | 1935 | static int idetape_write_filemark(ide_drive_t *drive) |
1da177e4 | 1936 | { |
d236d74c | 1937 | struct ide_atapi_pc pc; |
1da177e4 LT |
1938 | |
1939 | /* Write a filemark */ | |
1940 | idetape_create_write_filemark_cmd(drive, &pc, 1); | |
1941 | if (idetape_queue_pc_tail(drive, &pc)) { | |
1942 | printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); | |
1943 | return -EIO; | |
1944 | } | |
1945 | return 0; | |
1946 | } | |
1947 | ||
1948 | /* | |
d99c9da2 BP |
1949 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
1950 | * requested. | |
1da177e4 | 1951 | * |
d99c9da2 BP |
1952 | * Note: MTBSF and MTBSFM are not supported when the tape doesn't support |
1953 | * spacing over filemarks in the reverse direction. In this case, MTFSFM is also | |
5bd50dc6 | 1954 | * usually not supported. |
1da177e4 | 1955 | * |
d99c9da2 | 1956 | * The following commands are currently not supported: |
1da177e4 | 1957 | * |
d99c9da2 BP |
1958 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
1959 | * MT_ST_WRITE_THRESHOLD. | |
1da177e4 | 1960 | */ |
d99c9da2 | 1961 | static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) |
1da177e4 LT |
1962 | { |
1963 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1964 | struct ide_atapi_pc pc; |
5a04cfa9 | 1965 | int i, retval; |
1da177e4 | 1966 | |
8004a8c9 BP |
1967 | debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n", |
1968 | mt_op, mt_count); | |
5a04cfa9 | 1969 | |
1da177e4 | 1970 | switch (mt_op) { |
5a04cfa9 BP |
1971 | case MTFSF: |
1972 | case MTFSFM: | |
1973 | case MTBSF: | |
1974 | case MTBSFM: | |
1975 | if (!mt_count) | |
1976 | return 0; | |
1977 | return idetape_space_over_filemarks(drive, mt_op, mt_count); | |
1978 | default: | |
1979 | break; | |
1da177e4 | 1980 | } |
5a04cfa9 | 1981 | |
1da177e4 | 1982 | switch (mt_op) { |
5a04cfa9 BP |
1983 | case MTWEOF: |
1984 | if (tape->write_prot) | |
1985 | return -EACCES; | |
ec0fdb01 | 1986 | ide_tape_discard_merge_buffer(drive, 1); |
5a04cfa9 BP |
1987 | for (i = 0; i < mt_count; i++) { |
1988 | retval = idetape_write_filemark(drive); | |
1989 | if (retval) | |
1990 | return retval; | |
1991 | } | |
1992 | return 0; | |
1993 | case MTREW: | |
ec0fdb01 | 1994 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
1995 | if (idetape_rewind_tape(drive)) |
1996 | return -EIO; | |
1997 | return 0; | |
1998 | case MTLOAD: | |
ec0fdb01 | 1999 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
2000 | idetape_create_load_unload_cmd(drive, &pc, |
2001 | IDETAPE_LU_LOAD_MASK); | |
2002 | return idetape_queue_pc_tail(drive, &pc); | |
2003 | case MTUNLOAD: | |
2004 | case MTOFFL: | |
2005 | /* | |
2006 | * If door is locked, attempt to unlock before | |
2007 | * attempting to eject. | |
2008 | */ | |
2009 | if (tape->door_locked) { | |
2010 | if (idetape_create_prevent_cmd(drive, &pc, 0)) | |
2011 | if (!idetape_queue_pc_tail(drive, &pc)) | |
2012 | tape->door_locked = DOOR_UNLOCKED; | |
2013 | } | |
ec0fdb01 | 2014 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
2015 | idetape_create_load_unload_cmd(drive, &pc, |
2016 | !IDETAPE_LU_LOAD_MASK); | |
2017 | retval = idetape_queue_pc_tail(drive, &pc); | |
2018 | if (!retval) | |
f2e3ab52 | 2019 | clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags); |
5a04cfa9 BP |
2020 | return retval; |
2021 | case MTNOP: | |
ec0fdb01 | 2022 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
2023 | return idetape_flush_tape_buffers(drive); |
2024 | case MTRETEN: | |
ec0fdb01 | 2025 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
2026 | idetape_create_load_unload_cmd(drive, &pc, |
2027 | IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); | |
2028 | return idetape_queue_pc_tail(drive, &pc); | |
2029 | case MTEOM: | |
2030 | idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); | |
2031 | return idetape_queue_pc_tail(drive, &pc); | |
2032 | case MTERASE: | |
2033 | (void)idetape_rewind_tape(drive); | |
2034 | idetape_create_erase_cmd(&pc); | |
2035 | return idetape_queue_pc_tail(drive, &pc); | |
2036 | case MTSETBLK: | |
2037 | if (mt_count) { | |
2038 | if (mt_count < tape->blk_size || | |
2039 | mt_count % tape->blk_size) | |
1da177e4 | 2040 | return -EIO; |
5a04cfa9 | 2041 | tape->user_bs_factor = mt_count / tape->blk_size; |
f2e3ab52 | 2042 | clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags); |
5a04cfa9 | 2043 | } else |
f2e3ab52 | 2044 | set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags); |
5a04cfa9 BP |
2045 | return 0; |
2046 | case MTSEEK: | |
ec0fdb01 | 2047 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
2048 | return idetape_position_tape(drive, |
2049 | mt_count * tape->user_bs_factor, tape->partition, 0); | |
2050 | case MTSETPART: | |
ec0fdb01 | 2051 | ide_tape_discard_merge_buffer(drive, 0); |
5a04cfa9 BP |
2052 | return idetape_position_tape(drive, 0, mt_count, 0); |
2053 | case MTFSR: | |
2054 | case MTBSR: | |
2055 | case MTLOCK: | |
2056 | if (!idetape_create_prevent_cmd(drive, &pc, 1)) | |
1da177e4 | 2057 | return 0; |
5a04cfa9 BP |
2058 | retval = idetape_queue_pc_tail(drive, &pc); |
2059 | if (retval) | |
1da177e4 | 2060 | return retval; |
5a04cfa9 BP |
2061 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
2062 | return 0; | |
2063 | case MTUNLOCK: | |
2064 | if (!idetape_create_prevent_cmd(drive, &pc, 0)) | |
1da177e4 | 2065 | return 0; |
5a04cfa9 BP |
2066 | retval = idetape_queue_pc_tail(drive, &pc); |
2067 | if (retval) | |
2068 | return retval; | |
2069 | tape->door_locked = DOOR_UNLOCKED; | |
2070 | return 0; | |
2071 | default: | |
2072 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", | |
2073 | mt_op); | |
2074 | return -EIO; | |
1da177e4 LT |
2075 | } |
2076 | } | |
2077 | ||
2078 | /* | |
d99c9da2 BP |
2079 | * Our character device ioctls. General mtio.h magnetic io commands are |
2080 | * supported here, and not in the corresponding block interface. Our own | |
2081 | * ide-tape ioctls are supported on both interfaces. | |
1da177e4 | 2082 | */ |
d99c9da2 BP |
2083 | static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, |
2084 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
2085 | { |
2086 | struct ide_tape_obj *tape = ide_tape_f(file); | |
2087 | ide_drive_t *drive = tape->drive; | |
2088 | struct mtop mtop; | |
2089 | struct mtget mtget; | |
2090 | struct mtpos mtpos; | |
54bb2074 | 2091 | int block_offset = 0, position = tape->first_frame; |
1da177e4 LT |
2092 | void __user *argp = (void __user *)arg; |
2093 | ||
8004a8c9 | 2094 | debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd); |
1da177e4 | 2095 | |
54abf37e | 2096 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
d9df937a | 2097 | ide_tape_flush_merge_buffer(drive); |
1da177e4 LT |
2098 | idetape_flush_tape_buffers(drive); |
2099 | } | |
2100 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { | |
01a63aeb | 2101 | block_offset = tape->merge_bh_size / |
54bb2074 | 2102 | (tape->blk_size * tape->user_bs_factor); |
5a04cfa9 BP |
2103 | position = idetape_read_position(drive); |
2104 | if (position < 0) | |
1da177e4 LT |
2105 | return -EIO; |
2106 | } | |
2107 | switch (cmd) { | |
5a04cfa9 BP |
2108 | case MTIOCTOP: |
2109 | if (copy_from_user(&mtop, argp, sizeof(struct mtop))) | |
2110 | return -EFAULT; | |
2111 | return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); | |
2112 | case MTIOCGET: | |
2113 | memset(&mtget, 0, sizeof(struct mtget)); | |
2114 | mtget.mt_type = MT_ISSCSI2; | |
2115 | mtget.mt_blkno = position / tape->user_bs_factor - block_offset; | |
2116 | mtget.mt_dsreg = | |
2117 | ((tape->blk_size * tape->user_bs_factor) | |
2118 | << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; | |
2119 | ||
2120 | if (tape->drv_write_prot) | |
2121 | mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); | |
2122 | ||
2123 | if (copy_to_user(argp, &mtget, sizeof(struct mtget))) | |
2124 | return -EFAULT; | |
2125 | return 0; | |
2126 | case MTIOCPOS: | |
2127 | mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; | |
2128 | if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) | |
2129 | return -EFAULT; | |
2130 | return 0; | |
2131 | default: | |
2132 | if (tape->chrdev_dir == IDETAPE_DIR_READ) | |
ec0fdb01 | 2133 | ide_tape_discard_merge_buffer(drive, 1); |
5a04cfa9 | 2134 | return idetape_blkdev_ioctl(drive, cmd, arg); |
1da177e4 LT |
2135 | } |
2136 | } | |
2137 | ||
3cffb9ce BP |
2138 | /* |
2139 | * Do a mode sense page 0 with block descriptor and if it succeeds set the tape | |
2140 | * block size with the reported value. | |
2141 | */ | |
2142 | static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) | |
2143 | { | |
2144 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 2145 | struct ide_atapi_pc pc; |
3cffb9ce BP |
2146 | |
2147 | idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); | |
2148 | if (idetape_queue_pc_tail(drive, &pc)) { | |
2149 | printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); | |
54bb2074 | 2150 | if (tape->blk_size == 0) { |
3cffb9ce BP |
2151 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
2152 | "block size, assuming 32k\n"); | |
54bb2074 | 2153 | tape->blk_size = 32768; |
3cffb9ce BP |
2154 | } |
2155 | return; | |
2156 | } | |
d236d74c BP |
2157 | tape->blk_size = (pc.buf[4 + 5] << 16) + |
2158 | (pc.buf[4 + 6] << 8) + | |
2159 | pc.buf[4 + 7]; | |
2160 | tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7; | |
3cffb9ce | 2161 | } |
1da177e4 | 2162 | |
5a04cfa9 | 2163 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
1da177e4 LT |
2164 | { |
2165 | unsigned int minor = iminor(inode), i = minor & ~0xc0; | |
2166 | ide_drive_t *drive; | |
2167 | idetape_tape_t *tape; | |
d236d74c | 2168 | struct ide_atapi_pc pc; |
1da177e4 LT |
2169 | int retval; |
2170 | ||
8004a8c9 BP |
2171 | if (i >= MAX_HWIFS * MAX_DRIVES) |
2172 | return -ENXIO; | |
2173 | ||
04f4ac9d | 2174 | lock_kernel(); |
8004a8c9 | 2175 | tape = ide_tape_chrdev_get(i); |
04f4ac9d JC |
2176 | if (!tape) { |
2177 | unlock_kernel(); | |
8004a8c9 | 2178 | return -ENXIO; |
04f4ac9d | 2179 | } |
8004a8c9 BP |
2180 | |
2181 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); | |
2182 | ||
1da177e4 LT |
2183 | /* |
2184 | * We really want to do nonseekable_open(inode, filp); here, but some | |
2185 | * versions of tar incorrectly call lseek on tapes and bail out if that | |
2186 | * fails. So we disallow pread() and pwrite(), but permit lseeks. | |
2187 | */ | |
2188 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); | |
2189 | ||
1da177e4 LT |
2190 | drive = tape->drive; |
2191 | ||
2192 | filp->private_data = tape; | |
2193 | ||
f2e3ab52 | 2194 | if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) { |
1da177e4 LT |
2195 | retval = -EBUSY; |
2196 | goto out_put_tape; | |
2197 | } | |
2198 | ||
2199 | retval = idetape_wait_ready(drive, 60 * HZ); | |
2200 | if (retval) { | |
f2e3ab52 | 2201 | clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); |
1da177e4 LT |
2202 | printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); |
2203 | goto out_put_tape; | |
2204 | } | |
2205 | ||
2206 | idetape_read_position(drive); | |
f2e3ab52 | 2207 | if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags)) |
1da177e4 LT |
2208 | (void)idetape_rewind_tape(drive); |
2209 | ||
1da177e4 | 2210 | /* Read block size and write protect status from drive. */ |
3cffb9ce | 2211 | ide_tape_get_bsize_from_bdesc(drive); |
1da177e4 LT |
2212 | |
2213 | /* Set write protect flag if device is opened as read-only. */ | |
2214 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) | |
2215 | tape->write_prot = 1; | |
2216 | else | |
2217 | tape->write_prot = tape->drv_write_prot; | |
2218 | ||
2219 | /* Make sure drive isn't write protected if user wants to write. */ | |
2220 | if (tape->write_prot) { | |
2221 | if ((filp->f_flags & O_ACCMODE) == O_WRONLY || | |
2222 | (filp->f_flags & O_ACCMODE) == O_RDWR) { | |
f2e3ab52 | 2223 | clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); |
1da177e4 LT |
2224 | retval = -EROFS; |
2225 | goto out_put_tape; | |
2226 | } | |
2227 | } | |
2228 | ||
3c98bf34 | 2229 | /* Lock the tape drive door so user can't eject. */ |
54abf37e | 2230 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
1da177e4 LT |
2231 | if (idetape_create_prevent_cmd(drive, &pc, 1)) { |
2232 | if (!idetape_queue_pc_tail(drive, &pc)) { | |
2233 | if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) | |
2234 | tape->door_locked = DOOR_LOCKED; | |
2235 | } | |
2236 | } | |
2237 | } | |
04f4ac9d | 2238 | unlock_kernel(); |
1da177e4 LT |
2239 | return 0; |
2240 | ||
2241 | out_put_tape: | |
2242 | ide_tape_put(tape); | |
04f4ac9d | 2243 | unlock_kernel(); |
1da177e4 LT |
2244 | return retval; |
2245 | } | |
2246 | ||
5a04cfa9 | 2247 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
1da177e4 LT |
2248 | { |
2249 | idetape_tape_t *tape = drive->driver_data; | |
2250 | ||
d9df937a | 2251 | ide_tape_flush_merge_buffer(drive); |
077e3bdb BP |
2252 | tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0); |
2253 | if (tape->merge_bh != NULL) { | |
54bb2074 BP |
2254 | idetape_pad_zeros(drive, tape->blk_size * |
2255 | (tape->user_bs_factor - 1)); | |
077e3bdb BP |
2256 | ide_tape_kfree_buffer(tape); |
2257 | tape->merge_bh = NULL; | |
1da177e4 LT |
2258 | } |
2259 | idetape_write_filemark(drive); | |
2260 | idetape_flush_tape_buffers(drive); | |
2261 | idetape_flush_tape_buffers(drive); | |
2262 | } | |
2263 | ||
5a04cfa9 | 2264 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
1da177e4 LT |
2265 | { |
2266 | struct ide_tape_obj *tape = ide_tape_f(filp); | |
2267 | ide_drive_t *drive = tape->drive; | |
d236d74c | 2268 | struct ide_atapi_pc pc; |
1da177e4 LT |
2269 | unsigned int minor = iminor(inode); |
2270 | ||
2271 | lock_kernel(); | |
2272 | tape = drive->driver_data; | |
8004a8c9 BP |
2273 | |
2274 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); | |
1da177e4 | 2275 | |
54abf37e | 2276 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
1da177e4 | 2277 | idetape_write_release(drive, minor); |
54abf37e | 2278 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
1da177e4 | 2279 | if (minor < 128) |
ec0fdb01 | 2280 | ide_tape_discard_merge_buffer(drive, 1); |
1da177e4 | 2281 | } |
f64eee7b | 2282 | |
f2e3ab52 | 2283 | if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags)) |
1da177e4 | 2284 | (void) idetape_rewind_tape(drive); |
54abf37e | 2285 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
1da177e4 LT |
2286 | if (tape->door_locked == DOOR_LOCKED) { |
2287 | if (idetape_create_prevent_cmd(drive, &pc, 0)) { | |
2288 | if (!idetape_queue_pc_tail(drive, &pc)) | |
2289 | tape->door_locked = DOOR_UNLOCKED; | |
2290 | } | |
2291 | } | |
2292 | } | |
f2e3ab52 | 2293 | clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); |
1da177e4 LT |
2294 | ide_tape_put(tape); |
2295 | unlock_kernel(); | |
2296 | return 0; | |
2297 | } | |
2298 | ||
2299 | /* | |
71071b8e | 2300 | * check the contents of the ATAPI IDENTIFY command results. We return: |
1da177e4 | 2301 | * |
71071b8e BP |
2302 | * 1 - If the tape can be supported by us, based on the information we have so |
2303 | * far. | |
1da177e4 | 2304 | * |
71071b8e | 2305 | * 0 - If this tape driver is not currently supported by us. |
1da177e4 | 2306 | */ |
71071b8e | 2307 | static int idetape_identify_device(ide_drive_t *drive) |
1da177e4 | 2308 | { |
71071b8e | 2309 | u8 gcw[2], protocol, device_type, removable, packet_size; |
1da177e4 LT |
2310 | |
2311 | if (drive->id_read == 0) | |
2312 | return 1; | |
2313 | ||
4dde4492 | 2314 | *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG]; |
1da177e4 | 2315 | |
71071b8e BP |
2316 | protocol = (gcw[1] & 0xC0) >> 6; |
2317 | device_type = gcw[1] & 0x1F; | |
2318 | removable = !!(gcw[0] & 0x80); | |
2319 | packet_size = gcw[0] & 0x3; | |
1da177e4 | 2320 | |
71071b8e BP |
2321 | /* Check that we can support this device */ |
2322 | if (protocol != 2) | |
16422de3 | 2323 | printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n", |
71071b8e BP |
2324 | protocol); |
2325 | else if (device_type != 1) | |
16422de3 | 2326 | printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set " |
71071b8e BP |
2327 | "to tape\n", device_type); |
2328 | else if (!removable) | |
1da177e4 | 2329 | printk(KERN_ERR "ide-tape: The removable flag is not set\n"); |
71071b8e | 2330 | else if (packet_size != 0) { |
24d57f8b BP |
2331 | printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12" |
2332 | " bytes\n", packet_size); | |
1da177e4 LT |
2333 | } else |
2334 | return 1; | |
2335 | return 0; | |
2336 | } | |
2337 | ||
6d29c8f0 | 2338 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
1da177e4 | 2339 | { |
1da177e4 | 2340 | idetape_tape_t *tape = drive->driver_data; |
d236d74c | 2341 | struct ide_atapi_pc pc; |
801bd32e | 2342 | char fw_rev[4], vendor_id[8], product_id[16]; |
6d29c8f0 | 2343 | |
1da177e4 LT |
2344 | idetape_create_inquiry_cmd(&pc); |
2345 | if (idetape_queue_pc_tail(drive, &pc)) { | |
6d29c8f0 BP |
2346 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
2347 | tape->name); | |
1da177e4 LT |
2348 | return; |
2349 | } | |
d236d74c BP |
2350 | memcpy(vendor_id, &pc.buf[8], 8); |
2351 | memcpy(product_id, &pc.buf[16], 16); | |
2352 | memcpy(fw_rev, &pc.buf[32], 4); | |
41f81d54 | 2353 | |
801bd32e BP |
2354 | ide_fixstring(vendor_id, 8, 0); |
2355 | ide_fixstring(product_id, 16, 0); | |
2356 | ide_fixstring(fw_rev, 4, 0); | |
41f81d54 | 2357 | |
801bd32e | 2358 | printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n", |
41f81d54 | 2359 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
1da177e4 LT |
2360 | } |
2361 | ||
2362 | /* | |
b6422013 BP |
2363 | * Ask the tape about its various parameters. In particular, we will adjust our |
2364 | * data transfer buffer size to the recommended value as returned by the tape. | |
1da177e4 | 2365 | */ |
5a04cfa9 | 2366 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
1da177e4 LT |
2367 | { |
2368 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 2369 | struct ide_atapi_pc pc; |
b6422013 BP |
2370 | u8 *caps; |
2371 | u8 speed, max_speed; | |
47314fa4 | 2372 | |
1da177e4 LT |
2373 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
2374 | if (idetape_queue_pc_tail(drive, &pc)) { | |
b6422013 BP |
2375 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
2376 | " some default values\n"); | |
54bb2074 | 2377 | tape->blk_size = 512; |
b6422013 BP |
2378 | put_unaligned(52, (u16 *)&tape->caps[12]); |
2379 | put_unaligned(540, (u16 *)&tape->caps[14]); | |
2380 | put_unaligned(6*52, (u16 *)&tape->caps[16]); | |
1da177e4 LT |
2381 | return; |
2382 | } | |
d236d74c | 2383 | caps = pc.buf + 4 + pc.buf[3]; |
b6422013 BP |
2384 | |
2385 | /* convert to host order and save for later use */ | |
cd740ab0 HH |
2386 | speed = be16_to_cpup((__be16 *)&caps[14]); |
2387 | max_speed = be16_to_cpup((__be16 *)&caps[8]); | |
1da177e4 | 2388 | |
cd740ab0 HH |
2389 | *(u16 *)&caps[8] = max_speed; |
2390 | *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]); | |
2391 | *(u16 *)&caps[14] = speed; | |
2392 | *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]); | |
1da177e4 | 2393 | |
b6422013 BP |
2394 | if (!speed) { |
2395 | printk(KERN_INFO "ide-tape: %s: invalid tape speed " | |
2396 | "(assuming 650KB/sec)\n", drive->name); | |
cd740ab0 | 2397 | *(u16 *)&caps[14] = 650; |
1da177e4 | 2398 | } |
b6422013 BP |
2399 | if (!max_speed) { |
2400 | printk(KERN_INFO "ide-tape: %s: invalid max_speed " | |
2401 | "(assuming 650KB/sec)\n", drive->name); | |
cd740ab0 | 2402 | *(u16 *)&caps[8] = 650; |
1da177e4 LT |
2403 | } |
2404 | ||
b6422013 BP |
2405 | memcpy(&tape->caps, caps, 20); |
2406 | if (caps[7] & 0x02) | |
54bb2074 | 2407 | tape->blk_size = 512; |
b6422013 | 2408 | else if (caps[7] & 0x04) |
54bb2074 | 2409 | tape->blk_size = 1024; |
1da177e4 LT |
2410 | } |
2411 | ||
7662d046 | 2412 | #ifdef CONFIG_IDE_PROC_FS |
5a04cfa9 | 2413 | static void idetape_add_settings(ide_drive_t *drive) |
1da177e4 LT |
2414 | { |
2415 | idetape_tape_t *tape = drive->driver_data; | |
2416 | ||
b6422013 BP |
2417 | ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, |
2418 | 1, 2, (u16 *)&tape->caps[16], NULL); | |
b6422013 BP |
2419 | ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, |
2420 | 1, 1, (u16 *)&tape->caps[14], NULL); | |
f73850a3 BP |
2421 | ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff, |
2422 | 1, 1024, &tape->buffer_size, NULL); | |
54bb2074 BP |
2423 | ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, |
2424 | IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq, | |
2425 | NULL); | |
5a04cfa9 BP |
2426 | ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, |
2427 | 1, &drive->dsc_overlap, NULL); | |
5a04cfa9 BP |
2428 | ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, |
2429 | 1, 1, &tape->avg_speed, NULL); | |
8004a8c9 BP |
2430 | ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1, |
2431 | 1, &tape->debug_mask, NULL); | |
1da177e4 | 2432 | } |
7662d046 BZ |
2433 | #else |
2434 | static inline void idetape_add_settings(ide_drive_t *drive) { ; } | |
2435 | #endif | |
1da177e4 LT |
2436 | |
2437 | /* | |
3c98bf34 | 2438 | * The function below is called to: |
1da177e4 | 2439 | * |
3c98bf34 BP |
2440 | * 1. Initialize our various state variables. |
2441 | * 2. Ask the tape for its capabilities. | |
2442 | * 3. Allocate a buffer which will be used for data transfer. The buffer size | |
2443 | * is chosen based on the recommendation which we received in step 2. | |
1da177e4 | 2444 | * |
3c98bf34 BP |
2445 | * Note that at this point ide.c already assigned us an irq, so that we can |
2446 | * queue requests here and wait for their completion. | |
1da177e4 | 2447 | */ |
5a04cfa9 | 2448 | static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) |
1da177e4 | 2449 | { |
83042b24 | 2450 | unsigned long t; |
1da177e4 | 2451 | int speed; |
f73850a3 | 2452 | int buffer_size; |
71071b8e | 2453 | u8 gcw[2]; |
b6422013 | 2454 | u16 *ctl = (u16 *)&tape->caps[12]; |
1da177e4 | 2455 | |
776bb027 BP |
2456 | drive->pc_callback = ide_tape_callback; |
2457 | ||
54bb2074 | 2458 | spin_lock_init(&tape->lock); |
1da177e4 | 2459 | drive->dsc_overlap = 1; |
4166c199 BZ |
2460 | if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { |
2461 | printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", | |
2462 | tape->name); | |
2463 | drive->dsc_overlap = 0; | |
1da177e4 | 2464 | } |
1da177e4 | 2465 | /* Seagate Travan drives do not support DSC overlap. */ |
4dde4492 | 2466 | if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401")) |
1da177e4 LT |
2467 | drive->dsc_overlap = 0; |
2468 | tape->minor = minor; | |
2469 | tape->name[0] = 'h'; | |
2470 | tape->name[1] = 't'; | |
2471 | tape->name[2] = '0' + minor; | |
54abf37e | 2472 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 | 2473 | tape->pc = tape->pc_stack; |
4dde4492 BZ |
2474 | |
2475 | *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG]; | |
71071b8e BP |
2476 | |
2477 | /* Command packet DRQ type */ | |
2478 | if (((gcw[0] & 0x60) >> 5) == 1) | |
f2e3ab52 | 2479 | set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags); |
1da177e4 | 2480 | |
1da177e4 LT |
2481 | idetape_get_inquiry_results(drive); |
2482 | idetape_get_mode_sense_results(drive); | |
3cffb9ce | 2483 | ide_tape_get_bsize_from_bdesc(drive); |
1da177e4 | 2484 | tape->user_bs_factor = 1; |
f73850a3 BP |
2485 | tape->buffer_size = *ctl * tape->blk_size; |
2486 | while (tape->buffer_size > 0xffff) { | |
1da177e4 | 2487 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); |
b6422013 | 2488 | *ctl /= 2; |
f73850a3 | 2489 | tape->buffer_size = *ctl * tape->blk_size; |
1da177e4 | 2490 | } |
f73850a3 | 2491 | buffer_size = tape->buffer_size; |
a997a435 | 2492 | tape->pages_per_buffer = buffer_size / PAGE_SIZE; |
f73850a3 | 2493 | if (buffer_size % PAGE_SIZE) { |
a997a435 | 2494 | tape->pages_per_buffer++; |
f73850a3 | 2495 | tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE; |
1da177e4 LT |
2496 | } |
2497 | ||
83042b24 | 2498 | /* select the "best" DSC read/write polling freq */ |
b6422013 | 2499 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
1da177e4 | 2500 | |
f73850a3 | 2501 | t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); |
1da177e4 LT |
2502 | |
2503 | /* | |
3c98bf34 BP |
2504 | * Ensure that the number we got makes sense; limit it within |
2505 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. | |
1da177e4 | 2506 | */ |
a792bd5a HH |
2507 | tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, |
2508 | IDETAPE_DSC_RW_MAX); | |
1da177e4 | 2509 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
83042b24 | 2510 | "%lums tDSC%s\n", |
b6422013 | 2511 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
f73850a3 BP |
2512 | (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, |
2513 | tape->buffer_size / 1024, | |
54bb2074 | 2514 | tape->best_dsc_rw_freq * 1000 / HZ, |
1da177e4 LT |
2515 | drive->using_dma ? ", DMA":""); |
2516 | ||
2517 | idetape_add_settings(drive); | |
2518 | } | |
2519 | ||
4031bbe4 | 2520 | static void ide_tape_remove(ide_drive_t *drive) |
1da177e4 LT |
2521 | { |
2522 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 2523 | |
7662d046 | 2524 | ide_proc_unregister_driver(drive, tape->driver); |
1da177e4 LT |
2525 | |
2526 | ide_unregister_region(tape->disk); | |
2527 | ||
2528 | ide_tape_put(tape); | |
1da177e4 LT |
2529 | } |
2530 | ||
2531 | static void ide_tape_release(struct kref *kref) | |
2532 | { | |
2533 | struct ide_tape_obj *tape = to_ide_tape(kref); | |
2534 | ide_drive_t *drive = tape->drive; | |
2535 | struct gendisk *g = tape->disk; | |
2536 | ||
01a63aeb | 2537 | BUG_ON(tape->merge_bh_size); |
8604affd | 2538 | |
1da177e4 LT |
2539 | drive->dsc_overlap = 0; |
2540 | drive->driver_data = NULL; | |
dbc1272e | 2541 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
5a04cfa9 BP |
2542 | device_destroy(idetape_sysfs_class, |
2543 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); | |
1da177e4 LT |
2544 | idetape_devs[tape->minor] = NULL; |
2545 | g->private_data = NULL; | |
2546 | put_disk(g); | |
2547 | kfree(tape); | |
2548 | } | |
2549 | ||
ecfd80e4 | 2550 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 LT |
2551 | static int proc_idetape_read_name |
2552 | (char *page, char **start, off_t off, int count, int *eof, void *data) | |
2553 | { | |
2554 | ide_drive_t *drive = (ide_drive_t *) data; | |
2555 | idetape_tape_t *tape = drive->driver_data; | |
2556 | char *out = page; | |
2557 | int len; | |
2558 | ||
2559 | len = sprintf(out, "%s\n", tape->name); | |
2560 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); | |
2561 | } | |
2562 | ||
2563 | static ide_proc_entry_t idetape_proc[] = { | |
2564 | { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL }, | |
2565 | { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL }, | |
2566 | { NULL, 0, NULL, NULL } | |
2567 | }; | |
1da177e4 LT |
2568 | #endif |
2569 | ||
4031bbe4 | 2570 | static int ide_tape_probe(ide_drive_t *); |
1da177e4 | 2571 | |
1da177e4 | 2572 | static ide_driver_t idetape_driver = { |
8604affd | 2573 | .gen_driver = { |
4ef3b8f4 | 2574 | .owner = THIS_MODULE, |
8604affd BZ |
2575 | .name = "ide-tape", |
2576 | .bus = &ide_bus_type, | |
8604affd | 2577 | }, |
4031bbe4 RK |
2578 | .probe = ide_tape_probe, |
2579 | .remove = ide_tape_remove, | |
1da177e4 LT |
2580 | .version = IDETAPE_VERSION, |
2581 | .media = ide_tape, | |
1da177e4 | 2582 | .supports_dsc_overlap = 1, |
1da177e4 LT |
2583 | .do_request = idetape_do_request, |
2584 | .end_request = idetape_end_request, | |
2585 | .error = __ide_error, | |
7662d046 | 2586 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 | 2587 | .proc = idetape_proc, |
7662d046 | 2588 | #endif |
1da177e4 LT |
2589 | }; |
2590 | ||
3c98bf34 | 2591 | /* Our character device supporting functions, passed to register_chrdev. */ |
2b8693c0 | 2592 | static const struct file_operations idetape_fops = { |
1da177e4 LT |
2593 | .owner = THIS_MODULE, |
2594 | .read = idetape_chrdev_read, | |
2595 | .write = idetape_chrdev_write, | |
2596 | .ioctl = idetape_chrdev_ioctl, | |
2597 | .open = idetape_chrdev_open, | |
2598 | .release = idetape_chrdev_release, | |
2599 | }; | |
2600 | ||
2601 | static int idetape_open(struct inode *inode, struct file *filp) | |
2602 | { | |
2603 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
2604 | struct ide_tape_obj *tape; | |
1da177e4 | 2605 | |
5a04cfa9 BP |
2606 | tape = ide_tape_get(disk); |
2607 | if (!tape) | |
1da177e4 LT |
2608 | return -ENXIO; |
2609 | ||
1da177e4 LT |
2610 | return 0; |
2611 | } | |
2612 | ||
2613 | static int idetape_release(struct inode *inode, struct file *filp) | |
2614 | { | |
2615 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
2616 | struct ide_tape_obj *tape = ide_tape_g(disk); | |
1da177e4 LT |
2617 | |
2618 | ide_tape_put(tape); | |
2619 | ||
2620 | return 0; | |
2621 | } | |
2622 | ||
2623 | static int idetape_ioctl(struct inode *inode, struct file *file, | |
2624 | unsigned int cmd, unsigned long arg) | |
2625 | { | |
2626 | struct block_device *bdev = inode->i_bdev; | |
2627 | struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk); | |
2628 | ide_drive_t *drive = tape->drive; | |
2629 | int err = generic_ide_ioctl(drive, file, bdev, cmd, arg); | |
2630 | if (err == -EINVAL) | |
2631 | err = idetape_blkdev_ioctl(drive, cmd, arg); | |
2632 | return err; | |
2633 | } | |
2634 | ||
2635 | static struct block_device_operations idetape_block_ops = { | |
2636 | .owner = THIS_MODULE, | |
2637 | .open = idetape_open, | |
2638 | .release = idetape_release, | |
2639 | .ioctl = idetape_ioctl, | |
2640 | }; | |
2641 | ||
4031bbe4 | 2642 | static int ide_tape_probe(ide_drive_t *drive) |
1da177e4 LT |
2643 | { |
2644 | idetape_tape_t *tape; | |
2645 | struct gendisk *g; | |
2646 | int minor; | |
2647 | ||
2648 | if (!strstr("ide-tape", drive->driver_req)) | |
2649 | goto failed; | |
2a924662 | 2650 | |
1da177e4 LT |
2651 | if (drive->media != ide_tape) |
2652 | goto failed; | |
2a924662 | 2653 | |
5a04cfa9 BP |
2654 | if (!idetape_identify_device(drive)) { |
2655 | printk(KERN_ERR "ide-tape: %s: not supported by this version of" | |
2656 | " the driver\n", drive->name); | |
1da177e4 LT |
2657 | goto failed; |
2658 | } | |
5a04cfa9 | 2659 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
1da177e4 | 2660 | if (tape == NULL) { |
5a04cfa9 BP |
2661 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
2662 | drive->name); | |
1da177e4 LT |
2663 | goto failed; |
2664 | } | |
2665 | ||
2666 | g = alloc_disk(1 << PARTN_BITS); | |
2667 | if (!g) | |
2668 | goto out_free_tape; | |
2669 | ||
2670 | ide_init_disk(g, drive); | |
2671 | ||
7662d046 | 2672 | ide_proc_register_driver(drive, &idetape_driver); |
1da177e4 | 2673 | |
1da177e4 LT |
2674 | kref_init(&tape->kref); |
2675 | ||
2676 | tape->drive = drive; | |
2677 | tape->driver = &idetape_driver; | |
2678 | tape->disk = g; | |
2679 | ||
2680 | g->private_data = &tape->driver; | |
2681 | ||
2682 | drive->driver_data = tape; | |
2683 | ||
cf8b8975 | 2684 | mutex_lock(&idetape_ref_mutex); |
1da177e4 LT |
2685 | for (minor = 0; idetape_devs[minor]; minor++) |
2686 | ; | |
2687 | idetape_devs[minor] = tape; | |
cf8b8975 | 2688 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
2689 | |
2690 | idetape_setup(drive, tape, minor); | |
2691 | ||
6ecaaf94 GKH |
2692 | device_create_drvdata(idetape_sysfs_class, &drive->gendev, |
2693 | MKDEV(IDETAPE_MAJOR, minor), NULL, | |
2694 | "%s", tape->name); | |
2695 | device_create_drvdata(idetape_sysfs_class, &drive->gendev, | |
2696 | MKDEV(IDETAPE_MAJOR, minor + 128), NULL, | |
2697 | "n%s", tape->name); | |
d5dee80a | 2698 | |
1da177e4 LT |
2699 | g->fops = &idetape_block_ops; |
2700 | ide_register_region(g); | |
2701 | ||
2702 | return 0; | |
8604affd | 2703 | |
1da177e4 LT |
2704 | out_free_tape: |
2705 | kfree(tape); | |
2706 | failed: | |
8604affd | 2707 | return -ENODEV; |
1da177e4 LT |
2708 | } |
2709 | ||
5a04cfa9 | 2710 | static void __exit idetape_exit(void) |
1da177e4 | 2711 | { |
8604affd | 2712 | driver_unregister(&idetape_driver.gen_driver); |
d5dee80a | 2713 | class_destroy(idetape_sysfs_class); |
1da177e4 LT |
2714 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
2715 | } | |
2716 | ||
17514e8a | 2717 | static int __init idetape_init(void) |
1da177e4 | 2718 | { |
d5dee80a WD |
2719 | int error = 1; |
2720 | idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); | |
2721 | if (IS_ERR(idetape_sysfs_class)) { | |
2722 | idetape_sysfs_class = NULL; | |
2723 | printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); | |
2724 | error = -EBUSY; | |
2725 | goto out; | |
2726 | } | |
2727 | ||
1da177e4 | 2728 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
5a04cfa9 BP |
2729 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
2730 | " interface\n"); | |
d5dee80a WD |
2731 | error = -EBUSY; |
2732 | goto out_free_class; | |
1da177e4 | 2733 | } |
d5dee80a WD |
2734 | |
2735 | error = driver_register(&idetape_driver.gen_driver); | |
2736 | if (error) | |
2737 | goto out_free_driver; | |
2738 | ||
2739 | return 0; | |
2740 | ||
2741 | out_free_driver: | |
2742 | driver_unregister(&idetape_driver.gen_driver); | |
2743 | out_free_class: | |
2744 | class_destroy(idetape_sysfs_class); | |
2745 | out: | |
2746 | return error; | |
1da177e4 LT |
2747 | } |
2748 | ||
263756ec | 2749 | MODULE_ALIAS("ide:*m-tape*"); |
1da177e4 LT |
2750 | module_init(idetape_init); |
2751 | module_exit(idetape_exit); | |
2752 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); | |
9c145768 BP |
2753 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
2754 | MODULE_LICENSE("GPL"); |