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