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