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