]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - drivers/ide/ide-tape.c
ide-tape: remove idetape_remove_stage_head()
[mirror_ubuntu-kernels.git] / drivers / ide / ide-tape.c
1 /*
2 * IDE ATAPI streaming tape driver.
3 *
4 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
6 *
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).
13 *
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
16 */
17
18 #define IDETAPE_VERSION "1.20"
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/major.h>
30 #include <linux/errno.h>
31 #include <linux/genhd.h>
32 #include <linux/slab.h>
33 #include <linux/pci.h>
34 #include <linux/ide.h>
35 #include <linux/smp_lock.h>
36 #include <linux/completion.h>
37 #include <linux/bitops.h>
38 #include <linux/mutex.h>
39 #include <scsi/scsi.h>
40
41 #include <asm/byteorder.h>
42 #include <linux/irq.h>
43 #include <linux/uaccess.h>
44 #include <linux/io.h>
45 #include <asm/unaligned.h>
46 #include <linux/mtio.h>
47
48 enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59 };
60
61 /* define to see debug info */
62 #define IDETAPE_DEBUG_LOG 0
63
64 #if IDETAPE_DEBUG_LOG
65 #define debug_log(lvl, fmt, args...) \
66 { \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69 }
70 #else
71 #define debug_log(lvl, fmt, args...) do {} while (0)
72 #endif
73
74 /**************************** Tunable parameters *****************************/
75
76
77 /*
78 * Pipelined mode parameters.
79 *
80 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
83 *
84 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
87 */
88 #define IDETAPE_MIN_PIPELINE_STAGES 1
89 #define IDETAPE_MAX_PIPELINE_STAGES 400
90 #define IDETAPE_INCREASE_STAGES_RATE 20
91
92 /*
93 * After each failed packet command we issue a request sense command and retry
94 * the packet command IDETAPE_MAX_PC_RETRIES times.
95 *
96 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
97 */
98 #define IDETAPE_MAX_PC_RETRIES 3
99
100 /*
101 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
103 */
104 #define IDETAPE_PC_BUFFER_SIZE 256
105
106 /*
107 * In various places in the driver, we need to allocate storage
108 * for packet commands and requests, which will remain valid while
109 * we leave the driver to wait for an interrupt or a timeout event.
110 */
111 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
112
113 /*
114 * Some drives (for example, Seagate STT3401A Travan) require a very long
115 * timeout, because they don't return an interrupt or clear their busy bit
116 * until after the command completes (even retension commands).
117 */
118 #define IDETAPE_WAIT_CMD (900*HZ)
119
120 /*
121 * The following parameter is used to select the point in the internal tape fifo
122 * in which we will start to refill the buffer. Decreasing the following
123 * parameter will improve the system's latency and interactive response, while
124 * using a high value might improve system throughput.
125 */
126 #define IDETAPE_FIFO_THRESHOLD 2
127
128 /*
129 * DSC polling parameters.
130 *
131 * Polling for DSC (a single bit in the status register) is a very important
132 * function in ide-tape. There are two cases in which we poll for DSC:
133 *
134 * 1. Before a read/write packet command, to ensure that we can transfer data
135 * from/to the tape's data buffers, without causing an actual media access.
136 * In case the tape is not ready yet, we take out our request from the device
137 * request queue, so that ide.c could service requests from the other device
138 * on the same interface in the meantime.
139 *
140 * 2. After the successful initialization of a "media access packet command",
141 * which is a command that can take a long time to complete (the interval can
142 * range from several seconds to even an hour). Again, we postpone our request
143 * in the middle to free the bus for the other device. The polling frequency
144 * here should be lower than the read/write frequency since those media access
145 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
148 *
149 * We also set a timeout for the timer, in case something goes wrong. The
150 * timeout should be longer then the maximum execution time of a tape operation.
151 */
152
153 /* DSC timings. */
154 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
155 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
156 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
157 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
158 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
159 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
160 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
161
162 /*************************** End of tunable parameters ***********************/
163
164 /* Read/Write error simulation */
165 #define SIMULATE_ERRORS 0
166
167 /* tape directions */
168 enum {
169 IDETAPE_DIR_NONE = (1 << 0),
170 IDETAPE_DIR_READ = (1 << 1),
171 IDETAPE_DIR_WRITE = (1 << 2),
172 };
173
174 struct idetape_bh {
175 u32 b_size;
176 atomic_t b_count;
177 struct idetape_bh *b_reqnext;
178 char *b_data;
179 };
180
181 /* Tape door status */
182 #define DOOR_UNLOCKED 0
183 #define DOOR_LOCKED 1
184 #define DOOR_EXPLICITLY_LOCKED 2
185
186 /* Some defines for the SPACE command */
187 #define IDETAPE_SPACE_OVER_FILEMARK 1
188 #define IDETAPE_SPACE_TO_EOD 3
189
190 /* Some defines for the LOAD UNLOAD command */
191 #define IDETAPE_LU_LOAD_MASK 1
192 #define IDETAPE_LU_RETENSION_MASK 2
193 #define IDETAPE_LU_EOT_MASK 4
194
195 /*
196 * Special requests for our block device strategy routine.
197 *
198 * In order to service a character device command, we add special requests to
199 * the tail of our block device request queue and wait for their completion.
200 */
201
202 enum {
203 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
204 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
205 REQ_IDETAPE_READ = (1 << 2),
206 REQ_IDETAPE_WRITE = (1 << 3),
207 };
208
209 /* Error codes returned in rq->errors to the higher part of the driver. */
210 #define IDETAPE_ERROR_GENERAL 101
211 #define IDETAPE_ERROR_FILEMARK 102
212 #define IDETAPE_ERROR_EOD 103
213
214 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215 #define IDETAPE_BLOCK_DESCRIPTOR 0
216 #define IDETAPE_CAPABILITIES_PAGE 0x2a
217
218 /* Tape flag bits values. */
219 enum {
220 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
221 /* 0 When the tape position is unknown */
222 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
223 /* Device already opened */
224 IDETAPE_FLAG_BUSY = (1 << 2),
225 /* Error detected in a pipeline stage */
226 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
227 /* Attempt to auto-detect the current user block size */
228 IDETAPE_FLAG_DETECT_BS = (1 << 4),
229 /* Currently on a filemark */
230 IDETAPE_FLAG_FILEMARK = (1 << 5),
231 /* DRQ interrupt device */
232 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
233 /* pipeline active */
234 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
235 /* 0 = no tape is loaded, so we don't rewind after ejecting */
236 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
237 };
238
239 /* A pipeline stage. */
240 typedef struct idetape_stage_s {
241 struct request rq; /* The corresponding request */
242 struct idetape_bh *bh; /* The data buffers */
243 struct idetape_stage_s *next; /* Pointer to the next stage */
244 } idetape_stage_t;
245
246 /*
247 * Most of our global data which we need to save even as we leave the driver due
248 * to an interrupt or a timer event is stored in the struct defined below.
249 */
250 typedef struct ide_tape_obj {
251 ide_drive_t *drive;
252 ide_driver_t *driver;
253 struct gendisk *disk;
254 struct kref kref;
255
256 /*
257 * Since a typical character device operation requires more
258 * than one packet command, we provide here enough memory
259 * for the maximum of interconnected packet commands.
260 * The packet commands are stored in the circular array pc_stack.
261 * pc_stack_index points to the last used entry, and warps around
262 * to the start when we get to the last array entry.
263 *
264 * pc points to the current processed packet command.
265 *
266 * failed_pc points to the last failed packet command, or contains
267 * NULL if we do not need to retry any packet command. This is
268 * required since an additional packet command is needed before the
269 * retry, to get detailed information on what went wrong.
270 */
271 /* Current packet command */
272 struct ide_atapi_pc *pc;
273 /* Last failed packet command */
274 struct ide_atapi_pc *failed_pc;
275 /* Packet command stack */
276 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
277 /* Next free packet command storage space */
278 int pc_stack_index;
279 struct request rq_stack[IDETAPE_PC_STACK];
280 /* We implement a circular array */
281 int rq_stack_index;
282
283 /*
284 * DSC polling variables.
285 *
286 * While polling for DSC we use postponed_rq to postpone the current
287 * request so that ide.c will be able to service pending requests on the
288 * other device. Note that at most we will have only one DSC (usually
289 * data transfer) request in the device request queue. Additional
290 * requests can be queued in our internal pipeline, but they will be
291 * visible to ide.c only one at a time.
292 */
293 struct request *postponed_rq;
294 /* The time in which we started polling for DSC */
295 unsigned long dsc_polling_start;
296 /* Timer used to poll for dsc */
297 struct timer_list dsc_timer;
298 /* Read/Write dsc polling frequency */
299 unsigned long best_dsc_rw_freq;
300 unsigned long dsc_poll_freq;
301 unsigned long dsc_timeout;
302
303 /* Read position information */
304 u8 partition;
305 /* Current block */
306 unsigned int first_frame;
307
308 /* Last error information */
309 u8 sense_key, asc, ascq;
310
311 /* Character device operation */
312 unsigned int minor;
313 /* device name */
314 char name[4];
315 /* Current character device data transfer direction */
316 u8 chrdev_dir;
317
318 /* tape block size, usually 512 or 1024 bytes */
319 unsigned short blk_size;
320 int user_bs_factor;
321
322 /* Copy of the tape's Capabilities and Mechanical Page */
323 u8 caps[20];
324
325 /*
326 * Active data transfer request parameters.
327 *
328 * At most, there is only one ide-tape originated data transfer request
329 * in the device request queue. This allows ide.c to easily service
330 * requests from the other device when we postpone our active request.
331 * In the pipelined operation mode, we use our internal pipeline
332 * structure to hold more data requests. The data buffer size is chosen
333 * based on the tape's recommendation.
334 */
335 /* ptr to the request which is waiting in the device request queue */
336 struct request *active_data_rq;
337 /* Data buffer size chosen based on the tape's recommendation */
338 int stage_size;
339 idetape_stage_t *merge_stage;
340 int merge_stage_size;
341 struct idetape_bh *bh;
342 char *b_data;
343 int b_count;
344
345 /*
346 * Pipeline parameters.
347 *
348 * To accomplish non-pipelined mode, we simply set the following
349 * variables to zero (or NULL, where appropriate).
350 */
351 /* Number of currently used stages */
352 int nr_stages;
353 /* Number of pending stages */
354 int nr_pending_stages;
355 /* We will not allocate more than this number of stages */
356 int max_stages, min_pipeline, max_pipeline;
357 /* The first stage which will be removed from the pipeline */
358 idetape_stage_t *first_stage;
359 /* The currently active stage */
360 idetape_stage_t *active_stage;
361 /* Will be serviced after the currently active request */
362 idetape_stage_t *next_stage;
363 /* New requests will be added to the pipeline here */
364 idetape_stage_t *last_stage;
365 int pages_per_stage;
366 /* Wasted space in each stage */
367 int excess_bh_size;
368
369 /* Status/Action flags: long for set_bit */
370 unsigned long flags;
371 /* protects the ide-tape queue */
372 spinlock_t lock;
373
374 /* Measures average tape speed */
375 unsigned long avg_time;
376 int avg_size;
377 int avg_speed;
378
379 /* the door is currently locked */
380 int door_locked;
381 /* the tape hardware is write protected */
382 char drv_write_prot;
383 /* the tape is write protected (hardware or opened as read-only) */
384 char write_prot;
385
386 /*
387 * Limit the number of times a request can be postponed, to avoid an
388 * infinite postpone deadlock.
389 */
390 int postpone_cnt;
391
392 /* Speed control at the tape buffers input/output */
393 unsigned long insert_time;
394 int insert_size;
395 int insert_speed;
396 int measure_insert_time;
397
398 u32 debug_mask;
399 } idetape_tape_t;
400
401 static DEFINE_MUTEX(idetape_ref_mutex);
402
403 static struct class *idetape_sysfs_class;
404
405 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
406
407 #define ide_tape_g(disk) \
408 container_of((disk)->private_data, struct ide_tape_obj, driver)
409
410 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
411 {
412 struct ide_tape_obj *tape = NULL;
413
414 mutex_lock(&idetape_ref_mutex);
415 tape = ide_tape_g(disk);
416 if (tape)
417 kref_get(&tape->kref);
418 mutex_unlock(&idetape_ref_mutex);
419 return tape;
420 }
421
422 static void ide_tape_release(struct kref *);
423
424 static void ide_tape_put(struct ide_tape_obj *tape)
425 {
426 mutex_lock(&idetape_ref_mutex);
427 kref_put(&tape->kref, ide_tape_release);
428 mutex_unlock(&idetape_ref_mutex);
429 }
430
431 /*
432 * The variables below are used for the character device interface. Additional
433 * state variables are defined in our ide_drive_t structure.
434 */
435 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
436
437 #define ide_tape_f(file) ((file)->private_data)
438
439 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
440 {
441 struct ide_tape_obj *tape = NULL;
442
443 mutex_lock(&idetape_ref_mutex);
444 tape = idetape_devs[i];
445 if (tape)
446 kref_get(&tape->kref);
447 mutex_unlock(&idetape_ref_mutex);
448 return tape;
449 }
450
451 static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
452 unsigned int bcount)
453 {
454 struct idetape_bh *bh = pc->bh;
455 int count;
456
457 while (bcount) {
458 if (bh == NULL) {
459 printk(KERN_ERR "ide-tape: bh == NULL in "
460 "idetape_input_buffers\n");
461 ide_atapi_discard_data(drive, bcount);
462 return;
463 }
464 count = min(
465 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
466 bcount);
467 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
468 atomic_read(&bh->b_count), count);
469 bcount -= count;
470 atomic_add(count, &bh->b_count);
471 if (atomic_read(&bh->b_count) == bh->b_size) {
472 bh = bh->b_reqnext;
473 if (bh)
474 atomic_set(&bh->b_count, 0);
475 }
476 }
477 pc->bh = bh;
478 }
479
480 static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
481 unsigned int bcount)
482 {
483 struct idetape_bh *bh = pc->bh;
484 int count;
485
486 while (bcount) {
487 if (bh == NULL) {
488 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
489 __func__);
490 return;
491 }
492 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
493 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
494 bcount -= count;
495 pc->b_data += count;
496 pc->b_count -= count;
497 if (!pc->b_count) {
498 bh = bh->b_reqnext;
499 pc->bh = bh;
500 if (bh) {
501 pc->b_data = bh->b_data;
502 pc->b_count = atomic_read(&bh->b_count);
503 }
504 }
505 }
506 }
507
508 static void idetape_update_buffers(struct ide_atapi_pc *pc)
509 {
510 struct idetape_bh *bh = pc->bh;
511 int count;
512 unsigned int bcount = pc->xferred;
513
514 if (pc->flags & PC_FLAG_WRITING)
515 return;
516 while (bcount) {
517 if (bh == NULL) {
518 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
519 __func__);
520 return;
521 }
522 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
523 atomic_set(&bh->b_count, count);
524 if (atomic_read(&bh->b_count) == bh->b_size)
525 bh = bh->b_reqnext;
526 bcount -= count;
527 }
528 pc->bh = bh;
529 }
530
531 /*
532 * idetape_next_pc_storage returns a pointer to a place in which we can
533 * safely store a packet command, even though we intend to leave the
534 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
535 * commands is allocated at initialization time.
536 */
537 static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
538 {
539 idetape_tape_t *tape = drive->driver_data;
540
541 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
542
543 if (tape->pc_stack_index == IDETAPE_PC_STACK)
544 tape->pc_stack_index = 0;
545 return (&tape->pc_stack[tape->pc_stack_index++]);
546 }
547
548 /*
549 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
550 * Since we queue packet commands in the request queue, we need to
551 * allocate a request, along with the allocation of a packet command.
552 */
553
554 /**************************************************************
555 * *
556 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
557 * followed later on by kfree(). -ml *
558 * *
559 **************************************************************/
560
561 static struct request *idetape_next_rq_storage(ide_drive_t *drive)
562 {
563 idetape_tape_t *tape = drive->driver_data;
564
565 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
566
567 if (tape->rq_stack_index == IDETAPE_PC_STACK)
568 tape->rq_stack_index = 0;
569 return (&tape->rq_stack[tape->rq_stack_index++]);
570 }
571
572 static void idetape_init_pc(struct ide_atapi_pc *pc)
573 {
574 memset(pc->c, 0, 12);
575 pc->retries = 0;
576 pc->flags = 0;
577 pc->req_xfer = 0;
578 pc->buf = pc->pc_buf;
579 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
580 pc->bh = NULL;
581 pc->b_data = NULL;
582 }
583
584 /*
585 * called on each failed packet command retry to analyze the request sense. We
586 * currently do not utilize this information.
587 */
588 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
589 {
590 idetape_tape_t *tape = drive->driver_data;
591 struct ide_atapi_pc *pc = tape->failed_pc;
592
593 tape->sense_key = sense[2] & 0xF;
594 tape->asc = sense[12];
595 tape->ascq = sense[13];
596
597 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
598 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
599
600 /* Correct pc->xferred by asking the tape. */
601 if (pc->flags & PC_FLAG_DMA_ERROR) {
602 pc->xferred = pc->req_xfer -
603 tape->blk_size *
604 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
605 idetape_update_buffers(pc);
606 }
607
608 /*
609 * If error was the result of a zero-length read or write command,
610 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
611 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
612 */
613 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
614 /* length == 0 */
615 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
616 if (tape->sense_key == 5) {
617 /* don't report an error, everything's ok */
618 pc->error = 0;
619 /* don't retry read/write */
620 pc->flags |= PC_FLAG_ABORT;
621 }
622 }
623 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
624 pc->error = IDETAPE_ERROR_FILEMARK;
625 pc->flags |= PC_FLAG_ABORT;
626 }
627 if (pc->c[0] == WRITE_6) {
628 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
629 && tape->asc == 0x0 && tape->ascq == 0x2)) {
630 pc->error = IDETAPE_ERROR_EOD;
631 pc->flags |= PC_FLAG_ABORT;
632 }
633 }
634 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
635 if (tape->sense_key == 8) {
636 pc->error = IDETAPE_ERROR_EOD;
637 pc->flags |= PC_FLAG_ABORT;
638 }
639 if (!(pc->flags & PC_FLAG_ABORT) &&
640 pc->xferred)
641 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
642 }
643 }
644
645 static void idetape_activate_next_stage(ide_drive_t *drive)
646 {
647 idetape_tape_t *tape = drive->driver_data;
648 idetape_stage_t *stage = tape->next_stage;
649 struct request *rq = &stage->rq;
650
651 debug_log(DBG_PROCS, "Enter %s\n", __func__);
652
653 if (stage == NULL) {
654 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
655 " existing stage\n");
656 return;
657 }
658
659 rq->rq_disk = tape->disk;
660 rq->buffer = NULL;
661 rq->special = (void *)stage->bh;
662 tape->active_data_rq = rq;
663 tape->active_stage = stage;
664 tape->next_stage = stage->next;
665 }
666
667 /* Free a stage along with its related buffers completely. */
668 static void __idetape_kfree_stage(idetape_stage_t *stage)
669 {
670 struct idetape_bh *prev_bh, *bh = stage->bh;
671 int size;
672
673 while (bh != NULL) {
674 if (bh->b_data != NULL) {
675 size = (int) bh->b_size;
676 while (size > 0) {
677 free_page((unsigned long) bh->b_data);
678 size -= PAGE_SIZE;
679 bh->b_data += PAGE_SIZE;
680 }
681 }
682 prev_bh = bh;
683 bh = bh->b_reqnext;
684 kfree(prev_bh);
685 }
686 kfree(stage);
687 }
688
689 static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
690 {
691 __idetape_kfree_stage(stage);
692 }
693
694 /*
695 * This will free all the pipeline stages starting from new_last_stage->next
696 * to the end of the list, and point tape->last_stage to new_last_stage.
697 */
698 static void idetape_abort_pipeline(ide_drive_t *drive,
699 idetape_stage_t *new_last_stage)
700 {
701 idetape_tape_t *tape = drive->driver_data;
702 idetape_stage_t *stage = new_last_stage->next;
703 idetape_stage_t *nstage;
704
705 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
706
707 while (stage) {
708 nstage = stage->next;
709 idetape_kfree_stage(tape, stage);
710 --tape->nr_stages;
711 --tape->nr_pending_stages;
712 stage = nstage;
713 }
714 if (new_last_stage)
715 new_last_stage->next = NULL;
716 tape->last_stage = new_last_stage;
717 tape->next_stage = NULL;
718 }
719
720 /*
721 * Finish servicing a request and insert a pending pipeline request into the
722 * main device queue.
723 */
724 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
725 {
726 struct request *rq = HWGROUP(drive)->rq;
727 idetape_tape_t *tape = drive->driver_data;
728 unsigned long flags;
729 int error;
730 idetape_stage_t *active_stage;
731
732 debug_log(DBG_PROCS, "Enter %s\n", __func__);
733
734 switch (uptodate) {
735 case 0: error = IDETAPE_ERROR_GENERAL; break;
736 case 1: error = 0; break;
737 default: error = uptodate;
738 }
739 rq->errors = error;
740 if (error)
741 tape->failed_pc = NULL;
742
743 if (!blk_special_request(rq)) {
744 ide_end_request(drive, uptodate, nr_sects);
745 return 0;
746 }
747
748 spin_lock_irqsave(&tape->lock, flags);
749
750 /* The request was a pipelined data transfer request */
751 if (tape->active_data_rq == rq) {
752 active_stage = tape->active_stage;
753 tape->active_stage = NULL;
754 tape->active_data_rq = NULL;
755 tape->nr_pending_stages--;
756 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
757 if (error) {
758 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
759 &tape->flags);
760 if (error == IDETAPE_ERROR_EOD)
761 idetape_abort_pipeline(drive,
762 active_stage);
763 }
764 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
765 if (error == IDETAPE_ERROR_EOD) {
766 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
767 &tape->flags);
768 idetape_abort_pipeline(drive, active_stage);
769 }
770 }
771 if (tape->next_stage != NULL) {
772 idetape_activate_next_stage(drive);
773
774 /* Insert the next request into the request queue. */
775 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
776 ide_end);
777 } else if (!error) {
778 /*
779 * This is a part of the feedback loop which tries to
780 * find the optimum number of stages. We are starting
781 * from a minimum maximum number of stages, and if we
782 * sense that the pipeline is empty, we try to increase
783 * it, until we reach the user compile time memory
784 * limit.
785 */
786 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
787
788 tape->max_stages += max(i, 1);
789 tape->max_stages = max(tape->max_stages,
790 tape->min_pipeline);
791 tape->max_stages = min(tape->max_stages,
792 tape->max_pipeline);
793 }
794 }
795 ide_end_drive_cmd(drive, 0, 0);
796
797 if (tape->active_data_rq == NULL)
798 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
799 spin_unlock_irqrestore(&tape->lock, flags);
800 return 0;
801 }
802
803 static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
804 {
805 idetape_tape_t *tape = drive->driver_data;
806
807 debug_log(DBG_PROCS, "Enter %s\n", __func__);
808
809 if (!tape->pc->error) {
810 idetape_analyze_error(drive, tape->pc->buf);
811 idetape_end_request(drive, 1, 0);
812 } else {
813 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
814 "Aborting request!\n");
815 idetape_end_request(drive, 0, 0);
816 }
817 return ide_stopped;
818 }
819
820 static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
821 {
822 idetape_init_pc(pc);
823 pc->c[0] = REQUEST_SENSE;
824 pc->c[4] = 20;
825 pc->req_xfer = 20;
826 pc->idetape_callback = &idetape_request_sense_callback;
827 }
828
829 static void idetape_init_rq(struct request *rq, u8 cmd)
830 {
831 memset(rq, 0, sizeof(*rq));
832 rq->cmd_type = REQ_TYPE_SPECIAL;
833 rq->cmd[0] = cmd;
834 }
835
836 /*
837 * Generate a new packet command request in front of the request queue, before
838 * the current request, so that it will be processed immediately, on the next
839 * pass through the driver. The function below is called from the request
840 * handling part of the driver (the "bottom" part). Safe storage for the request
841 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
842 *
843 * Memory for those requests is pre-allocated at initialization time, and is
844 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
845 * the maximum possible number of inter-dependent packet commands.
846 *
847 * The higher level of the driver - The ioctl handler and the character device
848 * handling functions should queue request to the lower level part and wait for
849 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
850 */
851 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
852 struct request *rq)
853 {
854 struct ide_tape_obj *tape = drive->driver_data;
855
856 idetape_init_rq(rq, REQ_IDETAPE_PC1);
857 rq->buffer = (char *) pc;
858 rq->rq_disk = tape->disk;
859 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
860 }
861
862 /*
863 * idetape_retry_pc is called when an error was detected during the
864 * last packet command. We queue a request sense packet command in
865 * the head of the request list.
866 */
867 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
868 {
869 idetape_tape_t *tape = drive->driver_data;
870 struct ide_atapi_pc *pc;
871 struct request *rq;
872
873 (void)ide_read_error(drive);
874 pc = idetape_next_pc_storage(drive);
875 rq = idetape_next_rq_storage(drive);
876 idetape_create_request_sense_cmd(pc);
877 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
878 idetape_queue_pc_head(drive, pc, rq);
879 return ide_stopped;
880 }
881
882 /*
883 * Postpone the current request so that ide.c will be able to service requests
884 * from another device on the same hwgroup while we are polling for DSC.
885 */
886 static void idetape_postpone_request(ide_drive_t *drive)
887 {
888 idetape_tape_t *tape = drive->driver_data;
889
890 debug_log(DBG_PROCS, "Enter %s\n", __func__);
891
892 tape->postponed_rq = HWGROUP(drive)->rq;
893 ide_stall_queue(drive, tape->dsc_poll_freq);
894 }
895
896 typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
897
898 /*
899 * This is the usual interrupt handler which will be called during a packet
900 * command. We will transfer some of the data (as requested by the drive) and
901 * will re-point interrupt handler to us. When data transfer is finished, we
902 * will act according to the algorithm described before
903 * idetape_issue_pc.
904 */
905 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
906 {
907 ide_hwif_t *hwif = drive->hwif;
908 idetape_tape_t *tape = drive->driver_data;
909 struct ide_atapi_pc *pc = tape->pc;
910 xfer_func_t *xferfunc;
911 idetape_io_buf *iobuf;
912 unsigned int temp;
913 #if SIMULATE_ERRORS
914 static int error_sim_count;
915 #endif
916 u16 bcount;
917 u8 stat, ireason;
918
919 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
920
921 /* Clear the interrupt */
922 stat = ide_read_status(drive);
923
924 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
925 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
926 /*
927 * A DMA error is sometimes expected. For example,
928 * if the tape is crossing a filemark during a
929 * READ command, it will issue an irq and position
930 * itself before the filemark, so that only a partial
931 * data transfer will occur (which causes the DMA
932 * error). In that case, we will later ask the tape
933 * how much bytes of the original request were
934 * actually transferred (we can't receive that
935 * information from the DMA engine on most chipsets).
936 */
937
938 /*
939 * On the contrary, a DMA error is never expected;
940 * it usually indicates a hardware error or abort.
941 * If the tape crosses a filemark during a READ
942 * command, it will issue an irq and position itself
943 * after the filemark (not before). Only a partial
944 * data transfer will occur, but no DMA error.
945 * (AS, 19 Apr 2001)
946 */
947 pc->flags |= PC_FLAG_DMA_ERROR;
948 } else {
949 pc->xferred = pc->req_xfer;
950 idetape_update_buffers(pc);
951 }
952 debug_log(DBG_PROCS, "DMA finished\n");
953
954 }
955
956 /* No more interrupts */
957 if ((stat & DRQ_STAT) == 0) {
958 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
959 " transferred\n", pc->xferred);
960
961 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
962 local_irq_enable();
963
964 #if SIMULATE_ERRORS
965 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
966 (++error_sim_count % 100) == 0) {
967 printk(KERN_INFO "ide-tape: %s: simulating error\n",
968 tape->name);
969 stat |= ERR_STAT;
970 }
971 #endif
972 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
973 stat &= ~ERR_STAT;
974 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
975 /* Error detected */
976 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
977
978 if (pc->c[0] == REQUEST_SENSE) {
979 printk(KERN_ERR "ide-tape: I/O error in request"
980 " sense command\n");
981 return ide_do_reset(drive);
982 }
983 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
984 pc->c[0]);
985
986 /* Retry operation */
987 return idetape_retry_pc(drive);
988 }
989 pc->error = 0;
990 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
991 (stat & SEEK_STAT) == 0) {
992 /* Media access command */
993 tape->dsc_polling_start = jiffies;
994 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
995 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
996 /* Allow ide.c to handle other requests */
997 idetape_postpone_request(drive);
998 return ide_stopped;
999 }
1000 if (tape->failed_pc == pc)
1001 tape->failed_pc = NULL;
1002 /* Command finished - Call the callback function */
1003 return pc->idetape_callback(drive);
1004 }
1005
1006 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1007 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1008 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1009 "interrupts in DMA mode\n");
1010 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1011 ide_dma_off(drive);
1012 return ide_do_reset(drive);
1013 }
1014 /* Get the number of bytes to transfer on this interrupt. */
1015 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1016 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
1017
1018 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1019
1020 if (ireason & CD) {
1021 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
1022 return ide_do_reset(drive);
1023 }
1024 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
1025 /* Hopefully, we will never get here */
1026 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1027 (ireason & IO) ? "Write" : "Read");
1028 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1029 (ireason & IO) ? "Read" : "Write");
1030 return ide_do_reset(drive);
1031 }
1032 if (!(pc->flags & PC_FLAG_WRITING)) {
1033 /* Reading - Check that we have enough space */
1034 temp = pc->xferred + bcount;
1035 if (temp > pc->req_xfer) {
1036 if (temp > pc->buf_size) {
1037 printk(KERN_ERR "ide-tape: The tape wants to "
1038 "send us more data than expected "
1039 "- discarding data\n");
1040 ide_atapi_discard_data(drive, bcount);
1041 ide_set_handler(drive, &idetape_pc_intr,
1042 IDETAPE_WAIT_CMD, NULL);
1043 return ide_started;
1044 }
1045 debug_log(DBG_SENSE, "The tape wants to send us more "
1046 "data than expected - allowing transfer\n");
1047 }
1048 iobuf = &idetape_input_buffers;
1049 xferfunc = hwif->atapi_input_bytes;
1050 } else {
1051 iobuf = &idetape_output_buffers;
1052 xferfunc = hwif->atapi_output_bytes;
1053 }
1054
1055 if (pc->bh)
1056 iobuf(drive, pc, bcount);
1057 else
1058 xferfunc(drive, pc->cur_pos, bcount);
1059
1060 /* Update the current position */
1061 pc->xferred += bcount;
1062 pc->cur_pos += bcount;
1063
1064 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1065 pc->c[0], bcount);
1066
1067 /* And set the interrupt handler again */
1068 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1069 return ide_started;
1070 }
1071
1072 /*
1073 * Packet Command Interface
1074 *
1075 * The current Packet Command is available in tape->pc, and will not change
1076 * until we finish handling it. Each packet command is associated with a
1077 * callback function that will be called when the command is finished.
1078 *
1079 * The handling will be done in three stages:
1080 *
1081 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1082 * the interrupt handler to idetape_pc_intr.
1083 *
1084 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1085 * repeated until the device signals us that no more interrupts will be issued.
1086 *
1087 * 3. ATAPI Tape media access commands have immediate status with a delayed
1088 * process. In case of a successful initiation of a media access packet command,
1089 * the DSC bit will be set when the actual execution of the command is finished.
1090 * Since the tape drive will not issue an interrupt, we have to poll for this
1091 * event. In this case, we define the request as "low priority request" by
1092 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1093 * exit the driver.
1094 *
1095 * ide.c will then give higher priority to requests which originate from the
1096 * other device, until will change rq_status to RQ_ACTIVE.
1097 *
1098 * 4. When the packet command is finished, it will be checked for errors.
1099 *
1100 * 5. In case an error was found, we queue a request sense packet command in
1101 * front of the request queue and retry the operation up to
1102 * IDETAPE_MAX_PC_RETRIES times.
1103 *
1104 * 6. In case no error was found, or we decided to give up and not to retry
1105 * again, the callback function will be called and then we will handle the next
1106 * request.
1107 */
1108 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1109 {
1110 ide_hwif_t *hwif = drive->hwif;
1111 idetape_tape_t *tape = drive->driver_data;
1112 struct ide_atapi_pc *pc = tape->pc;
1113 int retries = 100;
1114 ide_startstop_t startstop;
1115 u8 ireason;
1116
1117 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1118 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1119 "yet DRQ isn't asserted\n");
1120 return startstop;
1121 }
1122 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1123 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1124 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1125 "a packet command, retrying\n");
1126 udelay(100);
1127 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1128 if (retries == 0) {
1129 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1130 "issuing a packet command, ignoring\n");
1131 ireason |= CD;
1132 ireason &= ~IO;
1133 }
1134 }
1135 if ((ireason & CD) == 0 || (ireason & IO)) {
1136 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1137 "a packet command\n");
1138 return ide_do_reset(drive);
1139 }
1140 /* Set the interrupt routine */
1141 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1142 #ifdef CONFIG_BLK_DEV_IDEDMA
1143 /* Begin DMA, if necessary */
1144 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
1145 hwif->dma_ops->dma_start(drive);
1146 #endif
1147 /* Send the actual packet */
1148 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1149 return ide_started;
1150 }
1151
1152 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1153 struct ide_atapi_pc *pc)
1154 {
1155 ide_hwif_t *hwif = drive->hwif;
1156 idetape_tape_t *tape = drive->driver_data;
1157 int dma_ok = 0;
1158 u16 bcount;
1159
1160 if (tape->pc->c[0] == REQUEST_SENSE &&
1161 pc->c[0] == REQUEST_SENSE) {
1162 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1163 "Two request sense in serial were issued\n");
1164 }
1165
1166 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
1167 tape->failed_pc = pc;
1168 /* Set the current packet command */
1169 tape->pc = pc;
1170
1171 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1172 (pc->flags & PC_FLAG_ABORT)) {
1173 /*
1174 * We will "abort" retrying a packet command in case legitimate
1175 * error code was received (crossing a filemark, or end of the
1176 * media, for example).
1177 */
1178 if (!(pc->flags & PC_FLAG_ABORT)) {
1179 if (!(pc->c[0] == TEST_UNIT_READY &&
1180 tape->sense_key == 2 && tape->asc == 4 &&
1181 (tape->ascq == 1 || tape->ascq == 8))) {
1182 printk(KERN_ERR "ide-tape: %s: I/O error, "
1183 "pc = %2x, key = %2x, "
1184 "asc = %2x, ascq = %2x\n",
1185 tape->name, pc->c[0],
1186 tape->sense_key, tape->asc,
1187 tape->ascq);
1188 }
1189 /* Giving up */
1190 pc->error = IDETAPE_ERROR_GENERAL;
1191 }
1192 tape->failed_pc = NULL;
1193 return pc->idetape_callback(drive);
1194 }
1195 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
1196
1197 pc->retries++;
1198 /* We haven't transferred any data yet */
1199 pc->xferred = 0;
1200 pc->cur_pos = pc->buf;
1201 /* Request to transfer the entire buffer at once */
1202 bcount = pc->req_xfer;
1203
1204 if (pc->flags & PC_FLAG_DMA_ERROR) {
1205 pc->flags &= ~PC_FLAG_DMA_ERROR;
1206 printk(KERN_WARNING "ide-tape: DMA disabled, "
1207 "reverting to PIO\n");
1208 ide_dma_off(drive);
1209 }
1210 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
1211 dma_ok = !hwif->dma_ops->dma_setup(drive);
1212
1213 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1214 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1215
1216 if (dma_ok)
1217 /* Will begin DMA later */
1218 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1219 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
1220 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1221 IDETAPE_WAIT_CMD, NULL);
1222 return ide_started;
1223 } else {
1224 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
1225 return idetape_transfer_pc(drive);
1226 }
1227 }
1228
1229 static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
1230 {
1231 idetape_tape_t *tape = drive->driver_data;
1232
1233 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1234
1235 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1236 return ide_stopped;
1237 }
1238
1239 /* A mode sense command is used to "sense" tape parameters. */
1240 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
1241 {
1242 idetape_init_pc(pc);
1243 pc->c[0] = MODE_SENSE;
1244 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1245 /* DBD = 1 - Don't return block descriptors */
1246 pc->c[1] = 8;
1247 pc->c[2] = page_code;
1248 /*
1249 * Changed pc->c[3] to 0 (255 will at best return unused info).
1250 *
1251 * For SCSI this byte is defined as subpage instead of high byte
1252 * of length and some IDE drives seem to interpret it this way
1253 * and return an error when 255 is used.
1254 */
1255 pc->c[3] = 0;
1256 /* We will just discard data in that case */
1257 pc->c[4] = 255;
1258 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1259 pc->req_xfer = 12;
1260 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1261 pc->req_xfer = 24;
1262 else
1263 pc->req_xfer = 50;
1264 pc->idetape_callback = &idetape_pc_callback;
1265 }
1266
1267 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
1268 {
1269 idetape_tape_t *tape = drive->driver_data;
1270 struct ide_atapi_pc *pc = tape->pc;
1271 u8 stat;
1272
1273 stat = ide_read_status(drive);
1274
1275 if (stat & SEEK_STAT) {
1276 if (stat & ERR_STAT) {
1277 /* Error detected */
1278 if (pc->c[0] != TEST_UNIT_READY)
1279 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1280 tape->name);
1281 /* Retry operation */
1282 return idetape_retry_pc(drive);
1283 }
1284 pc->error = 0;
1285 if (tape->failed_pc == pc)
1286 tape->failed_pc = NULL;
1287 } else {
1288 pc->error = IDETAPE_ERROR_GENERAL;
1289 tape->failed_pc = NULL;
1290 }
1291 return pc->idetape_callback(drive);
1292 }
1293
1294 static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
1295 {
1296 idetape_tape_t *tape = drive->driver_data;
1297 struct request *rq = HWGROUP(drive)->rq;
1298 int blocks = tape->pc->xferred / tape->blk_size;
1299
1300 tape->avg_size += blocks * tape->blk_size;
1301 tape->insert_size += blocks * tape->blk_size;
1302 if (tape->insert_size > 1024 * 1024)
1303 tape->measure_insert_time = 1;
1304 if (tape->measure_insert_time) {
1305 tape->measure_insert_time = 0;
1306 tape->insert_time = jiffies;
1307 tape->insert_size = 0;
1308 }
1309 if (time_after(jiffies, tape->insert_time))
1310 tape->insert_speed = tape->insert_size / 1024 * HZ /
1311 (jiffies - tape->insert_time);
1312 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1313 tape->avg_speed = tape->avg_size * HZ /
1314 (jiffies - tape->avg_time) / 1024;
1315 tape->avg_size = 0;
1316 tape->avg_time = jiffies;
1317 }
1318 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1319
1320 tape->first_frame += blocks;
1321 rq->current_nr_sectors -= blocks;
1322
1323 if (!tape->pc->error)
1324 idetape_end_request(drive, 1, 0);
1325 else
1326 idetape_end_request(drive, tape->pc->error, 0);
1327 return ide_stopped;
1328 }
1329
1330 static void idetape_create_read_cmd(idetape_tape_t *tape,
1331 struct ide_atapi_pc *pc,
1332 unsigned int length, struct idetape_bh *bh)
1333 {
1334 idetape_init_pc(pc);
1335 pc->c[0] = READ_6;
1336 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1337 pc->c[1] = 1;
1338 pc->idetape_callback = &idetape_rw_callback;
1339 pc->bh = bh;
1340 atomic_set(&bh->b_count, 0);
1341 pc->buf = NULL;
1342 pc->buf_size = length * tape->blk_size;
1343 pc->req_xfer = pc->buf_size;
1344 if (pc->req_xfer == tape->stage_size)
1345 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1346 }
1347
1348 static void idetape_create_write_cmd(idetape_tape_t *tape,
1349 struct ide_atapi_pc *pc,
1350 unsigned int length, struct idetape_bh *bh)
1351 {
1352 idetape_init_pc(pc);
1353 pc->c[0] = WRITE_6;
1354 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1355 pc->c[1] = 1;
1356 pc->idetape_callback = &idetape_rw_callback;
1357 pc->flags |= PC_FLAG_WRITING;
1358 pc->bh = bh;
1359 pc->b_data = bh->b_data;
1360 pc->b_count = atomic_read(&bh->b_count);
1361 pc->buf = NULL;
1362 pc->buf_size = length * tape->blk_size;
1363 pc->req_xfer = pc->buf_size;
1364 if (pc->req_xfer == tape->stage_size)
1365 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1366 }
1367
1368 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1369 struct request *rq, sector_t block)
1370 {
1371 idetape_tape_t *tape = drive->driver_data;
1372 struct ide_atapi_pc *pc = NULL;
1373 struct request *postponed_rq = tape->postponed_rq;
1374 u8 stat;
1375
1376 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1377 " current_nr_sectors: %d\n",
1378 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1379
1380 if (!blk_special_request(rq)) {
1381 /* We do not support buffer cache originated requests. */
1382 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1383 "request queue (%d)\n", drive->name, rq->cmd_type);
1384 ide_end_request(drive, 0, 0);
1385 return ide_stopped;
1386 }
1387
1388 /* Retry a failed packet command */
1389 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
1390 return idetape_issue_pc(drive, tape->failed_pc);
1391
1392 if (postponed_rq != NULL)
1393 if (rq != postponed_rq) {
1394 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1395 "Two DSC requests were queued\n");
1396 idetape_end_request(drive, 0, 0);
1397 return ide_stopped;
1398 }
1399
1400 tape->postponed_rq = NULL;
1401
1402 /*
1403 * If the tape is still busy, postpone our request and service
1404 * the other device meanwhile.
1405 */
1406 stat = ide_read_status(drive);
1407
1408 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1409 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1410
1411 if (drive->post_reset == 1) {
1412 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1413 drive->post_reset = 0;
1414 }
1415
1416 if (time_after(jiffies, tape->insert_time))
1417 tape->insert_speed = tape->insert_size / 1024 * HZ /
1418 (jiffies - tape->insert_time);
1419 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
1420 (stat & SEEK_STAT) == 0) {
1421 if (postponed_rq == NULL) {
1422 tape->dsc_polling_start = jiffies;
1423 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1424 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1425 } else if (time_after(jiffies, tape->dsc_timeout)) {
1426 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1427 tape->name);
1428 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1429 idetape_media_access_finished(drive);
1430 return ide_stopped;
1431 } else {
1432 return ide_do_reset(drive);
1433 }
1434 } else if (time_after(jiffies,
1435 tape->dsc_polling_start +
1436 IDETAPE_DSC_MA_THRESHOLD))
1437 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1438 idetape_postpone_request(drive);
1439 return ide_stopped;
1440 }
1441 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1442 tape->postpone_cnt = 0;
1443 pc = idetape_next_pc_storage(drive);
1444 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1445 (struct idetape_bh *)rq->special);
1446 goto out;
1447 }
1448 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1449 tape->postpone_cnt = 0;
1450 pc = idetape_next_pc_storage(drive);
1451 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1452 (struct idetape_bh *)rq->special);
1453 goto out;
1454 }
1455 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1456 pc = (struct ide_atapi_pc *) rq->buffer;
1457 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1458 rq->cmd[0] |= REQ_IDETAPE_PC2;
1459 goto out;
1460 }
1461 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1462 idetape_media_access_finished(drive);
1463 return ide_stopped;
1464 }
1465 BUG();
1466 out:
1467 return idetape_issue_pc(drive, pc);
1468 }
1469
1470 /* Pipeline related functions */
1471
1472 /*
1473 * The function below uses __get_free_page to allocate a pipeline stage, along
1474 * with all the necessary small buffers which together make a buffer of size
1475 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1476 * much as possible.
1477 *
1478 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1479 * don't want to) allocate a stage.
1480 *
1481 * Pipeline stages are optional and are used to increase performance. If we
1482 * can't allocate them, we'll manage without them.
1483 */
1484 static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1485 int clear)
1486 {
1487 idetape_stage_t *stage;
1488 struct idetape_bh *prev_bh, *bh;
1489 int pages = tape->pages_per_stage;
1490 char *b_data = NULL;
1491
1492 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1493 if (!stage)
1494 return NULL;
1495 stage->next = NULL;
1496
1497 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1498 bh = stage->bh;
1499 if (bh == NULL)
1500 goto abort;
1501 bh->b_reqnext = NULL;
1502 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1503 if (!bh->b_data)
1504 goto abort;
1505 if (clear)
1506 memset(bh->b_data, 0, PAGE_SIZE);
1507 bh->b_size = PAGE_SIZE;
1508 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1509
1510 while (--pages) {
1511 b_data = (char *) __get_free_page(GFP_KERNEL);
1512 if (!b_data)
1513 goto abort;
1514 if (clear)
1515 memset(b_data, 0, PAGE_SIZE);
1516 if (bh->b_data == b_data + PAGE_SIZE) {
1517 bh->b_size += PAGE_SIZE;
1518 bh->b_data -= PAGE_SIZE;
1519 if (full)
1520 atomic_add(PAGE_SIZE, &bh->b_count);
1521 continue;
1522 }
1523 if (b_data == bh->b_data + bh->b_size) {
1524 bh->b_size += PAGE_SIZE;
1525 if (full)
1526 atomic_add(PAGE_SIZE, &bh->b_count);
1527 continue;
1528 }
1529 prev_bh = bh;
1530 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1531 if (!bh) {
1532 free_page((unsigned long) b_data);
1533 goto abort;
1534 }
1535 bh->b_reqnext = NULL;
1536 bh->b_data = b_data;
1537 bh->b_size = PAGE_SIZE;
1538 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1539 prev_bh->b_reqnext = bh;
1540 }
1541 bh->b_size -= tape->excess_bh_size;
1542 if (full)
1543 atomic_sub(tape->excess_bh_size, &bh->b_count);
1544 return stage;
1545 abort:
1546 __idetape_kfree_stage(stage);
1547 return NULL;
1548 }
1549
1550 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1551 const char __user *buf, int n)
1552 {
1553 struct idetape_bh *bh = tape->bh;
1554 int count;
1555 int ret = 0;
1556
1557 while (n) {
1558 if (bh == NULL) {
1559 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1560 __func__);
1561 return 1;
1562 }
1563 count = min((unsigned int)
1564 (bh->b_size - atomic_read(&bh->b_count)),
1565 (unsigned int)n);
1566 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1567 count))
1568 ret = 1;
1569 n -= count;
1570 atomic_add(count, &bh->b_count);
1571 buf += count;
1572 if (atomic_read(&bh->b_count) == bh->b_size) {
1573 bh = bh->b_reqnext;
1574 if (bh)
1575 atomic_set(&bh->b_count, 0);
1576 }
1577 }
1578 tape->bh = bh;
1579 return ret;
1580 }
1581
1582 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1583 int n)
1584 {
1585 struct idetape_bh *bh = tape->bh;
1586 int count;
1587 int ret = 0;
1588
1589 while (n) {
1590 if (bh == NULL) {
1591 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1592 __func__);
1593 return 1;
1594 }
1595 count = min(tape->b_count, n);
1596 if (copy_to_user(buf, tape->b_data, count))
1597 ret = 1;
1598 n -= count;
1599 tape->b_data += count;
1600 tape->b_count -= count;
1601 buf += count;
1602 if (!tape->b_count) {
1603 bh = bh->b_reqnext;
1604 tape->bh = bh;
1605 if (bh) {
1606 tape->b_data = bh->b_data;
1607 tape->b_count = atomic_read(&bh->b_count);
1608 }
1609 }
1610 }
1611 return ret;
1612 }
1613
1614 static void idetape_init_merge_stage(idetape_tape_t *tape)
1615 {
1616 struct idetape_bh *bh = tape->merge_stage->bh;
1617
1618 tape->bh = bh;
1619 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1620 atomic_set(&bh->b_count, 0);
1621 else {
1622 tape->b_data = bh->b_data;
1623 tape->b_count = atomic_read(&bh->b_count);
1624 }
1625 }
1626
1627 /* Install a completion in a pending request and sleep until it is serviced. The
1628 * caller should ensure that the request will not be serviced before we install
1629 * the completion (usually by disabling interrupts).
1630 */
1631 static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
1632 {
1633 DECLARE_COMPLETION_ONSTACK(wait);
1634 idetape_tape_t *tape = drive->driver_data;
1635
1636 if (rq == NULL || !blk_special_request(rq)) {
1637 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1638 " request\n");
1639 return;
1640 }
1641 rq->end_io_data = &wait;
1642 rq->end_io = blk_end_sync_rq;
1643 spin_unlock_irq(&tape->lock);
1644 wait_for_completion(&wait);
1645 /* The stage and its struct request have been deallocated */
1646 spin_lock_irq(&tape->lock);
1647 }
1648
1649 static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
1650 {
1651 idetape_tape_t *tape = drive->driver_data;
1652 u8 *readpos = tape->pc->buf;
1653
1654 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1655
1656 if (!tape->pc->error) {
1657 debug_log(DBG_SENSE, "BOP - %s\n",
1658 (readpos[0] & 0x80) ? "Yes" : "No");
1659 debug_log(DBG_SENSE, "EOP - %s\n",
1660 (readpos[0] & 0x40) ? "Yes" : "No");
1661
1662 if (readpos[0] & 0x4) {
1663 printk(KERN_INFO "ide-tape: Block location is unknown"
1664 "to the tape\n");
1665 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1666 idetape_end_request(drive, 0, 0);
1667 } else {
1668 debug_log(DBG_SENSE, "Block Location - %u\n",
1669 be32_to_cpu(*(u32 *)&readpos[4]));
1670
1671 tape->partition = readpos[1];
1672 tape->first_frame =
1673 be32_to_cpu(*(u32 *)&readpos[4]);
1674 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1675 idetape_end_request(drive, 1, 0);
1676 }
1677 } else {
1678 idetape_end_request(drive, 0, 0);
1679 }
1680 return ide_stopped;
1681 }
1682
1683 /*
1684 * Write a filemark if write_filemark=1. Flush the device buffers without
1685 * writing a filemark otherwise.
1686 */
1687 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1688 struct ide_atapi_pc *pc, int write_filemark)
1689 {
1690 idetape_init_pc(pc);
1691 pc->c[0] = WRITE_FILEMARKS;
1692 pc->c[4] = write_filemark;
1693 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1694 pc->idetape_callback = &idetape_pc_callback;
1695 }
1696
1697 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1698 {
1699 idetape_init_pc(pc);
1700 pc->c[0] = TEST_UNIT_READY;
1701 pc->idetape_callback = &idetape_pc_callback;
1702 }
1703
1704 /*
1705 * We add a special packet command request to the tail of the request queue, and
1706 * wait for it to be serviced. This is not to be called from within the request
1707 * handling part of the driver! We allocate here data on the stack and it is
1708 * valid until the request is finished. This is not the case for the bottom part
1709 * of the driver, where we are always leaving the functions to wait for an
1710 * interrupt or a timer event.
1711 *
1712 * From the bottom part of the driver, we should allocate safe memory using
1713 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1714 * to the request list without waiting for it to be serviced! In that case, we
1715 * usually use idetape_queue_pc_head().
1716 */
1717 static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1718 {
1719 struct ide_tape_obj *tape = drive->driver_data;
1720 struct request rq;
1721
1722 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1723 rq.buffer = (char *) pc;
1724 rq.rq_disk = tape->disk;
1725 return ide_do_drive_cmd(drive, &rq, ide_wait);
1726 }
1727
1728 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1729 struct ide_atapi_pc *pc, int cmd)
1730 {
1731 idetape_init_pc(pc);
1732 pc->c[0] = START_STOP;
1733 pc->c[4] = cmd;
1734 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1735 pc->idetape_callback = &idetape_pc_callback;
1736 }
1737
1738 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1739 {
1740 idetape_tape_t *tape = drive->driver_data;
1741 struct ide_atapi_pc pc;
1742 int load_attempted = 0;
1743
1744 /* Wait for the tape to become ready */
1745 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
1746 timeout += jiffies;
1747 while (time_before(jiffies, timeout)) {
1748 idetape_create_test_unit_ready_cmd(&pc);
1749 if (!__idetape_queue_pc_tail(drive, &pc))
1750 return 0;
1751 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1752 || (tape->asc == 0x3A)) {
1753 /* no media */
1754 if (load_attempted)
1755 return -ENOMEDIUM;
1756 idetape_create_load_unload_cmd(drive, &pc,
1757 IDETAPE_LU_LOAD_MASK);
1758 __idetape_queue_pc_tail(drive, &pc);
1759 load_attempted = 1;
1760 /* not about to be ready */
1761 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1762 (tape->ascq == 1 || tape->ascq == 8)))
1763 return -EIO;
1764 msleep(100);
1765 }
1766 return -EIO;
1767 }
1768
1769 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1770 {
1771 return __idetape_queue_pc_tail(drive, pc);
1772 }
1773
1774 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1775 {
1776 struct ide_atapi_pc pc;
1777 int rc;
1778
1779 idetape_create_write_filemark_cmd(drive, &pc, 0);
1780 rc = idetape_queue_pc_tail(drive, &pc);
1781 if (rc)
1782 return rc;
1783 idetape_wait_ready(drive, 60 * 5 * HZ);
1784 return 0;
1785 }
1786
1787 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1788 {
1789 idetape_init_pc(pc);
1790 pc->c[0] = READ_POSITION;
1791 pc->req_xfer = 20;
1792 pc->idetape_callback = &idetape_read_position_callback;
1793 }
1794
1795 static int idetape_read_position(ide_drive_t *drive)
1796 {
1797 idetape_tape_t *tape = drive->driver_data;
1798 struct ide_atapi_pc pc;
1799 int position;
1800
1801 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1802
1803 idetape_create_read_position_cmd(&pc);
1804 if (idetape_queue_pc_tail(drive, &pc))
1805 return -1;
1806 position = tape->first_frame;
1807 return position;
1808 }
1809
1810 static void idetape_create_locate_cmd(ide_drive_t *drive,
1811 struct ide_atapi_pc *pc,
1812 unsigned int block, u8 partition, int skip)
1813 {
1814 idetape_init_pc(pc);
1815 pc->c[0] = POSITION_TO_ELEMENT;
1816 pc->c[1] = 2;
1817 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1818 pc->c[8] = partition;
1819 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1820 pc->idetape_callback = &idetape_pc_callback;
1821 }
1822
1823 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1824 struct ide_atapi_pc *pc, int prevent)
1825 {
1826 idetape_tape_t *tape = drive->driver_data;
1827
1828 /* device supports locking according to capabilities page */
1829 if (!(tape->caps[6] & 0x01))
1830 return 0;
1831
1832 idetape_init_pc(pc);
1833 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1834 pc->c[4] = prevent;
1835 pc->idetape_callback = &idetape_pc_callback;
1836 return 1;
1837 }
1838
1839 static int __idetape_discard_read_pipeline(ide_drive_t *drive)
1840 {
1841 idetape_tape_t *tape = drive->driver_data;
1842 unsigned long flags;
1843 int cnt;
1844
1845 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1846 return 0;
1847
1848 /* Remove merge stage. */
1849 cnt = tape->merge_stage_size / tape->blk_size;
1850 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
1851 ++cnt; /* Filemarks count as 1 sector */
1852 tape->merge_stage_size = 0;
1853 if (tape->merge_stage != NULL) {
1854 __idetape_kfree_stage(tape->merge_stage);
1855 tape->merge_stage = NULL;
1856 }
1857
1858 /* Clear pipeline flags. */
1859 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
1860 tape->chrdev_dir = IDETAPE_DIR_NONE;
1861
1862 /* Remove pipeline stages. */
1863 if (tape->first_stage == NULL)
1864 return 0;
1865
1866 spin_lock_irqsave(&tape->lock, flags);
1867 tape->next_stage = NULL;
1868 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
1869 idetape_wait_for_request(drive, tape->active_data_rq);
1870 spin_unlock_irqrestore(&tape->lock, flags);
1871
1872 while (tape->first_stage != NULL) {
1873 struct request *rq_ptr = &tape->first_stage->rq;
1874
1875 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
1876 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1877 ++cnt;
1878 }
1879 tape->nr_pending_stages = 0;
1880 tape->max_stages = tape->min_pipeline;
1881 return cnt;
1882 }
1883
1884 /*
1885 * Position the tape to the requested block using the LOCATE packet command.
1886 * A READ POSITION command is then issued to check where we are positioned. Like
1887 * all higher level operations, we queue the commands at the tail of the request
1888 * queue and wait for their completion.
1889 */
1890 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1891 u8 partition, int skip)
1892 {
1893 idetape_tape_t *tape = drive->driver_data;
1894 int retval;
1895 struct ide_atapi_pc pc;
1896
1897 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1898 __idetape_discard_read_pipeline(drive);
1899 idetape_wait_ready(drive, 60 * 5 * HZ);
1900 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1901 retval = idetape_queue_pc_tail(drive, &pc);
1902 if (retval)
1903 return (retval);
1904
1905 idetape_create_read_position_cmd(&pc);
1906 return (idetape_queue_pc_tail(drive, &pc));
1907 }
1908
1909 static void idetape_discard_read_pipeline(ide_drive_t *drive,
1910 int restore_position)
1911 {
1912 idetape_tape_t *tape = drive->driver_data;
1913 int cnt;
1914 int seek, position;
1915
1916 cnt = __idetape_discard_read_pipeline(drive);
1917 if (restore_position) {
1918 position = idetape_read_position(drive);
1919 seek = position > cnt ? position - cnt : 0;
1920 if (idetape_position_tape(drive, seek, 0, 0)) {
1921 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1922 " discard_pipeline()\n", tape->name);
1923 return;
1924 }
1925 }
1926 }
1927
1928 /*
1929 * Generate a read/write request for the block device interface and wait for it
1930 * to be serviced.
1931 */
1932 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1933 struct idetape_bh *bh)
1934 {
1935 idetape_tape_t *tape = drive->driver_data;
1936 struct request rq;
1937
1938 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1939
1940 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
1941 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
1942 __func__);
1943 return (0);
1944 }
1945
1946 idetape_init_rq(&rq, cmd);
1947 rq.rq_disk = tape->disk;
1948 rq.special = (void *)bh;
1949 rq.sector = tape->first_frame;
1950 rq.nr_sectors = blocks;
1951 rq.current_nr_sectors = blocks;
1952 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1953
1954 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1955 return 0;
1956
1957 if (tape->merge_stage)
1958 idetape_init_merge_stage(tape);
1959 if (rq.errors == IDETAPE_ERROR_GENERAL)
1960 return -EIO;
1961 return (tape->blk_size * (blocks-rq.current_nr_sectors));
1962 }
1963
1964 /* start servicing the pipeline stages, starting from tape->next_stage. */
1965 static void idetape_plug_pipeline(ide_drive_t *drive)
1966 {
1967 idetape_tape_t *tape = drive->driver_data;
1968
1969 if (tape->next_stage == NULL)
1970 return;
1971 if (!test_and_set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
1972 idetape_activate_next_stage(drive);
1973 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
1974 }
1975 }
1976
1977 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1978 {
1979 idetape_init_pc(pc);
1980 pc->c[0] = INQUIRY;
1981 pc->c[4] = 254;
1982 pc->req_xfer = 254;
1983 pc->idetape_callback = &idetape_pc_callback;
1984 }
1985
1986 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1987 struct ide_atapi_pc *pc)
1988 {
1989 idetape_init_pc(pc);
1990 pc->c[0] = REZERO_UNIT;
1991 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1992 pc->idetape_callback = &idetape_pc_callback;
1993 }
1994
1995 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1996 {
1997 idetape_init_pc(pc);
1998 pc->c[0] = ERASE;
1999 pc->c[1] = 1;
2000 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
2001 pc->idetape_callback = &idetape_pc_callback;
2002 }
2003
2004 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
2005 {
2006 idetape_init_pc(pc);
2007 pc->c[0] = SPACE;
2008 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
2009 pc->c[1] = cmd;
2010 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
2011 pc->idetape_callback = &idetape_pc_callback;
2012 }
2013
2014 /* Queue up a character device originated write request. */
2015 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
2016 {
2017 idetape_tape_t *tape = drive->driver_data;
2018 unsigned long flags;
2019
2020 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2021
2022 /* Attempt to allocate a new stage. Beware possible race conditions. */
2023 while (1) {
2024 spin_lock_irqsave(&tape->lock, flags);
2025 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
2026 idetape_wait_for_request(drive, tape->active_data_rq);
2027 spin_unlock_irqrestore(&tape->lock, flags);
2028 } else {
2029 spin_unlock_irqrestore(&tape->lock, flags);
2030 idetape_plug_pipeline(drive);
2031 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2032 &tape->flags))
2033 continue;
2034 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2035 blocks, tape->merge_stage->bh);
2036 }
2037 }
2038 }
2039
2040 /*
2041 * Wait until all pending pipeline requests are serviced. Typically called on
2042 * device close.
2043 */
2044 static void idetape_wait_for_pipeline(ide_drive_t *drive)
2045 {
2046 idetape_tape_t *tape = drive->driver_data;
2047 unsigned long flags;
2048
2049 while (tape->next_stage || test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2050 &tape->flags)) {
2051 idetape_plug_pipeline(drive);
2052 spin_lock_irqsave(&tape->lock, flags);
2053 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
2054 idetape_wait_for_request(drive, tape->active_data_rq);
2055 spin_unlock_irqrestore(&tape->lock, flags);
2056 }
2057 }
2058
2059 static void idetape_empty_write_pipeline(ide_drive_t *drive)
2060 {
2061 idetape_tape_t *tape = drive->driver_data;
2062 int blocks, min;
2063 struct idetape_bh *bh;
2064
2065 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2066 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2067 " but we are not writing.\n");
2068 return;
2069 }
2070 if (tape->merge_stage_size > tape->stage_size) {
2071 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2072 tape->merge_stage_size = tape->stage_size;
2073 }
2074 if (tape->merge_stage_size) {
2075 blocks = tape->merge_stage_size / tape->blk_size;
2076 if (tape->merge_stage_size % tape->blk_size) {
2077 unsigned int i;
2078
2079 blocks++;
2080 i = tape->blk_size - tape->merge_stage_size %
2081 tape->blk_size;
2082 bh = tape->bh->b_reqnext;
2083 while (bh) {
2084 atomic_set(&bh->b_count, 0);
2085 bh = bh->b_reqnext;
2086 }
2087 bh = tape->bh;
2088 while (i) {
2089 if (bh == NULL) {
2090 printk(KERN_INFO "ide-tape: bug,"
2091 " bh NULL\n");
2092 break;
2093 }
2094 min = min(i, (unsigned int)(bh->b_size -
2095 atomic_read(&bh->b_count)));
2096 memset(bh->b_data + atomic_read(&bh->b_count),
2097 0, min);
2098 atomic_add(min, &bh->b_count);
2099 i -= min;
2100 bh = bh->b_reqnext;
2101 }
2102 }
2103 (void) idetape_add_chrdev_write_request(drive, blocks);
2104 tape->merge_stage_size = 0;
2105 }
2106 idetape_wait_for_pipeline(drive);
2107 if (tape->merge_stage != NULL) {
2108 __idetape_kfree_stage(tape->merge_stage);
2109 tape->merge_stage = NULL;
2110 }
2111 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
2112 tape->chrdev_dir = IDETAPE_DIR_NONE;
2113
2114 /*
2115 * On the next backup, perform the feedback loop again. (I don't want to
2116 * keep sense information between backups, as some systems are
2117 * constantly on, and the system load can be totally different on the
2118 * next backup).
2119 */
2120 tape->max_stages = tape->min_pipeline;
2121 if (tape->first_stage != NULL ||
2122 tape->next_stage != NULL ||
2123 tape->last_stage != NULL ||
2124 tape->nr_stages != 0) {
2125 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2126 "first_stage %p, next_stage %p, "
2127 "last_stage %p, nr_stages %d\n",
2128 tape->first_stage, tape->next_stage,
2129 tape->last_stage, tape->nr_stages);
2130 }
2131 }
2132
2133 static int idetape_init_read(ide_drive_t *drive, int max_stages)
2134 {
2135 idetape_tape_t *tape = drive->driver_data;
2136 int bytes_read;
2137
2138 /* Initialize read operation */
2139 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2140 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2141 idetape_empty_write_pipeline(drive);
2142 idetape_flush_tape_buffers(drive);
2143 }
2144 if (tape->merge_stage || tape->merge_stage_size) {
2145 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2146 " 0 now\n");
2147 tape->merge_stage_size = 0;
2148 }
2149 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2150 if (!tape->merge_stage)
2151 return -ENOMEM;
2152 tape->chrdev_dir = IDETAPE_DIR_READ;
2153
2154 /*
2155 * Issue a read 0 command to ensure that DSC handshake is
2156 * switched from completion mode to buffer available mode.
2157 * No point in issuing this if DSC overlap isn't supported, some
2158 * drives (Seagate STT3401A) will return an error.
2159 */
2160 if (drive->dsc_overlap) {
2161 bytes_read = idetape_queue_rw_tail(drive,
2162 REQ_IDETAPE_READ, 0,
2163 tape->merge_stage->bh);
2164 if (bytes_read < 0) {
2165 __idetape_kfree_stage(tape->merge_stage);
2166 tape->merge_stage = NULL;
2167 tape->chrdev_dir = IDETAPE_DIR_NONE;
2168 return bytes_read;
2169 }
2170 }
2171 }
2172
2173 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
2174 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2175 tape->measure_insert_time = 1;
2176 tape->insert_time = jiffies;
2177 tape->insert_size = 0;
2178 tape->insert_speed = 0;
2179 idetape_plug_pipeline(drive);
2180 }
2181 }
2182 return 0;
2183 }
2184
2185 /*
2186 * Called from idetape_chrdev_read() to service a character device read request
2187 * and add read-ahead requests to our pipeline.
2188 */
2189 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
2190 {
2191 idetape_tape_t *tape = drive->driver_data;
2192
2193 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
2194
2195 /* If we are at a filemark, return a read length of 0 */
2196 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
2197 return 0;
2198
2199 idetape_init_read(drive, tape->max_stages);
2200
2201 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
2202 return 0;
2203
2204 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2205 tape->merge_stage->bh);
2206 }
2207
2208 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
2209 {
2210 idetape_tape_t *tape = drive->driver_data;
2211 struct idetape_bh *bh;
2212 int blocks;
2213
2214 while (bcount) {
2215 unsigned int count;
2216
2217 bh = tape->merge_stage->bh;
2218 count = min(tape->stage_size, bcount);
2219 bcount -= count;
2220 blocks = count / tape->blk_size;
2221 while (count) {
2222 atomic_set(&bh->b_count,
2223 min(count, (unsigned int)bh->b_size));
2224 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2225 count -= atomic_read(&bh->b_count);
2226 bh = bh->b_reqnext;
2227 }
2228 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2229 tape->merge_stage->bh);
2230 }
2231 }
2232
2233 /*
2234 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2235 * currently support only one partition.
2236 */
2237 static int idetape_rewind_tape(ide_drive_t *drive)
2238 {
2239 int retval;
2240 struct ide_atapi_pc pc;
2241 idetape_tape_t *tape;
2242 tape = drive->driver_data;
2243
2244 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2245
2246 idetape_create_rewind_cmd(drive, &pc);
2247 retval = idetape_queue_pc_tail(drive, &pc);
2248 if (retval)
2249 return retval;
2250
2251 idetape_create_read_position_cmd(&pc);
2252 retval = idetape_queue_pc_tail(drive, &pc);
2253 if (retval)
2254 return retval;
2255 return 0;
2256 }
2257
2258 /* mtio.h compatible commands should be issued to the chrdev interface. */
2259 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2260 unsigned long arg)
2261 {
2262 idetape_tape_t *tape = drive->driver_data;
2263 void __user *argp = (void __user *)arg;
2264
2265 struct idetape_config {
2266 int dsc_rw_frequency;
2267 int dsc_media_access_frequency;
2268 int nr_stages;
2269 } config;
2270
2271 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2272
2273 switch (cmd) {
2274 case 0x0340:
2275 if (copy_from_user(&config, argp, sizeof(config)))
2276 return -EFAULT;
2277 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2278 tape->max_stages = config.nr_stages;
2279 break;
2280 case 0x0350:
2281 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2282 config.nr_stages = tape->max_stages;
2283 if (copy_to_user(argp, &config, sizeof(config)))
2284 return -EFAULT;
2285 break;
2286 default:
2287 return -EIO;
2288 }
2289 return 0;
2290 }
2291
2292 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2293 int mt_count)
2294 {
2295 idetape_tape_t *tape = drive->driver_data;
2296 struct ide_atapi_pc pc;
2297 int retval, count = 0;
2298 int sprev = !!(tape->caps[4] & 0x20);
2299
2300 if (mt_count == 0)
2301 return 0;
2302 if (MTBSF == mt_op || MTBSFM == mt_op) {
2303 if (!sprev)
2304 return -EIO;
2305 mt_count = -mt_count;
2306 }
2307
2308 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2309 tape->merge_stage_size = 0;
2310 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
2311 ++count;
2312 idetape_discard_read_pipeline(drive, 0);
2313 }
2314
2315 /*
2316 * The filemark was not found in our internal pipeline; now we can issue
2317 * the space command.
2318 */
2319 switch (mt_op) {
2320 case MTFSF:
2321 case MTBSF:
2322 idetape_create_space_cmd(&pc, mt_count - count,
2323 IDETAPE_SPACE_OVER_FILEMARK);
2324 return idetape_queue_pc_tail(drive, &pc);
2325 case MTFSFM:
2326 case MTBSFM:
2327 if (!sprev)
2328 return -EIO;
2329 retval = idetape_space_over_filemarks(drive, MTFSF,
2330 mt_count - count);
2331 if (retval)
2332 return retval;
2333 count = (MTBSFM == mt_op ? 1 : -1);
2334 return idetape_space_over_filemarks(drive, MTFSF, count);
2335 default:
2336 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2337 mt_op);
2338 return -EIO;
2339 }
2340 }
2341
2342 /*
2343 * Our character device read / write functions.
2344 *
2345 * The tape is optimized to maximize throughput when it is transferring an
2346 * integral number of the "continuous transfer limit", which is a parameter of
2347 * the specific tape (26kB on my particular tape, 32kB for Onstream).
2348 *
2349 * As of version 1.3 of the driver, the character device provides an abstract
2350 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2351 * same backup/restore procedure is supported. The driver will internally
2352 * convert the requests to the recommended transfer unit, so that an unmatch
2353 * between the user's block size to the recommended size will only result in a
2354 * (slightly) increased driver overhead, but will no longer hit performance.
2355 * This is not applicable to Onstream.
2356 */
2357 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2358 size_t count, loff_t *ppos)
2359 {
2360 struct ide_tape_obj *tape = ide_tape_f(file);
2361 ide_drive_t *drive = tape->drive;
2362 ssize_t bytes_read, temp, actually_read = 0, rc;
2363 ssize_t ret = 0;
2364 u16 ctl = *(u16 *)&tape->caps[12];
2365
2366 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
2367
2368 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2369 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
2370 if (count > tape->blk_size &&
2371 (count % tape->blk_size) == 0)
2372 tape->user_bs_factor = count / tape->blk_size;
2373 }
2374 rc = idetape_init_read(drive, tape->max_stages);
2375 if (rc < 0)
2376 return rc;
2377 if (count == 0)
2378 return (0);
2379 if (tape->merge_stage_size) {
2380 actually_read = min((unsigned int)(tape->merge_stage_size),
2381 (unsigned int)count);
2382 if (idetape_copy_stage_to_user(tape, buf, actually_read))
2383 ret = -EFAULT;
2384 buf += actually_read;
2385 tape->merge_stage_size -= actually_read;
2386 count -= actually_read;
2387 }
2388 while (count >= tape->stage_size) {
2389 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
2390 if (bytes_read <= 0)
2391 goto finish;
2392 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
2393 ret = -EFAULT;
2394 buf += bytes_read;
2395 count -= bytes_read;
2396 actually_read += bytes_read;
2397 }
2398 if (count) {
2399 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
2400 if (bytes_read <= 0)
2401 goto finish;
2402 temp = min((unsigned long)count, (unsigned long)bytes_read);
2403 if (idetape_copy_stage_to_user(tape, buf, temp))
2404 ret = -EFAULT;
2405 actually_read += temp;
2406 tape->merge_stage_size = bytes_read-temp;
2407 }
2408 finish:
2409 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
2410 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2411
2412 idetape_space_over_filemarks(drive, MTFSF, 1);
2413 return 0;
2414 }
2415
2416 return ret ? ret : actually_read;
2417 }
2418
2419 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
2420 size_t count, loff_t *ppos)
2421 {
2422 struct ide_tape_obj *tape = ide_tape_f(file);
2423 ide_drive_t *drive = tape->drive;
2424 ssize_t actually_written = 0;
2425 ssize_t ret = 0;
2426 u16 ctl = *(u16 *)&tape->caps[12];
2427
2428 /* The drive is write protected. */
2429 if (tape->write_prot)
2430 return -EACCES;
2431
2432 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
2433
2434 /* Initialize write operation */
2435 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2436 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2437 idetape_discard_read_pipeline(drive, 1);
2438 if (tape->merge_stage || tape->merge_stage_size) {
2439 printk(KERN_ERR "ide-tape: merge_stage_size "
2440 "should be 0 now\n");
2441 tape->merge_stage_size = 0;
2442 }
2443 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2444 if (!tape->merge_stage)
2445 return -ENOMEM;
2446 tape->chrdev_dir = IDETAPE_DIR_WRITE;
2447 idetape_init_merge_stage(tape);
2448
2449 /*
2450 * Issue a write 0 command to ensure that DSC handshake is
2451 * switched from completion mode to buffer available mode. No
2452 * point in issuing this if DSC overlap isn't supported, some
2453 * drives (Seagate STT3401A) will return an error.
2454 */
2455 if (drive->dsc_overlap) {
2456 ssize_t retval = idetape_queue_rw_tail(drive,
2457 REQ_IDETAPE_WRITE, 0,
2458 tape->merge_stage->bh);
2459 if (retval < 0) {
2460 __idetape_kfree_stage(tape->merge_stage);
2461 tape->merge_stage = NULL;
2462 tape->chrdev_dir = IDETAPE_DIR_NONE;
2463 return retval;
2464 }
2465 }
2466 }
2467 if (count == 0)
2468 return (0);
2469 if (tape->merge_stage_size) {
2470 if (tape->merge_stage_size >= tape->stage_size) {
2471 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
2472 tape->merge_stage_size = 0;
2473 }
2474 actually_written = min((unsigned int)
2475 (tape->stage_size - tape->merge_stage_size),
2476 (unsigned int)count);
2477 if (idetape_copy_stage_from_user(tape, buf, actually_written))
2478 ret = -EFAULT;
2479 buf += actually_written;
2480 tape->merge_stage_size += actually_written;
2481 count -= actually_written;
2482
2483 if (tape->merge_stage_size == tape->stage_size) {
2484 ssize_t retval;
2485 tape->merge_stage_size = 0;
2486 retval = idetape_add_chrdev_write_request(drive, ctl);
2487 if (retval <= 0)
2488 return (retval);
2489 }
2490 }
2491 while (count >= tape->stage_size) {
2492 ssize_t retval;
2493 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
2494 ret = -EFAULT;
2495 buf += tape->stage_size;
2496 count -= tape->stage_size;
2497 retval = idetape_add_chrdev_write_request(drive, ctl);
2498 actually_written += tape->stage_size;
2499 if (retval <= 0)
2500 return (retval);
2501 }
2502 if (count) {
2503 actually_written += count;
2504 if (idetape_copy_stage_from_user(tape, buf, count))
2505 ret = -EFAULT;
2506 tape->merge_stage_size += count;
2507 }
2508 return ret ? ret : actually_written;
2509 }
2510
2511 static int idetape_write_filemark(ide_drive_t *drive)
2512 {
2513 struct ide_atapi_pc pc;
2514
2515 /* Write a filemark */
2516 idetape_create_write_filemark_cmd(drive, &pc, 1);
2517 if (idetape_queue_pc_tail(drive, &pc)) {
2518 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2519 return -EIO;
2520 }
2521 return 0;
2522 }
2523
2524 /*
2525 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2526 * requested.
2527 *
2528 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2529 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2530 * usually not supported (it is supported in the rare case in which we crossed
2531 * the filemark during our read-ahead pipelined operation mode).
2532 *
2533 * The following commands are currently not supported:
2534 *
2535 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2536 * MT_ST_WRITE_THRESHOLD.
2537 */
2538 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
2539 {
2540 idetape_tape_t *tape = drive->driver_data;
2541 struct ide_atapi_pc pc;
2542 int i, retval;
2543
2544 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2545 mt_op, mt_count);
2546
2547 /* Commands which need our pipelined read-ahead stages. */
2548 switch (mt_op) {
2549 case MTFSF:
2550 case MTFSFM:
2551 case MTBSF:
2552 case MTBSFM:
2553 if (!mt_count)
2554 return 0;
2555 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2556 default:
2557 break;
2558 }
2559
2560 switch (mt_op) {
2561 case MTWEOF:
2562 if (tape->write_prot)
2563 return -EACCES;
2564 idetape_discard_read_pipeline(drive, 1);
2565 for (i = 0; i < mt_count; i++) {
2566 retval = idetape_write_filemark(drive);
2567 if (retval)
2568 return retval;
2569 }
2570 return 0;
2571 case MTREW:
2572 idetape_discard_read_pipeline(drive, 0);
2573 if (idetape_rewind_tape(drive))
2574 return -EIO;
2575 return 0;
2576 case MTLOAD:
2577 idetape_discard_read_pipeline(drive, 0);
2578 idetape_create_load_unload_cmd(drive, &pc,
2579 IDETAPE_LU_LOAD_MASK);
2580 return idetape_queue_pc_tail(drive, &pc);
2581 case MTUNLOAD:
2582 case MTOFFL:
2583 /*
2584 * If door is locked, attempt to unlock before
2585 * attempting to eject.
2586 */
2587 if (tape->door_locked) {
2588 if (idetape_create_prevent_cmd(drive, &pc, 0))
2589 if (!idetape_queue_pc_tail(drive, &pc))
2590 tape->door_locked = DOOR_UNLOCKED;
2591 }
2592 idetape_discard_read_pipeline(drive, 0);
2593 idetape_create_load_unload_cmd(drive, &pc,
2594 !IDETAPE_LU_LOAD_MASK);
2595 retval = idetape_queue_pc_tail(drive, &pc);
2596 if (!retval)
2597 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
2598 return retval;
2599 case MTNOP:
2600 idetape_discard_read_pipeline(drive, 0);
2601 return idetape_flush_tape_buffers(drive);
2602 case MTRETEN:
2603 idetape_discard_read_pipeline(drive, 0);
2604 idetape_create_load_unload_cmd(drive, &pc,
2605 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2606 return idetape_queue_pc_tail(drive, &pc);
2607 case MTEOM:
2608 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2609 return idetape_queue_pc_tail(drive, &pc);
2610 case MTERASE:
2611 (void)idetape_rewind_tape(drive);
2612 idetape_create_erase_cmd(&pc);
2613 return idetape_queue_pc_tail(drive, &pc);
2614 case MTSETBLK:
2615 if (mt_count) {
2616 if (mt_count < tape->blk_size ||
2617 mt_count % tape->blk_size)
2618 return -EIO;
2619 tape->user_bs_factor = mt_count / tape->blk_size;
2620 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
2621 } else
2622 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
2623 return 0;
2624 case MTSEEK:
2625 idetape_discard_read_pipeline(drive, 0);
2626 return idetape_position_tape(drive,
2627 mt_count * tape->user_bs_factor, tape->partition, 0);
2628 case MTSETPART:
2629 idetape_discard_read_pipeline(drive, 0);
2630 return idetape_position_tape(drive, 0, mt_count, 0);
2631 case MTFSR:
2632 case MTBSR:
2633 case MTLOCK:
2634 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2635 return 0;
2636 retval = idetape_queue_pc_tail(drive, &pc);
2637 if (retval)
2638 return retval;
2639 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2640 return 0;
2641 case MTUNLOCK:
2642 if (!idetape_create_prevent_cmd(drive, &pc, 0))
2643 return 0;
2644 retval = idetape_queue_pc_tail(drive, &pc);
2645 if (retval)
2646 return retval;
2647 tape->door_locked = DOOR_UNLOCKED;
2648 return 0;
2649 default:
2650 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2651 mt_op);
2652 return -EIO;
2653 }
2654 }
2655
2656 /*
2657 * Our character device ioctls. General mtio.h magnetic io commands are
2658 * supported here, and not in the corresponding block interface. Our own
2659 * ide-tape ioctls are supported on both interfaces.
2660 */
2661 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2662 unsigned int cmd, unsigned long arg)
2663 {
2664 struct ide_tape_obj *tape = ide_tape_f(file);
2665 ide_drive_t *drive = tape->drive;
2666 struct mtop mtop;
2667 struct mtget mtget;
2668 struct mtpos mtpos;
2669 int block_offset = 0, position = tape->first_frame;
2670 void __user *argp = (void __user *)arg;
2671
2672 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2673
2674 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2675 idetape_empty_write_pipeline(drive);
2676 idetape_flush_tape_buffers(drive);
2677 }
2678 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2679 idetape_wait_for_pipeline(drive);
2680 block_offset = tape->merge_stage_size /
2681 (tape->blk_size * tape->user_bs_factor);
2682 position = idetape_read_position(drive);
2683 if (position < 0)
2684 return -EIO;
2685 }
2686 switch (cmd) {
2687 case MTIOCTOP:
2688 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2689 return -EFAULT;
2690 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2691 case MTIOCGET:
2692 memset(&mtget, 0, sizeof(struct mtget));
2693 mtget.mt_type = MT_ISSCSI2;
2694 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2695 mtget.mt_dsreg =
2696 ((tape->blk_size * tape->user_bs_factor)
2697 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2698
2699 if (tape->drv_write_prot)
2700 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2701
2702 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2703 return -EFAULT;
2704 return 0;
2705 case MTIOCPOS:
2706 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2707 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2708 return -EFAULT;
2709 return 0;
2710 default:
2711 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2712 idetape_discard_read_pipeline(drive, 1);
2713 return idetape_blkdev_ioctl(drive, cmd, arg);
2714 }
2715 }
2716
2717 /*
2718 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2719 * block size with the reported value.
2720 */
2721 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2722 {
2723 idetape_tape_t *tape = drive->driver_data;
2724 struct ide_atapi_pc pc;
2725
2726 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2727 if (idetape_queue_pc_tail(drive, &pc)) {
2728 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2729 if (tape->blk_size == 0) {
2730 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2731 "block size, assuming 32k\n");
2732 tape->blk_size = 32768;
2733 }
2734 return;
2735 }
2736 tape->blk_size = (pc.buf[4 + 5] << 16) +
2737 (pc.buf[4 + 6] << 8) +
2738 pc.buf[4 + 7];
2739 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2740 }
2741
2742 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2743 {
2744 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2745 ide_drive_t *drive;
2746 idetape_tape_t *tape;
2747 struct ide_atapi_pc pc;
2748 int retval;
2749
2750 if (i >= MAX_HWIFS * MAX_DRIVES)
2751 return -ENXIO;
2752
2753 tape = ide_tape_chrdev_get(i);
2754 if (!tape)
2755 return -ENXIO;
2756
2757 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2758
2759 /*
2760 * We really want to do nonseekable_open(inode, filp); here, but some
2761 * versions of tar incorrectly call lseek on tapes and bail out if that
2762 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2763 */
2764 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2765
2766 drive = tape->drive;
2767
2768 filp->private_data = tape;
2769
2770 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
2771 retval = -EBUSY;
2772 goto out_put_tape;
2773 }
2774
2775 retval = idetape_wait_ready(drive, 60 * HZ);
2776 if (retval) {
2777 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
2778 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2779 goto out_put_tape;
2780 }
2781
2782 idetape_read_position(drive);
2783 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
2784 (void)idetape_rewind_tape(drive);
2785
2786 if (tape->chrdev_dir != IDETAPE_DIR_READ)
2787 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
2788
2789 /* Read block size and write protect status from drive. */
2790 ide_tape_get_bsize_from_bdesc(drive);
2791
2792 /* Set write protect flag if device is opened as read-only. */
2793 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2794 tape->write_prot = 1;
2795 else
2796 tape->write_prot = tape->drv_write_prot;
2797
2798 /* Make sure drive isn't write protected if user wants to write. */
2799 if (tape->write_prot) {
2800 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2801 (filp->f_flags & O_ACCMODE) == O_RDWR) {
2802 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
2803 retval = -EROFS;
2804 goto out_put_tape;
2805 }
2806 }
2807
2808 /* Lock the tape drive door so user can't eject. */
2809 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2810 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2811 if (!idetape_queue_pc_tail(drive, &pc)) {
2812 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2813 tape->door_locked = DOOR_LOCKED;
2814 }
2815 }
2816 }
2817 return 0;
2818
2819 out_put_tape:
2820 ide_tape_put(tape);
2821 return retval;
2822 }
2823
2824 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2825 {
2826 idetape_tape_t *tape = drive->driver_data;
2827
2828 idetape_empty_write_pipeline(drive);
2829 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2830 if (tape->merge_stage != NULL) {
2831 idetape_pad_zeros(drive, tape->blk_size *
2832 (tape->user_bs_factor - 1));
2833 __idetape_kfree_stage(tape->merge_stage);
2834 tape->merge_stage = NULL;
2835 }
2836 idetape_write_filemark(drive);
2837 idetape_flush_tape_buffers(drive);
2838 idetape_flush_tape_buffers(drive);
2839 }
2840
2841 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2842 {
2843 struct ide_tape_obj *tape = ide_tape_f(filp);
2844 ide_drive_t *drive = tape->drive;
2845 struct ide_atapi_pc pc;
2846 unsigned int minor = iminor(inode);
2847
2848 lock_kernel();
2849 tape = drive->driver_data;
2850
2851 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2852
2853 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2854 idetape_write_release(drive, minor);
2855 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2856 if (minor < 128)
2857 idetape_discard_read_pipeline(drive, 1);
2858 else
2859 idetape_wait_for_pipeline(drive);
2860 }
2861
2862 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
2863 (void) idetape_rewind_tape(drive);
2864 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2865 if (tape->door_locked == DOOR_LOCKED) {
2866 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2867 if (!idetape_queue_pc_tail(drive, &pc))
2868 tape->door_locked = DOOR_UNLOCKED;
2869 }
2870 }
2871 }
2872 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
2873 ide_tape_put(tape);
2874 unlock_kernel();
2875 return 0;
2876 }
2877
2878 /*
2879 * check the contents of the ATAPI IDENTIFY command results. We return:
2880 *
2881 * 1 - If the tape can be supported by us, based on the information we have so
2882 * far.
2883 *
2884 * 0 - If this tape driver is not currently supported by us.
2885 */
2886 static int idetape_identify_device(ide_drive_t *drive)
2887 {
2888 u8 gcw[2], protocol, device_type, removable, packet_size;
2889
2890 if (drive->id_read == 0)
2891 return 1;
2892
2893 *((unsigned short *) &gcw) = drive->id->config;
2894
2895 protocol = (gcw[1] & 0xC0) >> 6;
2896 device_type = gcw[1] & 0x1F;
2897 removable = !!(gcw[0] & 0x80);
2898 packet_size = gcw[0] & 0x3;
2899
2900 /* Check that we can support this device */
2901 if (protocol != 2)
2902 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
2903 protocol);
2904 else if (device_type != 1)
2905 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
2906 "to tape\n", device_type);
2907 else if (!removable)
2908 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
2909 else if (packet_size != 0) {
2910 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2911 " bytes\n", packet_size);
2912 } else
2913 return 1;
2914 return 0;
2915 }
2916
2917 static void idetape_get_inquiry_results(ide_drive_t *drive)
2918 {
2919 idetape_tape_t *tape = drive->driver_data;
2920 struct ide_atapi_pc pc;
2921 char fw_rev[6], vendor_id[10], product_id[18];
2922
2923 idetape_create_inquiry_cmd(&pc);
2924 if (idetape_queue_pc_tail(drive, &pc)) {
2925 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2926 tape->name);
2927 return;
2928 }
2929 memcpy(vendor_id, &pc.buf[8], 8);
2930 memcpy(product_id, &pc.buf[16], 16);
2931 memcpy(fw_rev, &pc.buf[32], 4);
2932
2933 ide_fixstring(vendor_id, 10, 0);
2934 ide_fixstring(product_id, 18, 0);
2935 ide_fixstring(fw_rev, 6, 0);
2936
2937 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
2938 drive->name, tape->name, vendor_id, product_id, fw_rev);
2939 }
2940
2941 /*
2942 * Ask the tape about its various parameters. In particular, we will adjust our
2943 * data transfer buffer size to the recommended value as returned by the tape.
2944 */
2945 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2946 {
2947 idetape_tape_t *tape = drive->driver_data;
2948 struct ide_atapi_pc pc;
2949 u8 *caps;
2950 u8 speed, max_speed;
2951
2952 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2953 if (idetape_queue_pc_tail(drive, &pc)) {
2954 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2955 " some default values\n");
2956 tape->blk_size = 512;
2957 put_unaligned(52, (u16 *)&tape->caps[12]);
2958 put_unaligned(540, (u16 *)&tape->caps[14]);
2959 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2960 return;
2961 }
2962 caps = pc.buf + 4 + pc.buf[3];
2963
2964 /* convert to host order and save for later use */
2965 speed = be16_to_cpu(*(u16 *)&caps[14]);
2966 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
2967
2968 put_unaligned(max_speed, (u16 *)&caps[8]);
2969 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2970 put_unaligned(speed, (u16 *)&caps[14]);
2971 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2972
2973 if (!speed) {
2974 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2975 "(assuming 650KB/sec)\n", drive->name);
2976 put_unaligned(650, (u16 *)&caps[14]);
2977 }
2978 if (!max_speed) {
2979 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2980 "(assuming 650KB/sec)\n", drive->name);
2981 put_unaligned(650, (u16 *)&caps[8]);
2982 }
2983
2984 memcpy(&tape->caps, caps, 20);
2985 if (caps[7] & 0x02)
2986 tape->blk_size = 512;
2987 else if (caps[7] & 0x04)
2988 tape->blk_size = 1024;
2989 }
2990
2991 #ifdef CONFIG_IDE_PROC_FS
2992 static void idetape_add_settings(ide_drive_t *drive)
2993 {
2994 idetape_tape_t *tape = drive->driver_data;
2995
2996 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2997 1, 2, (u16 *)&tape->caps[16], NULL);
2998 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
2999 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3000 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3001 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3002 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3003 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3004 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3005 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3006 NULL);
3007 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3008 0xffff, tape->stage_size / 1024, 1,
3009 &tape->nr_pending_stages, NULL);
3010 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3011 1, 1, (u16 *)&tape->caps[14], NULL);
3012 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3013 1024, &tape->stage_size, NULL);
3014 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3015 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3016 NULL);
3017 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3018 1, &drive->dsc_overlap, NULL);
3019 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3020 1, 1, &tape->avg_speed, NULL);
3021 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3022 1, &tape->debug_mask, NULL);
3023 }
3024 #else
3025 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3026 #endif
3027
3028 /*
3029 * The function below is called to:
3030 *
3031 * 1. Initialize our various state variables.
3032 * 2. Ask the tape for its capabilities.
3033 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3034 * is chosen based on the recommendation which we received in step 2.
3035 *
3036 * Note that at this point ide.c already assigned us an irq, so that we can
3037 * queue requests here and wait for their completion.
3038 */
3039 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
3040 {
3041 unsigned long t1, tmid, tn, t;
3042 int speed;
3043 int stage_size;
3044 u8 gcw[2];
3045 struct sysinfo si;
3046 u16 *ctl = (u16 *)&tape->caps[12];
3047
3048 spin_lock_init(&tape->lock);
3049 drive->dsc_overlap = 1;
3050 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3051 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3052 tape->name);
3053 drive->dsc_overlap = 0;
3054 }
3055 /* Seagate Travan drives do not support DSC overlap. */
3056 if (strstr(drive->id->model, "Seagate STT3401"))
3057 drive->dsc_overlap = 0;
3058 tape->minor = minor;
3059 tape->name[0] = 'h';
3060 tape->name[1] = 't';
3061 tape->name[2] = '0' + minor;
3062 tape->chrdev_dir = IDETAPE_DIR_NONE;
3063 tape->pc = tape->pc_stack;
3064 *((unsigned short *) &gcw) = drive->id->config;
3065
3066 /* Command packet DRQ type */
3067 if (((gcw[0] & 0x60) >> 5) == 1)
3068 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
3069
3070 tape->min_pipeline = 10;
3071 tape->max_pipeline = 10;
3072 tape->max_stages = 10;
3073
3074 idetape_get_inquiry_results(drive);
3075 idetape_get_mode_sense_results(drive);
3076 ide_tape_get_bsize_from_bdesc(drive);
3077 tape->user_bs_factor = 1;
3078 tape->stage_size = *ctl * tape->blk_size;
3079 while (tape->stage_size > 0xffff) {
3080 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
3081 *ctl /= 2;
3082 tape->stage_size = *ctl * tape->blk_size;
3083 }
3084 stage_size = tape->stage_size;
3085 tape->pages_per_stage = stage_size / PAGE_SIZE;
3086 if (stage_size % PAGE_SIZE) {
3087 tape->pages_per_stage++;
3088 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3089 }
3090
3091 /* Select the "best" DSC read/write polling freq and pipeline size. */
3092 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
3093
3094 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3095
3096 /* Limit memory use for pipeline to 10% of physical memory */
3097 si_meminfo(&si);
3098 if (tape->max_stages * tape->stage_size >
3099 si.totalram * si.mem_unit / 10)
3100 tape->max_stages =
3101 si.totalram * si.mem_unit / (10 * tape->stage_size);
3102
3103 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3104 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3105 tape->max_pipeline =
3106 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3107 if (tape->max_stages == 0) {
3108 tape->max_stages = 1;
3109 tape->min_pipeline = 1;
3110 tape->max_pipeline = 1;
3111 }
3112
3113 t1 = (tape->stage_size * HZ) / (speed * 1000);
3114 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
3115 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3116
3117 if (tape->max_stages)
3118 t = tn;
3119 else
3120 t = t1;
3121
3122 /*
3123 * Ensure that the number we got makes sense; limit it within
3124 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3125 */
3126 tape->best_dsc_rw_freq = max_t(unsigned long,
3127 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3128 IDETAPE_DSC_RW_MIN);
3129 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3130 "%dkB pipeline, %lums tDSC%s\n",
3131 drive->name, tape->name, *(u16 *)&tape->caps[14],
3132 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
3133 tape->stage_size / 1024,
3134 tape->max_stages * tape->stage_size / 1024,
3135 tape->best_dsc_rw_freq * 1000 / HZ,
3136 drive->using_dma ? ", DMA":"");
3137
3138 idetape_add_settings(drive);
3139 }
3140
3141 static void ide_tape_remove(ide_drive_t *drive)
3142 {
3143 idetape_tape_t *tape = drive->driver_data;
3144
3145 ide_proc_unregister_driver(drive, tape->driver);
3146
3147 ide_unregister_region(tape->disk);
3148
3149 ide_tape_put(tape);
3150 }
3151
3152 static void ide_tape_release(struct kref *kref)
3153 {
3154 struct ide_tape_obj *tape = to_ide_tape(kref);
3155 ide_drive_t *drive = tape->drive;
3156 struct gendisk *g = tape->disk;
3157
3158 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3159
3160 drive->dsc_overlap = 0;
3161 drive->driver_data = NULL;
3162 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3163 device_destroy(idetape_sysfs_class,
3164 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
3165 idetape_devs[tape->minor] = NULL;
3166 g->private_data = NULL;
3167 put_disk(g);
3168 kfree(tape);
3169 }
3170
3171 #ifdef CONFIG_IDE_PROC_FS
3172 static int proc_idetape_read_name
3173 (char *page, char **start, off_t off, int count, int *eof, void *data)
3174 {
3175 ide_drive_t *drive = (ide_drive_t *) data;
3176 idetape_tape_t *tape = drive->driver_data;
3177 char *out = page;
3178 int len;
3179
3180 len = sprintf(out, "%s\n", tape->name);
3181 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3182 }
3183
3184 static ide_proc_entry_t idetape_proc[] = {
3185 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3186 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3187 { NULL, 0, NULL, NULL }
3188 };
3189 #endif
3190
3191 static int ide_tape_probe(ide_drive_t *);
3192
3193 static ide_driver_t idetape_driver = {
3194 .gen_driver = {
3195 .owner = THIS_MODULE,
3196 .name = "ide-tape",
3197 .bus = &ide_bus_type,
3198 },
3199 .probe = ide_tape_probe,
3200 .remove = ide_tape_remove,
3201 .version = IDETAPE_VERSION,
3202 .media = ide_tape,
3203 .supports_dsc_overlap = 1,
3204 .do_request = idetape_do_request,
3205 .end_request = idetape_end_request,
3206 .error = __ide_error,
3207 .abort = __ide_abort,
3208 #ifdef CONFIG_IDE_PROC_FS
3209 .proc = idetape_proc,
3210 #endif
3211 };
3212
3213 /* Our character device supporting functions, passed to register_chrdev. */
3214 static const struct file_operations idetape_fops = {
3215 .owner = THIS_MODULE,
3216 .read = idetape_chrdev_read,
3217 .write = idetape_chrdev_write,
3218 .ioctl = idetape_chrdev_ioctl,
3219 .open = idetape_chrdev_open,
3220 .release = idetape_chrdev_release,
3221 };
3222
3223 static int idetape_open(struct inode *inode, struct file *filp)
3224 {
3225 struct gendisk *disk = inode->i_bdev->bd_disk;
3226 struct ide_tape_obj *tape;
3227
3228 tape = ide_tape_get(disk);
3229 if (!tape)
3230 return -ENXIO;
3231
3232 return 0;
3233 }
3234
3235 static int idetape_release(struct inode *inode, struct file *filp)
3236 {
3237 struct gendisk *disk = inode->i_bdev->bd_disk;
3238 struct ide_tape_obj *tape = ide_tape_g(disk);
3239
3240 ide_tape_put(tape);
3241
3242 return 0;
3243 }
3244
3245 static int idetape_ioctl(struct inode *inode, struct file *file,
3246 unsigned int cmd, unsigned long arg)
3247 {
3248 struct block_device *bdev = inode->i_bdev;
3249 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3250 ide_drive_t *drive = tape->drive;
3251 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3252 if (err == -EINVAL)
3253 err = idetape_blkdev_ioctl(drive, cmd, arg);
3254 return err;
3255 }
3256
3257 static struct block_device_operations idetape_block_ops = {
3258 .owner = THIS_MODULE,
3259 .open = idetape_open,
3260 .release = idetape_release,
3261 .ioctl = idetape_ioctl,
3262 };
3263
3264 static int ide_tape_probe(ide_drive_t *drive)
3265 {
3266 idetape_tape_t *tape;
3267 struct gendisk *g;
3268 int minor;
3269
3270 if (!strstr("ide-tape", drive->driver_req))
3271 goto failed;
3272 if (!drive->present)
3273 goto failed;
3274 if (drive->media != ide_tape)
3275 goto failed;
3276 if (!idetape_identify_device(drive)) {
3277 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3278 " the driver\n", drive->name);
3279 goto failed;
3280 }
3281 if (drive->scsi) {
3282 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3283 " emulation.\n", drive->name);
3284 goto failed;
3285 }
3286 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
3287 if (tape == NULL) {
3288 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3289 drive->name);
3290 goto failed;
3291 }
3292
3293 g = alloc_disk(1 << PARTN_BITS);
3294 if (!g)
3295 goto out_free_tape;
3296
3297 ide_init_disk(g, drive);
3298
3299 ide_proc_register_driver(drive, &idetape_driver);
3300
3301 kref_init(&tape->kref);
3302
3303 tape->drive = drive;
3304 tape->driver = &idetape_driver;
3305 tape->disk = g;
3306
3307 g->private_data = &tape->driver;
3308
3309 drive->driver_data = tape;
3310
3311 mutex_lock(&idetape_ref_mutex);
3312 for (minor = 0; idetape_devs[minor]; minor++)
3313 ;
3314 idetape_devs[minor] = tape;
3315 mutex_unlock(&idetape_ref_mutex);
3316
3317 idetape_setup(drive, tape, minor);
3318
3319 device_create(idetape_sysfs_class, &drive->gendev,
3320 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3321 device_create(idetape_sysfs_class, &drive->gendev,
3322 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
3323
3324 g->fops = &idetape_block_ops;
3325 ide_register_region(g);
3326
3327 return 0;
3328
3329 out_free_tape:
3330 kfree(tape);
3331 failed:
3332 return -ENODEV;
3333 }
3334
3335 static void __exit idetape_exit(void)
3336 {
3337 driver_unregister(&idetape_driver.gen_driver);
3338 class_destroy(idetape_sysfs_class);
3339 unregister_chrdev(IDETAPE_MAJOR, "ht");
3340 }
3341
3342 static int __init idetape_init(void)
3343 {
3344 int error = 1;
3345 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3346 if (IS_ERR(idetape_sysfs_class)) {
3347 idetape_sysfs_class = NULL;
3348 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3349 error = -EBUSY;
3350 goto out;
3351 }
3352
3353 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3354 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3355 " interface\n");
3356 error = -EBUSY;
3357 goto out_free_class;
3358 }
3359
3360 error = driver_register(&idetape_driver.gen_driver);
3361 if (error)
3362 goto out_free_driver;
3363
3364 return 0;
3365
3366 out_free_driver:
3367 driver_unregister(&idetape_driver.gen_driver);
3368 out_free_class:
3369 class_destroy(idetape_sysfs_class);
3370 out:
3371 return error;
3372 }
3373
3374 MODULE_ALIAS("ide:*m-tape*");
3375 module_init(idetape_init);
3376 module_exit(idetape_exit);
3377 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
3378 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3379 MODULE_LICENSE("GPL");