Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying |
| 3 | file Documentation/scsi/st.txt for more information. |
| 4 | |
| 5 | History: |
| 6 | Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara. |
| 7 | Contribution and ideas from several people including (in alphabetical |
| 8 | order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk, |
| 9 | Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky, |
| 10 | Michael Schaefer, J"org Weule, and Eric Youngdale. |
| 11 | |
| 12 | Copyright 1992 - 2005 Kai Makisara |
| 13 | email Kai.Makisara@kolumbus.fi |
| 14 | |
| 15 | Some small formal changes - aeb, 950809 |
| 16 | |
| 17 | Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support |
| 18 | */ |
| 19 | |
Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 20 | static const char *verstr = "20050830"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <linux/module.h> |
| 23 | |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/mtio.h> |
Kai Makisara | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 32 | #include <linux/cdrom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/ioctl.h> |
| 34 | #include <linux/fcntl.h> |
| 35 | #include <linux/spinlock.h> |
| 36 | #include <linux/blkdev.h> |
| 37 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/cdev.h> |
| 39 | #include <linux/delay.h> |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 40 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <asm/uaccess.h> |
| 43 | #include <asm/dma.h> |
| 44 | #include <asm/system.h> |
| 45 | |
| 46 | #include <scsi/scsi.h> |
| 47 | #include <scsi/scsi_dbg.h> |
| 48 | #include <scsi/scsi_device.h> |
| 49 | #include <scsi/scsi_driver.h> |
| 50 | #include <scsi/scsi_eh.h> |
| 51 | #include <scsi/scsi_host.h> |
| 52 | #include <scsi/scsi_ioctl.h> |
Kai Makisara | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 53 | #include <scsi/sg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | /* The driver prints some debugging information on the console if DEBUG |
| 57 | is defined and non-zero. */ |
| 58 | #define DEBUG 0 |
| 59 | |
| 60 | #if DEBUG |
| 61 | /* The message level for the debug messages is currently set to KERN_NOTICE |
| 62 | so that people can easily see the messages. Later when the debugging messages |
| 63 | in the drivers are more widely classified, this may be changed to KERN_DEBUG. */ |
| 64 | #define ST_DEB_MSG KERN_NOTICE |
| 65 | #define DEB(a) a |
| 66 | #define DEBC(a) if (debugging) { a ; } |
| 67 | #else |
| 68 | #define DEB(a) |
| 69 | #define DEBC(a) |
| 70 | #endif |
| 71 | |
| 72 | #define ST_KILOBYTE 1024 |
| 73 | |
| 74 | #include "st_options.h" |
| 75 | #include "st.h" |
| 76 | |
| 77 | static int buffer_kbs; |
| 78 | static int max_sg_segs; |
| 79 | static int try_direct_io = TRY_DIRECT_IO; |
| 80 | static int try_rdio = 1; |
| 81 | static int try_wdio = 1; |
| 82 | |
| 83 | static int st_dev_max; |
| 84 | static int st_nr_dev; |
| 85 | |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 86 | static struct class *st_sysfs_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | MODULE_AUTHOR("Kai Makisara"); |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 89 | MODULE_DESCRIPTION("SCSI tape (st) driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | MODULE_LICENSE("GPL"); |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 91 | MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | /* Set 'perm' (4th argument) to 0 to disable module_param's definition |
| 94 | * of sysfs parameters (which module_param doesn't yet support). |
| 95 | * Sysfs parameters defined explicitly later. |
| 96 | */ |
| 97 | module_param_named(buffer_kbs, buffer_kbs, int, 0); |
| 98 | MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)"); |
| 99 | module_param_named(max_sg_segs, max_sg_segs, int, 0); |
| 100 | MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)"); |
| 101 | module_param_named(try_direct_io, try_direct_io, int, 0); |
| 102 | MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)"); |
| 103 | |
| 104 | /* Extra parameters for testing */ |
| 105 | module_param_named(try_rdio, try_rdio, int, 0); |
| 106 | MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible"); |
| 107 | module_param_named(try_wdio, try_wdio, int, 0); |
| 108 | MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible"); |
| 109 | |
| 110 | #ifndef MODULE |
| 111 | static int write_threshold_kbs; /* retained for compatibility */ |
| 112 | static struct st_dev_parm { |
| 113 | char *name; |
| 114 | int *val; |
| 115 | } parms[] __initdata = { |
| 116 | { |
| 117 | "buffer_kbs", &buffer_kbs |
| 118 | }, |
| 119 | { /* Retained for compatibility with 2.4 */ |
| 120 | "write_threshold_kbs", &write_threshold_kbs |
| 121 | }, |
| 122 | { |
| 123 | "max_sg_segs", NULL |
| 124 | }, |
| 125 | { |
| 126 | "try_direct_io", &try_direct_io |
| 127 | } |
| 128 | }; |
| 129 | #endif |
| 130 | |
| 131 | /* Restrict the number of modes so that names for all are assigned */ |
| 132 | #if ST_NBR_MODES > 16 |
| 133 | #error "Maximum number of modes is 16" |
| 134 | #endif |
| 135 | /* Bit reversed order to get same names for same minors with all |
| 136 | mode counts */ |
Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 137 | static const char *st_formats[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | "", "r", "k", "s", "l", "t", "o", "u", |
| 139 | "m", "v", "p", "x", "a", "y", "q", "z"}; |
| 140 | |
| 141 | /* The default definitions have been moved to st_options.h */ |
| 142 | |
| 143 | #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE) |
| 144 | |
| 145 | /* The buffer size should fit into the 24 bits for length in the |
| 146 | 6-byte SCSI read and write commands. */ |
| 147 | #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1) |
| 148 | #error "Buffer size should not exceed (2 << 24 - 1) bytes!" |
| 149 | #endif |
| 150 | |
| 151 | static int debugging = DEBUG; |
| 152 | |
| 153 | #define MAX_RETRIES 0 |
| 154 | #define MAX_WRITE_RETRIES 0 |
| 155 | #define MAX_READY_RETRIES 0 |
| 156 | #define NO_TAPE NOT_READY |
| 157 | |
| 158 | #define ST_TIMEOUT (900 * HZ) |
| 159 | #define ST_LONG_TIMEOUT (14000 * HZ) |
| 160 | |
| 161 | /* Remove mode bits and auto-rewind bit (7) */ |
| 162 | #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \ |
| 163 | (iminor(x) & ~(-1 << ST_MODE_SHIFT)) ) |
| 164 | #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT) |
| 165 | |
| 166 | /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */ |
| 167 | #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \ |
| 168 | (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) ) |
| 169 | |
| 170 | /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower |
| 171 | 24 bits) */ |
| 172 | #define SET_DENS_AND_BLK 0x10001 |
| 173 | |
| 174 | static DEFINE_RWLOCK(st_dev_arr_lock); |
| 175 | |
| 176 | static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE; |
| 177 | static int st_max_sg_segs = ST_MAX_SG; |
| 178 | |
| 179 | static struct scsi_tape **scsi_tapes = NULL; |
| 180 | |
| 181 | static int modes_defined; |
| 182 | |
| 183 | static struct st_buffer *new_tape_buffer(int, int, int); |
| 184 | static int enlarge_buffer(struct st_buffer *, int, int); |
| 185 | static void normalize_buffer(struct st_buffer *); |
| 186 | static int append_to_buffer(const char __user *, struct st_buffer *, int); |
| 187 | static int from_buffer(struct st_buffer *, char __user *, int); |
| 188 | static void move_buffer_data(struct st_buffer *, int); |
| 189 | static void buf_to_sg(struct st_buffer *, unsigned int); |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | static int sgl_map_user_pages(struct scatterlist *, const unsigned int, |
| 192 | unsigned long, size_t, int); |
| 193 | static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int); |
| 194 | |
| 195 | static int st_probe(struct device *); |
| 196 | static int st_remove(struct device *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | static void do_create_driverfs_files(void); |
| 199 | static void do_remove_driverfs_files(void); |
| 200 | static void do_create_class_files(struct scsi_tape *, int, int); |
| 201 | |
| 202 | static struct scsi_driver st_template = { |
| 203 | .owner = THIS_MODULE, |
| 204 | .gendrv = { |
| 205 | .name = "st", |
| 206 | .probe = st_probe, |
| 207 | .remove = st_remove, |
| 208 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | static int st_compression(struct scsi_tape *, int); |
| 212 | |
| 213 | static int find_partition(struct scsi_tape *); |
| 214 | static int switch_partition(struct scsi_tape *); |
| 215 | |
| 216 | static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long); |
| 217 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 218 | static void scsi_tape_release(struct kref *); |
| 219 | |
| 220 | #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref) |
| 221 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 222 | static DEFINE_MUTEX(st_ref_mutex); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | #include "osst_detect.h" |
| 226 | #ifndef SIGS_FROM_OSST |
| 227 | #define SIGS_FROM_OSST \ |
| 228 | {"OnStream", "SC-", "", "osst"}, \ |
| 229 | {"OnStream", "DI-", "", "osst"}, \ |
| 230 | {"OnStream", "DP-", "", "osst"}, \ |
| 231 | {"OnStream", "USB", "", "osst"}, \ |
| 232 | {"OnStream", "FW-", "", "osst"} |
| 233 | #endif |
| 234 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 235 | static struct scsi_tape *scsi_tape_get(int dev) |
| 236 | { |
| 237 | struct scsi_tape *STp = NULL; |
| 238 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 239 | mutex_lock(&st_ref_mutex); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 240 | write_lock(&st_dev_arr_lock); |
| 241 | |
| 242 | if (dev < st_dev_max && scsi_tapes != NULL) |
| 243 | STp = scsi_tapes[dev]; |
| 244 | if (!STp) goto out; |
| 245 | |
| 246 | kref_get(&STp->kref); |
| 247 | |
| 248 | if (!STp->device) |
| 249 | goto out_put; |
| 250 | |
| 251 | if (scsi_device_get(STp->device)) |
| 252 | goto out_put; |
| 253 | |
| 254 | goto out; |
| 255 | |
| 256 | out_put: |
| 257 | kref_put(&STp->kref, scsi_tape_release); |
| 258 | STp = NULL; |
| 259 | out: |
| 260 | write_unlock(&st_dev_arr_lock); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 261 | mutex_unlock(&st_ref_mutex); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 262 | return STp; |
| 263 | } |
| 264 | |
| 265 | static void scsi_tape_put(struct scsi_tape *STp) |
| 266 | { |
| 267 | struct scsi_device *sdev = STp->device; |
| 268 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 269 | mutex_lock(&st_ref_mutex); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 270 | kref_put(&STp->kref, scsi_tape_release); |
| 271 | scsi_device_put(sdev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 272 | mutex_unlock(&st_ref_mutex); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | struct st_reject_data { |
| 276 | char *vendor; |
| 277 | char *model; |
| 278 | char *rev; |
| 279 | char *driver_hint; /* Name of the correct driver, NULL if unknown */ |
| 280 | }; |
| 281 | |
| 282 | static struct st_reject_data reject_list[] = { |
| 283 | /* {"XXX", "Yy-", "", NULL}, example */ |
| 284 | SIGS_FROM_OSST, |
| 285 | {NULL, }}; |
| 286 | |
| 287 | /* If the device signature is on the list of incompatible drives, the |
| 288 | function returns a pointer to the name of the correct driver (if known) */ |
| 289 | static char * st_incompatible(struct scsi_device* SDp) |
| 290 | { |
| 291 | struct st_reject_data *rp; |
| 292 | |
| 293 | for (rp=&(reject_list[0]); rp->vendor != NULL; rp++) |
| 294 | if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) && |
| 295 | !strncmp(rp->model, SDp->model, strlen(rp->model)) && |
| 296 | !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) { |
| 297 | if (rp->driver_hint) |
| 298 | return rp->driver_hint; |
| 299 | else |
| 300 | return "unknown"; |
| 301 | } |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | static inline char *tape_name(struct scsi_tape *tape) |
| 307 | { |
| 308 | return tape->disk->disk_name; |
| 309 | } |
| 310 | |
| 311 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 312 | static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | const u8 *ucp; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 315 | const u8 *sense = SRpnt->sense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 317 | s->have_sense = scsi_normalize_sense(SRpnt->sense, |
| 318 | SCSI_SENSE_BUFFERSIZE, &s->sense_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | s->flags = 0; |
| 320 | |
| 321 | if (s->have_sense) { |
| 322 | s->deferred = 0; |
| 323 | s->remainder_valid = |
| 324 | scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64); |
| 325 | switch (sense[0] & 0x7f) { |
| 326 | case 0x71: |
| 327 | s->deferred = 1; |
| 328 | case 0x70: |
| 329 | s->fixed_format = 1; |
| 330 | s->flags = sense[2] & 0xe0; |
| 331 | break; |
| 332 | case 0x73: |
| 333 | s->deferred = 1; |
| 334 | case 0x72: |
| 335 | s->fixed_format = 0; |
| 336 | ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4); |
| 337 | s->flags = ucp ? (ucp[3] & 0xe0) : 0; |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* Convert the result to success code */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 345 | static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 347 | int result = SRpnt->result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | u8 scode; |
| 349 | DEB(const char *stp;) |
| 350 | char *name = tape_name(STp); |
| 351 | struct st_cmdstatus *cmdstatp; |
| 352 | |
| 353 | if (!result) |
| 354 | return 0; |
| 355 | |
| 356 | cmdstatp = &STp->buffer->cmdstat; |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 357 | st_analyze_sense(SRpnt, cmdstatp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | if (cmdstatp->have_sense) |
| 360 | scode = STp->buffer->cmdstat.sense_hdr.sense_key; |
| 361 | else |
| 362 | scode = 0; |
| 363 | |
| 364 | DEB( |
| 365 | if (debugging) { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 366 | printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | name, result, |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 368 | SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2], |
| 369 | SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (cmdstatp->have_sense) |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 371 | __scsi_print_sense("st", SRpnt->sense, SCSI_SENSE_BUFFERSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } ) /* end DEB */ |
| 373 | if (!debugging) { /* Abnormal conditions for tape */ |
| 374 | if (!cmdstatp->have_sense) |
| 375 | printk(KERN_WARNING |
| 376 | "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n", |
| 377 | name, result, suggestion(result), |
| 378 | driver_byte(result) & DRIVER_MASK, host_byte(result)); |
| 379 | else if (cmdstatp->have_sense && |
| 380 | scode != NO_SENSE && |
| 381 | scode != RECOVERED_ERROR && |
| 382 | /* scode != UNIT_ATTENTION && */ |
| 383 | scode != BLANK_CHECK && |
| 384 | scode != VOLUME_OVERFLOW && |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 385 | SRpnt->cmd[0] != MODE_SENSE && |
| 386 | SRpnt->cmd[0] != TEST_UNIT_READY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | printk(KERN_WARNING "%s: Error with sense data: ", name); |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 388 | __scsi_print_sense("st", SRpnt->sense, |
| 389 | SCSI_SENSE_BUFFERSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | if (cmdstatp->fixed_format && |
| 394 | STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */ |
| 395 | if (STp->cln_sense_value) |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 396 | STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | STp->cln_sense_mask) == STp->cln_sense_value); |
| 398 | else |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 399 | STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | STp->cln_sense_mask) != 0); |
| 401 | } |
| 402 | if (cmdstatp->have_sense && |
| 403 | cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17) |
| 404 | STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */ |
| 405 | |
| 406 | STp->pos_unknown |= STp->device->was_reset; |
| 407 | |
| 408 | if (cmdstatp->have_sense && |
| 409 | scode == RECOVERED_ERROR |
| 410 | #if ST_RECOVERED_WRITE_FATAL |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 411 | && SRpnt->cmd[0] != WRITE_6 |
| 412 | && SRpnt->cmd[0] != WRITE_FILEMARKS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | #endif |
| 414 | ) { |
| 415 | STp->recover_count++; |
| 416 | STp->recover_reg++; |
| 417 | |
| 418 | DEB( |
| 419 | if (debugging) { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 420 | if (SRpnt->cmd[0] == READ_6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | stp = "read"; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 422 | else if (SRpnt->cmd[0] == WRITE_6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | stp = "write"; |
| 424 | else |
| 425 | stp = "ioctl"; |
| 426 | printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp, |
| 427 | STp->recover_count); |
| 428 | } ) /* end DEB */ |
| 429 | |
| 430 | if (cmdstatp->flags == 0) |
| 431 | return 0; |
| 432 | } |
| 433 | return (-EIO); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /* Wakeup from interrupt */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 438 | static void st_sleep_done(void *data, char *sense, int result, int resid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 440 | struct st_request *SRpnt = data; |
| 441 | struct scsi_tape *STp = SRpnt->stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 443 | memcpy(SRpnt->sense, sense, SCSI_SENSE_BUFFERSIZE); |
| 444 | (STp->buffer)->cmdstat.midlevel_result = SRpnt->result = result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | DEB( STp->write_pending = 0; ) |
| 446 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 447 | if (SRpnt->waiting) |
| 448 | complete(SRpnt->waiting); |
| 449 | } |
| 450 | |
| 451 | static struct st_request *st_allocate_request(void) |
| 452 | { |
| 453 | return kzalloc(sizeof(struct st_request), GFP_KERNEL); |
| 454 | } |
| 455 | |
| 456 | static void st_release_request(struct st_request *streq) |
| 457 | { |
| 458 | kfree(streq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Do the scsi command. Waits until command performed if do_wait is true. |
| 462 | Otherwise write_behind_check() is used to check that the command |
| 463 | has finished. */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 464 | static struct st_request * |
| 465 | st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | int bytes, int direction, int timeout, int retries, int do_wait) |
| 467 | { |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 468 | struct completion *waiting; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 470 | /* if async, make sure there's no command outstanding */ |
| 471 | if (!do_wait && ((STp->buffer)->last_SRpnt)) { |
| 472 | printk(KERN_ERR "%s: Async command already active.\n", |
| 473 | tape_name(STp)); |
| 474 | if (signal_pending(current)) |
| 475 | (STp->buffer)->syscall_result = (-EINTR); |
| 476 | else |
| 477 | (STp->buffer)->syscall_result = (-EBUSY); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (SRpnt == NULL) { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 482 | SRpnt = st_allocate_request(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (SRpnt == NULL) { |
| 484 | DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n", |
| 485 | tape_name(STp)); ); |
| 486 | if (signal_pending(current)) |
| 487 | (STp->buffer)->syscall_result = (-EINTR); |
| 488 | else |
| 489 | (STp->buffer)->syscall_result = (-EBUSY); |
| 490 | return NULL; |
| 491 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 492 | SRpnt->stp = STp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 495 | /* If async IO, set last_SRpnt. This ptr tells write_behind_check |
| 496 | which IO is outstanding. It's nulled out when the IO completes. */ |
| 497 | if (!do_wait) |
| 498 | (STp->buffer)->last_SRpnt = SRpnt; |
| 499 | |
| 500 | waiting = &STp->wait; |
| 501 | init_completion(waiting); |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 502 | SRpnt->waiting = waiting; |
| 503 | |
| 504 | if (!STp->buffer->do_dio) |
| 505 | buf_to_sg(STp->buffer, bytes); |
| 506 | |
| 507 | memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | STp->buffer->cmdstat.have_sense = 0; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 509 | STp->buffer->syscall_result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
brking@us.ibm.com | bb1d107 | 2006-01-23 15:03:22 -0600 | [diff] [blame] | 511 | if (scsi_execute_async(STp->device, cmd, COMMAND_SIZE(cmd[0]), direction, |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 512 | &((STp->buffer)->sg[0]), bytes, (STp->buffer)->sg_segs, |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 513 | timeout, retries, SRpnt, st_sleep_done, GFP_KERNEL)) { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 514 | /* could not allocate the buffer or request was too large */ |
| 515 | (STp->buffer)->syscall_result = (-EBUSY); |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 516 | (STp->buffer)->last_SRpnt = NULL; |
| 517 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 518 | else if (do_wait) { |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 519 | wait_for_completion(waiting); |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 520 | SRpnt->waiting = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt); |
| 522 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return SRpnt; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if |
| 529 | write has been correct but EOM early warning reached, -EIO if write ended in |
| 530 | error or zero if write successful. Asynchronous writes are used only in |
| 531 | variable block mode. */ |
| 532 | static int write_behind_check(struct scsi_tape * STp) |
| 533 | { |
| 534 | int retval = 0; |
| 535 | struct st_buffer *STbuffer; |
| 536 | struct st_partstat *STps; |
| 537 | struct st_cmdstatus *cmdstatp; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 538 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | STbuffer = STp->buffer; |
| 541 | if (!STbuffer->writing) |
| 542 | return 0; |
| 543 | |
| 544 | DEB( |
| 545 | if (STp->write_pending) |
| 546 | STp->nbr_waits++; |
| 547 | else |
| 548 | STp->nbr_finished++; |
| 549 | ) /* end DEB */ |
| 550 | |
| 551 | wait_for_completion(&(STp->wait)); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 552 | SRpnt = STbuffer->last_SRpnt; |
| 553 | STbuffer->last_SRpnt = NULL; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 554 | SRpnt->waiting = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 556 | (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt); |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 557 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | STbuffer->buffer_bytes -= STbuffer->writing; |
| 560 | STps = &(STp->ps[STp->partition]); |
| 561 | if (STps->drv_block >= 0) { |
| 562 | if (STp->block_size == 0) |
| 563 | STps->drv_block++; |
| 564 | else |
| 565 | STps->drv_block += STbuffer->writing / STp->block_size; |
| 566 | } |
| 567 | |
| 568 | cmdstatp = &STbuffer->cmdstat; |
| 569 | if (STbuffer->syscall_result) { |
| 570 | retval = -EIO; |
| 571 | if (cmdstatp->have_sense && !cmdstatp->deferred && |
| 572 | (cmdstatp->flags & SENSE_EOM) && |
| 573 | (cmdstatp->sense_hdr.sense_key == NO_SENSE || |
| 574 | cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) { |
| 575 | /* EOM at write-behind, has all data been written? */ |
| 576 | if (!cmdstatp->remainder_valid || |
| 577 | cmdstatp->uremainder64 == 0) |
| 578 | retval = -ENOSPC; |
| 579 | } |
| 580 | if (retval == -EIO) |
| 581 | STps->drv_block = -1; |
| 582 | } |
| 583 | STbuffer->writing = 0; |
| 584 | |
| 585 | DEB(if (debugging && retval) |
| 586 | printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n", |
| 587 | tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */ |
| 588 | |
| 589 | return retval; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /* Step over EOF if it has been inadvertently crossed (ioctl not used because |
| 594 | it messes up the block number). */ |
| 595 | static int cross_eof(struct scsi_tape * STp, int forward) |
| 596 | { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 597 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | unsigned char cmd[MAX_COMMAND_SIZE]; |
| 599 | |
| 600 | cmd[0] = SPACE; |
| 601 | cmd[1] = 0x01; /* Space FileMarks */ |
| 602 | if (forward) { |
| 603 | cmd[2] = cmd[3] = 0; |
| 604 | cmd[4] = 1; |
| 605 | } else |
| 606 | cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */ |
| 607 | cmd[5] = 0; |
| 608 | |
| 609 | DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n", |
| 610 | tape_name(STp), forward ? "forward" : "backward")); |
| 611 | |
| 612 | SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE, |
| 613 | STp->device->timeout, MAX_RETRIES, 1); |
| 614 | if (!SRpnt) |
| 615 | return (STp->buffer)->syscall_result; |
| 616 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 617 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | SRpnt = NULL; |
| 619 | |
| 620 | if ((STp->buffer)->cmdstat.midlevel_result != 0) |
| 621 | printk(KERN_ERR "%s: Stepping over filemark %s failed.\n", |
| 622 | tape_name(STp), forward ? "forward" : "backward"); |
| 623 | |
| 624 | return (STp->buffer)->syscall_result; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | /* Flush the write buffer (never need to write if variable blocksize). */ |
| 629 | static int flush_write_buffer(struct scsi_tape * STp) |
| 630 | { |
| 631 | int offset, transfer, blks; |
| 632 | int result; |
| 633 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 634 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | struct st_partstat *STps; |
| 636 | |
| 637 | result = write_behind_check(STp); |
| 638 | if (result) |
| 639 | return result; |
| 640 | |
| 641 | result = 0; |
| 642 | if (STp->dirty == 1) { |
| 643 | |
| 644 | offset = (STp->buffer)->buffer_bytes; |
| 645 | transfer = ((offset + STp->block_size - 1) / |
| 646 | STp->block_size) * STp->block_size; |
| 647 | DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n", |
| 648 | tape_name(STp), transfer)); |
| 649 | |
| 650 | memset((STp->buffer)->b_data + offset, 0, transfer - offset); |
| 651 | |
| 652 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 653 | cmd[0] = WRITE_6; |
| 654 | cmd[1] = 1; |
| 655 | blks = transfer / STp->block_size; |
| 656 | cmd[2] = blks >> 16; |
| 657 | cmd[3] = blks >> 8; |
| 658 | cmd[4] = blks; |
| 659 | |
| 660 | SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE, |
| 661 | STp->device->timeout, MAX_WRITE_RETRIES, 1); |
| 662 | if (!SRpnt) |
| 663 | return (STp->buffer)->syscall_result; |
| 664 | |
| 665 | STps = &(STp->ps[STp->partition]); |
| 666 | if ((STp->buffer)->syscall_result != 0) { |
| 667 | struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; |
| 668 | |
| 669 | if (cmdstatp->have_sense && !cmdstatp->deferred && |
| 670 | (cmdstatp->flags & SENSE_EOM) && |
| 671 | (cmdstatp->sense_hdr.sense_key == NO_SENSE || |
| 672 | cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) && |
| 673 | (!cmdstatp->remainder_valid || |
| 674 | cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */ |
| 675 | STp->dirty = 0; |
| 676 | (STp->buffer)->buffer_bytes = 0; |
| 677 | if (STps->drv_block >= 0) |
| 678 | STps->drv_block += blks; |
| 679 | result = (-ENOSPC); |
| 680 | } else { |
| 681 | printk(KERN_ERR "%s: Error on flush.\n", |
| 682 | tape_name(STp)); |
| 683 | STps->drv_block = (-1); |
| 684 | result = (-EIO); |
| 685 | } |
| 686 | } else { |
| 687 | if (STps->drv_block >= 0) |
| 688 | STps->drv_block += blks; |
| 689 | STp->dirty = 0; |
| 690 | (STp->buffer)->buffer_bytes = 0; |
| 691 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 692 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | SRpnt = NULL; |
| 694 | } |
| 695 | return result; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /* Flush the tape buffer. The tape will be positioned correctly unless |
| 700 | seek_next is true. */ |
| 701 | static int flush_buffer(struct scsi_tape *STp, int seek_next) |
| 702 | { |
| 703 | int backspace, result; |
| 704 | struct st_buffer *STbuffer; |
| 705 | struct st_partstat *STps; |
| 706 | |
| 707 | STbuffer = STp->buffer; |
| 708 | |
| 709 | /* |
| 710 | * If there was a bus reset, block further access |
| 711 | * to this device. |
| 712 | */ |
| 713 | if (STp->pos_unknown) |
| 714 | return (-EIO); |
| 715 | |
| 716 | if (STp->ready != ST_READY) |
| 717 | return 0; |
| 718 | STps = &(STp->ps[STp->partition]); |
| 719 | if (STps->rw == ST_WRITING) /* Writing */ |
| 720 | return flush_write_buffer(STp); |
| 721 | |
| 722 | if (STp->block_size == 0) |
| 723 | return 0; |
| 724 | |
| 725 | backspace = ((STp->buffer)->buffer_bytes + |
| 726 | (STp->buffer)->read_pointer) / STp->block_size - |
| 727 | ((STp->buffer)->read_pointer + STp->block_size - 1) / |
| 728 | STp->block_size; |
| 729 | (STp->buffer)->buffer_bytes = 0; |
| 730 | (STp->buffer)->read_pointer = 0; |
| 731 | result = 0; |
| 732 | if (!seek_next) { |
| 733 | if (STps->eof == ST_FM_HIT) { |
| 734 | result = cross_eof(STp, 0); /* Back over the EOF hit */ |
| 735 | if (!result) |
| 736 | STps->eof = ST_NOEOF; |
| 737 | else { |
| 738 | if (STps->drv_file >= 0) |
| 739 | STps->drv_file++; |
| 740 | STps->drv_block = 0; |
| 741 | } |
| 742 | } |
| 743 | if (!result && backspace > 0) |
| 744 | result = st_int_ioctl(STp, MTBSR, backspace); |
| 745 | } else if (STps->eof == ST_FM_HIT) { |
| 746 | if (STps->drv_file >= 0) |
| 747 | STps->drv_file++; |
| 748 | STps->drv_block = 0; |
| 749 | STps->eof = ST_NOEOF; |
| 750 | } |
| 751 | return result; |
| 752 | |
| 753 | } |
| 754 | |
| 755 | /* Set the mode parameters */ |
| 756 | static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm) |
| 757 | { |
| 758 | int set_it = 0; |
| 759 | unsigned long arg; |
| 760 | char *name = tape_name(STp); |
| 761 | |
| 762 | if (!STp->density_changed && |
| 763 | STm->default_density >= 0 && |
| 764 | STm->default_density != STp->density) { |
| 765 | arg = STm->default_density; |
| 766 | set_it = 1; |
| 767 | } else |
| 768 | arg = STp->density; |
| 769 | arg <<= MT_ST_DENSITY_SHIFT; |
| 770 | if (!STp->blksize_changed && |
| 771 | STm->default_blksize >= 0 && |
| 772 | STm->default_blksize != STp->block_size) { |
| 773 | arg |= STm->default_blksize; |
| 774 | set_it = 1; |
| 775 | } else |
| 776 | arg |= STp->block_size; |
| 777 | if (set_it && |
| 778 | st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) { |
| 779 | printk(KERN_WARNING |
| 780 | "%s: Can't set default block size to %d bytes and density %x.\n", |
| 781 | name, STm->default_blksize, STm->default_density); |
| 782 | if (modes_defined) |
| 783 | return (-EINVAL); |
| 784 | } |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 789 | /* Lock or unlock the drive door. Don't use when st_request allocated. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | static int do_door_lock(struct scsi_tape * STp, int do_lock) |
| 791 | { |
| 792 | int retval, cmd; |
| 793 | DEB(char *name = tape_name(STp);) |
| 794 | |
| 795 | |
| 796 | cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK; |
| 797 | DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name, |
| 798 | do_lock ? "L" : "Unl")); |
| 799 | retval = scsi_ioctl(STp->device, cmd, NULL); |
| 800 | if (!retval) { |
| 801 | STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED; |
| 802 | } |
| 803 | else { |
| 804 | STp->door_locked = ST_LOCK_FAILS; |
| 805 | } |
| 806 | return retval; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | /* Set the internal state after reset */ |
| 811 | static void reset_state(struct scsi_tape *STp) |
| 812 | { |
| 813 | int i; |
| 814 | struct st_partstat *STps; |
| 815 | |
| 816 | STp->pos_unknown = 0; |
| 817 | for (i = 0; i < ST_NBR_PARTITIONS; i++) { |
| 818 | STps = &(STp->ps[i]); |
| 819 | STps->rw = ST_IDLE; |
| 820 | STps->eof = ST_NOEOF; |
| 821 | STps->at_sm = 0; |
| 822 | STps->last_block_valid = 0; |
| 823 | STps->drv_block = -1; |
| 824 | STps->drv_file = -1; |
| 825 | } |
| 826 | if (STp->can_partitions) { |
| 827 | STp->partition = find_partition(STp); |
| 828 | if (STp->partition < 0) |
| 829 | STp->partition = 0; |
| 830 | STp->new_partition = STp->partition; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | /* Test if the drive is ready. Returns either one of the codes below or a negative system |
| 835 | error code. */ |
| 836 | #define CHKRES_READY 0 |
| 837 | #define CHKRES_NEW_SESSION 1 |
| 838 | #define CHKRES_NOT_READY 2 |
| 839 | #define CHKRES_NO_TAPE 3 |
| 840 | |
| 841 | #define MAX_ATTENTIONS 10 |
| 842 | |
| 843 | static int test_ready(struct scsi_tape *STp, int do_wait) |
| 844 | { |
| 845 | int attentions, waits, max_wait, scode; |
| 846 | int retval = CHKRES_READY, new_session = 0; |
| 847 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 848 | struct st_request *SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; |
| 850 | |
| 851 | max_wait = do_wait ? ST_BLOCK_SECONDS : 0; |
| 852 | |
| 853 | for (attentions=waits=0; ; ) { |
| 854 | memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE); |
| 855 | cmd[0] = TEST_UNIT_READY; |
| 856 | SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, |
| 857 | STp->long_timeout, MAX_READY_RETRIES, 1); |
| 858 | |
| 859 | if (!SRpnt) { |
| 860 | retval = (STp->buffer)->syscall_result; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if (cmdstatp->have_sense) { |
| 865 | |
| 866 | scode = cmdstatp->sense_hdr.sense_key; |
| 867 | |
| 868 | if (scode == UNIT_ATTENTION) { /* New media? */ |
| 869 | new_session = 1; |
| 870 | if (attentions < MAX_ATTENTIONS) { |
| 871 | attentions++; |
| 872 | continue; |
| 873 | } |
| 874 | else { |
| 875 | retval = (-EIO); |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | if (scode == NOT_READY) { |
| 881 | if (waits < max_wait) { |
| 882 | if (msleep_interruptible(1000)) { |
| 883 | retval = (-EINTR); |
| 884 | break; |
| 885 | } |
| 886 | waits++; |
| 887 | continue; |
| 888 | } |
| 889 | else { |
| 890 | if ((STp->device)->scsi_level >= SCSI_2 && |
| 891 | cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */ |
| 892 | retval = CHKRES_NO_TAPE; |
| 893 | else |
| 894 | retval = CHKRES_NOT_READY; |
| 895 | break; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | retval = (STp->buffer)->syscall_result; |
| 901 | if (!retval) |
| 902 | retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY; |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | if (SRpnt != NULL) |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 907 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return retval; |
| 909 | } |
| 910 | |
| 911 | |
| 912 | /* See if the drive is ready and gather information about the tape. Return values: |
| 913 | < 0 negative error code from errno.h |
| 914 | 0 drive ready |
| 915 | 1 drive not ready (possibly no tape) |
| 916 | */ |
| 917 | static int check_tape(struct scsi_tape *STp, struct file *filp) |
| 918 | { |
| 919 | int i, retval, new_session = 0, do_wait; |
| 920 | unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning; |
| 921 | unsigned short st_flags = filp->f_flags; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 922 | struct st_request *SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | struct st_modedef *STm; |
| 924 | struct st_partstat *STps; |
| 925 | char *name = tape_name(STp); |
| 926 | struct inode *inode = filp->f_dentry->d_inode; |
| 927 | int mode = TAPE_MODE(inode); |
| 928 | |
| 929 | STp->ready = ST_READY; |
| 930 | |
| 931 | if (mode != STp->current_mode) { |
| 932 | DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n", |
| 933 | name, STp->current_mode, mode)); |
| 934 | new_session = 1; |
| 935 | STp->current_mode = mode; |
| 936 | } |
| 937 | STm = &(STp->modes[STp->current_mode]); |
| 938 | |
| 939 | saved_cleaning = STp->cleaning_req; |
| 940 | STp->cleaning_req = 0; |
| 941 | |
| 942 | do_wait = ((filp->f_flags & O_NONBLOCK) == 0); |
| 943 | retval = test_ready(STp, do_wait); |
| 944 | |
| 945 | if (retval < 0) |
| 946 | goto err_out; |
| 947 | |
| 948 | if (retval == CHKRES_NEW_SESSION) { |
| 949 | STp->pos_unknown = 0; |
| 950 | STp->partition = STp->new_partition = 0; |
| 951 | if (STp->can_partitions) |
| 952 | STp->nbr_partitions = 1; /* This guess will be updated later |
| 953 | if necessary */ |
| 954 | for (i = 0; i < ST_NBR_PARTITIONS; i++) { |
| 955 | STps = &(STp->ps[i]); |
| 956 | STps->rw = ST_IDLE; |
| 957 | STps->eof = ST_NOEOF; |
| 958 | STps->at_sm = 0; |
| 959 | STps->last_block_valid = 0; |
| 960 | STps->drv_block = 0; |
| 961 | STps->drv_file = 0; |
| 962 | } |
| 963 | new_session = 1; |
| 964 | } |
| 965 | else { |
| 966 | STp->cleaning_req |= saved_cleaning; |
| 967 | |
| 968 | if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) { |
| 969 | if (retval == CHKRES_NO_TAPE) |
| 970 | STp->ready = ST_NO_TAPE; |
| 971 | else |
| 972 | STp->ready = ST_NOT_READY; |
| 973 | |
| 974 | STp->density = 0; /* Clear the erroneous "residue" */ |
| 975 | STp->write_prot = 0; |
| 976 | STp->block_size = 0; |
| 977 | STp->ps[0].drv_file = STp->ps[0].drv_block = (-1); |
| 978 | STp->partition = STp->new_partition = 0; |
| 979 | STp->door_locked = ST_UNLOCKED; |
| 980 | return CHKRES_NOT_READY; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | if (STp->omit_blklims) |
| 985 | STp->min_block = STp->max_block = (-1); |
| 986 | else { |
| 987 | memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE); |
| 988 | cmd[0] = READ_BLOCK_LIMITS; |
| 989 | |
| 990 | SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE, |
| 991 | STp->device->timeout, MAX_READY_RETRIES, 1); |
| 992 | if (!SRpnt) { |
| 993 | retval = (STp->buffer)->syscall_result; |
| 994 | goto err_out; |
| 995 | } |
| 996 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 997 | if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | STp->max_block = ((STp->buffer)->b_data[1] << 16) | |
| 999 | ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3]; |
| 1000 | STp->min_block = ((STp->buffer)->b_data[4] << 8) | |
| 1001 | (STp->buffer)->b_data[5]; |
| 1002 | if ( DEB( debugging || ) !STp->inited) |
| 1003 | printk(KERN_WARNING |
| 1004 | "%s: Block limits %d - %d bytes.\n", name, |
| 1005 | STp->min_block, STp->max_block); |
| 1006 | } else { |
| 1007 | STp->min_block = STp->max_block = (-1); |
| 1008 | DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n", |
| 1009 | name)); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE); |
| 1014 | cmd[0] = MODE_SENSE; |
| 1015 | cmd[4] = 12; |
| 1016 | |
| 1017 | SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE, |
| 1018 | STp->device->timeout, MAX_READY_RETRIES, 1); |
| 1019 | if (!SRpnt) { |
| 1020 | retval = (STp->buffer)->syscall_result; |
| 1021 | goto err_out; |
| 1022 | } |
| 1023 | |
| 1024 | if ((STp->buffer)->syscall_result != 0) { |
| 1025 | DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name)); |
| 1026 | STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */ |
| 1027 | (STp->buffer)->syscall_result = 0; /* Prevent error propagation */ |
| 1028 | STp->drv_write_prot = 0; |
| 1029 | } else { |
| 1030 | DEBC(printk(ST_DEB_MSG |
| 1031 | "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n", |
| 1032 | name, |
| 1033 | (STp->buffer)->b_data[0], (STp->buffer)->b_data[1], |
| 1034 | (STp->buffer)->b_data[2], (STp->buffer)->b_data[3])); |
| 1035 | |
| 1036 | if ((STp->buffer)->b_data[3] >= 8) { |
| 1037 | STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7; |
| 1038 | STp->density = (STp->buffer)->b_data[4]; |
| 1039 | STp->block_size = (STp->buffer)->b_data[9] * 65536 + |
| 1040 | (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11]; |
| 1041 | DEBC(printk(ST_DEB_MSG |
| 1042 | "%s: Density %x, tape length: %x, drv buffer: %d\n", |
| 1043 | name, STp->density, (STp->buffer)->b_data[5] * 65536 + |
| 1044 | (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7], |
| 1045 | STp->drv_buffer)); |
| 1046 | } |
| 1047 | STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0; |
| 1048 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1049 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | SRpnt = NULL; |
| 1051 | STp->inited = 1; |
| 1052 | |
| 1053 | if (STp->block_size > 0) |
| 1054 | (STp->buffer)->buffer_blocks = |
| 1055 | (STp->buffer)->buffer_size / STp->block_size; |
| 1056 | else |
| 1057 | (STp->buffer)->buffer_blocks = 1; |
| 1058 | (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0; |
| 1059 | |
| 1060 | DEBC(printk(ST_DEB_MSG |
| 1061 | "%s: Block size: %d, buffer size: %d (%d blocks).\n", name, |
| 1062 | STp->block_size, (STp->buffer)->buffer_size, |
| 1063 | (STp->buffer)->buffer_blocks)); |
| 1064 | |
| 1065 | if (STp->drv_write_prot) { |
| 1066 | STp->write_prot = 1; |
| 1067 | |
| 1068 | DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name)); |
| 1069 | |
| 1070 | if (do_wait && |
| 1071 | ((st_flags & O_ACCMODE) == O_WRONLY || |
| 1072 | (st_flags & O_ACCMODE) == O_RDWR)) { |
| 1073 | retval = (-EROFS); |
| 1074 | goto err_out; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (STp->can_partitions && STp->nbr_partitions < 1) { |
| 1079 | /* This code is reached when the device is opened for the first time |
| 1080 | after the driver has been initialized with tape in the drive and the |
| 1081 | partition support has been enabled. */ |
| 1082 | DEBC(printk(ST_DEB_MSG |
| 1083 | "%s: Updating partition number in status.\n", name)); |
| 1084 | if ((STp->partition = find_partition(STp)) < 0) { |
| 1085 | retval = STp->partition; |
| 1086 | goto err_out; |
| 1087 | } |
| 1088 | STp->new_partition = STp->partition; |
| 1089 | STp->nbr_partitions = 1; /* This guess will be updated when necessary */ |
| 1090 | } |
| 1091 | |
| 1092 | if (new_session) { /* Change the drive parameters for the new mode */ |
| 1093 | STp->density_changed = STp->blksize_changed = 0; |
| 1094 | STp->compression_changed = 0; |
| 1095 | if (!(STm->defaults_for_writes) && |
| 1096 | (retval = set_mode_densblk(STp, STm)) < 0) |
| 1097 | goto err_out; |
| 1098 | |
| 1099 | if (STp->default_drvbuffer != 0xff) { |
| 1100 | if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer)) |
| 1101 | printk(KERN_WARNING |
| 1102 | "%s: Can't set default drive buffering to %d.\n", |
| 1103 | name, STp->default_drvbuffer); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | return CHKRES_READY; |
| 1108 | |
| 1109 | err_out: |
| 1110 | return retval; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | /* Open the device. Needs to be called with BKL only because of incrementing the SCSI host |
| 1115 | module count. */ |
| 1116 | static int st_open(struct inode *inode, struct file *filp) |
| 1117 | { |
| 1118 | int i, retval = (-EIO); |
| 1119 | struct scsi_tape *STp; |
| 1120 | struct st_partstat *STps; |
| 1121 | int dev = TAPE_NR(inode); |
| 1122 | char *name; |
| 1123 | |
| 1124 | /* |
| 1125 | * We really want to do nonseekable_open(inode, filp); here, but some |
| 1126 | * versions of tar incorrectly call lseek on tapes and bail out if that |
| 1127 | * fails. So we disallow pread() and pwrite(), but permit lseeks. |
| 1128 | */ |
| 1129 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 1130 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 1131 | if (!(STp = scsi_tape_get(dev))) |
| 1132 | return -ENXIO; |
| 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | write_lock(&st_dev_arr_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | filp->private_data = STp; |
| 1136 | name = tape_name(STp); |
| 1137 | |
| 1138 | if (STp->in_use) { |
| 1139 | write_unlock(&st_dev_arr_lock); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 1140 | scsi_tape_put(STp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); ) |
| 1142 | return (-EBUSY); |
| 1143 | } |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | STp->in_use = 1; |
| 1146 | write_unlock(&st_dev_arr_lock); |
| 1147 | STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0; |
| 1148 | |
| 1149 | if (!scsi_block_when_processing_errors(STp->device)) { |
| 1150 | retval = (-ENXIO); |
| 1151 | goto err_out; |
| 1152 | } |
| 1153 | |
| 1154 | /* See that we have at least a one page buffer available */ |
| 1155 | if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) { |
| 1156 | printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n", |
| 1157 | name); |
| 1158 | retval = (-EOVERFLOW); |
| 1159 | goto err_out; |
| 1160 | } |
| 1161 | |
| 1162 | (STp->buffer)->writing = 0; |
| 1163 | (STp->buffer)->syscall_result = 0; |
| 1164 | |
| 1165 | STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY); |
| 1166 | |
| 1167 | STp->dirty = 0; |
| 1168 | for (i = 0; i < ST_NBR_PARTITIONS; i++) { |
| 1169 | STps = &(STp->ps[i]); |
| 1170 | STps->rw = ST_IDLE; |
| 1171 | } |
| 1172 | STp->recover_count = 0; |
| 1173 | DEB( STp->nbr_waits = STp->nbr_finished = 0; |
| 1174 | STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; ) |
| 1175 | |
| 1176 | retval = check_tape(STp, filp); |
| 1177 | if (retval < 0) |
| 1178 | goto err_out; |
| 1179 | if ((filp->f_flags & O_NONBLOCK) == 0 && |
| 1180 | retval != CHKRES_READY) { |
| 1181 | retval = (-EIO); |
| 1182 | goto err_out; |
| 1183 | } |
| 1184 | return 0; |
| 1185 | |
| 1186 | err_out: |
| 1187 | normalize_buffer(STp->buffer); |
| 1188 | STp->in_use = 0; |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 1189 | scsi_tape_put(STp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | return retval; |
| 1191 | |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | /* Flush the tape buffer before close */ |
| 1196 | static int st_flush(struct file *filp) |
| 1197 | { |
| 1198 | int result = 0, result2; |
| 1199 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1200 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | struct scsi_tape *STp = filp->private_data; |
| 1202 | struct st_modedef *STm = &(STp->modes[STp->current_mode]); |
| 1203 | struct st_partstat *STps = &(STp->ps[STp->partition]); |
| 1204 | char *name = tape_name(STp); |
| 1205 | |
| 1206 | if (file_count(filp) > 1) |
| 1207 | return 0; |
| 1208 | |
| 1209 | if (STps->rw == ST_WRITING && !STp->pos_unknown) { |
| 1210 | result = flush_write_buffer(STp); |
| 1211 | if (result != 0 && result != (-ENOSPC)) |
| 1212 | goto out; |
| 1213 | } |
| 1214 | |
| 1215 | if (STp->can_partitions && |
| 1216 | (result2 = switch_partition(STp)) < 0) { |
| 1217 | DEBC(printk(ST_DEB_MSG |
| 1218 | "%s: switch_partition at close failed.\n", name)); |
| 1219 | if (result == 0) |
| 1220 | result = result2; |
| 1221 | goto out; |
| 1222 | } |
| 1223 | |
| 1224 | DEBC( if (STp->nbr_requests) |
| 1225 | printk(KERN_WARNING "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n", |
| 1226 | name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable)); |
| 1227 | |
| 1228 | if (STps->rw == ST_WRITING && !STp->pos_unknown) { |
| 1229 | struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; |
| 1230 | |
| 1231 | DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n", |
| 1232 | name, STp->nbr_waits, STp->nbr_finished); |
| 1233 | ) |
| 1234 | |
| 1235 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 1236 | cmd[0] = WRITE_FILEMARKS; |
| 1237 | cmd[4] = 1 + STp->two_fm; |
| 1238 | |
| 1239 | SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE, |
| 1240 | STp->device->timeout, MAX_WRITE_RETRIES, 1); |
| 1241 | if (!SRpnt) { |
| 1242 | result = (STp->buffer)->syscall_result; |
| 1243 | goto out; |
| 1244 | } |
| 1245 | |
| 1246 | if (STp->buffer->syscall_result == 0 || |
| 1247 | (cmdstatp->have_sense && !cmdstatp->deferred && |
| 1248 | (cmdstatp->flags & SENSE_EOM) && |
| 1249 | (cmdstatp->sense_hdr.sense_key == NO_SENSE || |
| 1250 | cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) && |
| 1251 | (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) { |
| 1252 | /* Write successful at EOM */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1253 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | SRpnt = NULL; |
| 1255 | if (STps->drv_file >= 0) |
| 1256 | STps->drv_file++; |
| 1257 | STps->drv_block = 0; |
| 1258 | if (STp->two_fm) |
| 1259 | cross_eof(STp, 0); |
| 1260 | STps->eof = ST_FM; |
| 1261 | } |
| 1262 | else { /* Write error */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1263 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | SRpnt = NULL; |
| 1265 | printk(KERN_ERR "%s: Error on write filemark.\n", name); |
| 1266 | if (result == 0) |
| 1267 | result = (-EIO); |
| 1268 | } |
| 1269 | |
| 1270 | DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n", |
| 1271 | name, cmd[4])); |
| 1272 | } else if (!STp->rew_at_close) { |
| 1273 | STps = &(STp->ps[STp->partition]); |
| 1274 | if (!STm->sysv || STps->rw != ST_READING) { |
| 1275 | if (STp->can_bsr) |
| 1276 | result = flush_buffer(STp, 0); |
| 1277 | else if (STps->eof == ST_FM_HIT) { |
| 1278 | result = cross_eof(STp, 0); |
| 1279 | if (result) { |
| 1280 | if (STps->drv_file >= 0) |
| 1281 | STps->drv_file++; |
| 1282 | STps->drv_block = 0; |
| 1283 | STps->eof = ST_FM; |
| 1284 | } else |
| 1285 | STps->eof = ST_NOEOF; |
| 1286 | } |
| 1287 | } else if ((STps->eof == ST_NOEOF && |
| 1288 | !(result = cross_eof(STp, 1))) || |
| 1289 | STps->eof == ST_FM_HIT) { |
| 1290 | if (STps->drv_file >= 0) |
| 1291 | STps->drv_file++; |
| 1292 | STps->drv_block = 0; |
| 1293 | STps->eof = ST_FM; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | out: |
| 1298 | if (STp->rew_at_close) { |
| 1299 | result2 = st_int_ioctl(STp, MTREW, 1); |
| 1300 | if (result == 0) |
| 1301 | result = result2; |
| 1302 | } |
| 1303 | return result; |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | /* Close the device and release it. BKL is not needed: this is the only thread |
| 1308 | accessing this tape. */ |
| 1309 | static int st_release(struct inode *inode, struct file *filp) |
| 1310 | { |
| 1311 | int result = 0; |
| 1312 | struct scsi_tape *STp = filp->private_data; |
| 1313 | |
| 1314 | if (STp->door_locked == ST_LOCKED_AUTO) |
| 1315 | do_door_lock(STp, 0); |
| 1316 | |
| 1317 | normalize_buffer(STp->buffer); |
| 1318 | write_lock(&st_dev_arr_lock); |
| 1319 | STp->in_use = 0; |
| 1320 | write_unlock(&st_dev_arr_lock); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 1321 | scsi_tape_put(STp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | return result; |
| 1324 | } |
| 1325 | |
| 1326 | /* The checks common to both reading and writing */ |
| 1327 | static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count) |
| 1328 | { |
| 1329 | ssize_t retval = 0; |
| 1330 | |
| 1331 | /* |
| 1332 | * If we are in the middle of error recovery, don't let anyone |
| 1333 | * else try and use this device. Also, if error recovery fails, it |
| 1334 | * may try and take the device offline, in which case all further |
| 1335 | * access to the device is prohibited. |
| 1336 | */ |
| 1337 | if (!scsi_block_when_processing_errors(STp->device)) { |
| 1338 | retval = (-ENXIO); |
| 1339 | goto out; |
| 1340 | } |
| 1341 | |
| 1342 | if (STp->ready != ST_READY) { |
| 1343 | if (STp->ready == ST_NO_TAPE) |
| 1344 | retval = (-ENOMEDIUM); |
| 1345 | else |
| 1346 | retval = (-EIO); |
| 1347 | goto out; |
| 1348 | } |
| 1349 | |
| 1350 | if (! STp->modes[STp->current_mode].defined) { |
| 1351 | retval = (-ENXIO); |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | /* |
| 1357 | * If there was a bus reset, block further access |
| 1358 | * to this device. |
| 1359 | */ |
| 1360 | if (STp->pos_unknown) { |
| 1361 | retval = (-EIO); |
| 1362 | goto out; |
| 1363 | } |
| 1364 | |
| 1365 | if (count == 0) |
| 1366 | goto out; |
| 1367 | |
| 1368 | DEB( |
| 1369 | if (!STp->in_use) { |
| 1370 | printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp)); |
| 1371 | retval = (-EIO); |
| 1372 | goto out; |
| 1373 | } ) /* end DEB */ |
| 1374 | |
| 1375 | if (STp->can_partitions && |
| 1376 | (retval = switch_partition(STp)) < 0) |
| 1377 | goto out; |
| 1378 | |
| 1379 | if (STp->block_size == 0 && STp->max_block > 0 && |
| 1380 | (count < STp->min_block || count > STp->max_block)) { |
| 1381 | retval = (-EINVAL); |
| 1382 | goto out; |
| 1383 | } |
| 1384 | |
| 1385 | if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED && |
| 1386 | !do_door_lock(STp, 1)) |
| 1387 | STp->door_locked = ST_LOCKED_AUTO; |
| 1388 | |
| 1389 | out: |
| 1390 | return retval; |
| 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | static int setup_buffering(struct scsi_tape *STp, const char __user *buf, |
| 1395 | size_t count, int is_read) |
| 1396 | { |
| 1397 | int i, bufsize, retval = 0; |
| 1398 | struct st_buffer *STbp = STp->buffer; |
| 1399 | |
| 1400 | if (is_read) |
| 1401 | i = STp->try_dio && try_rdio; |
| 1402 | else |
| 1403 | i = STp->try_dio && try_wdio; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | if (i && ((unsigned long)buf & queue_dma_alignment( |
| 1406 | STp->device->request_queue)) == 0) { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1407 | i = sgl_map_user_pages(&(STbp->sg[0]), STbp->use_sg, |
| 1408 | (unsigned long)buf, count, (is_read ? READ : WRITE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | if (i > 0) { |
| 1410 | STbp->do_dio = i; |
| 1411 | STbp->buffer_bytes = 0; /* can be used as transfer counter */ |
| 1412 | } |
| 1413 | else |
| 1414 | STbp->do_dio = 0; /* fall back to buffering with any error */ |
| 1415 | STbp->sg_segs = STbp->do_dio; |
| 1416 | STbp->frp_sg_current = 0; |
| 1417 | DEB( |
| 1418 | if (STbp->do_dio) { |
| 1419 | STp->nbr_dio++; |
| 1420 | STp->nbr_pages += STbp->do_dio; |
| 1421 | for (i=1; i < STbp->do_dio; i++) |
| 1422 | if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1) |
| 1423 | STp->nbr_combinable++; |
| 1424 | } |
| 1425 | ) |
| 1426 | } else |
| 1427 | STbp->do_dio = 0; |
| 1428 | DEB( STp->nbr_requests++; ) |
| 1429 | |
| 1430 | if (!STbp->do_dio) { |
| 1431 | if (STp->block_size) |
| 1432 | bufsize = STp->block_size > st_fixed_buffer_size ? |
| 1433 | STp->block_size : st_fixed_buffer_size; |
| 1434 | else |
| 1435 | bufsize = count; |
| 1436 | if (bufsize > STbp->buffer_size && |
| 1437 | !enlarge_buffer(STbp, bufsize, STp->restr_dma)) { |
| 1438 | printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n", |
| 1439 | tape_name(STp), bufsize); |
| 1440 | retval = (-EOVERFLOW); |
| 1441 | goto out; |
| 1442 | } |
| 1443 | if (STp->block_size) |
| 1444 | STbp->buffer_blocks = bufsize / STp->block_size; |
| 1445 | } |
| 1446 | |
| 1447 | out: |
| 1448 | return retval; |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | /* Can be called more than once after each setup_buffer() */ |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 1453 | static void release_buffering(struct scsi_tape *STp, int is_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | { |
| 1455 | struct st_buffer *STbp; |
| 1456 | |
| 1457 | STbp = STp->buffer; |
| 1458 | if (STbp->do_dio) { |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 1459 | sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, is_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | STbp->do_dio = 0; |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 1461 | STbp->sg_segs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | |
| 1466 | /* Write command */ |
| 1467 | static ssize_t |
| 1468 | st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos) |
| 1469 | { |
| 1470 | ssize_t total; |
| 1471 | ssize_t i, do_count, blks, transfer; |
| 1472 | ssize_t retval; |
| 1473 | int undone, retry_eot = 0, scode; |
| 1474 | int async_write; |
| 1475 | unsigned char cmd[MAX_COMMAND_SIZE]; |
| 1476 | const char __user *b_point; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1477 | struct st_request *SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | struct scsi_tape *STp = filp->private_data; |
| 1479 | struct st_modedef *STm; |
| 1480 | struct st_partstat *STps; |
| 1481 | struct st_buffer *STbp; |
| 1482 | char *name = tape_name(STp); |
| 1483 | |
| 1484 | if (down_interruptible(&STp->lock)) |
| 1485 | return -ERESTARTSYS; |
| 1486 | |
| 1487 | retval = rw_checks(STp, filp, count); |
| 1488 | if (retval || count == 0) |
| 1489 | goto out; |
| 1490 | |
| 1491 | /* Write must be integral number of blocks */ |
| 1492 | if (STp->block_size != 0 && (count % STp->block_size) != 0) { |
| 1493 | printk(KERN_WARNING "%s: Write not multiple of tape block size.\n", |
| 1494 | name); |
| 1495 | retval = (-EINVAL); |
| 1496 | goto out; |
| 1497 | } |
| 1498 | |
| 1499 | STm = &(STp->modes[STp->current_mode]); |
| 1500 | STps = &(STp->ps[STp->partition]); |
| 1501 | |
| 1502 | if (STp->write_prot) { |
| 1503 | retval = (-EACCES); |
| 1504 | goto out; |
| 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | if (STps->rw == ST_READING) { |
| 1509 | retval = flush_buffer(STp, 0); |
| 1510 | if (retval) |
| 1511 | goto out; |
| 1512 | STps->rw = ST_WRITING; |
| 1513 | } else if (STps->rw != ST_WRITING && |
| 1514 | STps->drv_file == 0 && STps->drv_block == 0) { |
| 1515 | if ((retval = set_mode_densblk(STp, STm)) < 0) |
| 1516 | goto out; |
| 1517 | if (STm->default_compression != ST_DONT_TOUCH && |
| 1518 | !(STp->compression_changed)) { |
| 1519 | if (st_compression(STp, (STm->default_compression == ST_YES))) { |
| 1520 | printk(KERN_WARNING "%s: Can't set default compression.\n", |
| 1521 | name); |
| 1522 | if (modes_defined) { |
| 1523 | retval = (-EINVAL); |
| 1524 | goto out; |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | STbp = STp->buffer; |
| 1531 | i = write_behind_check(STp); |
| 1532 | if (i) { |
| 1533 | if (i == -ENOSPC) |
| 1534 | STps->eof = ST_EOM_OK; |
| 1535 | else |
| 1536 | STps->eof = ST_EOM_ERROR; |
| 1537 | } |
| 1538 | |
| 1539 | if (STps->eof == ST_EOM_OK) { |
| 1540 | STps->eof = ST_EOD_1; /* allow next write */ |
| 1541 | retval = (-ENOSPC); |
| 1542 | goto out; |
| 1543 | } |
| 1544 | else if (STps->eof == ST_EOM_ERROR) { |
| 1545 | retval = (-EIO); |
| 1546 | goto out; |
| 1547 | } |
| 1548 | |
| 1549 | /* Check the buffer readability in cases where copy_user might catch |
| 1550 | the problems after some tape movement. */ |
| 1551 | if (STp->block_size != 0 && |
| 1552 | !STbp->do_dio && |
| 1553 | (copy_from_user(&i, buf, 1) != 0 || |
| 1554 | copy_from_user(&i, buf + count - 1, 1) != 0)) { |
| 1555 | retval = (-EFAULT); |
| 1556 | goto out; |
| 1557 | } |
| 1558 | |
| 1559 | retval = setup_buffering(STp, buf, count, 0); |
| 1560 | if (retval) |
| 1561 | goto out; |
| 1562 | |
| 1563 | total = count; |
| 1564 | |
| 1565 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 1566 | cmd[0] = WRITE_6; |
| 1567 | cmd[1] = (STp->block_size != 0); |
| 1568 | |
| 1569 | STps->rw = ST_WRITING; |
| 1570 | |
| 1571 | b_point = buf; |
| 1572 | while (count > 0 && !retry_eot) { |
| 1573 | |
| 1574 | if (STbp->do_dio) { |
| 1575 | do_count = count; |
| 1576 | } |
| 1577 | else { |
| 1578 | if (STp->block_size == 0) |
| 1579 | do_count = count; |
| 1580 | else { |
| 1581 | do_count = STbp->buffer_blocks * STp->block_size - |
| 1582 | STbp->buffer_bytes; |
| 1583 | if (do_count > count) |
| 1584 | do_count = count; |
| 1585 | } |
| 1586 | |
| 1587 | i = append_to_buffer(b_point, STbp, do_count); |
| 1588 | if (i) { |
| 1589 | retval = i; |
| 1590 | goto out; |
| 1591 | } |
| 1592 | } |
| 1593 | count -= do_count; |
| 1594 | b_point += do_count; |
| 1595 | |
| 1596 | async_write = STp->block_size == 0 && !STbp->do_dio && |
| 1597 | STm->do_async_writes && STps->eof < ST_EOM_OK; |
| 1598 | |
| 1599 | if (STp->block_size != 0 && STm->do_buffer_writes && |
| 1600 | !(STp->try_dio && try_wdio) && STps->eof < ST_EOM_OK && |
| 1601 | STbp->buffer_bytes < STbp->buffer_size) { |
| 1602 | STp->dirty = 1; |
| 1603 | /* Don't write a buffer that is not full enough. */ |
| 1604 | if (!async_write && count == 0) |
| 1605 | break; |
| 1606 | } |
| 1607 | |
| 1608 | retry_write: |
| 1609 | if (STp->block_size == 0) |
| 1610 | blks = transfer = do_count; |
| 1611 | else { |
| 1612 | if (!STbp->do_dio) |
| 1613 | blks = STbp->buffer_bytes; |
| 1614 | else |
| 1615 | blks = do_count; |
| 1616 | blks /= STp->block_size; |
| 1617 | transfer = blks * STp->block_size; |
| 1618 | } |
| 1619 | cmd[2] = blks >> 16; |
| 1620 | cmd[3] = blks >> 8; |
| 1621 | cmd[4] = blks; |
| 1622 | |
| 1623 | SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE, |
| 1624 | STp->device->timeout, MAX_WRITE_RETRIES, !async_write); |
| 1625 | if (!SRpnt) { |
| 1626 | retval = STbp->syscall_result; |
| 1627 | goto out; |
| 1628 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1629 | if (async_write && !STbp->syscall_result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | STbp->writing = transfer; |
| 1631 | STp->dirty = !(STbp->writing == |
| 1632 | STbp->buffer_bytes); |
| 1633 | SRpnt = NULL; /* Prevent releasing this request! */ |
| 1634 | DEB( STp->write_pending = 1; ) |
| 1635 | break; |
| 1636 | } |
| 1637 | |
| 1638 | if (STbp->syscall_result != 0) { |
| 1639 | struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; |
| 1640 | |
| 1641 | DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name)); |
| 1642 | if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) { |
| 1643 | scode = cmdstatp->sense_hdr.sense_key; |
| 1644 | if (cmdstatp->remainder_valid) |
| 1645 | undone = (int)cmdstatp->uremainder64; |
| 1646 | else if (STp->block_size == 0 && |
| 1647 | scode == VOLUME_OVERFLOW) |
| 1648 | undone = transfer; |
| 1649 | else |
| 1650 | undone = 0; |
| 1651 | if (STp->block_size != 0) |
| 1652 | undone *= STp->block_size; |
| 1653 | if (undone <= do_count) { |
| 1654 | /* Only data from this write is not written */ |
| 1655 | count += undone; |
| 1656 | do_count -= undone; |
| 1657 | if (STp->block_size) |
| 1658 | blks = (transfer - undone) / STp->block_size; |
| 1659 | STps->eof = ST_EOM_OK; |
| 1660 | /* Continue in fixed block mode if all written |
| 1661 | in this request but still something left to write |
| 1662 | (retval left to zero) |
| 1663 | */ |
| 1664 | if (STp->block_size == 0 || |
| 1665 | undone > 0 || count == 0) |
| 1666 | retval = (-ENOSPC); /* EOM within current request */ |
| 1667 | DEBC(printk(ST_DEB_MSG |
| 1668 | "%s: EOM with %d bytes unwritten.\n", |
| 1669 | name, (int)count)); |
| 1670 | } else { |
| 1671 | /* EOT within data buffered earlier (possible only |
| 1672 | in fixed block mode without direct i/o) */ |
| 1673 | if (!retry_eot && !cmdstatp->deferred && |
| 1674 | (scode == NO_SENSE || scode == RECOVERED_ERROR)) { |
| 1675 | move_buffer_data(STp->buffer, transfer - undone); |
| 1676 | retry_eot = 1; |
| 1677 | if (STps->drv_block >= 0) { |
| 1678 | STps->drv_block += (transfer - undone) / |
| 1679 | STp->block_size; |
| 1680 | } |
| 1681 | STps->eof = ST_EOM_OK; |
| 1682 | DEBC(printk(ST_DEB_MSG |
| 1683 | "%s: Retry write of %d bytes at EOM.\n", |
| 1684 | name, STp->buffer->buffer_bytes)); |
| 1685 | goto retry_write; |
| 1686 | } |
| 1687 | else { |
| 1688 | /* Either error within data buffered by driver or |
| 1689 | failed retry */ |
| 1690 | count -= do_count; |
| 1691 | blks = do_count = 0; |
| 1692 | STps->eof = ST_EOM_ERROR; |
| 1693 | STps->drv_block = (-1); /* Too cautious? */ |
| 1694 | retval = (-EIO); /* EOM for old data */ |
| 1695 | DEBC(printk(ST_DEB_MSG |
| 1696 | "%s: EOM with lost data.\n", |
| 1697 | name)); |
| 1698 | } |
| 1699 | } |
| 1700 | } else { |
| 1701 | count += do_count; |
| 1702 | STps->drv_block = (-1); /* Too cautious? */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1703 | retval = STbp->syscall_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | } |
| 1707 | |
| 1708 | if (STps->drv_block >= 0) { |
| 1709 | if (STp->block_size == 0) |
| 1710 | STps->drv_block += (do_count > 0); |
| 1711 | else |
| 1712 | STps->drv_block += blks; |
| 1713 | } |
| 1714 | |
| 1715 | STbp->buffer_bytes = 0; |
| 1716 | STp->dirty = 0; |
| 1717 | |
| 1718 | if (retval || retry_eot) { |
| 1719 | if (count < total) |
| 1720 | retval = total - count; |
| 1721 | goto out; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | if (STps->eof == ST_EOD_1) |
| 1726 | STps->eof = ST_EOM_OK; |
| 1727 | else if (STps->eof != ST_EOM_OK) |
| 1728 | STps->eof = ST_NOEOF; |
| 1729 | retval = total - count; |
| 1730 | |
| 1731 | out: |
| 1732 | if (SRpnt != NULL) |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1733 | st_release_request(SRpnt); |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 1734 | release_buffering(STp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | up(&STp->lock); |
| 1736 | |
| 1737 | return retval; |
| 1738 | } |
| 1739 | |
| 1740 | /* Read data from the tape. Returns zero in the normal case, one if the |
| 1741 | eof status has changed, and the negative error code in case of a |
| 1742 | fatal error. Otherwise updates the buffer and the eof state. |
| 1743 | |
| 1744 | Does release user buffer mapping if it is set. |
| 1745 | */ |
| 1746 | static long read_tape(struct scsi_tape *STp, long count, |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1747 | struct st_request ** aSRpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | { |
| 1749 | int transfer, blks, bytes; |
| 1750 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1751 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | struct st_modedef *STm; |
| 1753 | struct st_partstat *STps; |
| 1754 | struct st_buffer *STbp; |
| 1755 | int retval = 0; |
| 1756 | char *name = tape_name(STp); |
| 1757 | |
| 1758 | if (count == 0) |
| 1759 | return 0; |
| 1760 | |
| 1761 | STm = &(STp->modes[STp->current_mode]); |
| 1762 | STps = &(STp->ps[STp->partition]); |
| 1763 | if (STps->eof == ST_FM_HIT) |
| 1764 | return 1; |
| 1765 | STbp = STp->buffer; |
| 1766 | |
| 1767 | if (STp->block_size == 0) |
| 1768 | blks = bytes = count; |
| 1769 | else { |
| 1770 | if (!(STp->try_dio && try_rdio) && STm->do_read_ahead) { |
| 1771 | blks = (STp->buffer)->buffer_blocks; |
| 1772 | bytes = blks * STp->block_size; |
| 1773 | } else { |
| 1774 | bytes = count; |
| 1775 | if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size) |
| 1776 | bytes = (STp->buffer)->buffer_size; |
| 1777 | blks = bytes / STp->block_size; |
| 1778 | bytes = blks * STp->block_size; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 1783 | cmd[0] = READ_6; |
| 1784 | cmd[1] = (STp->block_size != 0); |
| 1785 | cmd[2] = blks >> 16; |
| 1786 | cmd[3] = blks >> 8; |
| 1787 | cmd[4] = blks; |
| 1788 | |
| 1789 | SRpnt = *aSRpnt; |
| 1790 | SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE, |
| 1791 | STp->device->timeout, MAX_RETRIES, 1); |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 1792 | release_buffering(STp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | *aSRpnt = SRpnt; |
| 1794 | if (!SRpnt) |
| 1795 | return STbp->syscall_result; |
| 1796 | |
| 1797 | STbp->read_pointer = 0; |
| 1798 | STps->at_sm = 0; |
| 1799 | |
| 1800 | /* Something to check */ |
| 1801 | if (STbp->syscall_result) { |
| 1802 | struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; |
| 1803 | |
| 1804 | retval = 1; |
| 1805 | DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", |
| 1806 | name, |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1807 | SRpnt->sense[0], SRpnt->sense[1], |
| 1808 | SRpnt->sense[2], SRpnt->sense[3], |
| 1809 | SRpnt->sense[4], SRpnt->sense[5], |
| 1810 | SRpnt->sense[6], SRpnt->sense[7])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | if (cmdstatp->have_sense) { |
| 1812 | |
| 1813 | if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK) |
| 1814 | cmdstatp->flags &= 0xcf; /* No need for EOM in this case */ |
| 1815 | |
| 1816 | if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */ |
| 1817 | /* Compute the residual count */ |
| 1818 | if (cmdstatp->remainder_valid) |
| 1819 | transfer = (int)cmdstatp->uremainder64; |
| 1820 | else |
| 1821 | transfer = 0; |
| 1822 | if (STp->block_size == 0 && |
| 1823 | cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR) |
| 1824 | transfer = bytes; |
| 1825 | |
| 1826 | if (cmdstatp->flags & SENSE_ILI) { /* ILI */ |
| 1827 | if (STp->block_size == 0) { |
| 1828 | if (transfer <= 0) { |
| 1829 | if (transfer < 0) |
| 1830 | printk(KERN_NOTICE |
| 1831 | "%s: Failed to read %d byte block with %d byte transfer.\n", |
| 1832 | name, bytes - transfer, bytes); |
| 1833 | if (STps->drv_block >= 0) |
| 1834 | STps->drv_block += 1; |
| 1835 | STbp->buffer_bytes = 0; |
| 1836 | return (-ENOMEM); |
| 1837 | } |
| 1838 | STbp->buffer_bytes = bytes - transfer; |
| 1839 | } else { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1840 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | SRpnt = *aSRpnt = NULL; |
| 1842 | if (transfer == blks) { /* We did not get anything, error */ |
| 1843 | printk(KERN_NOTICE "%s: Incorrect block size.\n", name); |
| 1844 | if (STps->drv_block >= 0) |
| 1845 | STps->drv_block += blks - transfer + 1; |
| 1846 | st_int_ioctl(STp, MTBSR, 1); |
| 1847 | return (-EIO); |
| 1848 | } |
| 1849 | /* We have some data, deliver it */ |
| 1850 | STbp->buffer_bytes = (blks - transfer) * |
| 1851 | STp->block_size; |
| 1852 | DEBC(printk(ST_DEB_MSG |
| 1853 | "%s: ILI but enough data received %ld %d.\n", |
| 1854 | name, count, STbp->buffer_bytes)); |
| 1855 | if (STps->drv_block >= 0) |
| 1856 | STps->drv_block += 1; |
| 1857 | if (st_int_ioctl(STp, MTBSR, 1)) |
| 1858 | return (-EIO); |
| 1859 | } |
| 1860 | } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */ |
| 1861 | if (STps->eof != ST_FM_HIT) |
| 1862 | STps->eof = ST_FM_HIT; |
| 1863 | else |
| 1864 | STps->eof = ST_EOD_2; |
| 1865 | if (STp->block_size == 0) |
| 1866 | STbp->buffer_bytes = 0; |
| 1867 | else |
| 1868 | STbp->buffer_bytes = |
| 1869 | bytes - transfer * STp->block_size; |
| 1870 | DEBC(printk(ST_DEB_MSG |
| 1871 | "%s: EOF detected (%d bytes read).\n", |
| 1872 | name, STbp->buffer_bytes)); |
| 1873 | } else if (cmdstatp->flags & SENSE_EOM) { |
| 1874 | if (STps->eof == ST_FM) |
| 1875 | STps->eof = ST_EOD_1; |
| 1876 | else |
| 1877 | STps->eof = ST_EOM_OK; |
| 1878 | if (STp->block_size == 0) |
| 1879 | STbp->buffer_bytes = bytes - transfer; |
| 1880 | else |
| 1881 | STbp->buffer_bytes = |
| 1882 | bytes - transfer * STp->block_size; |
| 1883 | |
| 1884 | DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n", |
| 1885 | name, STbp->buffer_bytes)); |
| 1886 | } |
| 1887 | } |
| 1888 | /* end of EOF, EOM, ILI test */ |
| 1889 | else { /* nonzero sense key */ |
| 1890 | DEBC(printk(ST_DEB_MSG |
| 1891 | "%s: Tape error while reading.\n", name)); |
| 1892 | STps->drv_block = (-1); |
| 1893 | if (STps->eof == ST_FM && |
| 1894 | cmdstatp->sense_hdr.sense_key == BLANK_CHECK) { |
| 1895 | DEBC(printk(ST_DEB_MSG |
| 1896 | "%s: Zero returned for first BLANK CHECK after EOF.\n", |
| 1897 | name)); |
| 1898 | STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */ |
| 1899 | } else /* Some other extended sense code */ |
| 1900 | retval = (-EIO); |
| 1901 | } |
| 1902 | |
| 1903 | if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */ |
| 1904 | STbp->buffer_bytes = 0; |
| 1905 | } |
| 1906 | /* End of extended sense test */ |
| 1907 | else { /* Non-extended sense */ |
| 1908 | retval = STbp->syscall_result; |
| 1909 | } |
| 1910 | |
| 1911 | } |
| 1912 | /* End of error handling */ |
| 1913 | else /* Read successful */ |
| 1914 | STbp->buffer_bytes = bytes; |
| 1915 | |
| 1916 | if (STps->drv_block >= 0) { |
| 1917 | if (STp->block_size == 0) |
| 1918 | STps->drv_block++; |
| 1919 | else |
| 1920 | STps->drv_block += STbp->buffer_bytes / STp->block_size; |
| 1921 | } |
| 1922 | return retval; |
| 1923 | } |
| 1924 | |
| 1925 | |
| 1926 | /* Read command */ |
| 1927 | static ssize_t |
| 1928 | st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos) |
| 1929 | { |
| 1930 | ssize_t total; |
| 1931 | ssize_t retval = 0; |
| 1932 | ssize_t i, transfer; |
| 1933 | int special, do_dio = 0; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 1934 | struct st_request *SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | struct scsi_tape *STp = filp->private_data; |
| 1936 | struct st_modedef *STm; |
| 1937 | struct st_partstat *STps; |
| 1938 | struct st_buffer *STbp = STp->buffer; |
| 1939 | DEB( char *name = tape_name(STp); ) |
| 1940 | |
| 1941 | if (down_interruptible(&STp->lock)) |
| 1942 | return -ERESTARTSYS; |
| 1943 | |
| 1944 | retval = rw_checks(STp, filp, count); |
| 1945 | if (retval || count == 0) |
| 1946 | goto out; |
| 1947 | |
| 1948 | STm = &(STp->modes[STp->current_mode]); |
| 1949 | if (!(STm->do_read_ahead) && STp->block_size != 0 && |
| 1950 | (count % STp->block_size) != 0) { |
| 1951 | retval = (-EINVAL); /* Read must be integral number of blocks */ |
| 1952 | goto out; |
| 1953 | } |
| 1954 | |
| 1955 | STps = &(STp->ps[STp->partition]); |
| 1956 | if (STps->rw == ST_WRITING) { |
| 1957 | retval = flush_buffer(STp, 0); |
| 1958 | if (retval) |
| 1959 | goto out; |
| 1960 | STps->rw = ST_READING; |
| 1961 | } |
| 1962 | DEB( |
| 1963 | if (debugging && STps->eof != ST_NOEOF) |
| 1964 | printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name, |
| 1965 | STps->eof, STbp->buffer_bytes); |
| 1966 | ) /* end DEB */ |
| 1967 | |
| 1968 | retval = setup_buffering(STp, buf, count, 1); |
| 1969 | if (retval) |
| 1970 | goto out; |
| 1971 | do_dio = STbp->do_dio; |
| 1972 | |
| 1973 | if (STbp->buffer_bytes == 0 && |
| 1974 | STps->eof >= ST_EOD_1) { |
| 1975 | if (STps->eof < ST_EOD) { |
| 1976 | STps->eof += 1; |
| 1977 | retval = 0; |
| 1978 | goto out; |
| 1979 | } |
| 1980 | retval = (-EIO); /* EOM or Blank Check */ |
| 1981 | goto out; |
| 1982 | } |
| 1983 | |
| 1984 | if (do_dio) { |
| 1985 | /* Check the buffer writability before any tape movement. Don't alter |
| 1986 | buffer data. */ |
| 1987 | if (copy_from_user(&i, buf, 1) != 0 || |
| 1988 | copy_to_user(buf, &i, 1) != 0 || |
| 1989 | copy_from_user(&i, buf + count - 1, 1) != 0 || |
| 1990 | copy_to_user(buf + count - 1, &i, 1) != 0) { |
| 1991 | retval = (-EFAULT); |
| 1992 | goto out; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | STps->rw = ST_READING; |
| 1997 | |
| 1998 | |
| 1999 | /* Loop until enough data in buffer or a special condition found */ |
| 2000 | for (total = 0, special = 0; total < count && !special;) { |
| 2001 | |
| 2002 | /* Get new data if the buffer is empty */ |
| 2003 | if (STbp->buffer_bytes == 0) { |
| 2004 | special = read_tape(STp, count - total, &SRpnt); |
| 2005 | if (special < 0) { /* No need to continue read */ |
| 2006 | retval = special; |
| 2007 | goto out; |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | /* Move the data from driver buffer to user buffer */ |
| 2012 | if (STbp->buffer_bytes > 0) { |
| 2013 | DEB( |
| 2014 | if (debugging && STps->eof != ST_NOEOF) |
| 2015 | printk(ST_DEB_MSG |
| 2016 | "%s: EOF up (%d). Left %d, needed %d.\n", name, |
| 2017 | STps->eof, STbp->buffer_bytes, |
| 2018 | (int)(count - total)); |
| 2019 | ) /* end DEB */ |
| 2020 | transfer = STbp->buffer_bytes < count - total ? |
| 2021 | STbp->buffer_bytes : count - total; |
| 2022 | if (!do_dio) { |
| 2023 | i = from_buffer(STbp, buf, transfer); |
| 2024 | if (i) { |
| 2025 | retval = i; |
| 2026 | goto out; |
| 2027 | } |
| 2028 | } |
| 2029 | buf += transfer; |
| 2030 | total += transfer; |
| 2031 | } |
| 2032 | |
| 2033 | if (STp->block_size == 0) |
| 2034 | break; /* Read only one variable length block */ |
| 2035 | |
| 2036 | } /* for (total = 0, special = 0; |
| 2037 | total < count && !special; ) */ |
| 2038 | |
| 2039 | /* Change the eof state if no data from tape or buffer */ |
| 2040 | if (total == 0) { |
| 2041 | if (STps->eof == ST_FM_HIT) { |
| 2042 | STps->eof = ST_FM; |
| 2043 | STps->drv_block = 0; |
| 2044 | if (STps->drv_file >= 0) |
| 2045 | STps->drv_file++; |
| 2046 | } else if (STps->eof == ST_EOD_1) { |
| 2047 | STps->eof = ST_EOD_2; |
| 2048 | STps->drv_block = 0; |
| 2049 | if (STps->drv_file >= 0) |
| 2050 | STps->drv_file++; |
| 2051 | } else if (STps->eof == ST_EOD_2) |
| 2052 | STps->eof = ST_EOD; |
| 2053 | } else if (STps->eof == ST_FM) |
| 2054 | STps->eof = ST_NOEOF; |
| 2055 | retval = total; |
| 2056 | |
| 2057 | out: |
| 2058 | if (SRpnt != NULL) { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2059 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | SRpnt = NULL; |
| 2061 | } |
| 2062 | if (do_dio) { |
Kai Makisara | 787926b | 2005-11-13 10:04:44 +0200 | [diff] [blame] | 2063 | release_buffering(STp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | STbp->buffer_bytes = 0; |
| 2065 | } |
| 2066 | up(&STp->lock); |
| 2067 | |
| 2068 | return retval; |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | |
| 2073 | DEB( |
| 2074 | /* Set the driver options */ |
| 2075 | static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name) |
| 2076 | { |
| 2077 | if (debugging) { |
| 2078 | printk(KERN_INFO |
| 2079 | "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n", |
| 2080 | name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes, |
| 2081 | STm->do_read_ahead); |
| 2082 | printk(KERN_INFO |
| 2083 | "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n", |
| 2084 | name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock); |
| 2085 | printk(KERN_INFO |
| 2086 | "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n", |
| 2087 | name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions, |
| 2088 | STp->scsi2_logical); |
| 2089 | printk(KERN_INFO |
| 2090 | "%s: sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate); |
| 2091 | printk(KERN_INFO "%s: debugging: %d\n", |
| 2092 | name, debugging); |
| 2093 | } |
| 2094 | } |
| 2095 | ) |
| 2096 | |
| 2097 | |
| 2098 | static int st_set_options(struct scsi_tape *STp, long options) |
| 2099 | { |
| 2100 | int value; |
| 2101 | long code; |
| 2102 | struct st_modedef *STm; |
| 2103 | char *name = tape_name(STp); |
| 2104 | struct cdev *cd0, *cd1; |
| 2105 | |
| 2106 | STm = &(STp->modes[STp->current_mode]); |
| 2107 | if (!STm->defined) { |
| 2108 | cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1]; |
| 2109 | memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef)); |
| 2110 | STm->cdevs[0] = cd0; STm->cdevs[1] = cd1; |
| 2111 | modes_defined = 1; |
| 2112 | DEBC(printk(ST_DEB_MSG |
| 2113 | "%s: Initialized mode %d definition from mode 0\n", |
| 2114 | name, STp->current_mode)); |
| 2115 | } |
| 2116 | |
| 2117 | code = options & MT_ST_OPTIONS; |
| 2118 | if (code == MT_ST_BOOLEANS) { |
| 2119 | STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0; |
| 2120 | STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0; |
| 2121 | STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0; |
| 2122 | STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0; |
| 2123 | STp->two_fm = (options & MT_ST_TWO_FM) != 0; |
| 2124 | STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0; |
| 2125 | STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0; |
| 2126 | STp->can_bsr = (options & MT_ST_CAN_BSR) != 0; |
| 2127 | STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0; |
| 2128 | if ((STp->device)->scsi_level >= SCSI_2) |
| 2129 | STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0; |
| 2130 | STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0; |
| 2131 | STp->immediate = (options & MT_ST_NOWAIT) != 0; |
| 2132 | STm->sysv = (options & MT_ST_SYSV) != 0; |
| 2133 | DEB( debugging = (options & MT_ST_DEBUGGING) != 0; |
| 2134 | st_log_options(STp, STm, name); ) |
| 2135 | } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) { |
| 2136 | value = (code == MT_ST_SETBOOLEANS); |
| 2137 | if ((options & MT_ST_BUFFER_WRITES) != 0) |
| 2138 | STm->do_buffer_writes = value; |
| 2139 | if ((options & MT_ST_ASYNC_WRITES) != 0) |
| 2140 | STm->do_async_writes = value; |
| 2141 | if ((options & MT_ST_DEF_WRITES) != 0) |
| 2142 | STm->defaults_for_writes = value; |
| 2143 | if ((options & MT_ST_READ_AHEAD) != 0) |
| 2144 | STm->do_read_ahead = value; |
| 2145 | if ((options & MT_ST_TWO_FM) != 0) |
| 2146 | STp->two_fm = value; |
| 2147 | if ((options & MT_ST_FAST_MTEOM) != 0) |
| 2148 | STp->fast_mteom = value; |
| 2149 | if ((options & MT_ST_AUTO_LOCK) != 0) |
| 2150 | STp->do_auto_lock = value; |
| 2151 | if ((options & MT_ST_CAN_BSR) != 0) |
| 2152 | STp->can_bsr = value; |
| 2153 | if ((options & MT_ST_NO_BLKLIMS) != 0) |
| 2154 | STp->omit_blklims = value; |
| 2155 | if ((STp->device)->scsi_level >= SCSI_2 && |
| 2156 | (options & MT_ST_CAN_PARTITIONS) != 0) |
| 2157 | STp->can_partitions = value; |
| 2158 | if ((options & MT_ST_SCSI2LOGICAL) != 0) |
| 2159 | STp->scsi2_logical = value; |
| 2160 | if ((options & MT_ST_NOWAIT) != 0) |
| 2161 | STp->immediate = value; |
| 2162 | if ((options & MT_ST_SYSV) != 0) |
| 2163 | STm->sysv = value; |
| 2164 | DEB( |
| 2165 | if ((options & MT_ST_DEBUGGING) != 0) |
| 2166 | debugging = value; |
| 2167 | st_log_options(STp, STm, name); ) |
| 2168 | } else if (code == MT_ST_WRITE_THRESHOLD) { |
| 2169 | /* Retained for compatibility */ |
| 2170 | } else if (code == MT_ST_DEF_BLKSIZE) { |
| 2171 | value = (options & ~MT_ST_OPTIONS); |
| 2172 | if (value == ~MT_ST_OPTIONS) { |
| 2173 | STm->default_blksize = (-1); |
| 2174 | DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name)); |
| 2175 | } else { |
| 2176 | STm->default_blksize = value; |
| 2177 | DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n", |
| 2178 | name, STm->default_blksize)); |
| 2179 | if (STp->ready == ST_READY) { |
| 2180 | STp->blksize_changed = 0; |
| 2181 | set_mode_densblk(STp, STm); |
| 2182 | } |
| 2183 | } |
| 2184 | } else if (code == MT_ST_TIMEOUTS) { |
| 2185 | value = (options & ~MT_ST_OPTIONS); |
| 2186 | if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) { |
| 2187 | STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ; |
| 2188 | DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name, |
| 2189 | (value & ~MT_ST_SET_LONG_TIMEOUT))); |
| 2190 | } else { |
| 2191 | STp->device->timeout = value * HZ; |
| 2192 | DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n", |
| 2193 | name, value) ); |
| 2194 | } |
| 2195 | } else if (code == MT_ST_SET_CLN) { |
| 2196 | value = (options & ~MT_ST_OPTIONS) & 0xff; |
| 2197 | if (value != 0 && |
| 2198 | value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE) |
| 2199 | return (-EINVAL); |
| 2200 | STp->cln_mode = value; |
| 2201 | STp->cln_sense_mask = (options >> 8) & 0xff; |
| 2202 | STp->cln_sense_value = (options >> 16) & 0xff; |
| 2203 | printk(KERN_INFO |
| 2204 | "%s: Cleaning request mode %d, mask %02x, value %02x\n", |
| 2205 | name, value, STp->cln_sense_mask, STp->cln_sense_value); |
| 2206 | } else if (code == MT_ST_DEF_OPTIONS) { |
| 2207 | code = (options & ~MT_ST_CLEAR_DEFAULT); |
| 2208 | value = (options & MT_ST_CLEAR_DEFAULT); |
| 2209 | if (code == MT_ST_DEF_DENSITY) { |
| 2210 | if (value == MT_ST_CLEAR_DEFAULT) { |
| 2211 | STm->default_density = (-1); |
| 2212 | DEBC( printk(KERN_INFO "%s: Density default disabled.\n", |
| 2213 | name)); |
| 2214 | } else { |
| 2215 | STm->default_density = value & 0xff; |
| 2216 | DEBC( printk(KERN_INFO "%s: Density default set to %x\n", |
| 2217 | name, STm->default_density)); |
| 2218 | if (STp->ready == ST_READY) { |
| 2219 | STp->density_changed = 0; |
| 2220 | set_mode_densblk(STp, STm); |
| 2221 | } |
| 2222 | } |
| 2223 | } else if (code == MT_ST_DEF_DRVBUFFER) { |
| 2224 | if (value == MT_ST_CLEAR_DEFAULT) { |
| 2225 | STp->default_drvbuffer = 0xff; |
| 2226 | DEBC( printk(KERN_INFO |
| 2227 | "%s: Drive buffer default disabled.\n", name)); |
| 2228 | } else { |
| 2229 | STp->default_drvbuffer = value & 7; |
| 2230 | DEBC( printk(KERN_INFO |
| 2231 | "%s: Drive buffer default set to %x\n", |
| 2232 | name, STp->default_drvbuffer)); |
| 2233 | if (STp->ready == ST_READY) |
| 2234 | st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer); |
| 2235 | } |
| 2236 | } else if (code == MT_ST_DEF_COMPRESSION) { |
| 2237 | if (value == MT_ST_CLEAR_DEFAULT) { |
| 2238 | STm->default_compression = ST_DONT_TOUCH; |
| 2239 | DEBC( printk(KERN_INFO |
| 2240 | "%s: Compression default disabled.\n", name)); |
| 2241 | } else { |
| 2242 | if ((value & 0xff00) != 0) { |
| 2243 | STp->c_algo = (value & 0xff00) >> 8; |
| 2244 | DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n", |
| 2245 | name, STp->c_algo)); |
| 2246 | } |
| 2247 | if ((value & 0xff) != 0xff) { |
| 2248 | STm->default_compression = (value & 1 ? ST_YES : ST_NO); |
| 2249 | DEBC( printk(KERN_INFO "%s: Compression default set to %x\n", |
| 2250 | name, (value & 1))); |
| 2251 | if (STp->ready == ST_READY) { |
| 2252 | STp->compression_changed = 0; |
| 2253 | st_compression(STp, (STm->default_compression == ST_YES)); |
| 2254 | } |
| 2255 | } |
| 2256 | } |
| 2257 | } |
| 2258 | } else |
| 2259 | return (-EIO); |
| 2260 | |
| 2261 | return 0; |
| 2262 | } |
| 2263 | |
| 2264 | #define MODE_HEADER_LENGTH 4 |
| 2265 | |
| 2266 | /* Mode header and page byte offsets */ |
| 2267 | #define MH_OFF_DATA_LENGTH 0 |
| 2268 | #define MH_OFF_MEDIUM_TYPE 1 |
| 2269 | #define MH_OFF_DEV_SPECIFIC 2 |
| 2270 | #define MH_OFF_BDESCS_LENGTH 3 |
| 2271 | #define MP_OFF_PAGE_NBR 0 |
| 2272 | #define MP_OFF_PAGE_LENGTH 1 |
| 2273 | |
| 2274 | /* Mode header and page bit masks */ |
| 2275 | #define MH_BIT_WP 0x80 |
| 2276 | #define MP_MSK_PAGE_NBR 0x3f |
| 2277 | |
| 2278 | /* Don't return block descriptors */ |
| 2279 | #define MODE_SENSE_OMIT_BDESCS 0x08 |
| 2280 | |
| 2281 | #define MODE_SELECT_PAGE_FORMAT 0x10 |
| 2282 | |
| 2283 | /* Read a mode page into the tape buffer. The block descriptors are included |
| 2284 | if incl_block_descs is true. The page control is ored to the page number |
| 2285 | parameter, if necessary. */ |
| 2286 | static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs) |
| 2287 | { |
| 2288 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2289 | struct st_request *SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | |
| 2291 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 2292 | cmd[0] = MODE_SENSE; |
| 2293 | if (omit_block_descs) |
| 2294 | cmd[1] = MODE_SENSE_OMIT_BDESCS; |
| 2295 | cmd[2] = page; |
| 2296 | cmd[4] = 255; |
| 2297 | |
| 2298 | SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE, |
| 2299 | STp->device->timeout, 0, 1); |
| 2300 | if (SRpnt == NULL) |
| 2301 | return (STp->buffer)->syscall_result; |
| 2302 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2303 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | |
| 2305 | return (STp->buffer)->syscall_result; |
| 2306 | } |
| 2307 | |
| 2308 | |
| 2309 | /* Send the mode page in the tape buffer to the drive. Assumes that the mode data |
| 2310 | in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */ |
| 2311 | static int write_mode_page(struct scsi_tape *STp, int page, int slow) |
| 2312 | { |
| 2313 | int pgo; |
| 2314 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2315 | struct st_request *SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | |
| 2317 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 2318 | cmd[0] = MODE_SELECT; |
| 2319 | cmd[1] = MODE_SELECT_PAGE_FORMAT; |
| 2320 | pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH]; |
| 2321 | cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2; |
| 2322 | |
| 2323 | /* Clear reserved fields */ |
| 2324 | (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0; |
| 2325 | (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0; |
| 2326 | (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP; |
| 2327 | (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR; |
| 2328 | |
| 2329 | SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE, |
| 2330 | (slow ? STp->long_timeout : STp->device->timeout), 0, 1); |
| 2331 | if (SRpnt == NULL) |
| 2332 | return (STp->buffer)->syscall_result; |
| 2333 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2334 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
| 2336 | return (STp->buffer)->syscall_result; |
| 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | #define COMPRESSION_PAGE 0x0f |
| 2341 | #define COMPRESSION_PAGE_LENGTH 16 |
| 2342 | |
| 2343 | #define CP_OFF_DCE_DCC 2 |
| 2344 | #define CP_OFF_C_ALGO 7 |
| 2345 | |
| 2346 | #define DCE_MASK 0x80 |
| 2347 | #define DCC_MASK 0x40 |
| 2348 | #define RED_MASK 0x60 |
| 2349 | |
| 2350 | |
| 2351 | /* Control the compression with mode page 15. Algorithm not changed if zero. |
| 2352 | |
| 2353 | The block descriptors are read and written because Sony SDT-7000 does not |
| 2354 | work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>). |
| 2355 | Including block descriptors should not cause any harm to other drives. */ |
| 2356 | |
| 2357 | static int st_compression(struct scsi_tape * STp, int state) |
| 2358 | { |
| 2359 | int retval; |
| 2360 | int mpoffs; /* Offset to mode page start */ |
| 2361 | unsigned char *b_data = (STp->buffer)->b_data; |
| 2362 | DEB( char *name = tape_name(STp); ) |
| 2363 | |
| 2364 | if (STp->ready != ST_READY) |
| 2365 | return (-EIO); |
| 2366 | |
| 2367 | /* Read the current page contents */ |
| 2368 | retval = read_mode_page(STp, COMPRESSION_PAGE, 0); |
| 2369 | if (retval) { |
| 2370 | DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n", |
| 2371 | name)); |
| 2372 | return (-EIO); |
| 2373 | } |
| 2374 | |
| 2375 | mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH]; |
| 2376 | DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name, |
| 2377 | (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0))); |
| 2378 | |
| 2379 | /* Check if compression can be changed */ |
| 2380 | if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) { |
| 2381 | DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name)); |
| 2382 | return (-EIO); |
| 2383 | } |
| 2384 | |
| 2385 | /* Do the change */ |
| 2386 | if (state) { |
| 2387 | b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK; |
| 2388 | if (STp->c_algo != 0) |
| 2389 | b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo; |
| 2390 | } |
| 2391 | else { |
| 2392 | b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK; |
| 2393 | if (STp->c_algo != 0) |
| 2394 | b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */ |
| 2395 | } |
| 2396 | |
| 2397 | retval = write_mode_page(STp, COMPRESSION_PAGE, 0); |
| 2398 | if (retval) { |
| 2399 | DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name)); |
| 2400 | return (-EIO); |
| 2401 | } |
| 2402 | DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n", |
| 2403 | name, state)); |
| 2404 | |
| 2405 | STp->compression_changed = 1; |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
| 2409 | |
| 2410 | /* Process the load and unload commands (does unload if the load code is zero) */ |
| 2411 | static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code) |
| 2412 | { |
| 2413 | int retval = (-EIO), timeout; |
| 2414 | DEB( char *name = tape_name(STp); ) |
| 2415 | unsigned char cmd[MAX_COMMAND_SIZE]; |
| 2416 | struct st_partstat *STps; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2417 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
| 2419 | if (STp->ready != ST_READY && !load_code) { |
| 2420 | if (STp->ready == ST_NO_TAPE) |
| 2421 | return (-ENOMEDIUM); |
| 2422 | else |
| 2423 | return (-EIO); |
| 2424 | } |
| 2425 | |
| 2426 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 2427 | cmd[0] = START_STOP; |
| 2428 | if (load_code) |
| 2429 | cmd[4] |= 1; |
| 2430 | /* |
| 2431 | * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A |
| 2432 | */ |
| 2433 | if (load_code >= 1 + MT_ST_HPLOADER_OFFSET |
| 2434 | && load_code <= 6 + MT_ST_HPLOADER_OFFSET) { |
| 2435 | DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n", |
| 2436 | name, (cmd[4]) ? "" : "un", |
| 2437 | load_code - MT_ST_HPLOADER_OFFSET)); |
| 2438 | cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */ |
| 2439 | } |
| 2440 | if (STp->immediate) { |
| 2441 | cmd[1] = 1; /* Don't wait for completion */ |
| 2442 | timeout = STp->device->timeout; |
| 2443 | } |
| 2444 | else |
| 2445 | timeout = STp->long_timeout; |
| 2446 | |
| 2447 | DEBC( |
| 2448 | if (!load_code) |
| 2449 | printk(ST_DEB_MSG "%s: Unloading tape.\n", name); |
| 2450 | else |
| 2451 | printk(ST_DEB_MSG "%s: Loading tape.\n", name); |
| 2452 | ); |
| 2453 | |
| 2454 | SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE, |
| 2455 | timeout, MAX_RETRIES, 1); |
| 2456 | if (!SRpnt) |
| 2457 | return (STp->buffer)->syscall_result; |
| 2458 | |
| 2459 | retval = (STp->buffer)->syscall_result; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2460 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | |
| 2462 | if (!retval) { /* SCSI command successful */ |
| 2463 | |
| 2464 | if (!load_code) { |
| 2465 | STp->rew_at_close = 0; |
| 2466 | STp->ready = ST_NO_TAPE; |
| 2467 | } |
| 2468 | else { |
| 2469 | STp->rew_at_close = STp->autorew_dev; |
| 2470 | retval = check_tape(STp, filp); |
| 2471 | if (retval > 0) |
| 2472 | retval = 0; |
| 2473 | } |
| 2474 | } |
| 2475 | else { |
| 2476 | STps = &(STp->ps[STp->partition]); |
| 2477 | STps->drv_file = STps->drv_block = (-1); |
| 2478 | } |
| 2479 | |
| 2480 | return retval; |
| 2481 | } |
| 2482 | |
| 2483 | #if DEBUG |
| 2484 | #define ST_DEB_FORWARD 0 |
| 2485 | #define ST_DEB_BACKWARD 1 |
| 2486 | static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd) |
| 2487 | { |
| 2488 | s32 sc; |
| 2489 | |
| 2490 | sc = cmd[2] & 0x80 ? 0xff000000 : 0; |
| 2491 | sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4]; |
| 2492 | if (direction) |
| 2493 | sc = -sc; |
| 2494 | printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name, |
| 2495 | direction ? "backward" : "forward", sc, units); |
| 2496 | } |
| 2497 | #endif |
| 2498 | |
| 2499 | |
| 2500 | /* Internal ioctl function */ |
| 2501 | static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg) |
| 2502 | { |
| 2503 | int timeout; |
| 2504 | long ltmp; |
| 2505 | int ioctl_result; |
| 2506 | int chg_eof = 1; |
| 2507 | unsigned char cmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2508 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | struct st_partstat *STps; |
| 2510 | int fileno, blkno, at_sm, undone; |
| 2511 | int datalen = 0, direction = DMA_NONE; |
| 2512 | char *name = tape_name(STp); |
| 2513 | |
| 2514 | WARN_ON(STp->buffer->do_dio != 0); |
| 2515 | if (STp->ready != ST_READY) { |
| 2516 | if (STp->ready == ST_NO_TAPE) |
| 2517 | return (-ENOMEDIUM); |
| 2518 | else |
| 2519 | return (-EIO); |
| 2520 | } |
| 2521 | timeout = STp->long_timeout; |
| 2522 | STps = &(STp->ps[STp->partition]); |
| 2523 | fileno = STps->drv_file; |
| 2524 | blkno = STps->drv_block; |
| 2525 | at_sm = STps->at_sm; |
| 2526 | |
| 2527 | memset(cmd, 0, MAX_COMMAND_SIZE); |
| 2528 | switch (cmd_in) { |
| 2529 | case MTFSFM: |
| 2530 | chg_eof = 0; /* Changed from the FSF after this */ |
| 2531 | case MTFSF: |
| 2532 | cmd[0] = SPACE; |
| 2533 | cmd[1] = 0x01; /* Space FileMarks */ |
| 2534 | cmd[2] = (arg >> 16); |
| 2535 | cmd[3] = (arg >> 8); |
| 2536 | cmd[4] = arg; |
| 2537 | DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);) |
| 2538 | if (fileno >= 0) |
| 2539 | fileno += arg; |
| 2540 | blkno = 0; |
| 2541 | at_sm &= (arg == 0); |
| 2542 | break; |
| 2543 | case MTBSFM: |
| 2544 | chg_eof = 0; /* Changed from the FSF after this */ |
| 2545 | case MTBSF: |
| 2546 | cmd[0] = SPACE; |
| 2547 | cmd[1] = 0x01; /* Space FileMarks */ |
| 2548 | ltmp = (-arg); |
| 2549 | cmd[2] = (ltmp >> 16); |
| 2550 | cmd[3] = (ltmp >> 8); |
| 2551 | cmd[4] = ltmp; |
| 2552 | DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);) |
| 2553 | if (fileno >= 0) |
| 2554 | fileno -= arg; |
| 2555 | blkno = (-1); /* We can't know the block number */ |
| 2556 | at_sm &= (arg == 0); |
| 2557 | break; |
| 2558 | case MTFSR: |
| 2559 | cmd[0] = SPACE; |
| 2560 | cmd[1] = 0x00; /* Space Blocks */ |
| 2561 | cmd[2] = (arg >> 16); |
| 2562 | cmd[3] = (arg >> 8); |
| 2563 | cmd[4] = arg; |
| 2564 | DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);) |
| 2565 | if (blkno >= 0) |
| 2566 | blkno += arg; |
| 2567 | at_sm &= (arg == 0); |
| 2568 | break; |
| 2569 | case MTBSR: |
| 2570 | cmd[0] = SPACE; |
| 2571 | cmd[1] = 0x00; /* Space Blocks */ |
| 2572 | ltmp = (-arg); |
| 2573 | cmd[2] = (ltmp >> 16); |
| 2574 | cmd[3] = (ltmp >> 8); |
| 2575 | cmd[4] = ltmp; |
| 2576 | DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);) |
| 2577 | if (blkno >= 0) |
| 2578 | blkno -= arg; |
| 2579 | at_sm &= (arg == 0); |
| 2580 | break; |
| 2581 | case MTFSS: |
| 2582 | cmd[0] = SPACE; |
| 2583 | cmd[1] = 0x04; /* Space Setmarks */ |
| 2584 | cmd[2] = (arg >> 16); |
| 2585 | cmd[3] = (arg >> 8); |
| 2586 | cmd[4] = arg; |
| 2587 | DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);) |
| 2588 | if (arg != 0) { |
| 2589 | blkno = fileno = (-1); |
| 2590 | at_sm = 1; |
| 2591 | } |
| 2592 | break; |
| 2593 | case MTBSS: |
| 2594 | cmd[0] = SPACE; |
| 2595 | cmd[1] = 0x04; /* Space Setmarks */ |
| 2596 | ltmp = (-arg); |
| 2597 | cmd[2] = (ltmp >> 16); |
| 2598 | cmd[3] = (ltmp >> 8); |
| 2599 | cmd[4] = ltmp; |
| 2600 | DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);) |
| 2601 | if (arg != 0) { |
| 2602 | blkno = fileno = (-1); |
| 2603 | at_sm = 1; |
| 2604 | } |
| 2605 | break; |
| 2606 | case MTWEOF: |
| 2607 | case MTWSM: |
| 2608 | if (STp->write_prot) |
| 2609 | return (-EACCES); |
| 2610 | cmd[0] = WRITE_FILEMARKS; |
| 2611 | if (cmd_in == MTWSM) |
| 2612 | cmd[1] = 2; |
| 2613 | cmd[2] = (arg >> 16); |
| 2614 | cmd[3] = (arg >> 8); |
| 2615 | cmd[4] = arg; |
| 2616 | timeout = STp->device->timeout; |
| 2617 | DEBC( |
| 2618 | if (cmd_in == MTWEOF) |
| 2619 | printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name, |
| 2620 | cmd[2] * 65536 + cmd[3] * 256 + cmd[4]); |
| 2621 | else |
| 2622 | printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name, |
| 2623 | cmd[2] * 65536 + cmd[3] * 256 + cmd[4]); |
| 2624 | ) |
| 2625 | if (fileno >= 0) |
| 2626 | fileno += arg; |
| 2627 | blkno = 0; |
| 2628 | at_sm = (cmd_in == MTWSM); |
| 2629 | break; |
| 2630 | case MTREW: |
| 2631 | cmd[0] = REZERO_UNIT; |
| 2632 | if (STp->immediate) { |
| 2633 | cmd[1] = 1; /* Don't wait for completion */ |
| 2634 | timeout = STp->device->timeout; |
| 2635 | } |
| 2636 | DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name)); |
| 2637 | fileno = blkno = at_sm = 0; |
| 2638 | break; |
| 2639 | case MTNOP: |
| 2640 | DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name)); |
| 2641 | return 0; /* Should do something ? */ |
| 2642 | break; |
| 2643 | case MTRETEN: |
| 2644 | cmd[0] = START_STOP; |
| 2645 | if (STp->immediate) { |
| 2646 | cmd[1] = 1; /* Don't wait for completion */ |
| 2647 | timeout = STp->device->timeout; |
| 2648 | } |
| 2649 | cmd[4] = 3; |
| 2650 | DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name)); |
| 2651 | fileno = blkno = at_sm = 0; |
| 2652 | break; |
| 2653 | case MTEOM: |
| 2654 | if (!STp->fast_mteom) { |
| 2655 | /* space to the end of tape */ |
| 2656 | ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff); |
| 2657 | fileno = STps->drv_file; |
| 2658 | if (STps->eof >= ST_EOD_1) |
| 2659 | return 0; |
| 2660 | /* The next lines would hide the number of spaced FileMarks |
| 2661 | That's why I inserted the previous lines. I had no luck |
| 2662 | with detecting EOM with FSF, so we go now to EOM. |
| 2663 | Joerg Weule */ |
| 2664 | } else |
| 2665 | fileno = (-1); |
| 2666 | cmd[0] = SPACE; |
| 2667 | cmd[1] = 3; |
| 2668 | DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n", |
| 2669 | name)); |
| 2670 | blkno = -1; |
| 2671 | at_sm = 0; |
| 2672 | break; |
| 2673 | case MTERASE: |
| 2674 | if (STp->write_prot) |
| 2675 | return (-EACCES); |
| 2676 | cmd[0] = ERASE; |
| 2677 | cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */ |
| 2678 | if (STp->immediate) { |
| 2679 | cmd[1] |= 2; /* Don't wait for completion */ |
| 2680 | timeout = STp->device->timeout; |
| 2681 | } |
| 2682 | else |
| 2683 | timeout = STp->long_timeout * 8; |
| 2684 | |
| 2685 | DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name)); |
| 2686 | fileno = blkno = at_sm = 0; |
| 2687 | break; |
| 2688 | case MTSETBLK: /* Set block length */ |
| 2689 | case MTSETDENSITY: /* Set tape density */ |
| 2690 | case MTSETDRVBUFFER: /* Set drive buffering */ |
| 2691 | case SET_DENS_AND_BLK: /* Set density and block size */ |
| 2692 | chg_eof = 0; |
| 2693 | if (STp->dirty || (STp->buffer)->buffer_bytes != 0) |
| 2694 | return (-EIO); /* Not allowed if data in buffer */ |
| 2695 | if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) && |
| 2696 | (arg & MT_ST_BLKSIZE_MASK) != 0 && |
| 2697 | STp->max_block > 0 && |
| 2698 | ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block || |
| 2699 | (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) { |
| 2700 | printk(KERN_WARNING "%s: Illegal block size.\n", name); |
| 2701 | return (-EINVAL); |
| 2702 | } |
| 2703 | cmd[0] = MODE_SELECT; |
| 2704 | if ((STp->use_pf & USE_PF)) |
| 2705 | cmd[1] = MODE_SELECT_PAGE_FORMAT; |
| 2706 | cmd[4] = datalen = 12; |
| 2707 | direction = DMA_TO_DEVICE; |
| 2708 | |
| 2709 | memset((STp->buffer)->b_data, 0, 12); |
| 2710 | if (cmd_in == MTSETDRVBUFFER) |
| 2711 | (STp->buffer)->b_data[2] = (arg & 7) << 4; |
| 2712 | else |
| 2713 | (STp->buffer)->b_data[2] = |
| 2714 | STp->drv_buffer << 4; |
| 2715 | (STp->buffer)->b_data[3] = 8; /* block descriptor length */ |
| 2716 | if (cmd_in == MTSETDENSITY) { |
| 2717 | (STp->buffer)->b_data[4] = arg; |
| 2718 | STp->density_changed = 1; /* At least we tried ;-) */ |
| 2719 | } else if (cmd_in == SET_DENS_AND_BLK) |
| 2720 | (STp->buffer)->b_data[4] = arg >> 24; |
| 2721 | else |
| 2722 | (STp->buffer)->b_data[4] = STp->density; |
| 2723 | if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) { |
| 2724 | ltmp = arg & MT_ST_BLKSIZE_MASK; |
| 2725 | if (cmd_in == MTSETBLK) |
| 2726 | STp->blksize_changed = 1; /* At least we tried ;-) */ |
| 2727 | } else |
| 2728 | ltmp = STp->block_size; |
| 2729 | (STp->buffer)->b_data[9] = (ltmp >> 16); |
| 2730 | (STp->buffer)->b_data[10] = (ltmp >> 8); |
| 2731 | (STp->buffer)->b_data[11] = ltmp; |
| 2732 | timeout = STp->device->timeout; |
| 2733 | DEBC( |
| 2734 | if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) |
| 2735 | printk(ST_DEB_MSG |
| 2736 | "%s: Setting block size to %d bytes.\n", name, |
| 2737 | (STp->buffer)->b_data[9] * 65536 + |
| 2738 | (STp->buffer)->b_data[10] * 256 + |
| 2739 | (STp->buffer)->b_data[11]); |
| 2740 | if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK) |
| 2741 | printk(ST_DEB_MSG |
| 2742 | "%s: Setting density code to %x.\n", name, |
| 2743 | (STp->buffer)->b_data[4]); |
| 2744 | if (cmd_in == MTSETDRVBUFFER) |
| 2745 | printk(ST_DEB_MSG |
| 2746 | "%s: Setting drive buffer code to %d.\n", name, |
| 2747 | ((STp->buffer)->b_data[2] >> 4) & 7); |
| 2748 | ) |
| 2749 | break; |
| 2750 | default: |
| 2751 | return (-ENOSYS); |
| 2752 | } |
| 2753 | |
| 2754 | SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction, |
| 2755 | timeout, MAX_RETRIES, 1); |
| 2756 | if (!SRpnt) |
| 2757 | return (STp->buffer)->syscall_result; |
| 2758 | |
| 2759 | ioctl_result = (STp->buffer)->syscall_result; |
| 2760 | |
| 2761 | if (!ioctl_result) { /* SCSI command successful */ |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2762 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | SRpnt = NULL; |
| 2764 | STps->drv_block = blkno; |
| 2765 | STps->drv_file = fileno; |
| 2766 | STps->at_sm = at_sm; |
| 2767 | |
| 2768 | if (cmd_in == MTBSFM) |
| 2769 | ioctl_result = st_int_ioctl(STp, MTFSF, 1); |
| 2770 | else if (cmd_in == MTFSFM) |
| 2771 | ioctl_result = st_int_ioctl(STp, MTBSF, 1); |
| 2772 | |
| 2773 | if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) { |
| 2774 | int old_block_size = STp->block_size; |
| 2775 | STp->block_size = arg & MT_ST_BLKSIZE_MASK; |
| 2776 | if (STp->block_size != 0) { |
| 2777 | if (old_block_size == 0) |
| 2778 | normalize_buffer(STp->buffer); |
| 2779 | (STp->buffer)->buffer_blocks = |
| 2780 | (STp->buffer)->buffer_size / STp->block_size; |
| 2781 | } |
| 2782 | (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0; |
| 2783 | if (cmd_in == SET_DENS_AND_BLK) |
| 2784 | STp->density = arg >> MT_ST_DENSITY_SHIFT; |
| 2785 | } else if (cmd_in == MTSETDRVBUFFER) |
| 2786 | STp->drv_buffer = (arg & 7); |
| 2787 | else if (cmd_in == MTSETDENSITY) |
| 2788 | STp->density = arg; |
| 2789 | |
| 2790 | if (cmd_in == MTEOM) |
| 2791 | STps->eof = ST_EOD; |
| 2792 | else if (cmd_in == MTFSF) |
| 2793 | STps->eof = ST_FM; |
| 2794 | else if (chg_eof) |
| 2795 | STps->eof = ST_NOEOF; |
| 2796 | |
| 2797 | if (cmd_in == MTWEOF) |
| 2798 | STps->rw = ST_IDLE; |
| 2799 | } else { /* SCSI command was not completely successful. Don't return |
| 2800 | from this block without releasing the SCSI command block! */ |
| 2801 | struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; |
| 2802 | |
| 2803 | if (cmdstatp->flags & SENSE_EOM) { |
| 2804 | if (cmd_in != MTBSF && cmd_in != MTBSFM && |
| 2805 | cmd_in != MTBSR && cmd_in != MTBSS) |
| 2806 | STps->eof = ST_EOM_OK; |
| 2807 | STps->drv_block = 0; |
| 2808 | } |
| 2809 | |
| 2810 | if (cmdstatp->remainder_valid) |
| 2811 | undone = (int)cmdstatp->uremainder64; |
| 2812 | else |
| 2813 | undone = 0; |
| 2814 | |
| 2815 | if (cmd_in == MTWEOF && |
| 2816 | cmdstatp->have_sense && |
| 2817 | (cmdstatp->flags & SENSE_EOM) && |
| 2818 | (cmdstatp->sense_hdr.sense_key == NO_SENSE || |
| 2819 | cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) && |
| 2820 | undone == 0) { |
| 2821 | ioctl_result = 0; /* EOF written succesfully at EOM */ |
| 2822 | if (fileno >= 0) |
| 2823 | fileno++; |
| 2824 | STps->drv_file = fileno; |
| 2825 | STps->eof = ST_NOEOF; |
| 2826 | } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) { |
| 2827 | if (fileno >= 0) |
| 2828 | STps->drv_file = fileno - undone; |
| 2829 | else |
| 2830 | STps->drv_file = fileno; |
| 2831 | STps->drv_block = -1; |
| 2832 | STps->eof = ST_NOEOF; |
| 2833 | } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) { |
| 2834 | if (arg > 0 && undone < 0) /* Some drives get this wrong */ |
| 2835 | undone = (-undone); |
| 2836 | if (STps->drv_file >= 0) |
| 2837 | STps->drv_file = fileno + undone; |
| 2838 | STps->drv_block = 0; |
| 2839 | STps->eof = ST_NOEOF; |
| 2840 | } else if (cmd_in == MTFSR) { |
| 2841 | if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */ |
| 2842 | if (STps->drv_file >= 0) |
| 2843 | STps->drv_file++; |
| 2844 | STps->drv_block = 0; |
| 2845 | STps->eof = ST_FM; |
| 2846 | } else { |
| 2847 | if (blkno >= undone) |
| 2848 | STps->drv_block = blkno - undone; |
| 2849 | else |
| 2850 | STps->drv_block = (-1); |
| 2851 | STps->eof = ST_NOEOF; |
| 2852 | } |
| 2853 | } else if (cmd_in == MTBSR) { |
| 2854 | if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */ |
| 2855 | STps->drv_file--; |
| 2856 | STps->drv_block = (-1); |
| 2857 | } else { |
| 2858 | if (arg > 0 && undone < 0) /* Some drives get this wrong */ |
| 2859 | undone = (-undone); |
| 2860 | if (STps->drv_block >= 0) |
| 2861 | STps->drv_block = blkno + undone; |
| 2862 | } |
| 2863 | STps->eof = ST_NOEOF; |
| 2864 | } else if (cmd_in == MTEOM) { |
| 2865 | STps->drv_file = (-1); |
| 2866 | STps->drv_block = (-1); |
| 2867 | STps->eof = ST_EOD; |
| 2868 | } else if (cmd_in == MTSETBLK || |
| 2869 | cmd_in == MTSETDENSITY || |
| 2870 | cmd_in == MTSETDRVBUFFER || |
| 2871 | cmd_in == SET_DENS_AND_BLK) { |
| 2872 | if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST && |
| 2873 | !(STp->use_pf & PF_TESTED)) { |
| 2874 | /* Try the other possible state of Page Format if not |
| 2875 | already tried */ |
| 2876 | STp->use_pf = !STp->use_pf | PF_TESTED; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2877 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | SRpnt = NULL; |
| 2879 | return st_int_ioctl(STp, cmd_in, arg); |
| 2880 | } |
| 2881 | } else if (chg_eof) |
| 2882 | STps->eof = ST_NOEOF; |
| 2883 | |
| 2884 | if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK) |
| 2885 | STps->eof = ST_EOD; |
| 2886 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2887 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | SRpnt = NULL; |
| 2889 | } |
| 2890 | |
| 2891 | return ioctl_result; |
| 2892 | } |
| 2893 | |
| 2894 | |
| 2895 | /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc |
| 2896 | structure. */ |
| 2897 | |
| 2898 | static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition, |
| 2899 | int logical) |
| 2900 | { |
| 2901 | int result; |
| 2902 | unsigned char scmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2903 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | DEB( char *name = tape_name(STp); ) |
| 2905 | |
| 2906 | if (STp->ready != ST_READY) |
| 2907 | return (-EIO); |
| 2908 | |
| 2909 | memset(scmd, 0, MAX_COMMAND_SIZE); |
| 2910 | if ((STp->device)->scsi_level < SCSI_2) { |
| 2911 | scmd[0] = QFA_REQUEST_BLOCK; |
| 2912 | scmd[4] = 3; |
| 2913 | } else { |
| 2914 | scmd[0] = READ_POSITION; |
| 2915 | if (!logical && !STp->scsi2_logical) |
| 2916 | scmd[1] = 1; |
| 2917 | } |
| 2918 | SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE, |
| 2919 | STp->device->timeout, MAX_READY_RETRIES, 1); |
| 2920 | if (!SRpnt) |
| 2921 | return (STp->buffer)->syscall_result; |
| 2922 | |
| 2923 | if ((STp->buffer)->syscall_result != 0 || |
| 2924 | (STp->device->scsi_level >= SCSI_2 && |
| 2925 | ((STp->buffer)->b_data[0] & 4) != 0)) { |
| 2926 | *block = *partition = 0; |
| 2927 | DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name)); |
| 2928 | result = (-EIO); |
| 2929 | } else { |
| 2930 | result = 0; |
| 2931 | if ((STp->device)->scsi_level < SCSI_2) { |
| 2932 | *block = ((STp->buffer)->b_data[0] << 16) |
| 2933 | + ((STp->buffer)->b_data[1] << 8) |
| 2934 | + (STp->buffer)->b_data[2]; |
| 2935 | *partition = 0; |
| 2936 | } else { |
| 2937 | *block = ((STp->buffer)->b_data[4] << 24) |
| 2938 | + ((STp->buffer)->b_data[5] << 16) |
| 2939 | + ((STp->buffer)->b_data[6] << 8) |
| 2940 | + (STp->buffer)->b_data[7]; |
| 2941 | *partition = (STp->buffer)->b_data[1]; |
| 2942 | if (((STp->buffer)->b_data[0] & 0x80) && |
| 2943 | (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */ |
| 2944 | STp->ps[0].drv_block = STp->ps[0].drv_file = 0; |
| 2945 | } |
| 2946 | DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name, |
| 2947 | *block, *partition)); |
| 2948 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2949 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | SRpnt = NULL; |
| 2951 | |
| 2952 | return result; |
| 2953 | } |
| 2954 | |
| 2955 | |
| 2956 | /* Set the tape block and partition. Negative partition means that only the |
| 2957 | block should be set in vendor specific way. */ |
| 2958 | static int set_location(struct scsi_tape *STp, unsigned int block, int partition, |
| 2959 | int logical) |
| 2960 | { |
| 2961 | struct st_partstat *STps; |
| 2962 | int result, p; |
| 2963 | unsigned int blk; |
| 2964 | int timeout; |
| 2965 | unsigned char scmd[MAX_COMMAND_SIZE]; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 2966 | struct st_request *SRpnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | DEB( char *name = tape_name(STp); ) |
| 2968 | |
| 2969 | if (STp->ready != ST_READY) |
| 2970 | return (-EIO); |
| 2971 | timeout = STp->long_timeout; |
| 2972 | STps = &(STp->ps[STp->partition]); |
| 2973 | |
| 2974 | DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n", |
| 2975 | name, block, partition)); |
| 2976 | DEB(if (partition < 0) |
| 2977 | return (-EIO); ) |
| 2978 | |
| 2979 | /* Update the location at the partition we are leaving */ |
| 2980 | if ((!STp->can_partitions && partition != 0) || |
| 2981 | partition >= ST_NBR_PARTITIONS) |
| 2982 | return (-EINVAL); |
| 2983 | if (partition != STp->partition) { |
| 2984 | if (get_location(STp, &blk, &p, 1)) |
| 2985 | STps->last_block_valid = 0; |
| 2986 | else { |
| 2987 | STps->last_block_valid = 1; |
| 2988 | STps->last_block_visited = blk; |
| 2989 | DEBC(printk(ST_DEB_MSG |
| 2990 | "%s: Visited block %d for partition %d saved.\n", |
| 2991 | name, blk, STp->partition)); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | memset(scmd, 0, MAX_COMMAND_SIZE); |
| 2996 | if ((STp->device)->scsi_level < SCSI_2) { |
| 2997 | scmd[0] = QFA_SEEK_BLOCK; |
| 2998 | scmd[2] = (block >> 16); |
| 2999 | scmd[3] = (block >> 8); |
| 3000 | scmd[4] = block; |
| 3001 | scmd[5] = 0; |
| 3002 | } else { |
| 3003 | scmd[0] = SEEK_10; |
| 3004 | scmd[3] = (block >> 24); |
| 3005 | scmd[4] = (block >> 16); |
| 3006 | scmd[5] = (block >> 8); |
| 3007 | scmd[6] = block; |
| 3008 | if (!logical && !STp->scsi2_logical) |
| 3009 | scmd[1] = 4; |
| 3010 | if (STp->partition != partition) { |
| 3011 | scmd[1] |= 2; |
| 3012 | scmd[8] = partition; |
| 3013 | DEBC(printk(ST_DEB_MSG |
| 3014 | "%s: Trying to change partition from %d to %d\n", |
| 3015 | name, STp->partition, partition)); |
| 3016 | } |
| 3017 | } |
| 3018 | if (STp->immediate) { |
| 3019 | scmd[1] |= 1; /* Don't wait for completion */ |
| 3020 | timeout = STp->device->timeout; |
| 3021 | } |
| 3022 | |
| 3023 | SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE, |
| 3024 | timeout, MAX_READY_RETRIES, 1); |
| 3025 | if (!SRpnt) |
| 3026 | return (STp->buffer)->syscall_result; |
| 3027 | |
| 3028 | STps->drv_block = STps->drv_file = (-1); |
| 3029 | STps->eof = ST_NOEOF; |
| 3030 | if ((STp->buffer)->syscall_result != 0) { |
| 3031 | result = (-EIO); |
| 3032 | if (STp->can_partitions && |
| 3033 | (STp->device)->scsi_level >= SCSI_2 && |
| 3034 | (p = find_partition(STp)) >= 0) |
| 3035 | STp->partition = p; |
| 3036 | } else { |
| 3037 | if (STp->can_partitions) { |
| 3038 | STp->partition = partition; |
| 3039 | STps = &(STp->ps[partition]); |
| 3040 | if (!STps->last_block_valid || |
| 3041 | STps->last_block_visited != block) { |
| 3042 | STps->at_sm = 0; |
| 3043 | STps->rw = ST_IDLE; |
| 3044 | } |
| 3045 | } else |
| 3046 | STps->at_sm = 0; |
| 3047 | if (block == 0) |
| 3048 | STps->drv_block = STps->drv_file = 0; |
| 3049 | result = 0; |
| 3050 | } |
| 3051 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 3052 | st_release_request(SRpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | SRpnt = NULL; |
| 3054 | |
| 3055 | return result; |
| 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | /* Find the current partition number for the drive status. Called from open and |
| 3060 | returns either partition number of negative error code. */ |
| 3061 | static int find_partition(struct scsi_tape *STp) |
| 3062 | { |
| 3063 | int i, partition; |
| 3064 | unsigned int block; |
| 3065 | |
| 3066 | if ((i = get_location(STp, &block, &partition, 1)) < 0) |
| 3067 | return i; |
| 3068 | if (partition >= ST_NBR_PARTITIONS) |
| 3069 | return (-EIO); |
| 3070 | return partition; |
| 3071 | } |
| 3072 | |
| 3073 | |
| 3074 | /* Change the partition if necessary */ |
| 3075 | static int switch_partition(struct scsi_tape *STp) |
| 3076 | { |
| 3077 | struct st_partstat *STps; |
| 3078 | |
| 3079 | if (STp->partition == STp->new_partition) |
| 3080 | return 0; |
| 3081 | STps = &(STp->ps[STp->new_partition]); |
| 3082 | if (!STps->last_block_valid) |
| 3083 | STps->last_block_visited = 0; |
| 3084 | return set_location(STp, STps->last_block_visited, STp->new_partition, 1); |
| 3085 | } |
| 3086 | |
| 3087 | /* Functions for reading and writing the medium partition mode page. */ |
| 3088 | |
| 3089 | #define PART_PAGE 0x11 |
| 3090 | #define PART_PAGE_FIXED_LENGTH 8 |
| 3091 | |
| 3092 | #define PP_OFF_MAX_ADD_PARTS 2 |
| 3093 | #define PP_OFF_NBR_ADD_PARTS 3 |
| 3094 | #define PP_OFF_FLAGS 4 |
| 3095 | #define PP_OFF_PART_UNITS 6 |
| 3096 | #define PP_OFF_RESERVED 7 |
| 3097 | |
| 3098 | #define PP_BIT_IDP 0x20 |
| 3099 | #define PP_MSK_PSUM_MB 0x10 |
| 3100 | |
| 3101 | /* Get the number of partitions on the tape. As a side effect reads the |
| 3102 | mode page into the tape buffer. */ |
| 3103 | static int nbr_partitions(struct scsi_tape *STp) |
| 3104 | { |
| 3105 | int result; |
| 3106 | DEB( char *name = tape_name(STp); ) |
| 3107 | |
| 3108 | if (STp->ready != ST_READY) |
| 3109 | return (-EIO); |
| 3110 | |
| 3111 | result = read_mode_page(STp, PART_PAGE, 1); |
| 3112 | |
| 3113 | if (result) { |
| 3114 | DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n", |
| 3115 | name)); |
| 3116 | result = (-EIO); |
| 3117 | } else { |
| 3118 | result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + |
| 3119 | PP_OFF_NBR_ADD_PARTS] + 1; |
| 3120 | DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result)); |
| 3121 | } |
| 3122 | |
| 3123 | return result; |
| 3124 | } |
| 3125 | |
| 3126 | |
| 3127 | /* Partition the tape into two partitions if size > 0 or one partition if |
| 3128 | size == 0. |
| 3129 | |
| 3130 | The block descriptors are read and written because Sony SDT-7000 does not |
| 3131 | work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>). |
| 3132 | |
| 3133 | My HP C1533A drive returns only one partition size field. This is used to |
| 3134 | set the size of partition 1. There is no size field for the default partition. |
| 3135 | Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is |
| 3136 | used to set the size of partition 1 (this is what the SCSI-3 standard specifies). |
| 3137 | The following algorithm is used to accommodate both drives: if the number of |
| 3138 | partition size fields is greater than the maximum number of additional partitions |
| 3139 | in the mode page, the second field is used. Otherwise the first field is used. |
| 3140 | |
| 3141 | For Seagate DDS drives the page length must be 8 when no partitions is defined |
| 3142 | and 10 when 1 partition is defined (information from Eric Lee Green). This is |
| 3143 | is acceptable also to some other old drives and enforced if the first partition |
| 3144 | size field is used for the first additional partition size. |
| 3145 | */ |
| 3146 | static int partition_tape(struct scsi_tape *STp, int size) |
| 3147 | { |
| 3148 | char *name = tape_name(STp); |
| 3149 | int result; |
| 3150 | int pgo, psd_cnt, psdo; |
| 3151 | unsigned char *bp; |
| 3152 | |
| 3153 | result = read_mode_page(STp, PART_PAGE, 0); |
| 3154 | if (result) { |
| 3155 | DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name)); |
| 3156 | return result; |
| 3157 | } |
| 3158 | /* The mode page is in the buffer. Let's modify it and write it. */ |
| 3159 | bp = (STp->buffer)->b_data; |
| 3160 | pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH]; |
| 3161 | DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n", |
| 3162 | name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2)); |
| 3163 | |
| 3164 | psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2; |
| 3165 | psdo = pgo + PART_PAGE_FIXED_LENGTH; |
| 3166 | if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) { |
| 3167 | bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */ |
| 3168 | psdo += 2; |
| 3169 | } |
| 3170 | memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2); |
| 3171 | |
| 3172 | DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name, |
| 3173 | psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS], |
| 3174 | bp[pgo + PP_OFF_NBR_ADD_PARTS])); |
| 3175 | |
| 3176 | if (size <= 0) { |
| 3177 | bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0; |
| 3178 | if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS]) |
| 3179 | bp[pgo + MP_OFF_PAGE_LENGTH] = 6; |
| 3180 | DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n", |
| 3181 | name)); |
| 3182 | } else { |
| 3183 | bp[psdo] = (size >> 8) & 0xff; |
| 3184 | bp[psdo + 1] = size & 0xff; |
| 3185 | bp[pgo + 3] = 1; |
| 3186 | if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8) |
| 3187 | bp[pgo + MP_OFF_PAGE_LENGTH] = 8; |
| 3188 | DEBC(printk(ST_DEB_MSG |
| 3189 | "%s: Formatting tape with two partitions (1 = %d MB).\n", |
| 3190 | name, size)); |
| 3191 | } |
| 3192 | bp[pgo + PP_OFF_PART_UNITS] = 0; |
| 3193 | bp[pgo + PP_OFF_RESERVED] = 0; |
| 3194 | bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB; |
| 3195 | |
| 3196 | result = write_mode_page(STp, PART_PAGE, 1); |
| 3197 | if (result) { |
| 3198 | printk(KERN_INFO "%s: Partitioning of tape failed.\n", name); |
| 3199 | result = (-EIO); |
| 3200 | } |
| 3201 | |
| 3202 | return result; |
| 3203 | } |
| 3204 | |
| 3205 | |
| 3206 | |
| 3207 | /* The ioctl command */ |
| 3208 | static int st_ioctl(struct inode *inode, struct file *file, |
| 3209 | unsigned int cmd_in, unsigned long arg) |
| 3210 | { |
| 3211 | int i, cmd_nr, cmd_type, bt; |
| 3212 | int retval = 0; |
| 3213 | unsigned int blk; |
| 3214 | struct scsi_tape *STp = file->private_data; |
| 3215 | struct st_modedef *STm; |
| 3216 | struct st_partstat *STps; |
| 3217 | char *name = tape_name(STp); |
| 3218 | void __user *p = (void __user *)arg; |
| 3219 | |
| 3220 | if (down_interruptible(&STp->lock)) |
| 3221 | return -ERESTARTSYS; |
| 3222 | |
| 3223 | DEB( |
| 3224 | if (debugging && !STp->in_use) { |
| 3225 | printk(ST_DEB_MSG "%s: Incorrect device.\n", name); |
| 3226 | retval = (-EIO); |
| 3227 | goto out; |
| 3228 | } ) /* end DEB */ |
| 3229 | |
| 3230 | STm = &(STp->modes[STp->current_mode]); |
| 3231 | STps = &(STp->ps[STp->partition]); |
| 3232 | |
| 3233 | /* |
| 3234 | * If we are in the middle of error recovery, don't let anyone |
| 3235 | * else try and use this device. Also, if error recovery fails, it |
| 3236 | * may try and take the device offline, in which case all further |
| 3237 | * access to the device is prohibited. |
| 3238 | */ |
| 3239 | retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file); |
| 3240 | if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV) |
| 3241 | goto out; |
| 3242 | retval = 0; |
| 3243 | |
| 3244 | cmd_type = _IOC_TYPE(cmd_in); |
| 3245 | cmd_nr = _IOC_NR(cmd_in); |
| 3246 | |
| 3247 | if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) { |
| 3248 | struct mtop mtc; |
| 3249 | |
| 3250 | if (_IOC_SIZE(cmd_in) != sizeof(mtc)) { |
| 3251 | retval = (-EINVAL); |
| 3252 | goto out; |
| 3253 | } |
| 3254 | |
| 3255 | i = copy_from_user(&mtc, p, sizeof(struct mtop)); |
| 3256 | if (i) { |
| 3257 | retval = (-EFAULT); |
| 3258 | goto out; |
| 3259 | } |
| 3260 | |
| 3261 | if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) { |
| 3262 | printk(KERN_WARNING |
| 3263 | "%s: MTSETDRVBUFFER only allowed for root.\n", name); |
| 3264 | retval = (-EPERM); |
| 3265 | goto out; |
| 3266 | } |
| 3267 | if (!STm->defined && |
| 3268 | (mtc.mt_op != MTSETDRVBUFFER && |
| 3269 | (mtc.mt_count & MT_ST_OPTIONS) == 0)) { |
| 3270 | retval = (-ENXIO); |
| 3271 | goto out; |
| 3272 | } |
| 3273 | |
| 3274 | if (!STp->pos_unknown) { |
| 3275 | |
| 3276 | if (STps->eof == ST_FM_HIT) { |
| 3277 | if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM || |
| 3278 | mtc.mt_op == MTEOM) { |
| 3279 | mtc.mt_count -= 1; |
| 3280 | if (STps->drv_file >= 0) |
| 3281 | STps->drv_file += 1; |
| 3282 | } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) { |
| 3283 | mtc.mt_count += 1; |
| 3284 | if (STps->drv_file >= 0) |
| 3285 | STps->drv_file += 1; |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | if (mtc.mt_op == MTSEEK) { |
| 3290 | /* Old position must be restored if partition will be |
| 3291 | changed */ |
| 3292 | i = !STp->can_partitions || |
| 3293 | (STp->new_partition != STp->partition); |
| 3294 | } else { |
| 3295 | i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL || |
| 3296 | mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM || |
| 3297 | mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD || |
| 3298 | mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM || |
| 3299 | mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM || |
| 3300 | mtc.mt_op == MTCOMPRESSION; |
| 3301 | } |
| 3302 | i = flush_buffer(STp, i); |
| 3303 | if (i < 0) { |
| 3304 | retval = i; |
| 3305 | goto out; |
| 3306 | } |
| 3307 | if (STps->rw == ST_WRITING && |
| 3308 | (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL || |
| 3309 | mtc.mt_op == MTSEEK || |
| 3310 | mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) { |
| 3311 | i = st_int_ioctl(STp, MTWEOF, 1); |
| 3312 | if (i < 0) { |
| 3313 | retval = i; |
| 3314 | goto out; |
| 3315 | } |
| 3316 | if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) |
| 3317 | mtc.mt_count++; |
| 3318 | STps->rw = ST_IDLE; |
| 3319 | } |
| 3320 | |
| 3321 | } else { |
| 3322 | /* |
| 3323 | * If there was a bus reset, block further access |
| 3324 | * to this device. If the user wants to rewind the tape, |
| 3325 | * then reset the flag and allow access again. |
| 3326 | */ |
| 3327 | if (mtc.mt_op != MTREW && |
| 3328 | mtc.mt_op != MTOFFL && |
| 3329 | mtc.mt_op != MTRETEN && |
| 3330 | mtc.mt_op != MTERASE && |
| 3331 | mtc.mt_op != MTSEEK && |
| 3332 | mtc.mt_op != MTEOM) { |
| 3333 | retval = (-EIO); |
| 3334 | goto out; |
| 3335 | } |
| 3336 | reset_state(STp); |
| 3337 | /* remove this when the midlevel properly clears was_reset */ |
| 3338 | STp->device->was_reset = 0; |
| 3339 | } |
| 3340 | |
| 3341 | if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK && |
| 3342 | mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM && |
| 3343 | mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART) |
| 3344 | STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */ |
| 3345 | |
| 3346 | if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED) |
| 3347 | do_door_lock(STp, 0); /* Ignore result! */ |
| 3348 | |
| 3349 | if (mtc.mt_op == MTSETDRVBUFFER && |
| 3350 | (mtc.mt_count & MT_ST_OPTIONS) != 0) { |
| 3351 | retval = st_set_options(STp, mtc.mt_count); |
| 3352 | goto out; |
| 3353 | } |
| 3354 | |
| 3355 | if (mtc.mt_op == MTSETPART) { |
| 3356 | if (!STp->can_partitions || |
| 3357 | mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) { |
| 3358 | retval = (-EINVAL); |
| 3359 | goto out; |
| 3360 | } |
| 3361 | if (mtc.mt_count >= STp->nbr_partitions && |
| 3362 | (STp->nbr_partitions = nbr_partitions(STp)) < 0) { |
| 3363 | retval = (-EIO); |
| 3364 | goto out; |
| 3365 | } |
| 3366 | if (mtc.mt_count >= STp->nbr_partitions) { |
| 3367 | retval = (-EINVAL); |
| 3368 | goto out; |
| 3369 | } |
| 3370 | STp->new_partition = mtc.mt_count; |
| 3371 | retval = 0; |
| 3372 | goto out; |
| 3373 | } |
| 3374 | |
| 3375 | if (mtc.mt_op == MTMKPART) { |
| 3376 | if (!STp->can_partitions) { |
| 3377 | retval = (-EINVAL); |
| 3378 | goto out; |
| 3379 | } |
| 3380 | if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 || |
| 3381 | (i = partition_tape(STp, mtc.mt_count)) < 0) { |
| 3382 | retval = i; |
| 3383 | goto out; |
| 3384 | } |
| 3385 | for (i = 0; i < ST_NBR_PARTITIONS; i++) { |
| 3386 | STp->ps[i].rw = ST_IDLE; |
| 3387 | STp->ps[i].at_sm = 0; |
| 3388 | STp->ps[i].last_block_valid = 0; |
| 3389 | } |
| 3390 | STp->partition = STp->new_partition = 0; |
| 3391 | STp->nbr_partitions = 1; /* Bad guess ?-) */ |
| 3392 | STps->drv_block = STps->drv_file = 0; |
| 3393 | retval = 0; |
| 3394 | goto out; |
| 3395 | } |
| 3396 | |
| 3397 | if (mtc.mt_op == MTSEEK) { |
| 3398 | i = set_location(STp, mtc.mt_count, STp->new_partition, 0); |
| 3399 | if (!STp->can_partitions) |
| 3400 | STp->ps[0].rw = ST_IDLE; |
| 3401 | retval = i; |
| 3402 | goto out; |
| 3403 | } |
| 3404 | |
| 3405 | if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) { |
| 3406 | retval = do_load_unload(STp, file, 0); |
| 3407 | goto out; |
| 3408 | } |
| 3409 | |
| 3410 | if (mtc.mt_op == MTLOAD) { |
| 3411 | retval = do_load_unload(STp, file, max(1, mtc.mt_count)); |
| 3412 | goto out; |
| 3413 | } |
| 3414 | |
| 3415 | if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) { |
| 3416 | retval = do_door_lock(STp, (mtc.mt_op == MTLOCK)); |
| 3417 | goto out; |
| 3418 | } |
| 3419 | |
| 3420 | if (STp->can_partitions && STp->ready == ST_READY && |
| 3421 | (i = switch_partition(STp)) < 0) { |
| 3422 | retval = i; |
| 3423 | goto out; |
| 3424 | } |
| 3425 | |
| 3426 | if (mtc.mt_op == MTCOMPRESSION) |
| 3427 | retval = st_compression(STp, (mtc.mt_count & 1)); |
| 3428 | else |
| 3429 | retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count); |
| 3430 | goto out; |
| 3431 | } |
| 3432 | if (!STm->defined) { |
| 3433 | retval = (-ENXIO); |
| 3434 | goto out; |
| 3435 | } |
| 3436 | |
| 3437 | if ((i = flush_buffer(STp, 0)) < 0) { |
| 3438 | retval = i; |
| 3439 | goto out; |
| 3440 | } |
| 3441 | if (STp->can_partitions && |
| 3442 | (i = switch_partition(STp)) < 0) { |
| 3443 | retval = i; |
| 3444 | goto out; |
| 3445 | } |
| 3446 | |
| 3447 | if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) { |
| 3448 | struct mtget mt_status; |
| 3449 | |
| 3450 | if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) { |
| 3451 | retval = (-EINVAL); |
| 3452 | goto out; |
| 3453 | } |
| 3454 | |
| 3455 | mt_status.mt_type = STp->tape_type; |
| 3456 | mt_status.mt_dsreg = |
| 3457 | ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) | |
| 3458 | ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK); |
| 3459 | mt_status.mt_blkno = STps->drv_block; |
| 3460 | mt_status.mt_fileno = STps->drv_file; |
| 3461 | if (STp->block_size != 0) { |
| 3462 | if (STps->rw == ST_WRITING) |
| 3463 | mt_status.mt_blkno += |
| 3464 | (STp->buffer)->buffer_bytes / STp->block_size; |
| 3465 | else if (STps->rw == ST_READING) |
| 3466 | mt_status.mt_blkno -= |
| 3467 | ((STp->buffer)->buffer_bytes + |
| 3468 | STp->block_size - 1) / STp->block_size; |
| 3469 | } |
| 3470 | |
| 3471 | mt_status.mt_gstat = 0; |
| 3472 | if (STp->drv_write_prot) |
| 3473 | mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff); |
| 3474 | if (mt_status.mt_blkno == 0) { |
| 3475 | if (mt_status.mt_fileno == 0) |
| 3476 | mt_status.mt_gstat |= GMT_BOT(0xffffffff); |
| 3477 | else |
| 3478 | mt_status.mt_gstat |= GMT_EOF(0xffffffff); |
| 3479 | } |
| 3480 | mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT); |
| 3481 | mt_status.mt_resid = STp->partition; |
| 3482 | if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR) |
| 3483 | mt_status.mt_gstat |= GMT_EOT(0xffffffff); |
| 3484 | else if (STps->eof >= ST_EOM_OK) |
| 3485 | mt_status.mt_gstat |= GMT_EOD(0xffffffff); |
| 3486 | if (STp->density == 1) |
| 3487 | mt_status.mt_gstat |= GMT_D_800(0xffffffff); |
| 3488 | else if (STp->density == 2) |
| 3489 | mt_status.mt_gstat |= GMT_D_1600(0xffffffff); |
| 3490 | else if (STp->density == 3) |
| 3491 | mt_status.mt_gstat |= GMT_D_6250(0xffffffff); |
| 3492 | if (STp->ready == ST_READY) |
| 3493 | mt_status.mt_gstat |= GMT_ONLINE(0xffffffff); |
| 3494 | if (STp->ready == ST_NO_TAPE) |
| 3495 | mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff); |
| 3496 | if (STps->at_sm) |
| 3497 | mt_status.mt_gstat |= GMT_SM(0xffffffff); |
| 3498 | if (STm->do_async_writes || |
| 3499 | (STm->do_buffer_writes && STp->block_size != 0) || |
| 3500 | STp->drv_buffer != 0) |
| 3501 | mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff); |
| 3502 | if (STp->cleaning_req) |
| 3503 | mt_status.mt_gstat |= GMT_CLN(0xffffffff); |
| 3504 | |
| 3505 | i = copy_to_user(p, &mt_status, sizeof(struct mtget)); |
| 3506 | if (i) { |
| 3507 | retval = (-EFAULT); |
| 3508 | goto out; |
| 3509 | } |
| 3510 | |
| 3511 | STp->recover_reg = 0; /* Clear after read */ |
| 3512 | retval = 0; |
| 3513 | goto out; |
| 3514 | } /* End of MTIOCGET */ |
| 3515 | if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) { |
| 3516 | struct mtpos mt_pos; |
| 3517 | if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) { |
| 3518 | retval = (-EINVAL); |
| 3519 | goto out; |
| 3520 | } |
| 3521 | if ((i = get_location(STp, &blk, &bt, 0)) < 0) { |
| 3522 | retval = i; |
| 3523 | goto out; |
| 3524 | } |
| 3525 | mt_pos.mt_blkno = blk; |
| 3526 | i = copy_to_user(p, &mt_pos, sizeof(struct mtpos)); |
| 3527 | if (i) |
| 3528 | retval = (-EFAULT); |
| 3529 | goto out; |
| 3530 | } |
| 3531 | up(&STp->lock); |
| 3532 | switch (cmd_in) { |
| 3533 | case SCSI_IOCTL_GET_IDLUN: |
| 3534 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 3535 | break; |
| 3536 | default: |
Kai Makisara | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 3537 | if ((cmd_in == SG_IO || |
| 3538 | cmd_in == SCSI_IOCTL_SEND_COMMAND || |
| 3539 | cmd_in == CDROM_SEND_PACKET) && |
| 3540 | !capable(CAP_SYS_RAWIO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | i = -EPERM; |
| 3542 | else |
| 3543 | i = scsi_cmd_ioctl(file, STp->disk, cmd_in, p); |
| 3544 | if (i != -ENOTTY) |
| 3545 | return i; |
| 3546 | break; |
| 3547 | } |
Kai Makisara | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 3548 | retval = scsi_ioctl(STp->device, cmd_in, p); |
| 3549 | if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */ |
| 3550 | STp->rew_at_close = 0; |
| 3551 | STp->ready = ST_NO_TAPE; |
| 3552 | } |
| 3553 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | |
| 3555 | out: |
| 3556 | up(&STp->lock); |
| 3557 | return retval; |
| 3558 | } |
| 3559 | |
| 3560 | #ifdef CONFIG_COMPAT |
| 3561 | static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3562 | { |
| 3563 | struct scsi_tape *STp = file->private_data; |
| 3564 | struct scsi_device *sdev = STp->device; |
| 3565 | int ret = -ENOIOCTLCMD; |
| 3566 | if (sdev->host->hostt->compat_ioctl) { |
| 3567 | |
| 3568 | ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); |
| 3569 | |
| 3570 | } |
| 3571 | return ret; |
| 3572 | } |
| 3573 | #endif |
| 3574 | |
| 3575 | |
| 3576 | |
| 3577 | /* Try to allocate a new tape buffer. Calling function must not hold |
| 3578 | dev_arr_lock. */ |
| 3579 | static struct st_buffer * |
| 3580 | new_tape_buffer(int from_initialization, int need_dma, int max_sg) |
| 3581 | { |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 3582 | int i, got = 0; |
Al Viro | c53033f | 2005-10-21 03:22:08 -0400 | [diff] [blame] | 3583 | gfp_t priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | struct st_buffer *tb; |
| 3585 | |
| 3586 | if (from_initialization) |
| 3587 | priority = GFP_ATOMIC; |
| 3588 | else |
| 3589 | priority = GFP_KERNEL; |
| 3590 | |
| 3591 | i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) + |
| 3592 | max_sg * sizeof(struct st_buf_fragment); |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 3593 | tb = kzalloc(i, priority); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | if (!tb) { |
| 3595 | printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n"); |
| 3596 | return NULL; |
| 3597 | } |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 3598 | tb->frp_segs = tb->orig_frp_segs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | tb->use_sg = max_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg); |
| 3601 | |
| 3602 | tb->in_use = 1; |
| 3603 | tb->dma = need_dma; |
| 3604 | tb->buffer_size = got; |
| 3605 | |
| 3606 | return tb; |
| 3607 | } |
| 3608 | |
| 3609 | |
| 3610 | /* Try to allocate enough space in the tape buffer */ |
| 3611 | static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma) |
| 3612 | { |
Al Viro | c53033f | 2005-10-21 03:22:08 -0400 | [diff] [blame] | 3613 | int segs, nbr, max_segs, b_size, order, got; |
| 3614 | gfp_t priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | |
| 3616 | if (new_size <= STbuffer->buffer_size) |
| 3617 | return 1; |
| 3618 | |
| 3619 | if (STbuffer->buffer_size <= PAGE_SIZE) |
| 3620 | normalize_buffer(STbuffer); /* Avoid extra segment */ |
| 3621 | |
| 3622 | max_segs = STbuffer->use_sg; |
| 3623 | nbr = max_segs - STbuffer->frp_segs; |
| 3624 | if (nbr <= 0) |
| 3625 | return 0; |
| 3626 | |
| 3627 | priority = GFP_KERNEL | __GFP_NOWARN; |
| 3628 | if (need_dma) |
| 3629 | priority |= GFP_DMA; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 3630 | for (b_size = PAGE_SIZE, order=0; order <= 6 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | b_size < new_size - STbuffer->buffer_size; |
| 3632 | order++, b_size *= 2) |
| 3633 | ; /* empty */ |
| 3634 | |
| 3635 | for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size; |
| 3636 | segs < max_segs && got < new_size;) { |
| 3637 | STbuffer->frp[segs].page = alloc_pages(priority, order); |
| 3638 | if (STbuffer->frp[segs].page == NULL) { |
| 3639 | if (new_size - got <= (max_segs - segs) * b_size / 2) { |
| 3640 | b_size /= 2; /* Large enough for the rest of the buffers */ |
| 3641 | order--; |
| 3642 | continue; |
| 3643 | } |
| 3644 | DEB(STbuffer->buffer_size = got); |
| 3645 | normalize_buffer(STbuffer); |
| 3646 | return 0; |
| 3647 | } |
| 3648 | STbuffer->frp[segs].length = b_size; |
| 3649 | STbuffer->frp_segs += 1; |
| 3650 | got += b_size; |
| 3651 | STbuffer->buffer_size = got; |
| 3652 | segs++; |
| 3653 | } |
| 3654 | STbuffer->b_data = page_address(STbuffer->frp[0].page); |
| 3655 | |
| 3656 | return 1; |
| 3657 | } |
| 3658 | |
| 3659 | |
| 3660 | /* Release the extra buffer */ |
| 3661 | static void normalize_buffer(struct st_buffer * STbuffer) |
| 3662 | { |
| 3663 | int i, order; |
| 3664 | |
| 3665 | for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) { |
| 3666 | order = get_order(STbuffer->frp[i].length); |
| 3667 | __free_pages(STbuffer->frp[i].page, order); |
| 3668 | STbuffer->buffer_size -= STbuffer->frp[i].length; |
| 3669 | } |
| 3670 | STbuffer->frp_segs = STbuffer->orig_frp_segs; |
| 3671 | STbuffer->frp_sg_current = 0; |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 3672 | STbuffer->sg_segs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | |
| 3676 | /* Move data from the user buffer to the tape buffer. Returns zero (success) or |
| 3677 | negative error code. */ |
| 3678 | static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count) |
| 3679 | { |
| 3680 | int i, cnt, res, offset; |
| 3681 | |
| 3682 | for (i = 0, offset = st_bp->buffer_bytes; |
| 3683 | i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++) |
| 3684 | offset -= st_bp->frp[i].length; |
| 3685 | if (i == st_bp->frp_segs) { /* Should never happen */ |
| 3686 | printk(KERN_WARNING "st: append_to_buffer offset overflow.\n"); |
| 3687 | return (-EIO); |
| 3688 | } |
| 3689 | for (; i < st_bp->frp_segs && do_count > 0; i++) { |
| 3690 | cnt = st_bp->frp[i].length - offset < do_count ? |
| 3691 | st_bp->frp[i].length - offset : do_count; |
| 3692 | res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt); |
| 3693 | if (res) |
| 3694 | return (-EFAULT); |
| 3695 | do_count -= cnt; |
| 3696 | st_bp->buffer_bytes += cnt; |
| 3697 | ubp += cnt; |
| 3698 | offset = 0; |
| 3699 | } |
| 3700 | if (do_count) /* Should never happen */ |
| 3701 | return (-EIO); |
| 3702 | |
| 3703 | return 0; |
| 3704 | } |
| 3705 | |
| 3706 | |
| 3707 | /* Move data from the tape buffer to the user buffer. Returns zero (success) or |
| 3708 | negative error code. */ |
| 3709 | static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count) |
| 3710 | { |
| 3711 | int i, cnt, res, offset; |
| 3712 | |
| 3713 | for (i = 0, offset = st_bp->read_pointer; |
| 3714 | i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++) |
| 3715 | offset -= st_bp->frp[i].length; |
| 3716 | if (i == st_bp->frp_segs) { /* Should never happen */ |
| 3717 | printk(KERN_WARNING "st: from_buffer offset overflow.\n"); |
| 3718 | return (-EIO); |
| 3719 | } |
| 3720 | for (; i < st_bp->frp_segs && do_count > 0; i++) { |
| 3721 | cnt = st_bp->frp[i].length - offset < do_count ? |
| 3722 | st_bp->frp[i].length - offset : do_count; |
| 3723 | res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt); |
| 3724 | if (res) |
| 3725 | return (-EFAULT); |
| 3726 | do_count -= cnt; |
| 3727 | st_bp->buffer_bytes -= cnt; |
| 3728 | st_bp->read_pointer += cnt; |
| 3729 | ubp += cnt; |
| 3730 | offset = 0; |
| 3731 | } |
| 3732 | if (do_count) /* Should never happen */ |
| 3733 | return (-EIO); |
| 3734 | |
| 3735 | return 0; |
| 3736 | } |
| 3737 | |
| 3738 | |
| 3739 | /* Move data towards start of buffer */ |
| 3740 | static void move_buffer_data(struct st_buffer * st_bp, int offset) |
| 3741 | { |
| 3742 | int src_seg, dst_seg, src_offset = 0, dst_offset; |
| 3743 | int count, total; |
| 3744 | |
| 3745 | if (offset == 0) |
| 3746 | return; |
| 3747 | |
| 3748 | total=st_bp->buffer_bytes - offset; |
| 3749 | for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) { |
| 3750 | src_offset = offset; |
| 3751 | if (src_offset < st_bp->frp[src_seg].length) |
| 3752 | break; |
| 3753 | offset -= st_bp->frp[src_seg].length; |
| 3754 | } |
| 3755 | |
| 3756 | st_bp->buffer_bytes = st_bp->read_pointer = total; |
| 3757 | for (dst_seg=dst_offset=0; total > 0; ) { |
| 3758 | count = min(st_bp->frp[dst_seg].length - dst_offset, |
| 3759 | st_bp->frp[src_seg].length - src_offset); |
| 3760 | memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset, |
| 3761 | page_address(st_bp->frp[src_seg].page) + src_offset, count); |
| 3762 | src_offset += count; |
| 3763 | if (src_offset >= st_bp->frp[src_seg].length) { |
| 3764 | src_seg++; |
| 3765 | src_offset = 0; |
| 3766 | } |
| 3767 | dst_offset += count; |
| 3768 | if (dst_offset >= st_bp->frp[dst_seg].length) { |
| 3769 | dst_seg++; |
| 3770 | dst_offset = 0; |
| 3771 | } |
| 3772 | total -= count; |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | /* Fill the s/g list up to the length required for this transfer */ |
| 3778 | static void buf_to_sg(struct st_buffer *STbp, unsigned int length) |
| 3779 | { |
| 3780 | int i; |
| 3781 | unsigned int count; |
| 3782 | struct scatterlist *sg; |
| 3783 | struct st_buf_fragment *frp; |
| 3784 | |
| 3785 | if (length == STbp->frp_sg_current) |
| 3786 | return; /* work already done */ |
| 3787 | |
| 3788 | sg = &(STbp->sg[0]); |
| 3789 | frp = STbp->frp; |
| 3790 | for (i=count=0; count < length; i++) { |
| 3791 | sg[i].page = frp[i].page; |
| 3792 | if (length - count > frp[i].length) |
| 3793 | sg[i].length = frp[i].length; |
| 3794 | else |
| 3795 | sg[i].length = length - count; |
| 3796 | count += sg[i].length; |
| 3797 | sg[i].offset = 0; |
| 3798 | } |
| 3799 | STbp->sg_segs = i; |
| 3800 | STbp->frp_sg_current = length; |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | /* Validate the options from command line or module parameters */ |
| 3805 | static void validate_options(void) |
| 3806 | { |
| 3807 | if (buffer_kbs > 0) |
| 3808 | st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE; |
| 3809 | if (max_sg_segs >= ST_FIRST_SG) |
| 3810 | st_max_sg_segs = max_sg_segs; |
| 3811 | } |
| 3812 | |
| 3813 | #ifndef MODULE |
| 3814 | /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt. |
| 3815 | */ |
| 3816 | static int __init st_setup(char *str) |
| 3817 | { |
| 3818 | int i, len, ints[5]; |
| 3819 | char *stp; |
| 3820 | |
| 3821 | stp = get_options(str, ARRAY_SIZE(ints), ints); |
| 3822 | |
| 3823 | if (ints[0] > 0) { |
| 3824 | for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++) |
| 3825 | if (parms[i].val) |
| 3826 | *parms[i].val = ints[i + 1]; |
| 3827 | } else { |
| 3828 | while (stp != NULL) { |
| 3829 | for (i = 0; i < ARRAY_SIZE(parms); i++) { |
| 3830 | len = strlen(parms[i].name); |
| 3831 | if (!strncmp(stp, parms[i].name, len) && |
| 3832 | (*(stp + len) == ':' || *(stp + len) == '=')) { |
| 3833 | if (parms[i].val) |
| 3834 | *parms[i].val = |
| 3835 | simple_strtoul(stp + len + 1, NULL, 0); |
| 3836 | else |
| 3837 | printk(KERN_WARNING "st: Obsolete parameter %s\n", |
| 3838 | parms[i].name); |
| 3839 | break; |
| 3840 | } |
| 3841 | } |
| 3842 | if (i >= sizeof(parms) / sizeof(struct st_dev_parm)) |
| 3843 | printk(KERN_WARNING "st: invalid parameter in '%s'\n", |
| 3844 | stp); |
| 3845 | stp = strchr(stp, ','); |
| 3846 | if (stp) |
| 3847 | stp++; |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | validate_options(); |
| 3852 | |
| 3853 | return 1; |
| 3854 | } |
| 3855 | |
| 3856 | __setup("st=", st_setup); |
| 3857 | |
| 3858 | #endif |
| 3859 | |
| 3860 | static struct file_operations st_fops = |
| 3861 | { |
| 3862 | .owner = THIS_MODULE, |
| 3863 | .read = st_read, |
| 3864 | .write = st_write, |
| 3865 | .ioctl = st_ioctl, |
| 3866 | #ifdef CONFIG_COMPAT |
| 3867 | .compat_ioctl = st_compat_ioctl, |
| 3868 | #endif |
| 3869 | .open = st_open, |
| 3870 | .flush = st_flush, |
| 3871 | .release = st_release, |
| 3872 | }; |
| 3873 | |
| 3874 | static int st_probe(struct device *dev) |
| 3875 | { |
| 3876 | struct scsi_device *SDp = to_scsi_device(dev); |
| 3877 | struct gendisk *disk = NULL; |
| 3878 | struct cdev *cdev = NULL; |
| 3879 | struct scsi_tape *tpnt = NULL; |
| 3880 | struct st_modedef *STm; |
| 3881 | struct st_partstat *STps; |
| 3882 | struct st_buffer *buffer; |
| 3883 | int i, j, mode, dev_num, error; |
| 3884 | char *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | |
| 3886 | if (SDp->type != TYPE_TAPE) |
| 3887 | return -ENODEV; |
| 3888 | if ((stp = st_incompatible(SDp))) { |
Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 3889 | sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | printk(KERN_INFO "st: The suggested driver is %s.\n", stp); |
| 3891 | return -ENODEV; |
| 3892 | } |
| 3893 | |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 3894 | i = min(SDp->request_queue->max_hw_segments, |
| 3895 | SDp->request_queue->max_phys_segments); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | if (st_max_sg_segs < i) |
| 3897 | i = st_max_sg_segs; |
| 3898 | buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i); |
| 3899 | if (buffer == NULL) { |
| 3900 | printk(KERN_ERR |
| 3901 | "st: Can't allocate new tape buffer. Device not attached.\n"); |
| 3902 | goto out; |
| 3903 | } |
| 3904 | |
| 3905 | disk = alloc_disk(1); |
| 3906 | if (!disk) { |
| 3907 | printk(KERN_ERR "st: out of memory. Device not attached.\n"); |
| 3908 | goto out_buffer_free; |
| 3909 | } |
| 3910 | |
| 3911 | write_lock(&st_dev_arr_lock); |
| 3912 | if (st_nr_dev >= st_dev_max) { |
| 3913 | struct scsi_tape **tmp_da; |
| 3914 | int tmp_dev_max; |
| 3915 | |
| 3916 | tmp_dev_max = max(st_nr_dev * 2, 8); |
| 3917 | if (tmp_dev_max > ST_MAX_TAPES) |
| 3918 | tmp_dev_max = ST_MAX_TAPES; |
| 3919 | if (tmp_dev_max <= st_nr_dev) { |
| 3920 | write_unlock(&st_dev_arr_lock); |
| 3921 | printk(KERN_ERR "st: Too many tape devices (max. %d).\n", |
| 3922 | ST_MAX_TAPES); |
| 3923 | goto out_put_disk; |
| 3924 | } |
| 3925 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 3926 | tmp_da = kzalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | if (tmp_da == NULL) { |
| 3928 | write_unlock(&st_dev_arr_lock); |
| 3929 | printk(KERN_ERR "st: Can't extend device array.\n"); |
| 3930 | goto out_put_disk; |
| 3931 | } |
| 3932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | if (scsi_tapes != NULL) { |
| 3934 | memcpy(tmp_da, scsi_tapes, |
| 3935 | st_dev_max * sizeof(struct scsi_tape *)); |
| 3936 | kfree(scsi_tapes); |
| 3937 | } |
| 3938 | scsi_tapes = tmp_da; |
| 3939 | |
| 3940 | st_dev_max = tmp_dev_max; |
| 3941 | } |
| 3942 | |
| 3943 | for (i = 0; i < st_dev_max; i++) |
| 3944 | if (scsi_tapes[i] == NULL) |
| 3945 | break; |
| 3946 | if (i >= st_dev_max) |
| 3947 | panic("scsi_devices corrupt (st)"); |
| 3948 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 3949 | tpnt = kzalloc(sizeof(struct scsi_tape), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | if (tpnt == NULL) { |
| 3951 | write_unlock(&st_dev_arr_lock); |
| 3952 | printk(KERN_ERR "st: Can't allocate device descriptor.\n"); |
| 3953 | goto out_put_disk; |
| 3954 | } |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 3955 | kref_init(&tpnt->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | tpnt->disk = disk; |
| 3957 | sprintf(disk->disk_name, "st%d", i); |
| 3958 | disk->private_data = &tpnt->driver; |
| 3959 | disk->queue = SDp->request_queue; |
| 3960 | tpnt->driver = &st_template; |
| 3961 | scsi_tapes[i] = tpnt; |
| 3962 | dev_num = i; |
| 3963 | |
| 3964 | tpnt->device = SDp; |
| 3965 | if (SDp->scsi_level <= 2) |
| 3966 | tpnt->tape_type = MT_ISSCSI1; |
| 3967 | else |
| 3968 | tpnt->tape_type = MT_ISSCSI2; |
| 3969 | |
| 3970 | tpnt->buffer = buffer; |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 3971 | tpnt->buffer->last_SRpnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | |
| 3973 | tpnt->inited = 0; |
| 3974 | tpnt->dirty = 0; |
| 3975 | tpnt->in_use = 0; |
| 3976 | tpnt->drv_buffer = 1; /* Try buffering if no mode sense */ |
| 3977 | tpnt->restr_dma = (SDp->host)->unchecked_isa_dma; |
| 3978 | tpnt->use_pf = (SDp->scsi_level >= SCSI_2); |
| 3979 | tpnt->density = 0; |
| 3980 | tpnt->do_auto_lock = ST_AUTO_LOCK; |
| 3981 | tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */ |
| 3982 | tpnt->can_partitions = 0; |
| 3983 | tpnt->two_fm = ST_TWO_FM; |
| 3984 | tpnt->fast_mteom = ST_FAST_MTEOM; |
| 3985 | tpnt->scsi2_logical = ST_SCSI2LOGICAL; |
| 3986 | tpnt->immediate = ST_NOWAIT; |
| 3987 | tpnt->default_drvbuffer = 0xff; /* No forced buffering */ |
| 3988 | tpnt->partition = 0; |
| 3989 | tpnt->new_partition = 0; |
| 3990 | tpnt->nbr_partitions = 0; |
| 3991 | tpnt->device->timeout = ST_TIMEOUT; |
| 3992 | tpnt->long_timeout = ST_LONG_TIMEOUT; |
| 3993 | tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma; |
| 3994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | for (i = 0; i < ST_NBR_MODES; i++) { |
| 3996 | STm = &(tpnt->modes[i]); |
| 3997 | STm->defined = 0; |
| 3998 | STm->sysv = ST_SYSV; |
| 3999 | STm->defaults_for_writes = 0; |
| 4000 | STm->do_async_writes = ST_ASYNC_WRITES; |
| 4001 | STm->do_buffer_writes = ST_BUFFER_WRITES; |
| 4002 | STm->do_read_ahead = ST_READ_AHEAD; |
| 4003 | STm->default_compression = ST_DONT_TOUCH; |
| 4004 | STm->default_blksize = (-1); /* No forced size */ |
| 4005 | STm->default_density = (-1); /* No forced density */ |
| 4006 | } |
| 4007 | |
| 4008 | for (i = 0; i < ST_NBR_PARTITIONS; i++) { |
| 4009 | STps = &(tpnt->ps[i]); |
| 4010 | STps->rw = ST_IDLE; |
| 4011 | STps->eof = ST_NOEOF; |
| 4012 | STps->at_sm = 0; |
| 4013 | STps->last_block_valid = 0; |
| 4014 | STps->drv_block = (-1); |
| 4015 | STps->drv_file = (-1); |
| 4016 | } |
| 4017 | |
| 4018 | tpnt->current_mode = 0; |
| 4019 | tpnt->modes[0].defined = 1; |
| 4020 | |
| 4021 | tpnt->density_changed = tpnt->compression_changed = |
| 4022 | tpnt->blksize_changed = 0; |
| 4023 | init_MUTEX(&tpnt->lock); |
| 4024 | |
| 4025 | st_nr_dev++; |
| 4026 | write_unlock(&st_dev_arr_lock); |
| 4027 | |
| 4028 | for (mode = 0; mode < ST_NBR_MODES; ++mode) { |
| 4029 | STm = &(tpnt->modes[mode]); |
| 4030 | for (j=0; j < 2; j++) { |
| 4031 | cdev = cdev_alloc(); |
| 4032 | if (!cdev) { |
| 4033 | printk(KERN_ERR |
| 4034 | "st%d: out of memory. Device not attached.\n", |
| 4035 | dev_num); |
| 4036 | goto out_free_tape; |
| 4037 | } |
| 4038 | cdev->owner = THIS_MODULE; |
| 4039 | cdev->ops = &st_fops; |
| 4040 | |
| 4041 | error = cdev_add(cdev, |
| 4042 | MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)), |
| 4043 | 1); |
| 4044 | if (error) { |
| 4045 | printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n", |
| 4046 | dev_num, j ? "non" : "auto", mode); |
| 4047 | printk(KERN_ERR "st%d: Device not attached.\n", dev_num); |
| 4048 | goto out_free_tape; |
| 4049 | } |
| 4050 | STm->cdevs[j] = cdev; |
| 4051 | |
| 4052 | } |
| 4053 | do_create_class_files(tpnt, dev_num, mode); |
| 4054 | } |
| 4055 | |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 4056 | sdev_printk(KERN_WARNING, SDp, |
| 4057 | "Attached scsi tape %s", tape_name(tpnt)); |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 4058 | printk(KERN_WARNING "%s: try direct i/o: %s (alignment %d B)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | tape_name(tpnt), tpnt->try_dio ? "yes" : "no", |
Mike Christie | 8b05b77 | 2005-11-08 04:06:44 -0600 | [diff] [blame] | 4060 | queue_dma_alignment(SDp->request_queue) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | |
| 4062 | return 0; |
| 4063 | |
| 4064 | out_free_tape: |
| 4065 | for (mode=0; mode < ST_NBR_MODES; mode++) { |
| 4066 | STm = &(tpnt->modes[mode]); |
| 4067 | sysfs_remove_link(&tpnt->device->sdev_gendev.kobj, |
| 4068 | "tape"); |
| 4069 | for (j=0; j < 2; j++) { |
| 4070 | if (STm->cdevs[j]) { |
| 4071 | if (cdev == STm->cdevs[j]) |
| 4072 | cdev = NULL; |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4073 | class_device_destroy(st_sysfs_class, |
| 4074 | MKDEV(SCSI_TAPE_MAJOR, |
| 4075 | TAPE_MINOR(i, mode, j))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | cdev_del(STm->cdevs[j]); |
| 4077 | } |
| 4078 | } |
| 4079 | } |
| 4080 | if (cdev) |
| 4081 | cdev_del(cdev); |
| 4082 | write_lock(&st_dev_arr_lock); |
| 4083 | scsi_tapes[dev_num] = NULL; |
| 4084 | st_nr_dev--; |
| 4085 | write_unlock(&st_dev_arr_lock); |
| 4086 | out_put_disk: |
| 4087 | put_disk(disk); |
Jesper Juhl | c9475cb | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 4088 | kfree(tpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | out_buffer_free: |
| 4090 | kfree(buffer); |
| 4091 | out: |
| 4092 | return -ENODEV; |
| 4093 | }; |
| 4094 | |
| 4095 | |
| 4096 | static int st_remove(struct device *dev) |
| 4097 | { |
| 4098 | struct scsi_device *SDp = to_scsi_device(dev); |
| 4099 | struct scsi_tape *tpnt; |
| 4100 | int i, j, mode; |
| 4101 | |
| 4102 | write_lock(&st_dev_arr_lock); |
| 4103 | for (i = 0; i < st_dev_max; i++) { |
| 4104 | tpnt = scsi_tapes[i]; |
| 4105 | if (tpnt != NULL && tpnt->device == SDp) { |
| 4106 | scsi_tapes[i] = NULL; |
| 4107 | st_nr_dev--; |
| 4108 | write_unlock(&st_dev_arr_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | sysfs_remove_link(&tpnt->device->sdev_gendev.kobj, |
| 4110 | "tape"); |
| 4111 | for (mode = 0; mode < ST_NBR_MODES; ++mode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | for (j=0; j < 2; j++) { |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4113 | class_device_destroy(st_sysfs_class, |
| 4114 | MKDEV(SCSI_TAPE_MAJOR, |
| 4115 | TAPE_MINOR(i, mode, j))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | cdev_del(tpnt->modes[mode].cdevs[j]); |
| 4117 | tpnt->modes[mode].cdevs[j] = NULL; |
| 4118 | } |
| 4119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 4121 | mutex_lock(&st_ref_mutex); |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 4122 | kref_put(&tpnt->kref, scsi_tape_release); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 4123 | mutex_unlock(&st_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | return 0; |
| 4125 | } |
| 4126 | } |
| 4127 | |
| 4128 | write_unlock(&st_dev_arr_lock); |
| 4129 | return 0; |
| 4130 | } |
| 4131 | |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 4132 | /** |
| 4133 | * scsi_tape_release - Called to free the Scsi_Tape structure |
| 4134 | * @kref: pointer to embedded kref |
| 4135 | * |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 4136 | * st_ref_mutex must be held entering this routine. Because it is |
Kai Makisara | f03a567 | 2005-08-02 13:40:47 +0300 | [diff] [blame] | 4137 | * called on last put, you should always use the scsi_tape_get() |
| 4138 | * scsi_tape_put() helpers which manipulate the semaphore directly |
| 4139 | * and never do a direct kref_put(). |
| 4140 | **/ |
| 4141 | static void scsi_tape_release(struct kref *kref) |
| 4142 | { |
| 4143 | struct scsi_tape *tpnt = to_scsi_tape(kref); |
| 4144 | struct gendisk *disk = tpnt->disk; |
| 4145 | |
| 4146 | tpnt->device = NULL; |
| 4147 | |
| 4148 | if (tpnt->buffer) { |
| 4149 | tpnt->buffer->orig_frp_segs = 0; |
| 4150 | normalize_buffer(tpnt->buffer); |
| 4151 | kfree(tpnt->buffer); |
| 4152 | } |
| 4153 | |
| 4154 | disk->private_data = NULL; |
| 4155 | put_disk(disk); |
| 4156 | kfree(tpnt); |
| 4157 | return; |
| 4158 | } |
| 4159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | static int __init init_st(void) |
| 4161 | { |
| 4162 | validate_options(); |
| 4163 | |
| 4164 | printk(KERN_INFO |
| 4165 | "st: Version %s, fixed bufsize %d, s/g segs %d\n", |
| 4166 | verstr, st_fixed_buffer_size, st_max_sg_segs); |
| 4167 | |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4168 | st_sysfs_class = class_create(THIS_MODULE, "scsi_tape"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | if (IS_ERR(st_sysfs_class)) { |
| 4170 | st_sysfs_class = NULL; |
| 4171 | printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n"); |
| 4172 | return 1; |
| 4173 | } |
| 4174 | |
| 4175 | if (!register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0), |
| 4176 | ST_MAX_TAPE_ENTRIES, "st")) { |
| 4177 | if (scsi_register_driver(&st_template.gendrv) == 0) { |
| 4178 | do_create_driverfs_files(); |
| 4179 | return 0; |
| 4180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | ST_MAX_TAPE_ENTRIES); |
| 4183 | } |
Kai Makisara | c2c96f4 | 2005-08-02 12:21:51 +0300 | [diff] [blame] | 4184 | class_destroy(st_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | |
| 4186 | printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", SCSI_TAPE_MAJOR); |
| 4187 | return 1; |
| 4188 | } |
| 4189 | |
| 4190 | static void __exit exit_st(void) |
| 4191 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | do_remove_driverfs_files(); |
| 4193 | scsi_unregister_driver(&st_template.gendrv); |
| 4194 | unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0), |
| 4195 | ST_MAX_TAPE_ENTRIES); |
Kai Makisara | c2c96f4 | 2005-08-02 12:21:51 +0300 | [diff] [blame] | 4196 | class_destroy(st_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | kfree(scsi_tapes); |
| 4198 | printk(KERN_INFO "st: Unloaded.\n"); |
| 4199 | } |
| 4200 | |
| 4201 | module_init(init_st); |
| 4202 | module_exit(exit_st); |
| 4203 | |
| 4204 | |
| 4205 | /* The sysfs driver interface. Read-only at the moment */ |
| 4206 | static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf) |
| 4207 | { |
| 4208 | return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io); |
| 4209 | } |
| 4210 | static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL); |
| 4211 | |
| 4212 | static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf) |
| 4213 | { |
| 4214 | return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size); |
| 4215 | } |
| 4216 | static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL); |
| 4217 | |
| 4218 | static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf) |
| 4219 | { |
| 4220 | return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs); |
| 4221 | } |
| 4222 | static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL); |
| 4223 | |
| 4224 | static ssize_t st_version_show(struct device_driver *ddd, char *buf) |
| 4225 | { |
| 4226 | return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr); |
| 4227 | } |
| 4228 | static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL); |
| 4229 | |
| 4230 | static void do_create_driverfs_files(void) |
| 4231 | { |
| 4232 | struct device_driver *driverfs = &st_template.gendrv; |
| 4233 | |
| 4234 | driver_create_file(driverfs, &driver_attr_try_direct_io); |
| 4235 | driver_create_file(driverfs, &driver_attr_fixed_buffer_size); |
| 4236 | driver_create_file(driverfs, &driver_attr_max_sg_segs); |
| 4237 | driver_create_file(driverfs, &driver_attr_version); |
| 4238 | } |
| 4239 | |
| 4240 | static void do_remove_driverfs_files(void) |
| 4241 | { |
| 4242 | struct device_driver *driverfs = &st_template.gendrv; |
| 4243 | |
| 4244 | driver_remove_file(driverfs, &driver_attr_version); |
| 4245 | driver_remove_file(driverfs, &driver_attr_max_sg_segs); |
| 4246 | driver_remove_file(driverfs, &driver_attr_fixed_buffer_size); |
| 4247 | driver_remove_file(driverfs, &driver_attr_try_direct_io); |
| 4248 | } |
| 4249 | |
| 4250 | |
| 4251 | /* The sysfs simple class interface */ |
| 4252 | static ssize_t st_defined_show(struct class_device *class_dev, char *buf) |
| 4253 | { |
| 4254 | struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); |
| 4255 | ssize_t l = 0; |
| 4256 | |
| 4257 | l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined); |
| 4258 | return l; |
| 4259 | } |
| 4260 | |
| 4261 | CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL); |
| 4262 | |
| 4263 | static ssize_t st_defblk_show(struct class_device *class_dev, char *buf) |
| 4264 | { |
| 4265 | struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); |
| 4266 | ssize_t l = 0; |
| 4267 | |
| 4268 | l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize); |
| 4269 | return l; |
| 4270 | } |
| 4271 | |
| 4272 | CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL); |
| 4273 | |
| 4274 | static ssize_t st_defdensity_show(struct class_device *class_dev, char *buf) |
| 4275 | { |
| 4276 | struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); |
| 4277 | ssize_t l = 0; |
| 4278 | char *fmt; |
| 4279 | |
| 4280 | fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n"; |
| 4281 | l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density); |
| 4282 | return l; |
| 4283 | } |
| 4284 | |
| 4285 | CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL); |
| 4286 | |
| 4287 | static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf) |
| 4288 | { |
| 4289 | struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); |
| 4290 | ssize_t l = 0; |
| 4291 | |
| 4292 | l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1); |
| 4293 | return l; |
| 4294 | } |
| 4295 | |
| 4296 | CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL); |
| 4297 | |
| 4298 | static void do_create_class_files(struct scsi_tape *STp, int dev_num, int mode) |
| 4299 | { |
| 4300 | int i, rew, error; |
| 4301 | char name[10]; |
| 4302 | struct class_device *st_class_member; |
| 4303 | |
| 4304 | if (!st_sysfs_class) |
| 4305 | return; |
| 4306 | |
| 4307 | for (rew=0; rew < 2; rew++) { |
| 4308 | /* Make sure that the minor numbers corresponding to the four |
| 4309 | first modes always get the same names */ |
| 4310 | i = mode << (4 - ST_NBR_MODE_BITS); |
| 4311 | snprintf(name, 10, "%s%s%s", rew ? "n" : "", |
| 4312 | STp->disk->disk_name, st_formats[i]); |
| 4313 | st_class_member = |
Greg Kroah-Hartman | 53f4654 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 4314 | class_device_create(st_sysfs_class, NULL, |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4315 | MKDEV(SCSI_TAPE_MAJOR, |
| 4316 | TAPE_MINOR(dev_num, mode, rew)), |
| 4317 | &STp->device->sdev_gendev, "%s", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | if (IS_ERR(st_class_member)) { |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4319 | printk(KERN_WARNING "st%d: class_device_create failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | dev_num); |
| 4321 | goto out; |
| 4322 | } |
| 4323 | class_set_devdata(st_class_member, &STp->modes[mode]); |
| 4324 | |
| 4325 | class_device_create_file(st_class_member, |
| 4326 | &class_device_attr_defined); |
| 4327 | class_device_create_file(st_class_member, |
| 4328 | &class_device_attr_default_blksize); |
| 4329 | class_device_create_file(st_class_member, |
| 4330 | &class_device_attr_default_density); |
| 4331 | class_device_create_file(st_class_member, |
| 4332 | &class_device_attr_default_compression); |
| 4333 | if (mode == 0 && rew == 0) { |
| 4334 | error = sysfs_create_link(&STp->device->sdev_gendev.kobj, |
| 4335 | &st_class_member->kobj, |
| 4336 | "tape"); |
| 4337 | if (error) { |
| 4338 | printk(KERN_ERR |
| 4339 | "st%d: Can't create sysfs link from SCSI device.\n", |
| 4340 | dev_num); |
| 4341 | } |
| 4342 | } |
| 4343 | } |
| 4344 | out: |
| 4345 | return; |
| 4346 | } |
| 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | /* The following functions may be useful for a larger audience. */ |
| 4349 | static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages, |
| 4350 | unsigned long uaddr, size_t count, int rw) |
| 4351 | { |
James Bottomley | 07542b8 | 2005-08-31 20:27:22 -0400 | [diff] [blame] | 4352 | unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 4353 | unsigned long start = uaddr >> PAGE_SHIFT; |
| 4354 | const int nr_pages = end - start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | int res, i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | struct page **pages; |
| 4357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | /* User attempted Overflow! */ |
| 4359 | if ((uaddr + count) < uaddr) |
| 4360 | return -EINVAL; |
| 4361 | |
| 4362 | /* Too big */ |
| 4363 | if (nr_pages > max_pages) |
| 4364 | return -ENOMEM; |
| 4365 | |
| 4366 | /* Hmm? */ |
| 4367 | if (count == 0) |
| 4368 | return 0; |
| 4369 | |
| 4370 | if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL) |
| 4371 | return -ENOMEM; |
| 4372 | |
| 4373 | /* Try to fault in all of the necessary pages */ |
| 4374 | down_read(¤t->mm->mmap_sem); |
| 4375 | /* rw==READ means read from drive, write into memory area */ |
| 4376 | res = get_user_pages( |
| 4377 | current, |
| 4378 | current->mm, |
| 4379 | uaddr, |
| 4380 | nr_pages, |
| 4381 | rw == READ, |
| 4382 | 0, /* don't force */ |
| 4383 | pages, |
| 4384 | NULL); |
| 4385 | up_read(¤t->mm->mmap_sem); |
| 4386 | |
| 4387 | /* Errors and no page mapped should return here */ |
| 4388 | if (res < nr_pages) |
| 4389 | goto out_unmap; |
| 4390 | |
| 4391 | for (i=0; i < nr_pages; i++) { |
| 4392 | /* FIXME: flush superflous for rw==READ, |
| 4393 | * probably wrong function for rw==WRITE |
| 4394 | */ |
| 4395 | flush_dcache_page(pages[i]); |
| 4396 | } |
| 4397 | |
| 4398 | /* Populate the scatter/gather list */ |
| 4399 | sgl[0].page = pages[0]; |
| 4400 | sgl[0].offset = uaddr & ~PAGE_MASK; |
| 4401 | if (nr_pages > 1) { |
| 4402 | sgl[0].length = PAGE_SIZE - sgl[0].offset; |
| 4403 | count -= sgl[0].length; |
| 4404 | for (i=1; i < nr_pages ; i++) { |
| 4405 | sgl[i].offset = 0; |
| 4406 | sgl[i].page = pages[i]; |
| 4407 | sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE; |
| 4408 | count -= PAGE_SIZE; |
| 4409 | } |
| 4410 | } |
| 4411 | else { |
| 4412 | sgl[0].length = count; |
| 4413 | } |
| 4414 | |
| 4415 | kfree(pages); |
| 4416 | return nr_pages; |
| 4417 | |
| 4418 | out_unmap: |
| 4419 | if (res > 0) { |
| 4420 | for (j=0; j < res; j++) |
| 4421 | page_cache_release(pages[j]); |
Hugh Dickins | 6bc733e | 2005-12-01 20:21:57 +0000 | [diff] [blame] | 4422 | res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | } |
| 4424 | kfree(pages); |
| 4425 | return res; |
| 4426 | } |
| 4427 | |
| 4428 | |
| 4429 | /* And unmap them... */ |
| 4430 | static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages, |
| 4431 | int dirtied) |
| 4432 | { |
| 4433 | int i; |
| 4434 | |
| 4435 | for (i=0; i < nr_pages; i++) { |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 4436 | struct page *page = sgl[i].page; |
| 4437 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 4438 | if (dirtied) |
| 4439 | SetPageDirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | /* FIXME: cache flush missing for rw==READ |
| 4441 | * FIXME: call the correct reference counting function |
| 4442 | */ |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 4443 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | return 0; |
| 4447 | } |