Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Borislav Petkov | 5ce78af | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 2 | * IDE ATAPI streaming tape driver. |
| 3 | * |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 4 | * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il> |
| 5 | * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Borislav Petkov | 5ce78af | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 14 | * For a historical changelog see |
| 15 | * Documentation/ide/ChangeLog.ide-tape.1995-2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Bartlomiej Zolnierkiewicz | 51509ee | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 18 | #define DRV_NAME "ide-tape" |
| 19 | |
Borislav Petkov | dfe7993 | 2008-02-06 02:57:55 +0100 | [diff] [blame] | 20 | #define IDETAPE_VERSION "1.20" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Marcelo Feitoza Parisi | 9bae1ff | 2006-03-28 01:56:46 -0800 | [diff] [blame] | 30 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
| 33 | #include <linux/genhd.h> |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 34 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/slab.h> |
| 36 | #include <linux/pci.h> |
| 37 | #include <linux/ide.h> |
| 38 | #include <linux/smp_lock.h> |
| 39 | #include <linux/completion.h> |
| 40 | #include <linux/bitops.h> |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 41 | #include <linux/mutex.h> |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 42 | #include <scsi/scsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/byteorder.h> |
Borislav Petkov | c837cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 45 | #include <linux/irq.h> |
| 46 | #include <linux/uaccess.h> |
| 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/mtio.h> |
| 50 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 51 | /* define to see debug info */ |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 52 | #undef IDETAPE_DEBUG_LOG |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 53 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 54 | #ifdef IDETAPE_DEBUG_LOG |
| 55 | #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args) |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 56 | #else |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 57 | #define ide_debug_log(lvl, fmt, args...) do {} while (0) |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /**************************** Tunable parameters *****************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 62 | * After each failed packet command we issue a request sense command and retry |
| 63 | * the packet command IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 65 | * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
| 67 | #define IDETAPE_MAX_PC_RETRIES 3 |
| 68 | |
| 69 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 70 | * The following parameter is used to select the point in the internal tape fifo |
| 71 | * in which we will start to refill the buffer. Decreasing the following |
| 72 | * parameter will improve the system's latency and interactive response, while |
| 73 | * using a high value might improve system throughput. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 75 | #define IDETAPE_FIFO_THRESHOLD 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 78 | * DSC polling parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 80 | * Polling for DSC (a single bit in the status register) is a very important |
| 81 | * function in ide-tape. There are two cases in which we poll for DSC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 83 | * 1. Before a read/write packet command, to ensure that we can transfer data |
| 84 | * from/to the tape's data buffers, without causing an actual media access. |
| 85 | * In case the tape is not ready yet, we take out our request from the device |
| 86 | * request queue, so that ide.c could service requests from the other device |
| 87 | * on the same interface in the meantime. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 89 | * 2. After the successful initialization of a "media access packet command", |
| 90 | * which is a command that can take a long time to complete (the interval can |
| 91 | * range from several seconds to even an hour). Again, we postpone our request |
| 92 | * in the middle to free the bus for the other device. The polling frequency |
| 93 | * here should be lower than the read/write frequency since those media access |
| 94 | * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST |
| 95 | * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD |
| 96 | * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 98 | * We also set a timeout for the timer, in case something goes wrong. The |
| 99 | * timeout should be longer then the maximum execution time of a tape operation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 101 | |
| 102 | /* DSC timings. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */ |
| 104 | #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */ |
| 105 | #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */ |
| 106 | #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */ |
| 107 | #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */ |
| 108 | #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */ |
| 109 | #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */ |
| 110 | |
| 111 | /*************************** End of tunable parameters ***********************/ |
| 112 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 113 | /* tape directions */ |
| 114 | enum { |
| 115 | IDETAPE_DIR_NONE = (1 << 0), |
| 116 | IDETAPE_DIR_READ = (1 << 1), |
| 117 | IDETAPE_DIR_WRITE = (1 << 2), |
| 118 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Borislav Petkov | 03056b9 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 120 | /* Tape door status */ |
| 121 | #define DOOR_UNLOCKED 0 |
| 122 | #define DOOR_LOCKED 1 |
| 123 | #define DOOR_EXPLICITLY_LOCKED 2 |
| 124 | |
| 125 | /* Some defines for the SPACE command */ |
| 126 | #define IDETAPE_SPACE_OVER_FILEMARK 1 |
| 127 | #define IDETAPE_SPACE_TO_EOD 3 |
| 128 | |
| 129 | /* Some defines for the LOAD UNLOAD command */ |
| 130 | #define IDETAPE_LU_LOAD_MASK 1 |
| 131 | #define IDETAPE_LU_RETENSION_MASK 2 |
| 132 | #define IDETAPE_LU_EOT_MASK 4 |
| 133 | |
Borislav Petkov | 03056b9 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 134 | /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ |
| 135 | #define IDETAPE_BLOCK_DESCRIPTOR 0 |
| 136 | #define IDETAPE_CAPABILITIES_PAGE 0x2a |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 139 | * Most of our global data which we need to save even as we leave the driver due |
| 140 | * to an interrupt or a timer event is stored in the struct defined below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
| 142 | typedef struct ide_tape_obj { |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 143 | ide_drive_t *drive; |
| 144 | struct ide_driver *driver; |
| 145 | struct gendisk *disk; |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 146 | struct device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Bartlomiej Zolnierkiewicz | 2e8a6f8 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 148 | /* used by REQ_IDETAPE_{READ,WRITE} requests */ |
| 149 | struct ide_atapi_pc queued_pc; |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame] | 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 152 | * DSC polling variables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 154 | * While polling for DSC we use postponed_rq to postpone the current |
| 155 | * request so that ide.c will be able to service pending requests on the |
| 156 | * other device. Note that at most we will have only one DSC (usually |
Borislav Petkov | 5bd50dc | 2008-04-27 15:38:28 +0200 | [diff] [blame] | 157 | * data transfer) request in the device request queue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | */ |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 159 | bool postponed_rq; |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* The time in which we started polling for DSC */ |
| 162 | unsigned long dsc_polling_start; |
| 163 | /* Timer used to poll for dsc */ |
| 164 | struct timer_list dsc_timer; |
| 165 | /* Read/Write dsc polling frequency */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 166 | unsigned long best_dsc_rw_freq; |
| 167 | unsigned long dsc_poll_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | unsigned long dsc_timeout; |
| 169 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 170 | /* Read position information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | u8 partition; |
| 172 | /* Current block */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 173 | unsigned int first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 175 | /* Last error information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | u8 sense_key, asc, ascq; |
| 177 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 178 | /* Character device operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | unsigned int minor; |
| 180 | /* device name */ |
| 181 | char name[4]; |
| 182 | /* Current character device data transfer direction */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 183 | u8 chrdev_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 185 | /* tape block size, usually 512 or 1024 bytes */ |
| 186 | unsigned short blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | int user_bs_factor; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* Copy of the tape's Capabilities and Mechanical Page */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 190 | u8 caps[20]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 193 | * Active data transfer request parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 195 | * At most, there is only one ide-tape originated data transfer request |
| 196 | * in the device request queue. This allows ide.c to easily service |
| 197 | * requests from the other device when we postpone our active request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | */ |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 199 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 200 | /* Data buffer size chosen based on the tape's recommendation */ |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 201 | int buffer_size; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 202 | /* Staging buffer of buffer_size bytes */ |
| 203 | void *buf; |
| 204 | /* The read/write cursor */ |
| 205 | void *cur; |
| 206 | /* The number of valid bytes in buf */ |
| 207 | size_t valid; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 208 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 209 | /* Measures average tape speed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | unsigned long avg_time; |
| 211 | int avg_size; |
| 212 | int avg_speed; |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* the door is currently locked */ |
| 215 | int door_locked; |
| 216 | /* the tape hardware is write protected */ |
| 217 | char drv_write_prot; |
| 218 | /* the tape is write protected (hardware or opened as read-only) */ |
| 219 | char write_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } idetape_tape_t; |
| 221 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 222 | static DEFINE_MUTEX(idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 224 | static struct class *idetape_sysfs_class; |
| 225 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 226 | static void ide_tape_release(struct device *); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 227 | |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 228 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
| 229 | |
| 230 | static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev, |
| 231 | unsigned int i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | struct ide_tape_obj *tape = NULL; |
| 234 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 235 | mutex_lock(&idetape_ref_mutex); |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 236 | |
| 237 | if (cdev) |
| 238 | tape = idetape_devs[i]; |
| 239 | else |
| 240 | tape = ide_drv_g(disk, ide_tape_obj); |
| 241 | |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 242 | if (tape) { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 243 | if (ide_device_get(tape->drive)) |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 244 | tape = NULL; |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 245 | else |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 246 | get_device(&tape->dev); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 247 | } |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 248 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 249 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return tape; |
| 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | static void ide_tape_put(struct ide_tape_obj *tape) |
| 254 | { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 255 | ide_drive_t *drive = tape->drive; |
| 256 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 257 | mutex_lock(&idetape_ref_mutex); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 258 | put_device(&tape->dev); |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 259 | ide_device_put(drive); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 260 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 264 | * called on each failed packet command retry to analyze the request sense. We |
| 265 | * currently do not utilize this information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | */ |
Borislav Petkov | ae3a838 | 2009-05-02 10:58:17 +0200 | [diff] [blame] | 267 | static void idetape_analyze_error(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 270 | struct ide_atapi_pc *pc = drive->failed_pc; |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 271 | struct request *rq = drive->hwif->rq; |
Borislav Petkov | ae3a838 | 2009-05-02 10:58:17 +0200 | [diff] [blame] | 272 | u8 *sense = bio_data(rq->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 274 | tape->sense_key = sense[2] & 0xF; |
| 275 | tape->asc = sense[12]; |
| 276 | tape->ascq = sense[13]; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 277 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 278 | ide_debug_log(IDE_DBG_FUNC, |
| 279 | "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x", |
| 280 | rq->cmd[0], tape->sense_key, tape->asc, tape->ascq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 282 | /* correct remaining bytes to transfer */ |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 283 | if (pc->flags & PC_FLAG_DMA_ERROR) |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 284 | rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * If error was the result of a zero-length read or write command, |
| 288 | * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives |
| 289 | * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. |
| 290 | */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 291 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 292 | /* length == 0 */ |
| 293 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { |
| 294 | if (tape->sense_key == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* don't report an error, everything's ok */ |
| 296 | pc->error = 0; |
| 297 | /* don't retry read/write */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 298 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 301 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 302 | pc->error = IDE_DRV_ERROR_FILEMARK; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 303 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 305 | if (pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 306 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
| 307 | && tape->asc == 0x0 && tape->ascq == 0x2)) { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 308 | pc->error = IDE_DRV_ERROR_EOD; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 309 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 312 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 313 | if (tape->sense_key == 8) { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 314 | pc->error = IDE_DRV_ERROR_EOD; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 315 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 317 | if (!(pc->flags & PC_FLAG_ABORT) && |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 318 | (blk_rq_bytes(rq) - rq->resid_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
| 320 | } |
| 321 | } |
| 322 | |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 323 | static void ide_tape_handle_dsc(ide_drive_t *); |
| 324 | |
Bartlomiej Zolnierkiewicz | 03a2faa | 2009-03-27 12:46:36 +0100 | [diff] [blame] | 325 | static int ide_tape_callback(ide_drive_t *drive, int dsc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 328 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 329 | struct request *rq = drive->hwif->rq; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 330 | int uptodate = pc->error ? 0 : 1; |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 331 | int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 333 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0], |
| 334 | dsc, err); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 335 | |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 336 | if (dsc) |
| 337 | ide_tape_handle_dsc(drive); |
| 338 | |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 339 | if (drive->failed_pc == pc) |
| 340 | drive->failed_pc = NULL; |
Bartlomiej Zolnierkiewicz | dd2e9a0 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 341 | |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 342 | if (pc->c[0] == REQUEST_SENSE) { |
| 343 | if (uptodate) |
Borislav Petkov | ae3a838 | 2009-05-02 10:58:17 +0200 | [diff] [blame] | 344 | idetape_analyze_error(drive); |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 345 | else |
| 346 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE " |
| 347 | "itself - Aborting request!\n"); |
| 348 | } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 349 | unsigned int blocks = |
| 350 | (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 351 | |
| 352 | tape->avg_size += blocks * tape->blk_size; |
| 353 | |
| 354 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { |
| 355 | tape->avg_speed = tape->avg_size * HZ / |
| 356 | (jiffies - tape->avg_time) / 1024; |
| 357 | tape->avg_size = 0; |
| 358 | tape->avg_time = jiffies; |
| 359 | } |
| 360 | |
| 361 | tape->first_frame += blocks; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 362 | |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 363 | if (pc->error) { |
| 364 | uptodate = 0; |
| 365 | err = pc->error; |
| 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 368 | rq->errors = err; |
| 369 | |
Bartlomiej Zolnierkiewicz | 03a2faa | 2009-03-27 12:46:36 +0100 | [diff] [blame] | 370 | return uptodate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 374 | * Postpone the current request so that ide.c will be able to service requests |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 375 | * from another device on the same port while we are polling for DSC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 377 | static void ide_tape_stall_queue(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
| 379 | idetape_tape_t *tape = drive->driver_data; |
| 380 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 381 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu", |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 382 | drive->hwif->rq->cmd[0], tape->dsc_poll_freq); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 383 | |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 384 | tape->postponed_rq = true; |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 385 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 386 | ide_stall_queue(drive, tape->dsc_poll_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Bartlomiej Zolnierkiewicz | 74e63e74 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 389 | static void ide_tape_handle_dsc(ide_drive_t *drive) |
| 390 | { |
| 391 | idetape_tape_t *tape = drive->driver_data; |
| 392 | |
| 393 | /* Media access command */ |
| 394 | tape->dsc_polling_start = jiffies; |
| 395 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; |
| 396 | tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; |
| 397 | /* Allow ide.c to handle other requests */ |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 398 | ide_tape_stall_queue(drive); |
Bartlomiej Zolnierkiewicz | 74e63e74 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 402 | * Packet Command Interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 404 | * The current Packet Command is available in drive->pc, and will not change |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 405 | * until we finish handling it. Each packet command is associated with a |
| 406 | * callback function that will be called when the command is finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 408 | * The handling will be done in three stages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | * |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 410 | * 1. ide_tape_issue_pc will send the packet command to the drive, and will set |
Bartlomiej Zolnierkiewicz | aa5d2de7 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 411 | * the interrupt handler to ide_pc_intr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | * |
Bartlomiej Zolnierkiewicz | aa5d2de7 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 413 | * 2. On each interrupt, ide_pc_intr will be called. This step will be |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 414 | * repeated until the device signals us that no more interrupts will be issued. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 416 | * 3. ATAPI Tape media access commands have immediate status with a delayed |
| 417 | * process. In case of a successful initiation of a media access packet command, |
| 418 | * the DSC bit will be set when the actual execution of the command is finished. |
| 419 | * Since the tape drive will not issue an interrupt, we have to poll for this |
| 420 | * event. In this case, we define the request as "low priority request" by |
| 421 | * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and |
| 422 | * exit the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 424 | * ide.c will then give higher priority to requests which originate from the |
| 425 | * other device, until will change rq_status to RQ_ACTIVE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 427 | * 4. When the packet command is finished, it will be checked for errors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 429 | * 5. In case an error was found, we queue a request sense packet command in |
| 430 | * front of the request queue and retry the operation up to |
| 431 | * IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 433 | * 6. In case no error was found, or we decided to give up and not to retry |
| 434 | * again, the callback function will be called and then we will handle the next |
| 435 | * request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 438 | static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive, |
| 439 | struct ide_cmd *cmd, |
| 440 | struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 443 | struct request *rq = drive->hwif->rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 445 | if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
| 446 | drive->failed_pc = pc; |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* Set the current packet command */ |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 449 | drive->pc = pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 452 | (pc->flags & PC_FLAG_ABORT)) { |
Tejun Heo | b3071d1 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 455 | * We will "abort" retrying a packet command in case legitimate |
| 456 | * error code was received (crossing a filemark, or end of the |
| 457 | * media, for example). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 459 | if (!(pc->flags & PC_FLAG_ABORT)) { |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 460 | if (!(pc->c[0] == TEST_UNIT_READY && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | tape->sense_key == 2 && tape->asc == 4 && |
| 462 | (tape->ascq == 1 || tape->ascq == 8))) { |
| 463 | printk(KERN_ERR "ide-tape: %s: I/O error, " |
| 464 | "pc = %2x, key = %2x, " |
| 465 | "asc = %2x, ascq = %2x\n", |
| 466 | tape->name, pc->c[0], |
| 467 | tape->sense_key, tape->asc, |
| 468 | tape->ascq); |
| 469 | } |
| 470 | /* Giving up */ |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 471 | pc->error = IDE_DRV_ERROR_GENERAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
Tejun Heo | b3071d1 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 473 | |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 474 | drive->failed_pc = NULL; |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 475 | drive->pc_callback(drive, 0); |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 476 | ide_complete_rq(drive, -EIO, blk_rq_bytes(rq)); |
Bartlomiej Zolnierkiewicz | 92f5daf | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 477 | return ide_stopped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 479 | ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries, |
| 480 | pc->c[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | pc->retries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 484 | return ide_issue_pc(drive, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 487 | /* A mode sense command is used to "sense" tape parameters. */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 488 | static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 490 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 491 | pc->c[0] = MODE_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 493 | /* DBD = 1 - Don't return block descriptors */ |
| 494 | pc->c[1] = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | pc->c[2] = page_code; |
| 496 | /* |
| 497 | * Changed pc->c[3] to 0 (255 will at best return unused info). |
| 498 | * |
| 499 | * For SCSI this byte is defined as subpage instead of high byte |
| 500 | * of length and some IDE drives seem to interpret it this way |
| 501 | * and return an error when 255 is used. |
| 502 | */ |
| 503 | pc->c[3] = 0; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 504 | /* We will just discard data in that case */ |
| 505 | pc->c[4] = 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 507 | pc->req_xfer = 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 509 | pc->req_xfer = 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | else |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 511 | pc->req_xfer = 50; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 514 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 516 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 518 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 519 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 521 | stat = hwif->tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 522 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 523 | if (stat & ATA_DSC) { |
| 524 | if (stat & ATA_ERR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | /* Error detected */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 526 | if (pc->c[0] != TEST_UNIT_READY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | printk(KERN_ERR "ide-tape: %s: I/O error, ", |
| 528 | tape->name); |
| 529 | /* Retry operation */ |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 530 | ide_retry_pc(drive); |
Bartlomiej Zolnierkiewicz | 258ec41 | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 531 | return ide_stopped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | pc->error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } else { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 535 | pc->error = IDE_DRV_ERROR_GENERAL; |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 536 | drive->failed_pc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 538 | drive->pc_callback(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return ide_stopped; |
| 540 | } |
| 541 | |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 542 | static void ide_tape_create_rw_cmd(idetape_tape_t *tape, |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 543 | struct ide_atapi_pc *pc, struct request *rq, |
| 544 | u8 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
Borislav Petkov | 10c0b34 | 2009-05-01 20:29:53 +0200 | [diff] [blame] | 546 | unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9); |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 547 | |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 548 | ide_init_pc(pc); |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 549 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | pc->c[1] = 1; |
Borislav Petkov | 19f52a7 | 2009-05-04 09:53:03 +0200 | [diff] [blame] | 551 | |
| 552 | if (blk_rq_bytes(rq) == tape->buffer_size) |
Bartlomiej Zolnierkiewicz | 5e33109 | 2008-07-15 21:21:56 +0200 | [diff] [blame] | 553 | pc->flags |= PC_FLAG_DMA_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 555 | if (opcode == READ_6) |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 556 | pc->c[0] = READ_6; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 557 | else if (opcode == WRITE_6) { |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 558 | pc->c[0] = WRITE_6; |
| 559 | pc->flags |= PC_FLAG_WRITING; |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 560 | } |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 561 | |
| 562 | memcpy(rq->cmd, pc->c, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | static ide_startstop_t idetape_do_request(ide_drive_t *drive, |
| 566 | struct request *rq, sector_t block) |
| 567 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 568 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 570 | struct ide_atapi_pc *pc = NULL; |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 571 | struct ide_cmd cmd; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 572 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 574 | ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u", |
| 575 | rq->cmd[0], (unsigned long long)blk_rq_pos(rq), |
| 576 | blk_rq_sectors(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
Bartlomiej Zolnierkiewicz | 2c7eaa4 | 2009-06-15 22:16:10 +0200 | [diff] [blame] | 578 | BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 580 | /* Retry a failed packet command */ |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 581 | if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) { |
| 582 | pc = drive->failed_pc; |
Bartlomiej Zolnierkiewicz | 28c7214 | 2008-07-15 21:21:59 +0200 | [diff] [blame] | 583 | goto out; |
| 584 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | /* |
| 587 | * If the tape is still busy, postpone our request and service |
| 588 | * the other device meanwhile. |
| 589 | */ |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 590 | stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 592 | if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 && |
| 593 | (rq->cmd[13] & REQ_IDETAPE_PC2) == 0) |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 594 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 596 | if (drive->dev_flags & IDE_DFLAG_POST_RESET) { |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 597 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 598 | drive->dev_flags &= ~IDE_DFLAG_POST_RESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 601 | if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) && |
| 602 | !(stat & ATA_DSC)) { |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 603 | if (!tape->postponed_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | tape->dsc_polling_start = jiffies; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 605 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; |
| 607 | } else if (time_after(jiffies, tape->dsc_timeout)) { |
| 608 | printk(KERN_ERR "ide-tape: %s: DSC timeout\n", |
| 609 | tape->name); |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 610 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | idetape_media_access_finished(drive); |
| 612 | return ide_stopped; |
| 613 | } else { |
| 614 | return ide_do_reset(drive); |
| 615 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 616 | } else if (time_after(jiffies, |
| 617 | tape->dsc_polling_start + |
| 618 | IDETAPE_DSC_MA_THRESHOLD)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 619 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 620 | ide_tape_stall_queue(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return ide_stopped; |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 622 | } else { |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 623 | drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC; |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 624 | tape->postponed_rq = false; |
| 625 | } |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 626 | |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 627 | if (rq->cmd[13] & REQ_IDETAPE_READ) { |
Bartlomiej Zolnierkiewicz | 2e8a6f8 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 628 | pc = &tape->queued_pc; |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 629 | ide_tape_create_rw_cmd(tape, pc, rq, READ_6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | goto out; |
| 631 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 632 | if (rq->cmd[13] & REQ_IDETAPE_WRITE) { |
Bartlomiej Zolnierkiewicz | 2e8a6f8 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 633 | pc = &tape->queued_pc; |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 634 | ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | goto out; |
| 636 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 637 | if (rq->cmd[13] & REQ_IDETAPE_PC1) { |
Tejun Heo | c267cc1 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 638 | pc = (struct ide_atapi_pc *)rq->special; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 639 | rq->cmd[13] &= ~(REQ_IDETAPE_PC1); |
| 640 | rq->cmd[13] |= REQ_IDETAPE_PC2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | goto out; |
| 642 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 643 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | idetape_media_access_finished(drive); |
| 645 | return ide_stopped; |
| 646 | } |
| 647 | BUG(); |
Bartlomiej Zolnierkiewicz | 28c7214 | 2008-07-15 21:21:59 +0200 | [diff] [blame] | 648 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 649 | out: |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 650 | /* prepare sense request for this command */ |
| 651 | ide_prep_sense(drive, rq); |
| 652 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 653 | memset(&cmd, 0, sizeof(cmd)); |
| 654 | |
| 655 | if (rq_data_dir(rq)) |
| 656 | cmd.tf_flags |= IDE_TFLAG_WRITE; |
| 657 | |
| 658 | cmd.rq = rq; |
| 659 | |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 660 | ide_init_sg_cmd(&cmd, blk_rq_bytes(rq)); |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 661 | ide_map_sg(drive, &cmd); |
| 662 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 663 | return ide_tape_issue_pc(drive, &cmd, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 667 | * Write a filemark if write_filemark=1. Flush the device buffers without |
| 668 | * writing a filemark otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 670 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 671 | struct ide_atapi_pc *pc, int write_filemark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 673 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 674 | pc->c[0] = WRITE_FILEMARKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | pc->c[4] = write_filemark; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 676 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) |
| 680 | { |
| 681 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 682 | struct gendisk *disk = tape->disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | int load_attempted = 0; |
| 684 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 685 | /* Wait for the tape to become ready */ |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 686 | set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | timeout += jiffies; |
| 688 | while (time_before(jiffies, timeout)) { |
Bartlomiej Zolnierkiewicz | de699ad | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 689 | if (ide_do_test_unit_ready(drive, disk) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return 0; |
| 691 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 692 | || (tape->asc == 0x3A)) { |
| 693 | /* no media */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (load_attempted) |
| 695 | return -ENOMEDIUM; |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 696 | ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | load_attempted = 1; |
| 698 | /* not about to be ready */ |
| 699 | } else if (!(tape->sense_key == 2 && tape->asc == 4 && |
| 700 | (tape->ascq == 1 || tape->ascq == 8))) |
| 701 | return -EIO; |
Nishanth Aravamudan | 80ce45f | 2005-09-10 00:27:08 -0700 | [diff] [blame] | 702 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | return -EIO; |
| 705 | } |
| 706 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 707 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 709 | struct ide_tape_obj *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 710 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | int rc; |
| 712 | |
| 713 | idetape_create_write_filemark_cmd(drive, &pc, 0); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 714 | rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 715 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return rc; |
| 717 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 718 | return 0; |
| 719 | } |
| 720 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 721 | static int ide_tape_read_position(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 724 | struct ide_atapi_pc pc; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 725 | u8 buf[20]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 727 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 729 | /* prep cmd */ |
| 730 | ide_init_pc(&pc); |
| 731 | pc.c[0] = READ_POSITION; |
| 732 | pc.req_xfer = 20; |
| 733 | |
| 734 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return -1; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 736 | |
| 737 | if (!pc.error) { |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 738 | ide_debug_log(IDE_DBG_FUNC, "BOP - %s", |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 739 | (buf[0] & 0x80) ? "Yes" : "No"); |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 740 | ide_debug_log(IDE_DBG_FUNC, "EOP - %s", |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 741 | (buf[0] & 0x40) ? "Yes" : "No"); |
| 742 | |
| 743 | if (buf[0] & 0x4) { |
| 744 | printk(KERN_INFO "ide-tape: Block location is unknown" |
| 745 | "to the tape\n"); |
Bartlomiej Zolnierkiewicz | 8dcce40 | 2009-06-13 12:00:54 +0200 | [diff] [blame] | 746 | clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), |
| 747 | &drive->atapi_flags); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 748 | return -1; |
| 749 | } else { |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 750 | ide_debug_log(IDE_DBG_FUNC, "Block Location: %u", |
| 751 | be32_to_cpup((__be32 *)&buf[4])); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 752 | |
| 753 | tape->partition = buf[1]; |
| 754 | tape->first_frame = be32_to_cpup((__be32 *)&buf[4]); |
Bartlomiej Zolnierkiewicz | 8dcce40 | 2009-06-13 12:00:54 +0200 | [diff] [blame] | 755 | set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), |
| 756 | &drive->atapi_flags); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
| 760 | return tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 763 | static void idetape_create_locate_cmd(ide_drive_t *drive, |
| 764 | struct ide_atapi_pc *pc, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 765 | unsigned int block, u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 767 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 768 | pc->c[0] = POSITION_TO_ELEMENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | pc->c[1] = 2; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 770 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | pc->c[8] = partition; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 772 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 775 | static void __ide_tape_discard_merge_buffer(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
| 777 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 779 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
Borislav Petkov | 9798630 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 780 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 782 | clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 783 | tape->valid = 0; |
| 784 | if (tape->buf != NULL) { |
| 785 | kfree(tape->buf); |
| 786 | tape->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 789 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 793 | * Position the tape to the requested block using the LOCATE packet command. |
| 794 | * A READ POSITION command is then issued to check where we are positioned. Like |
| 795 | * all higher level operations, we queue the commands at the tail of the request |
| 796 | * queue and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 798 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
| 799 | u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
| 801 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 802 | struct gendisk *disk = tape->disk; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 803 | int ret; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 804 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 806 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 807 | __ide_tape_discard_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 809 | idetape_create_locate_cmd(drive, &pc, block, partition, skip); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 810 | ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
| 811 | if (ret) |
| 812 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 814 | ret = ide_tape_read_position(drive); |
| 815 | if (ret < 0) |
| 816 | return ret; |
| 817 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 820 | static void ide_tape_discard_merge_buffer(ide_drive_t *drive, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 821 | int restore_position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { |
| 823 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | int seek, position; |
| 825 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 826 | __ide_tape_discard_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | if (restore_position) { |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 828 | position = ide_tape_read_position(drive); |
Borislav Petkov | 9798630 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 829 | seek = position > 0 ? position : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (idetape_position_tape(drive, seek, 0, 0)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 831 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 832 | " %s\n", tape->name, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 839 | * Generate a read/write request for the block device interface and wait for it |
| 840 | * to be serviced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | */ |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 842 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
| 844 | idetape_tape_t *tape = drive->driver_data; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 845 | struct request *rq; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 846 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 848 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size); |
| 849 | |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 850 | BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE); |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 851 | BUG_ON(size < 0 || size % tape->blk_size); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 852 | |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 853 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 854 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 855 | rq->cmd[13] = cmd; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 856 | rq->rq_disk = tape->disk; |
Linus Torvalds | c905959 | 2009-06-11 10:52:27 -0700 | [diff] [blame] | 857 | rq->__sector = tape->first_frame; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 858 | |
| 859 | if (size) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 860 | ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size, |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 861 | __GFP_WAIT); |
| 862 | if (ret) |
| 863 | goto out_put; |
| 864 | } |
| 865 | |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 866 | blk_execute_rq(drive->queue, tape->disk, rq, 0); |
| 867 | |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 868 | /* calculate the number of transferred bytes and update buffer state */ |
Tejun Heo | c3a4d78 | 2009-05-07 22:24:37 +0900 | [diff] [blame] | 869 | size -= rq->resid_len; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 870 | tape->cur = tape->buf; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 871 | if (cmd == REQ_IDETAPE_READ) |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 872 | tape->valid = size; |
| 873 | else |
| 874 | tape->valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 876 | ret = size; |
| 877 | if (rq->errors == IDE_DRV_ERROR_GENERAL) |
| 878 | ret = -EIO; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 879 | out_put: |
| 880 | blk_put_request(rq); |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 881 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 884 | static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 886 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 887 | pc->c[0] = INQUIRY; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 888 | pc->c[4] = 254; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 889 | pc->req_xfer = 254; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 892 | static void idetape_create_rewind_cmd(ide_drive_t *drive, |
| 893 | struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 895 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 896 | pc->c[0] = REZERO_UNIT; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 897 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 900 | static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 902 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 903 | pc->c[0] = ERASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | pc->c[1] = 1; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 905 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 908 | static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 910 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 911 | pc->c[0] = SPACE; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 912 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | pc->c[1] = cmd; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 914 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 917 | static void ide_tape_flush_merge_buffer(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | { |
| 919 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | 55a5d29 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 920 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 921 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 922 | printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 923 | " but we are not writing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return; |
| 925 | } |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 926 | if (tape->buf) { |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 927 | size_t aligned = roundup(tape->valid, tape->blk_size); |
| 928 | |
| 929 | memset(tape->cur, 0, aligned - tape->valid); |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 930 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 931 | kfree(tape->buf); |
| 932 | tape->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 934 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 937 | static int idetape_init_rw(ide_drive_t *drive, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | { |
| 939 | idetape_tape_t *tape = drive->driver_data; |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 940 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 942 | BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 944 | if (tape->chrdev_dir == dir) |
| 945 | return 0; |
| 946 | |
| 947 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
| 948 | ide_tape_discard_merge_buffer(drive, 1); |
| 949 | else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
| 950 | ide_tape_flush_merge_buffer(drive); |
| 951 | idetape_flush_tape_buffers(drive); |
| 952 | } |
| 953 | |
| 954 | if (tape->buf || tape->valid) { |
| 955 | printk(KERN_ERR "ide-tape: valid should be 0 now\n"); |
| 956 | tape->valid = 0; |
| 957 | } |
| 958 | |
| 959 | tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); |
| 960 | if (!tape->buf) |
| 961 | return -ENOMEM; |
| 962 | tape->chrdev_dir = dir; |
| 963 | tape->cur = tape->buf; |
| 964 | |
| 965 | /* |
| 966 | * Issue a 0 rw command to ensure that DSC handshake is |
| 967 | * switched from completion mode to buffer available mode. No |
| 968 | * point in issuing this if DSC overlap isn't supported, some |
| 969 | * drives (Seagate STT3401A) will return an error. |
| 970 | */ |
| 971 | if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) { |
| 972 | int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ |
| 973 | : REQ_IDETAPE_WRITE; |
| 974 | |
| 975 | rc = idetape_queue_rw_tail(drive, cmd, 0); |
| 976 | if (rc < 0) { |
| 977 | kfree(tape->buf); |
| 978 | tape->buf = NULL; |
| 979 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
| 980 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
Borislav Petkov | 5e69bd9 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | return 0; |
| 985 | } |
| 986 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 987 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { |
| 989 | idetape_tape_t *tape = drive->driver_data; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 990 | |
| 991 | memset(tape->buf, 0, tape->buffer_size); |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | while (bcount) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 994 | unsigned int count = min(tape->buffer_size, bcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 996 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | bcount -= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1002 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
| 1003 | * currently support only one partition. |
| 1004 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1005 | static int idetape_rewind_tape(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | { |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1007 | struct ide_tape_obj *tape = drive->driver_data; |
| 1008 | struct gendisk *disk = tape->disk; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1009 | struct ide_atapi_pc pc; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1010 | int ret; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1011 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1012 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | idetape_create_rewind_cmd(drive, &pc); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1015 | ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
| 1016 | if (ret) |
| 1017 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1019 | ret = ide_tape_read_position(drive); |
| 1020 | if (ret < 0) |
| 1021 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1025 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1026 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
| 1027 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | { |
| 1029 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | void __user *argp = (void __user *)arg; |
| 1031 | |
Borislav Petkov | d59823f | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1032 | struct idetape_config { |
| 1033 | int dsc_rw_frequency; |
| 1034 | int dsc_media_access_frequency; |
| 1035 | int nr_stages; |
| 1036 | } config; |
| 1037 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1038 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1041 | case 0x0340: |
| 1042 | if (copy_from_user(&config, argp, sizeof(config))) |
| 1043 | return -EFAULT; |
| 1044 | tape->best_dsc_rw_freq = config.dsc_rw_frequency; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1045 | break; |
| 1046 | case 0x0350: |
Michael Buesch | 2fc2111 | 2009-07-19 09:15:19 +0000 | [diff] [blame] | 1047 | memset(&config, 0, sizeof(config)); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1048 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1049 | config.nr_stages = 1; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1050 | if (copy_to_user(argp, &config, sizeof(config))) |
| 1051 | return -EFAULT; |
| 1052 | break; |
| 1053 | default: |
| 1054 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1059 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
| 1060 | int mt_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | { |
| 1062 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1063 | struct gendisk *disk = tape->disk; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1064 | struct ide_atapi_pc pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1065 | int retval, count = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1066 | int sprev = !!(tape->caps[4] & 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1068 | |
| 1069 | ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count); |
| 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | if (mt_count == 0) |
| 1072 | return 0; |
| 1073 | if (MTBSF == mt_op || MTBSFM == mt_op) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1074 | if (!sprev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1076 | mt_count = -mt_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1079 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1080 | tape->valid = 0; |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1081 | if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK), |
| 1082 | &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | ++count; |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1084 | ide_tape_discard_merge_buffer(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1088 | case MTFSF: |
| 1089 | case MTBSF: |
| 1090 | idetape_create_space_cmd(&pc, mt_count - count, |
| 1091 | IDETAPE_SPACE_OVER_FILEMARK); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1092 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1093 | case MTFSFM: |
| 1094 | case MTBSFM: |
| 1095 | if (!sprev) |
| 1096 | return -EIO; |
| 1097 | retval = idetape_space_over_filemarks(drive, MTFSF, |
| 1098 | mt_count - count); |
| 1099 | if (retval) |
| 1100 | return retval; |
| 1101 | count = (MTBSFM == mt_op ? 1 : -1); |
| 1102 | return idetape_space_over_filemarks(drive, MTFSF, count); |
| 1103 | default: |
| 1104 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 1105 | mt_op); |
| 1106 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1111 | * Our character device read / write functions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1113 | * The tape is optimized to maximize throughput when it is transferring an |
| 1114 | * integral number of the "continuous transfer limit", which is a parameter of |
| 1115 | * the specific tape (26kB on my particular tape, 32kB for Onstream). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1117 | * As of version 1.3 of the driver, the character device provides an abstract |
| 1118 | * continuous view of the media - any mix of block sizes (even 1 byte) on the |
| 1119 | * same backup/restore procedure is supported. The driver will internally |
| 1120 | * convert the requests to the recommended transfer unit, so that an unmatch |
| 1121 | * between the user's block size to the recommended size will only result in a |
| 1122 | * (slightly) increased driver overhead, but will no longer hit performance. |
| 1123 | * This is not applicable to Onstream. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1125 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
| 1126 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1128 | struct ide_tape_obj *tape = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | ide_drive_t *drive = tape->drive; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1130 | size_t done = 0; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1131 | ssize_t ret = 0; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1132 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1134 | ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1136 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1137 | if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1138 | if (count > tape->blk_size && |
| 1139 | (count % tape->blk_size) == 0) |
| 1140 | tape->user_bs_factor = count / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | } |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1142 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1143 | rc = idetape_init_rw(drive, IDETAPE_DIR_READ); |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1144 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | return rc; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1146 | |
| 1147 | while (done < count) { |
| 1148 | size_t todo; |
| 1149 | |
| 1150 | /* refill if staging buffer is empty */ |
| 1151 | if (!tape->valid) { |
| 1152 | /* If we are at a filemark, nothing more to read */ |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1153 | if (test_bit(ilog2(IDE_AFLAG_FILEMARK), |
| 1154 | &drive->atapi_flags)) |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1155 | break; |
| 1156 | /* read */ |
| 1157 | if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, |
| 1158 | tape->buffer_size) <= 0) |
| 1159 | break; |
| 1160 | } |
| 1161 | |
| 1162 | /* copy out */ |
| 1163 | todo = min_t(size_t, count - done, tape->valid); |
| 1164 | if (copy_to_user(buf + done, tape->cur, todo)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1165 | ret = -EFAULT; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1166 | |
| 1167 | tape->cur += todo; |
| 1168 | tape->valid -= todo; |
| 1169 | done += todo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1171 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1172 | if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | idetape_space_over_filemarks(drive, MTFSF, 1); |
| 1174 | return 0; |
| 1175 | } |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1176 | |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1177 | return ret ? ret : done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1180 | static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | size_t count, loff_t *ppos) |
| 1182 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1183 | struct ide_tape_obj *tape = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | ide_drive_t *drive = tape->drive; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1185 | size_t done = 0; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1186 | ssize_t ret = 0; |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1187 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
| 1189 | /* The drive is write protected. */ |
| 1190 | if (tape->write_prot) |
| 1191 | return -EACCES; |
| 1192 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1193 | ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | /* Initialize write operation */ |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1196 | rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE); |
| 1197 | if (rc < 0) |
| 1198 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1200 | while (done < count) { |
| 1201 | size_t todo; |
| 1202 | |
| 1203 | /* flush if staging buffer is full */ |
| 1204 | if (tape->valid == tape->buffer_size && |
| 1205 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, |
| 1206 | tape->buffer_size) <= 0) |
| 1207 | return rc; |
| 1208 | |
| 1209 | /* copy in */ |
| 1210 | todo = min_t(size_t, count - done, |
| 1211 | tape->buffer_size - tape->valid); |
| 1212 | if (copy_from_user(tape->cur, buf + done, todo)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1213 | ret = -EFAULT; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1214 | |
| 1215 | tape->cur += todo; |
| 1216 | tape->valid += todo; |
| 1217 | done += todo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | } |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1219 | |
| 1220 | return ret ? ret : done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1223 | static int idetape_write_filemark(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | { |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1225 | struct ide_tape_obj *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1226 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | /* Write a filemark */ |
| 1229 | idetape_create_write_filemark_cmd(drive, &pc, 1); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1230 | if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); |
| 1232 | return -EIO; |
| 1233 | } |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1238 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
| 1239 | * requested. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1241 | * Note: MTBSF and MTBSFM are not supported when the tape doesn't support |
| 1242 | * spacing over filemarks in the reverse direction. In this case, MTFSFM is also |
Borislav Petkov | 5bd50dc | 2008-04-27 15:38:28 +0200 | [diff] [blame] | 1243 | * usually not supported. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1245 | * The following commands are currently not supported: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1247 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
| 1248 | * MT_ST_WRITE_THRESHOLD. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1250 | static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | { |
| 1252 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1253 | struct gendisk *disk = tape->disk; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1254 | struct ide_atapi_pc pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1255 | int i, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1257 | ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d", |
| 1258 | mt_op, mt_count); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1261 | case MTFSF: |
| 1262 | case MTFSFM: |
| 1263 | case MTBSF: |
| 1264 | case MTBSFM: |
| 1265 | if (!mt_count) |
| 1266 | return 0; |
| 1267 | return idetape_space_over_filemarks(drive, mt_op, mt_count); |
| 1268 | default: |
| 1269 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1273 | case MTWEOF: |
| 1274 | if (tape->write_prot) |
| 1275 | return -EACCES; |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1276 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1277 | for (i = 0; i < mt_count; i++) { |
| 1278 | retval = idetape_write_filemark(drive); |
| 1279 | if (retval) |
| 1280 | return retval; |
| 1281 | } |
| 1282 | return 0; |
| 1283 | case MTREW: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1284 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1285 | if (idetape_rewind_tape(drive)) |
| 1286 | return -EIO; |
| 1287 | return 0; |
| 1288 | case MTLOAD: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1289 | ide_tape_discard_merge_buffer(drive, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 1290 | return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1291 | case MTUNLOAD: |
| 1292 | case MTOFFL: |
| 1293 | /* |
| 1294 | * If door is locked, attempt to unlock before |
| 1295 | * attempting to eject. |
| 1296 | */ |
| 1297 | if (tape->door_locked) { |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1298 | if (!ide_set_media_lock(drive, disk, 0)) |
Bartlomiej Zolnierkiewicz | 385a4b8 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 1299 | tape->door_locked = DOOR_UNLOCKED; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1300 | } |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1301 | ide_tape_discard_merge_buffer(drive, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 1302 | retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1303 | if (!retval) |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1304 | clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), |
| 1305 | &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1306 | return retval; |
| 1307 | case MTNOP: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1308 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1309 | return idetape_flush_tape_buffers(drive); |
| 1310 | case MTRETEN: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1311 | ide_tape_discard_merge_buffer(drive, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 1312 | return ide_do_start_stop(drive, disk, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1313 | IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1314 | case MTEOM: |
| 1315 | idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1316 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1317 | case MTERASE: |
| 1318 | (void)idetape_rewind_tape(drive); |
| 1319 | idetape_create_erase_cmd(&pc); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1320 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1321 | case MTSETBLK: |
| 1322 | if (mt_count) { |
| 1323 | if (mt_count < tape->blk_size || |
| 1324 | mt_count % tape->blk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1326 | tape->user_bs_factor = mt_count / tape->blk_size; |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1327 | clear_bit(ilog2(IDE_AFLAG_DETECT_BS), |
| 1328 | &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1329 | } else |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1330 | set_bit(ilog2(IDE_AFLAG_DETECT_BS), |
| 1331 | &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1332 | return 0; |
| 1333 | case MTSEEK: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1334 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1335 | return idetape_position_tape(drive, |
| 1336 | mt_count * tape->user_bs_factor, tape->partition, 0); |
| 1337 | case MTSETPART: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1338 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1339 | return idetape_position_tape(drive, 0, mt_count, 0); |
| 1340 | case MTFSR: |
| 1341 | case MTBSR: |
| 1342 | case MTLOCK: |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1343 | retval = ide_set_media_lock(drive, disk, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1344 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | return retval; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1346 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
| 1347 | return 0; |
| 1348 | case MTUNLOCK: |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1349 | retval = ide_set_media_lock(drive, disk, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1350 | if (retval) |
| 1351 | return retval; |
| 1352 | tape->door_locked = DOOR_UNLOCKED; |
| 1353 | return 0; |
| 1354 | default: |
| 1355 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 1356 | mt_op); |
| 1357 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1362 | * Our character device ioctls. General mtio.h magnetic io commands are |
| 1363 | * supported here, and not in the corresponding block interface. Our own |
| 1364 | * ide-tape ioctls are supported on both interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1366 | static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, |
| 1367 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1369 | struct ide_tape_obj *tape = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | ide_drive_t *drive = tape->drive; |
| 1371 | struct mtop mtop; |
| 1372 | struct mtget mtget; |
| 1373 | struct mtpos mtpos; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1374 | int block_offset = 0, position = tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | void __user *argp = (void __user *)arg; |
| 1376 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1377 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1379 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1380 | ide_tape_flush_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | idetape_flush_tape_buffers(drive); |
| 1382 | } |
| 1383 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1384 | block_offset = tape->valid / |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1385 | (tape->blk_size * tape->user_bs_factor); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1386 | position = ide_tape_read_position(drive); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1387 | if (position < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | return -EIO; |
| 1389 | } |
| 1390 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1391 | case MTIOCTOP: |
| 1392 | if (copy_from_user(&mtop, argp, sizeof(struct mtop))) |
| 1393 | return -EFAULT; |
| 1394 | return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); |
| 1395 | case MTIOCGET: |
| 1396 | memset(&mtget, 0, sizeof(struct mtget)); |
| 1397 | mtget.mt_type = MT_ISSCSI2; |
| 1398 | mtget.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 1399 | mtget.mt_dsreg = |
| 1400 | ((tape->blk_size * tape->user_bs_factor) |
| 1401 | << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1402 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1403 | if (tape->drv_write_prot) |
| 1404 | mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); |
| 1405 | |
| 1406 | if (copy_to_user(argp, &mtget, sizeof(struct mtget))) |
| 1407 | return -EFAULT; |
| 1408 | return 0; |
| 1409 | case MTIOCPOS: |
| 1410 | mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 1411 | if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) |
| 1412 | return -EFAULT; |
| 1413 | return 0; |
| 1414 | default: |
| 1415 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1416 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1417 | return idetape_blkdev_ioctl(drive, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1421 | /* |
| 1422 | * Do a mode sense page 0 with block descriptor and if it succeeds set the tape |
| 1423 | * block size with the reported value. |
| 1424 | */ |
| 1425 | static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) |
| 1426 | { |
| 1427 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1428 | struct ide_atapi_pc pc; |
Borislav Petkov | 837272b | 2009-05-04 09:48:57 +0200 | [diff] [blame] | 1429 | u8 buf[12]; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1430 | |
| 1431 | idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); |
Borislav Petkov | 837272b | 2009-05-04 09:48:57 +0200 | [diff] [blame] | 1432 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1433 | printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1434 | if (tape->blk_size == 0) { |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1435 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
| 1436 | "block size, assuming 32k\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1437 | tape->blk_size = 32768; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1438 | } |
| 1439 | return; |
| 1440 | } |
Borislav Petkov | 837272b | 2009-05-04 09:48:57 +0200 | [diff] [blame] | 1441 | tape->blk_size = (buf[4 + 5] << 16) + |
| 1442 | (buf[4 + 6] << 8) + |
| 1443 | buf[4 + 7]; |
| 1444 | tape->drv_write_prot = (buf[2] & 0x80) >> 7; |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1445 | |
| 1446 | ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d", |
| 1447 | tape->blk_size, tape->drv_write_prot); |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1450 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | { |
| 1452 | unsigned int minor = iminor(inode), i = minor & ~0xc0; |
| 1453 | ide_drive_t *drive; |
| 1454 | idetape_tape_t *tape; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | int retval; |
| 1456 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1457 | if (i >= MAX_HWIFS * MAX_DRIVES) |
| 1458 | return -ENXIO; |
| 1459 | |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1460 | lock_kernel(); |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 1461 | tape = ide_tape_get(NULL, true, i); |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1462 | if (!tape) { |
| 1463 | unlock_kernel(); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1464 | return -ENXIO; |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1465 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1466 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1467 | drive = tape->drive; |
| 1468 | filp->private_data = tape; |
| 1469 | |
| 1470 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /* |
| 1473 | * We really want to do nonseekable_open(inode, filp); here, but some |
| 1474 | * versions of tar incorrectly call lseek on tapes and bail out if that |
| 1475 | * fails. So we disallow pread() and pwrite(), but permit lseeks. |
| 1476 | */ |
| 1477 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 1478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1480 | if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | retval = -EBUSY; |
| 1482 | goto out_put_tape; |
| 1483 | } |
| 1484 | |
| 1485 | retval = idetape_wait_ready(drive, 60 * HZ); |
| 1486 | if (retval) { |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1487 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); |
| 1489 | goto out_put_tape; |
| 1490 | } |
| 1491 | |
Borislav Petkov | 79ca743 | 2009-06-15 07:32:04 +0200 | [diff] [blame] | 1492 | ide_tape_read_position(drive); |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1493 | if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | (void)idetape_rewind_tape(drive); |
| 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | /* Read block size and write protect status from drive. */ |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1497 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
| 1499 | /* Set write protect flag if device is opened as read-only. */ |
| 1500 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) |
| 1501 | tape->write_prot = 1; |
| 1502 | else |
| 1503 | tape->write_prot = tape->drv_write_prot; |
| 1504 | |
| 1505 | /* Make sure drive isn't write protected if user wants to write. */ |
| 1506 | if (tape->write_prot) { |
| 1507 | if ((filp->f_flags & O_ACCMODE) == O_WRONLY || |
| 1508 | (filp->f_flags & O_ACCMODE) == O_RDWR) { |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1509 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | retval = -EROFS; |
| 1511 | goto out_put_tape; |
| 1512 | } |
| 1513 | } |
| 1514 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1515 | /* Lock the tape drive door so user can't eject. */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1516 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1517 | if (!ide_set_media_lock(drive, tape->disk, 1)) { |
Bartlomiej Zolnierkiewicz | 385a4b8 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 1518 | if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) |
| 1519 | tape->door_locked = DOOR_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1522 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | return 0; |
| 1524 | |
| 1525 | out_put_tape: |
| 1526 | ide_tape_put(tape); |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1527 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | return retval; |
| 1529 | } |
| 1530 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1531 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
| 1533 | idetape_tape_t *tape = drive->driver_data; |
| 1534 | |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1535 | ide_tape_flush_merge_buffer(drive); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1536 | tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); |
| 1537 | if (tape->buf != NULL) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1538 | idetape_pad_zeros(drive, tape->blk_size * |
| 1539 | (tape->user_bs_factor - 1)); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1540 | kfree(tape->buf); |
| 1541 | tape->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } |
| 1543 | idetape_write_filemark(drive); |
| 1544 | idetape_flush_tape_buffers(drive); |
| 1545 | idetape_flush_tape_buffers(drive); |
| 1546 | } |
| 1547 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1548 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1550 | struct ide_tape_obj *tape = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | ide_drive_t *drive = tape->drive; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | unsigned int minor = iminor(inode); |
| 1553 | |
| 1554 | lock_kernel(); |
| 1555 | tape = drive->driver_data; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1556 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1557 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1559 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | idetape_write_release(drive, minor); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1561 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | if (minor < 128) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1563 | ide_tape_discard_merge_buffer(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
Borislav Petkov | f64eee7 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1565 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1566 | if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), |
| 1567 | &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | (void) idetape_rewind_tape(drive); |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1569 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1570 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (tape->door_locked == DOOR_LOCKED) { |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1572 | if (!ide_set_media_lock(drive, tape->disk, 0)) |
Bartlomiej Zolnierkiewicz | 385a4b8 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 1573 | tape->door_locked = DOOR_UNLOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
| 1575 | } |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1576 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | ide_tape_put(tape); |
| 1578 | unlock_kernel(); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1582 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1585 | struct ide_atapi_pc pc; |
Bartlomiej Zolnierkiewicz | 41fa9f8 | 2009-03-31 20:15:25 +0200 | [diff] [blame] | 1586 | u8 pc_buf[256]; |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 1587 | char fw_rev[4], vendor_id[8], product_id[16]; |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | idetape_create_inquiry_cmd(&pc); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1590 | if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) { |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1591 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
| 1592 | tape->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | return; |
| 1594 | } |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1595 | memcpy(vendor_id, &pc_buf[8], 8); |
| 1596 | memcpy(product_id, &pc_buf[16], 16); |
| 1597 | memcpy(fw_rev, &pc_buf[32], 4); |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1598 | |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 1599 | ide_fixstring(vendor_id, 8, 0); |
| 1600 | ide_fixstring(product_id, 16, 0); |
| 1601 | ide_fixstring(fw_rev, 4, 0); |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1602 | |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 1603 | printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n", |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1604 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1608 | * Ask the tape about its various parameters. In particular, we will adjust our |
| 1609 | * data transfer buffer size to the recommended value as returned by the tape. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1611 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
| 1613 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1614 | struct ide_atapi_pc pc; |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1615 | u8 buf[24], *caps; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1616 | u8 speed, max_speed; |
Borislav Petkov | 47314fa | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 1617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1619 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1620 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
| 1621 | " some default values\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1622 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1623 | put_unaligned(52, (u16 *)&tape->caps[12]); |
| 1624 | put_unaligned(540, (u16 *)&tape->caps[14]); |
| 1625 | put_unaligned(6*52, (u16 *)&tape->caps[16]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | return; |
| 1627 | } |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1628 | caps = buf + 4 + buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1630 | /* convert to host order and save for later use */ |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1631 | speed = be16_to_cpup((__be16 *)&caps[14]); |
| 1632 | max_speed = be16_to_cpup((__be16 *)&caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1634 | *(u16 *)&caps[8] = max_speed; |
| 1635 | *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]); |
| 1636 | *(u16 *)&caps[14] = speed; |
| 1637 | *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1638 | |
| 1639 | if (!speed) { |
| 1640 | printk(KERN_INFO "ide-tape: %s: invalid tape speed " |
| 1641 | "(assuming 650KB/sec)\n", drive->name); |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1642 | *(u16 *)&caps[14] = 650; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | } |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1644 | if (!max_speed) { |
| 1645 | printk(KERN_INFO "ide-tape: %s: invalid max_speed " |
| 1646 | "(assuming 650KB/sec)\n", drive->name); |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1647 | *(u16 *)&caps[8] = 650; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1650 | memcpy(&tape->caps, caps, 20); |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1651 | |
| 1652 | /* device lacks locking support according to capabilities page */ |
| 1653 | if ((caps[6] & 1) == 0) |
Bartlomiej Zolnierkiewicz | 42619d3 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 1654 | drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1655 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1656 | if (caps[7] & 0x02) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1657 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1658 | else if (caps[7] & 0x04) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1659 | tape->blk_size = 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1662 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1663 | #define ide_tape_devset_get(name, field) \ |
| 1664 | static int get_##name(ide_drive_t *drive) \ |
| 1665 | { \ |
| 1666 | idetape_tape_t *tape = drive->driver_data; \ |
| 1667 | return tape->field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1669 | |
| 1670 | #define ide_tape_devset_set(name, field) \ |
| 1671 | static int set_##name(ide_drive_t *drive, int arg) \ |
| 1672 | { \ |
| 1673 | idetape_tape_t *tape = drive->driver_data; \ |
| 1674 | tape->field = arg; \ |
| 1675 | return 0; \ |
| 1676 | } |
| 1677 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1678 | #define ide_tape_devset_rw_field(_name, _field) \ |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1679 | ide_tape_devset_get(_name, _field) \ |
| 1680 | ide_tape_devset_set(_name, _field) \ |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1681 | IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name) |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1682 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1683 | #define ide_tape_devset_r_field(_name, _field) \ |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1684 | ide_tape_devset_get(_name, _field) \ |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1685 | IDE_DEVSET(_name, 0, get_##_name, NULL) |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1686 | |
| 1687 | static int mulf_tdsc(ide_drive_t *drive) { return 1000; } |
| 1688 | static int divf_tdsc(ide_drive_t *drive) { return HZ; } |
| 1689 | static int divf_buffer(ide_drive_t *drive) { return 2; } |
| 1690 | static int divf_buffer_size(ide_drive_t *drive) { return 1024; } |
| 1691 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1692 | ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1693 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1694 | ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1695 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1696 | ide_tape_devset_r_field(avg_speed, avg_speed); |
| 1697 | ide_tape_devset_r_field(speed, caps[14]); |
| 1698 | ide_tape_devset_r_field(buffer, caps[16]); |
| 1699 | ide_tape_devset_r_field(buffer_size, buffer_size); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1700 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1701 | static const struct ide_proc_devset idetape_settings[] = { |
| 1702 | __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL), |
| 1703 | __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer), |
| 1704 | __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size), |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1705 | __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL), |
| 1706 | __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL), |
| 1707 | __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, |
| 1708 | mulf_tdsc, divf_tdsc), |
Hannes Eder | 71bfc7a | 2009-03-05 16:10:56 +0100 | [diff] [blame] | 1709 | { NULL }, |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1710 | }; |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1711 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | |
| 1713 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1714 | * The function below is called to: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1716 | * 1. Initialize our various state variables. |
| 1717 | * 2. Ask the tape for its capabilities. |
| 1718 | * 3. Allocate a buffer which will be used for data transfer. The buffer size |
| 1719 | * is chosen based on the recommendation which we received in step 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1721 | * Note that at this point ide.c already assigned us an irq, so that we can |
| 1722 | * queue requests here and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1724 | static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | { |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1726 | unsigned long t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | int speed; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1728 | int buffer_size; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1729 | u16 *ctl = (u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1731 | ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor); |
| 1732 | |
| 1733 | drive->pc_callback = ide_tape_callback; |
Borislav Petkov | 776bb02 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 1734 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1735 | drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP; |
| 1736 | |
Bartlomiej Zolnierkiewicz | 4166c19 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 1737 | if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { |
| 1738 | printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", |
| 1739 | tape->name); |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1740 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | } |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | /* Seagate Travan drives do not support DSC overlap. */ |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 1744 | if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401")) |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1745 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
| 1746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | tape->minor = minor; |
| 1748 | tape->name[0] = 'h'; |
| 1749 | tape->name[1] = 't'; |
| 1750 | tape->name[2] = '0' + minor; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1751 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 1752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | idetape_get_inquiry_results(drive); |
| 1754 | idetape_get_mode_sense_results(drive); |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1755 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | tape->user_bs_factor = 1; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1757 | tape->buffer_size = *ctl * tape->blk_size; |
| 1758 | while (tape->buffer_size > 0xffff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1760 | *ctl /= 2; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1761 | tape->buffer_size = *ctl * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | } |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1763 | buffer_size = tape->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1765 | /* select the "best" DSC read/write polling freq */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1766 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1768 | t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
| 1770 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1771 | * Ensure that the number we got makes sense; limit it within |
| 1772 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | */ |
Harvey Harrison | a792bd5 | 2008-07-15 21:21:41 +0200 | [diff] [blame] | 1774 | tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, |
| 1775 | IDETAPE_DSC_RW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1777 | "%lums tDSC%s\n", |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1778 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1779 | (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, |
| 1780 | tape->buffer_size / 1024, |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1781 | tape->best_dsc_rw_freq * 1000 / HZ, |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1782 | (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | |
Bartlomiej Zolnierkiewicz | 1e874f4 | 2008-10-10 22:39:27 +0200 | [diff] [blame] | 1784 | ide_proc_register_driver(drive, tape->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1787 | static void ide_tape_remove(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | { |
| 1789 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1791 | ide_proc_unregister_driver(drive, tape->driver); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1792 | device_del(&tape->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | ide_unregister_region(tape->disk); |
| 1794 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1795 | mutex_lock(&idetape_ref_mutex); |
| 1796 | put_device(&tape->dev); |
| 1797 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1800 | static void ide_tape_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | { |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1802 | struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | ide_drive_t *drive = tape->drive; |
| 1804 | struct gendisk *g = tape->disk; |
| 1805 | |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1806 | BUG_ON(tape->valid); |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1807 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1808 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | drive->driver_data = NULL; |
Tony Jones | dbc1272 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 1810 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1811 | device_destroy(idetape_sysfs_class, |
| 1812 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | idetape_devs[tape->minor] = NULL; |
| 1814 | g->private_data = NULL; |
| 1815 | put_disk(g); |
| 1816 | kfree(tape); |
| 1817 | } |
| 1818 | |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 1819 | #ifdef CONFIG_IDE_PROC_FS |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1820 | static int idetape_name_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | { |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1822 | ide_drive_t *drive = (ide_drive_t *) m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1825 | seq_printf(m, "%s\n", tape->name); |
| 1826 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1829 | static int idetape_name_proc_open(struct inode *inode, struct file *file) |
| 1830 | { |
| 1831 | return single_open(file, idetape_name_proc_show, PDE(inode)->data); |
| 1832 | } |
| 1833 | |
| 1834 | static const struct file_operations idetape_name_proc_fops = { |
| 1835 | .owner = THIS_MODULE, |
| 1836 | .open = idetape_name_proc_open, |
| 1837 | .read = seq_read, |
| 1838 | .llseek = seq_lseek, |
| 1839 | .release = single_release, |
| 1840 | }; |
| 1841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | static ide_proc_entry_t idetape_proc[] = { |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1843 | { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops }, |
| 1844 | { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops }, |
| 1845 | {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | }; |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 1847 | |
| 1848 | static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive) |
| 1849 | { |
| 1850 | return idetape_proc; |
| 1851 | } |
| 1852 | |
| 1853 | static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive) |
| 1854 | { |
| 1855 | return idetape_settings; |
| 1856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | #endif |
| 1858 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1859 | static int ide_tape_probe(ide_drive_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 1861 | static struct ide_driver idetape_driver = { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1862 | .gen_driver = { |
Laurent Riffard | 4ef3b8f | 2005-11-18 22:15:40 +0100 | [diff] [blame] | 1863 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1864 | .name = "ide-tape", |
| 1865 | .bus = &ide_bus_type, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1866 | }, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1867 | .probe = ide_tape_probe, |
| 1868 | .remove = ide_tape_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | .version = IDETAPE_VERSION, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | .do_request = idetape_do_request, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1871 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 1872 | .proc_entries = ide_tape_proc_entries, |
| 1873 | .proc_devsets = ide_tape_proc_devsets, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1874 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | }; |
| 1876 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1877 | /* Our character device supporting functions, passed to register_chrdev. */ |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 1878 | static const struct file_operations idetape_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | .owner = THIS_MODULE, |
| 1880 | .read = idetape_chrdev_read, |
| 1881 | .write = idetape_chrdev_write, |
| 1882 | .ioctl = idetape_chrdev_ioctl, |
| 1883 | .open = idetape_chrdev_open, |
| 1884 | .release = idetape_chrdev_release, |
| 1885 | }; |
| 1886 | |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1887 | static int idetape_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | { |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 1889 | struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1891 | if (!tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | return -ENXIO; |
| 1893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | return 0; |
| 1895 | } |
| 1896 | |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1897 | static int idetape_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1899 | struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | |
| 1901 | ide_tape_put(tape); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | return 0; |
| 1903 | } |
| 1904 | |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1905 | static int idetape_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | unsigned int cmd, unsigned long arg) |
| 1907 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1908 | struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | ide_drive_t *drive = tape->drive; |
Al Viro | 1bddd9e | 2008-09-02 17:19:43 -0400 | [diff] [blame] | 1910 | int err = generic_ide_ioctl(drive, bdev, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | if (err == -EINVAL) |
| 1912 | err = idetape_blkdev_ioctl(drive, cmd, arg); |
| 1913 | return err; |
| 1914 | } |
| 1915 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1916 | static const struct block_device_operations idetape_block_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | .owner = THIS_MODULE, |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1918 | .open = idetape_open, |
| 1919 | .release = idetape_release, |
| 1920 | .locked_ioctl = idetape_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | }; |
| 1922 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1923 | static int ide_tape_probe(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | { |
| 1925 | idetape_tape_t *tape; |
| 1926 | struct gendisk *g; |
| 1927 | int minor; |
| 1928 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1929 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
| 1930 | |
| 1931 | if (!strstr(DRV_NAME, drive->driver_req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 1933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | if (drive->media != ide_tape) |
| 1935 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 1936 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1937 | if ((drive->dev_flags & IDE_DFLAG_ID_READ) && |
| 1938 | ide_check_atapi_device(drive, DRV_NAME) == 0) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1939 | printk(KERN_ERR "ide-tape: %s: not supported by this version of" |
| 1940 | " the driver\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | goto failed; |
| 1942 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1943 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | if (tape == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1945 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
| 1946 | drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | goto failed; |
| 1948 | } |
| 1949 | |
| 1950 | g = alloc_disk(1 << PARTN_BITS); |
| 1951 | if (!g) |
| 1952 | goto out_free_tape; |
| 1953 | |
| 1954 | ide_init_disk(g, drive); |
| 1955 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1956 | tape->dev.parent = &drive->gendev; |
| 1957 | tape->dev.release = ide_tape_release; |
| 1958 | dev_set_name(&tape->dev, dev_name(&drive->gendev)); |
| 1959 | |
| 1960 | if (device_register(&tape->dev)) |
| 1961 | goto out_free_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | |
| 1963 | tape->drive = drive; |
| 1964 | tape->driver = &idetape_driver; |
| 1965 | tape->disk = g; |
| 1966 | |
| 1967 | g->private_data = &tape->driver; |
| 1968 | |
| 1969 | drive->driver_data = tape; |
| 1970 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 1971 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | for (minor = 0; idetape_devs[minor]; minor++) |
| 1973 | ; |
| 1974 | idetape_devs[minor] = tape; |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 1975 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
| 1977 | idetape_setup(drive, tape, minor); |
| 1978 | |
Greg Kroah-Hartman | 3ee074b | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1979 | device_create(idetape_sysfs_class, &drive->gendev, |
| 1980 | MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name); |
| 1981 | device_create(idetape_sysfs_class, &drive->gendev, |
| 1982 | MKDEV(IDETAPE_MAJOR, minor + 128), NULL, |
| 1983 | "n%s", tape->name); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 1984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | g->fops = &idetape_block_ops; |
| 1986 | ide_register_region(g); |
| 1987 | |
| 1988 | return 0; |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1989 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1990 | out_free_disk: |
| 1991 | put_disk(g); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | out_free_tape: |
| 1993 | kfree(tape); |
| 1994 | failed: |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1995 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1998 | static void __exit idetape_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2000 | driver_unregister(&idetape_driver.gen_driver); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2001 | class_destroy(idetape_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
| 2003 | } |
| 2004 | |
Bartlomiej Zolnierkiewicz | 17514e8 | 2005-11-19 22:24:35 +0100 | [diff] [blame] | 2005 | static int __init idetape_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | { |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2007 | int error = 1; |
| 2008 | idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); |
| 2009 | if (IS_ERR(idetape_sysfs_class)) { |
| 2010 | idetape_sysfs_class = NULL; |
| 2011 | printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); |
| 2012 | error = -EBUSY; |
| 2013 | goto out; |
| 2014 | } |
| 2015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2017 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
| 2018 | " interface\n"); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2019 | error = -EBUSY; |
| 2020 | goto out_free_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2022 | |
| 2023 | error = driver_register(&idetape_driver.gen_driver); |
| 2024 | if (error) |
| 2025 | goto out_free_driver; |
| 2026 | |
| 2027 | return 0; |
| 2028 | |
| 2029 | out_free_driver: |
| 2030 | driver_unregister(&idetape_driver.gen_driver); |
| 2031 | out_free_class: |
| 2032 | class_destroy(idetape_sysfs_class); |
| 2033 | out: |
| 2034 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Kay Sievers | 263756e | 2005-12-12 18:03:44 +0100 | [diff] [blame] | 2037 | MODULE_ALIAS("ide:*m-tape*"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | module_init(idetape_init); |
| 2039 | module_exit(idetape_exit); |
| 2040 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); |
Borislav Petkov | 9c14576 | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2041 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
| 2042 | MODULE_LICENSE("GPL"); |