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