Blame view

drivers/scsi/st.c 120 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
  /*
     SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
     file Documentation/scsi/st.txt for more information.
  
     History:
     Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
     Contribution and ideas from several people including (in alphabetical
     order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk,
     Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky,
     Michael Schaefer, J"org Weule, and Eric Youngdale.
3e51d3c92   Kai Makisara   [SCSI] st: add MT...
11
     Copyright 1992 - 2010 Kai Makisara
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
14
15
16
17
     email Kai.Makisara@kolumbus.fi
  
     Some small formal changes - aeb, 950809
  
     Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
   */
373daacfc   Kai Makisara   [SCSI] st: Store ...
18
  static const char *verstr = "20101219";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
24
25
26
27
  
  #include <linux/module.h>
  
  #include <linux/fs.h>
  #include <linux/kernel.h>
  #include <linux/sched.h>
  #include <linux/mm.h>
  #include <linux/init.h>
  #include <linux/string.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
28
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
  #include <linux/errno.h>
  #include <linux/mtio.h>
16c4b3e20   Kai Makisara   [SCSI] SCSI tape:...
31
  #include <linux/cdrom.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
  #include <linux/ioctl.h>
  #include <linux/fcntl.h>
  #include <linux/spinlock.h>
  #include <linux/blkdev.h>
  #include <linux/moduleparam.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  #include <linux/cdev.h>
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
38
  #include <linux/idr.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
  #include <linux/delay.h>
0b9506723   Arjan van de Ven   [SCSI] turn most ...
40
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
  
  #include <asm/uaccess.h>
  #include <asm/dma.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
48
49
50
51
  
  #include <scsi/scsi.h>
  #include <scsi/scsi_dbg.h>
  #include <scsi/scsi_device.h>
  #include <scsi/scsi_driver.h>
  #include <scsi/scsi_eh.h>
  #include <scsi/scsi_host.h>
  #include <scsi/scsi_ioctl.h>
16c4b3e20   Kai Makisara   [SCSI] SCSI tape:...
52
  #include <scsi/sg.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
56
  
  
  /* The driver prints some debugging information on the console if DEBUG
     is defined and non-zero. */
2bec708a8   Laurence Oberman   st: add a debug_f...
57
58
  #define DEBUG 1
  #define NO_DEBUG 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59

b30d8bca5   Hannes Reinecke   scsi: Implement s...
60
  #define ST_DEB_MSG  KERN_NOTICE
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
64
  #if DEBUG
  /* The message level for the debug messages is currently set to KERN_NOTICE
     so that people can easily see the messages. Later when the debugging messages
     in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  #define DEB(a) a
  #define DEBC(a) if (debugging) { a ; }
  #else
  #define DEB(a)
  #define DEBC(a)
  #endif
  
  #define ST_KILOBYTE 1024
  
  #include "st_options.h"
  #include "st.h"
  
  static int buffer_kbs;
  static int max_sg_segs;
  static int try_direct_io = TRY_DIRECT_IO;
  static int try_rdio = 1;
  static int try_wdio = 1;
2bec708a8   Laurence Oberman   st: add a debug_f...
82
  static int debug_flag;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83

af23782be   Jeff Mahoney   [SCSI] st: Use st...
84
  static struct class st_sysfs_class;
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
85
  static const struct attribute_group *st_dev_groups[];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
  
  MODULE_AUTHOR("Kai Makisara");
f018fa552   Rene Herman   [SCSI] MODULE_ALI...
88
  MODULE_DESCRIPTION("SCSI tape (st) driver");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
  MODULE_LICENSE("GPL");
f018fa552   Rene Herman   [SCSI] MODULE_ALI...
90
  MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);
d7b8bcb0a   Michael Tokarev   [SCSI] modalias f...
91
  MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
96
97
98
99
100
101
102
  
  /* Set 'perm' (4th argument) to 0 to disable module_param's definition
   * of sysfs parameters (which module_param doesn't yet support).
   * Sysfs parameters defined explicitly later.
   */
  module_param_named(buffer_kbs, buffer_kbs, int, 0);
  MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
  module_param_named(max_sg_segs, max_sg_segs, int, 0);
  MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
  module_param_named(try_direct_io, try_direct_io, int, 0);
  MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
2bec708a8   Laurence Oberman   st: add a debug_f...
103
104
  module_param_named(debug_flag, debug_flag, int, 0);
  MODULE_PARM_DESC(debug_flag, "Enable DEBUG, same as setting debugging=1");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  
  /* Extra parameters for testing */
  module_param_named(try_rdio, try_rdio, int, 0);
  MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
  module_param_named(try_wdio, try_wdio, int, 0);
  MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
  
  #ifndef MODULE
  static int write_threshold_kbs;  /* retained for compatibility */
  static struct st_dev_parm {
  	char *name;
  	int *val;
  } parms[] __initdata = {
  	{
  		"buffer_kbs", &buffer_kbs
  	},
  	{       /* Retained for compatibility with 2.4 */
  		"write_threshold_kbs", &write_threshold_kbs
  	},
  	{
  		"max_sg_segs", NULL
  	},
  	{
  		"try_direct_io", &try_direct_io
2bec708a8   Laurence Oberman   st: add a debug_f...
129
130
131
  	},
  	{
  		"debug_flag", &debug_flag
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
134
135
136
137
138
139
140
141
  	}
  };
  #endif
  
  /* Restrict the number of modes so that names for all are assigned */
  #if ST_NBR_MODES > 16
  #error "Maximum number of modes is 16"
  #endif
  /* Bit reversed order to get same names for same minors with all
     mode counts */
0ad78200b   Arjan van de Ven   [SCSI] Mark some ...
142
  static const char *st_formats[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  	"",  "r", "k", "s", "l", "t", "o", "u",
  	"m", "v", "p", "x", "a", "y", "q", "z"}; 
  
  /* The default definitions have been moved to st_options.h */
  
  #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
  
  /* The buffer size should fit into the 24 bits for length in the
     6-byte SCSI read and write commands. */
  #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
  #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
  #endif
  
  static int debugging = DEBUG;
  
  #define MAX_RETRIES 0
  #define MAX_WRITE_RETRIES 0
  #define MAX_READY_RETRIES 0
  #define NO_TAPE  NOT_READY
  
  #define ST_TIMEOUT (900 * HZ)
  #define ST_LONG_TIMEOUT (14000 * HZ)
  
  /* Remove mode bits and auto-rewind bit (7) */
  #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
      (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
  #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
  
  /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
  #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
    (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
  
  /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
     24 bits) */
  #define SET_DENS_AND_BLK 0x10001
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
  static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
  static int st_max_sg_segs = ST_MAX_SG;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  static int modes_defined;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  static int enlarge_buffer(struct st_buffer *, int, int);
40f6b36c6   Kai Makisara   [SCSI] st: add op...
182
  static void clear_buffer(struct st_buffer *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
  static void normalize_buffer(struct st_buffer *);
  static int append_to_buffer(const char __user *, struct st_buffer *, int);
  static int from_buffer(struct st_buffer *, char __user *, int);
  static void move_buffer_data(struct st_buffer *, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187

6620742f7   FUJITA Tomonori   [SCSI] st: conver...
188
  static int sgl_map_user_pages(struct st_buffer *, const unsigned int,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
  			      unsigned long, size_t, int);
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
190
  static int sgl_unmap_user_pages(struct st_buffer *, const unsigned int, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
  
  static int st_probe(struct device *);
  static int st_remove(struct device *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194

405ae7d38   Robert P. J. Day   Replace remaining...
195
196
  static int do_create_sysfs_files(void);
  static void do_remove_sysfs_files(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
198
  
  static struct scsi_driver st_template = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
  	.gendrv = {
  		.name		= "st",
3af6b3526   Christoph Hellwig   scsi: remove scsi...
201
  		.owner		= THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
  		.probe		= st_probe,
  		.remove		= st_remove,
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
208
209
210
211
212
  };
  
  static int st_compression(struct scsi_tape *, int);
  
  static int find_partition(struct scsi_tape *);
  static int switch_partition(struct scsi_tape *);
  
  static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
213
214
215
  static void scsi_tape_release(struct kref *);
  
  #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
0b9506723   Arjan van de Ven   [SCSI] turn most ...
216
  static DEFINE_MUTEX(st_ref_mutex);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
217
218
219
  static DEFINE_SPINLOCK(st_index_lock);
  static DEFINE_SPINLOCK(st_use_lock);
  static DEFINE_IDR(st_index_idr);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
220

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
223
224
225
226
227
228
229
230
  
  #include "osst_detect.h"
  #ifndef SIGS_FROM_OSST
  #define SIGS_FROM_OSST \
  	{"OnStream", "SC-", "", "osst"}, \
  	{"OnStream", "DI-", "", "osst"}, \
  	{"OnStream", "DP-", "", "osst"}, \
  	{"OnStream", "USB", "", "osst"}, \
  	{"OnStream", "FW-", "", "osst"}
  #endif
f03a56705   Kai Makisara   [SCSI] drivers/sc...
231
232
233
  static struct scsi_tape *scsi_tape_get(int dev)
  {
  	struct scsi_tape *STp = NULL;
0b9506723   Arjan van de Ven   [SCSI] turn most ...
234
  	mutex_lock(&st_ref_mutex);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
235
  	spin_lock(&st_index_lock);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
236

6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
237
  	STp = idr_find(&st_index_idr, dev);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  	if (!STp) goto out;
  
  	kref_get(&STp->kref);
  
  	if (!STp->device)
  		goto out_put;
  
  	if (scsi_device_get(STp->device))
  		goto out_put;
  
  	goto out;
  
  out_put:
  	kref_put(&STp->kref, scsi_tape_release);
  	STp = NULL;
  out:
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
254
  	spin_unlock(&st_index_lock);
0b9506723   Arjan van de Ven   [SCSI] turn most ...
255
  	mutex_unlock(&st_ref_mutex);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
256
257
258
259
260
261
  	return STp;
  }
  
  static void scsi_tape_put(struct scsi_tape *STp)
  {
  	struct scsi_device *sdev = STp->device;
0b9506723   Arjan van de Ven   [SCSI] turn most ...
262
  	mutex_lock(&st_ref_mutex);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
263
264
  	kref_put(&STp->kref, scsi_tape_release);
  	scsi_device_put(sdev);
0b9506723   Arjan van de Ven   [SCSI] turn most ...
265
  	mutex_unlock(&st_ref_mutex);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
266
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  struct st_reject_data {
  	char *vendor;
  	char *model;
  	char *rev;
  	char *driver_hint; /* Name of the correct driver, NULL if unknown */
  };
  
  static struct st_reject_data reject_list[] = {
  	/* {"XXX", "Yy-", "", NULL},  example */
  	SIGS_FROM_OSST,
  	{NULL, }};
  
  /* If the device signature is on the list of incompatible drives, the
     function returns a pointer to the name of the correct driver (if known) */
  static char * st_incompatible(struct scsi_device* SDp)
  {
  	struct st_reject_data *rp;
  
  	for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
  		if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
  		    !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
  		    !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
  			if (rp->driver_hint)
  				return rp->driver_hint;
  			else
  				return "unknown";
  		}
  	return NULL;
  }
  
  
  static inline char *tape_name(struct scsi_tape *tape)
  {
  	return tape->disk->disk_name;
  }
b30d8bca5   Hannes Reinecke   scsi: Implement s...
302
  #define st_printk(prefix, t, fmt, a...) \
22e0d9941   Hannes Reinecke   scsi: introduce s...
303
  	sdev_prefix_printk(prefix, (t)->device, tape_name(t), fmt, ##a)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
304
305
306
307
308
309
  #ifdef DEBUG
  #define DEBC_printk(t, fmt, a...) \
  	if (debugging) { st_printk(ST_DEB_MSG, t, fmt, ##a ); }
  #else
  #define DEBC_printk(t, fmt, a...)
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310

8b05b773b   Mike Christie   [SCSI] convert st...
311
  static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
  {
  	const u8 *ucp;
8b05b773b   Mike Christie   [SCSI] convert st...
314
  	const u8 *sense = SRpnt->sense;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315

8b05b773b   Mike Christie   [SCSI] convert st...
316
317
  	s->have_sense = scsi_normalize_sense(SRpnt->sense,
  				SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  	s->flags = 0;
  
  	if (s->have_sense) {
  		s->deferred = 0;
  		s->remainder_valid =
  			scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
  		switch (sense[0] & 0x7f) {
  		case 0x71:
  			s->deferred = 1;
  		case 0x70:
  			s->fixed_format = 1;
  			s->flags = sense[2] & 0xe0;
  			break;
  		case 0x73:
  			s->deferred = 1;
  		case 0x72:
  			s->fixed_format = 0;
  			ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
  			s->flags = ucp ? (ucp[3] & 0xe0) : 0;
  			break;
  		}
  	}
  }
  
  
  /* Convert the result to success code */
8b05b773b   Mike Christie   [SCSI] convert st...
344
  static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
  {
8b05b773b   Mike Christie   [SCSI] convert st...
346
  	int result = SRpnt->result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
348
349
350
351
352
353
354
355
  	u8 scode;
  	DEB(const char *stp;)
  	char *name = tape_name(STp);
  	struct st_cmdstatus *cmdstatp;
  
  	if (!result)
  		return 0;
  
  	cmdstatp = &STp->buffer->cmdstat;
f03a56705   Kai Makisara   [SCSI] drivers/sc...
356
  	st_analyze_sense(SRpnt, cmdstatp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
361
  
  	if (cmdstatp->have_sense)
  		scode = STp->buffer->cmdstat.sense_hdr.sense_key;
  	else
  		scode = 0;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
362
363
364
365
366
367
368
  	DEB(
  	if (debugging) {
  		st_printk(ST_DEB_MSG, STp,
  			    "Error: %x, cmd: %x %x %x %x %x %x
  ", result,
  			    SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
  			    SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
  		if (cmdstatp->have_sense)
d811b848e   Hannes Reinecke   scsi: use sdev as...
370
371
  			__scsi_print_sense(STp->device, name,
  					   SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
373
374
  	} ) /* end DEB */
  	if (!debugging) { /* Abnormal conditions for tape */
  		if (!cmdstatp->have_sense)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
375
376
377
378
  			st_printk(KERN_WARNING, STp,
  			       "Error %x (driver bt 0x%x, host bt 0x%x).
  ",
  			       result, driver_byte(result), host_byte(result));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
382
383
384
  		else if (cmdstatp->have_sense &&
  			 scode != NO_SENSE &&
  			 scode != RECOVERED_ERROR &&
  			 /* scode != UNIT_ATTENTION && */
  			 scode != BLANK_CHECK &&
  			 scode != VOLUME_OVERFLOW &&
8b05b773b   Mike Christie   [SCSI] convert st...
385
386
  			 SRpnt->cmd[0] != MODE_SENSE &&
  			 SRpnt->cmd[0] != TEST_UNIT_READY) {
4e73ea7b0   Luben Tuikov   [SCSI] st.c: Impr...
387

d811b848e   Hannes Reinecke   scsi: use sdev as...
388
389
  			__scsi_print_sense(STp->device, name,
  					   SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
392
393
394
395
  		}
  	}
  
  	if (cmdstatp->fixed_format &&
  	    STp->cln_mode >= EXTENDED_SENSE_START) {  /* Only fixed format sense */
  		if (STp->cln_sense_value)
8b05b773b   Mike Christie   [SCSI] convert st...
396
  			STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
  					       STp->cln_sense_mask) == STp->cln_sense_value);
  		else
8b05b773b   Mike Christie   [SCSI] convert st...
399
  			STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
402
403
404
405
406
407
408
409
410
  					       STp->cln_sense_mask) != 0);
  	}
  	if (cmdstatp->have_sense &&
  	    cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
  		STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
  
  	STp->pos_unknown |= STp->device->was_reset;
  
  	if (cmdstatp->have_sense &&
  	    scode == RECOVERED_ERROR
  #if ST_RECOVERED_WRITE_FATAL
8b05b773b   Mike Christie   [SCSI] convert st...
411
412
  	    && SRpnt->cmd[0] != WRITE_6
  	    && SRpnt->cmd[0] != WRITE_FILEMARKS
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
  #endif
  	    ) {
  		STp->recover_count++;
  		STp->recover_reg++;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
417
  		DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
  		if (debugging) {
8b05b773b   Mike Christie   [SCSI] convert st...
419
  			if (SRpnt->cmd[0] == READ_6)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
  				stp = "read";
8b05b773b   Mike Christie   [SCSI] convert st...
421
  			else if (SRpnt->cmd[0] == WRITE_6)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
  				stp = "write";
  			else
  				stp = "ioctl";
b30d8bca5   Hannes Reinecke   scsi: Implement s...
425
426
427
428
  			st_printk(ST_DEB_MSG, STp,
  				  "Recovered %s error (%d).
  ",
  				  stp, STp->recover_count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
431
432
433
434
435
  		} ) /* end DEB */
  
  		if (cmdstatp->flags == 0)
  			return 0;
  	}
  	return (-EIO);
  }
4deba245d   FUJITA Tomonori   [SCSI] st: move s...
436
  static struct st_request *st_allocate_request(struct scsi_tape *stp)
8b05b773b   Mike Christie   [SCSI] convert st...
437
  {
4deba245d   FUJITA Tomonori   [SCSI] st: move s...
438
439
440
441
442
443
  	struct st_request *streq;
  
  	streq = kzalloc(sizeof(*streq), GFP_KERNEL);
  	if (streq)
  		streq->stp = stp;
  	else {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
444
445
446
  		st_printk(KERN_ERR, stp,
  			  "Can't get SCSI request.
  ");
4deba245d   FUJITA Tomonori   [SCSI] st: move s...
447
448
449
450
451
452
453
  		if (signal_pending(current))
  			stp->buffer->syscall_result = -EINTR;
  		else
  			stp->buffer->syscall_result = -EBUSY;
  	}
  
  	return streq;
8b05b773b   Mike Christie   [SCSI] convert st...
454
455
456
457
458
  }
  
  static void st_release_request(struct st_request *streq)
  {
  	kfree(streq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  }
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
460
461
462
463
  static void st_scsi_execute_end(struct request *req, int uptodate)
  {
  	struct st_request *SRpnt = req->end_io_data;
  	struct scsi_tape *STp = SRpnt->stp;
c68bf8eea   Petr Uzel   [SCSI] st: fix ra...
464
  	struct bio *tmp;
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
465
466
  
  	STp->buffer->cmdstat.midlevel_result = SRpnt->result = req->errors;
c3a4d78c5   Tejun Heo   block: add rq->re...
467
  	STp->buffer->cmdstat.residual = req->resid_len;
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
468

c68bf8eea   Petr Uzel   [SCSI] st: fix ra...
469
  	tmp = SRpnt->bio;
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
470
471
  	if (SRpnt->waiting)
  		complete(SRpnt->waiting);
c68bf8eea   Petr Uzel   [SCSI] st: fix ra...
472
  	blk_rq_unmap_user(tmp);
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  	__blk_put_request(req->q, req);
  }
  
  static int st_scsi_execute(struct st_request *SRpnt, const unsigned char *cmd,
  			   int data_direction, void *buffer, unsigned bufflen,
  			   int timeout, int retries)
  {
  	struct request *req;
  	struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data;
  	int err = 0;
  	int write = (data_direction == DMA_TO_DEVICE);
  
  	req = blk_get_request(SRpnt->stp->device->request_queue, write,
  			      GFP_KERNEL);
a492f0754   Joe Lawrence   block,scsi: fixup...
487
  	if (IS_ERR(req))
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
488
  		return DRIVER_ERROR << 24;
f27b087b8   Jens Axboe   block: add blk_rq...
489
  	blk_rq_set_block_pc(req);
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
490
491
492
  	req->cmd_flags |= REQ_QUIET;
  
  	mdata->null_mapped = 1;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
493
494
495
496
497
498
499
  	if (bufflen) {
  		err = blk_rq_map_user(req->q, req, mdata, NULL, bufflen,
  				      GFP_KERNEL);
  		if (err) {
  			blk_put_request(req);
  			return DRIVER_ERROR << 24;
  		}
13b53b443   FUJITA Tomonori   [SCSI] st: add st...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  	}
  
  	SRpnt->bio = req->bio;
  	req->cmd_len = COMMAND_SIZE(cmd[0]);
  	memset(req->cmd, 0, BLK_MAX_CDB);
  	memcpy(req->cmd, cmd, req->cmd_len);
  	req->sense = SRpnt->sense;
  	req->sense_len = 0;
  	req->timeout = timeout;
  	req->retries = retries;
  	req->end_io_data = SRpnt;
  
  	blk_execute_rq_nowait(req->q, NULL, req, 1, st_scsi_execute_end);
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
517
  /* Do the scsi command. Waits until command performed if do_wait is true.
     Otherwise write_behind_check() is used to check that the command
     has finished. */
8b05b773b   Mike Christie   [SCSI] convert st...
518
519
  static struct st_request *
  st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
521
  	   int bytes, int direction, int timeout, int retries, int do_wait)
  {
f03a56705   Kai Makisara   [SCSI] drivers/sc...
522
  	struct completion *waiting;
6d4762678   FUJITA Tomonori   [SCSI] st: conver...
523
524
  	struct rq_map_data *mdata = &STp->buffer->map_data;
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525

f03a56705   Kai Makisara   [SCSI] drivers/sc...
526
527
  	/* if async, make sure there's no command outstanding */
  	if (!do_wait && ((STp->buffer)->last_SRpnt)) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
528
529
530
  		st_printk(KERN_ERR, STp,
  			  "Async command already active.
  ");
f03a56705   Kai Makisara   [SCSI] drivers/sc...
531
532
533
534
535
536
  		if (signal_pending(current))
  			(STp->buffer)->syscall_result = (-EINTR);
  		else
  			(STp->buffer)->syscall_result = (-EBUSY);
  		return NULL;
  	}
4deba245d   FUJITA Tomonori   [SCSI] st: move s...
537
538
539
  	if (!SRpnt) {
  		SRpnt = st_allocate_request(STp);
  		if (!SRpnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
  			return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
  	}
f03a56705   Kai Makisara   [SCSI] drivers/sc...
542
543
544
545
546
547
548
  	/* If async IO, set last_SRpnt. This ptr tells write_behind_check
  	   which IO is outstanding. It's nulled out when the IO completes. */
  	if (!do_wait)
  		(STp->buffer)->last_SRpnt = SRpnt;
  
  	waiting = &STp->wait;
  	init_completion(waiting);
8b05b773b   Mike Christie   [SCSI] convert st...
549
  	SRpnt->waiting = waiting;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550

6620742f7   FUJITA Tomonori   [SCSI] st: conver...
551
  	if (STp->buffer->do_dio) {
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
552
  		mdata->page_order = 0;
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
553
554
555
  		mdata->nr_entries = STp->buffer->sg_segs;
  		mdata->pages = STp->buffer->mapped_pages;
  	} else {
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
556
  		mdata->page_order = STp->buffer->reserved_page_order;
6d4762678   FUJITA Tomonori   [SCSI] st: conver...
557
558
  		mdata->nr_entries =
  			DIV_ROUND_UP(bytes, PAGE_SIZE << mdata->page_order);
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
559
560
  		mdata->pages = STp->buffer->reserved_pages;
  		mdata->offset = 0;
6d4762678   FUJITA Tomonori   [SCSI] st: conver...
561
  	}
8b05b773b   Mike Christie   [SCSI] convert st...
562
563
564
  	memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
  	STp->buffer->cmdstat.have_sense = 0;
  	STp->buffer->syscall_result = 0;
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
565
566
  	ret = st_scsi_execute(SRpnt, cmd, direction, NULL, bytes, timeout,
  			      retries);
6d4762678   FUJITA Tomonori   [SCSI] st: conver...
567
  	if (ret) {
8b05b773b   Mike Christie   [SCSI] convert st...
568
569
  		/* could not allocate the buffer or request was too large */
  		(STp->buffer)->syscall_result = (-EBUSY);
787926b1b   Kai Makisara   [SCSI] Fix st oop...
570
  		(STp->buffer)->last_SRpnt = NULL;
6d4762678   FUJITA Tomonori   [SCSI] st: conver...
571
  	} else if (do_wait) {
f03a56705   Kai Makisara   [SCSI] drivers/sc...
572
  		wait_for_completion(waiting);
8b05b773b   Mike Christie   [SCSI] convert st...
573
  		SRpnt->waiting = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
  		(STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
  	}
8b05b773b   Mike Christie   [SCSI] convert st...
576

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
579
580
581
582
583
584
585
586
587
588
589
590
  	return SRpnt;
  }
  
  
  /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
     write has been correct but EOM early warning reached, -EIO if write ended in
     error or zero if write successful. Asynchronous writes are used only in
     variable block mode. */
  static int write_behind_check(struct scsi_tape * STp)
  {
  	int retval = 0;
  	struct st_buffer *STbuffer;
  	struct st_partstat *STps;
  	struct st_cmdstatus *cmdstatp;
8b05b773b   Mike Christie   [SCSI] convert st...
591
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
593
594
595
  
  	STbuffer = STp->buffer;
  	if (!STbuffer->writing)
  		return 0;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
596
  	DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
599
600
  	if (STp->write_pending)
  		STp->nbr_waits++;
  	else
  		STp->nbr_finished++;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
601
  	) /* end DEB */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
603
  
  	wait_for_completion(&(STp->wait));
f03a56705   Kai Makisara   [SCSI] drivers/sc...
604
605
  	SRpnt = STbuffer->last_SRpnt;
  	STbuffer->last_SRpnt = NULL;
8b05b773b   Mike Christie   [SCSI] convert st...
606
  	SRpnt->waiting = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
607

f03a56705   Kai Makisara   [SCSI] drivers/sc...
608
  	(STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
8b05b773b   Mike Christie   [SCSI] convert st...
609
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  
  	STbuffer->buffer_bytes -= STbuffer->writing;
  	STps = &(STp->ps[STp->partition]);
  	if (STps->drv_block >= 0) {
  		if (STp->block_size == 0)
  			STps->drv_block++;
  		else
  			STps->drv_block += STbuffer->writing / STp->block_size;
  	}
  
  	cmdstatp = &STbuffer->cmdstat;
  	if (STbuffer->syscall_result) {
  		retval = -EIO;
  		if (cmdstatp->have_sense && !cmdstatp->deferred &&
  		    (cmdstatp->flags & SENSE_EOM) &&
  		    (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  		     cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
  			/* EOM at write-behind, has all data been written? */
  			if (!cmdstatp->remainder_valid ||
  			    cmdstatp->uremainder64 == 0)
  				retval = -ENOSPC;
  		}
  		if (retval == -EIO)
  			STps->drv_block = -1;
  	}
  	STbuffer->writing = 0;
  
  	DEB(if (debugging && retval)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
638
639
640
641
  		    st_printk(ST_DEB_MSG, STp,
  				"Async write error %x, return value %d.
  ",
  				STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
644
645
646
647
648
649
650
  
  	return retval;
  }
  
  
  /* Step over EOF if it has been inadvertently crossed (ioctl not used because
     it messes up the block number). */
  static int cross_eof(struct scsi_tape * STp, int forward)
  {
8b05b773b   Mike Christie   [SCSI] convert st...
651
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
654
655
656
657
658
659
660
661
  	unsigned char cmd[MAX_COMMAND_SIZE];
  
  	cmd[0] = SPACE;
  	cmd[1] = 0x01;		/* Space FileMarks */
  	if (forward) {
  		cmd[2] = cmd[3] = 0;
  		cmd[4] = 1;
  	} else
  		cmd[2] = cmd[3] = cmd[4] = 0xff;	/* -1 filemarks */
  	cmd[5] = 0;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
662
663
664
  	DEBC_printk(STp, "Stepping over filemark %s.
  ",
  		    forward ? "forward" : "backward");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665

02ae2c0e8   Kai Makisara   [SCSI] st: integr...
666
667
668
  	SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
  			   STp->device->request_queue->rq_timeout,
  			   MAX_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
  	if (!SRpnt)
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
670
  		return (STp->buffer)->syscall_result;
39ade4b1a   FUJITA Tomonori   [SCSI] st: conver...
671

02ae2c0e8   Kai Makisara   [SCSI] st: integr...
672
673
  	st_release_request(SRpnt);
  	SRpnt = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
675
  
  	if ((STp->buffer)->cmdstat.midlevel_result != 0)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
676
677
678
679
  		st_printk(KERN_ERR, STp,
  			  "Stepping over filemark %s failed.
  ",
  			  forward ? "forward" : "backward");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680

02ae2c0e8   Kai Makisara   [SCSI] st: integr...
681
  	return (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
683
684
685
  }
  
  
  /* Flush the write buffer (never need to write if variable blocksize). */
8ef8d5941   Adrian Bunk   [SCSI] st: rename...
686
  static int st_flush_write_buffer(struct scsi_tape * STp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
  {
786231af0   Kai Makisara   [SCSI] st: Remove...
688
  	int transfer, blks;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
690
  	int result;
  	unsigned char cmd[MAX_COMMAND_SIZE];
8b05b773b   Mike Christie   [SCSI] convert st...
691
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
693
694
695
696
697
698
699
  	struct st_partstat *STps;
  
  	result = write_behind_check(STp);
  	if (result)
  		return result;
  
  	result = 0;
  	if (STp->dirty == 1) {
786231af0   Kai Makisara   [SCSI] st: Remove...
700
  		transfer = STp->buffer->buffer_bytes;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
701
702
  		DEBC_printk(STp, "Flushing %d bytes.
  ", transfer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
705
706
707
708
709
710
711
712
  		memset(cmd, 0, MAX_COMMAND_SIZE);
  		cmd[0] = WRITE_6;
  		cmd[1] = 1;
  		blks = transfer / STp->block_size;
  		cmd[2] = blks >> 16;
  		cmd[3] = blks >> 8;
  		cmd[4] = blks;
  
  		SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
a02488ed7   James Bottomley   [SCSI] st: update...
713
714
  				   STp->device->request_queue->rq_timeout,
  				   MAX_WRITE_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  		if (!SRpnt)
  			return (STp->buffer)->syscall_result;
  
  		STps = &(STp->ps[STp->partition]);
  		if ((STp->buffer)->syscall_result != 0) {
  			struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  
  			if (cmdstatp->have_sense && !cmdstatp->deferred &&
  			    (cmdstatp->flags & SENSE_EOM) &&
  			    (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  			     cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
  			    (!cmdstatp->remainder_valid ||
  			     cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
  				STp->dirty = 0;
  				(STp->buffer)->buffer_bytes = 0;
  				if (STps->drv_block >= 0)
  					STps->drv_block += blks;
  				result = (-ENOSPC);
  			} else {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
734
735
  				st_printk(KERN_ERR, STp, "Error on flush.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
740
741
742
743
744
  				STps->drv_block = (-1);
  				result = (-EIO);
  			}
  		} else {
  			if (STps->drv_block >= 0)
  				STps->drv_block += blks;
  			STp->dirty = 0;
  			(STp->buffer)->buffer_bytes = 0;
  		}
8b05b773b   Mike Christie   [SCSI] convert st...
745
  		st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
  		SRpnt = NULL;
  	}
  	return result;
  }
  
  
  /* Flush the tape buffer. The tape will be positioned correctly unless
     seek_next is true. */
  static int flush_buffer(struct scsi_tape *STp, int seek_next)
  {
  	int backspace, result;
  	struct st_buffer *STbuffer;
  	struct st_partstat *STps;
  
  	STbuffer = STp->buffer;
  
  	/*
  	 * If there was a bus reset, block further access
  	 * to this device.
  	 */
  	if (STp->pos_unknown)
  		return (-EIO);
  
  	if (STp->ready != ST_READY)
  		return 0;
  	STps = &(STp->ps[STp->partition]);
  	if (STps->rw == ST_WRITING)	/* Writing */
8ef8d5941   Adrian Bunk   [SCSI] st: rename...
773
  		return st_flush_write_buffer(STp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
  
  	if (STp->block_size == 0)
  		return 0;
  
  	backspace = ((STp->buffer)->buffer_bytes +
  		     (STp->buffer)->read_pointer) / STp->block_size -
  	    ((STp->buffer)->read_pointer + STp->block_size - 1) /
  	    STp->block_size;
  	(STp->buffer)->buffer_bytes = 0;
  	(STp->buffer)->read_pointer = 0;
  	result = 0;
  	if (!seek_next) {
  		if (STps->eof == ST_FM_HIT) {
  			result = cross_eof(STp, 0);	/* Back over the EOF hit */
  			if (!result)
  				STps->eof = ST_NOEOF;
  			else {
  				if (STps->drv_file >= 0)
  					STps->drv_file++;
  				STps->drv_block = 0;
  			}
  		}
  		if (!result && backspace > 0)
  			result = st_int_ioctl(STp, MTBSR, backspace);
  	} else if (STps->eof == ST_FM_HIT) {
  		if (STps->drv_file >= 0)
  			STps->drv_file++;
  		STps->drv_block = 0;
  		STps->eof = ST_NOEOF;
  	}
  	return result;
  
  }
  
  /* Set the mode parameters */
  static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
  {
  	int set_it = 0;
  	unsigned long arg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  
  	if (!STp->density_changed &&
  	    STm->default_density >= 0 &&
  	    STm->default_density != STp->density) {
  		arg = STm->default_density;
  		set_it = 1;
  	} else
  		arg = STp->density;
  	arg <<= MT_ST_DENSITY_SHIFT;
  	if (!STp->blksize_changed &&
  	    STm->default_blksize >= 0 &&
  	    STm->default_blksize != STp->block_size) {
  		arg |= STm->default_blksize;
  		set_it = 1;
  	} else
  		arg |= STp->block_size;
  	if (set_it &&
  	    st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
831
832
833
834
835
  		st_printk(KERN_WARNING, STp,
  			  "Can't set default block size to %d bytes "
  			  "and density %x.
  ",
  			  STm->default_blksize, STm->default_density);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
836
837
838
839
840
  		if (modes_defined)
  			return (-EINVAL);
  	}
  	return 0;
  }
8b05b773b   Mike Christie   [SCSI] convert st...
841
  /* Lock or unlock the drive door. Don't use when st_request allocated. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
  static int do_door_lock(struct scsi_tape * STp, int do_lock)
  {
dccfa688c   Christoph Hellwig   st: call scsi_set...
844
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845

b30d8bca5   Hannes Reinecke   scsi: Implement s...
846
847
  	DEBC_printk(STp, "%socking drive door.
  ", do_lock ? "L" : "Unl");
dccfa688c   Christoph Hellwig   st: call scsi_set...
848
849
850
851
  
  	retval = scsi_set_medium_removal(STp->device,
  			do_lock ? SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
  	if (!retval)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
  		STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
dccfa688c   Christoph Hellwig   st: call scsi_set...
853
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
  		STp->door_locked = ST_LOCK_FAILS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
  	return retval;
  }
  
  
  /* Set the internal state after reset */
  static void reset_state(struct scsi_tape *STp)
  {
  	int i;
  	struct st_partstat *STps;
  
  	STp->pos_unknown = 0;
  	for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  		STps = &(STp->ps[i]);
  		STps->rw = ST_IDLE;
  		STps->eof = ST_NOEOF;
  		STps->at_sm = 0;
  		STps->last_block_valid = 0;
  		STps->drv_block = -1;
  		STps->drv_file = -1;
  	}
  	if (STp->can_partitions) {
  		STp->partition = find_partition(STp);
  		if (STp->partition < 0)
  			STp->partition = 0;
  		STp->new_partition = STp->partition;
  	}
  }
  
  /* Test if the drive is ready. Returns either one of the codes below or a negative system
     error code. */
  #define CHKRES_READY       0
  #define CHKRES_NEW_SESSION 1
  #define CHKRES_NOT_READY   2
  #define CHKRES_NO_TAPE     3
  
  #define MAX_ATTENTIONS    10
  
  static int test_ready(struct scsi_tape *STp, int do_wait)
  {
  	int attentions, waits, max_wait, scode;
  	int retval = CHKRES_READY, new_session = 0;
  	unsigned char cmd[MAX_COMMAND_SIZE];
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
897
  	struct st_request *SRpnt = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
899
900
901
902
903
904
  	struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  
  	max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
  
  	for (attentions=waits=0; ; ) {
  		memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
  		cmd[0] = TEST_UNIT_READY;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
905
906
  		SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
  				   STp->long_timeout, MAX_READY_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907

02ae2c0e8   Kai Makisara   [SCSI] st: integr...
908
909
  		if (!SRpnt) {
  			retval = (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  			break;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
911
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
  
  		if (cmdstatp->have_sense) {
  
  			scode = cmdstatp->sense_hdr.sense_key;
  
  			if (scode == UNIT_ATTENTION) { /* New media? */
  				new_session = 1;
  				if (attentions < MAX_ATTENTIONS) {
  					attentions++;
  					continue;
  				}
  				else {
  					retval = (-EIO);
  					break;
  				}
  			}
  
  			if (scode == NOT_READY) {
  				if (waits < max_wait) {
  					if (msleep_interruptible(1000)) {
  						retval = (-EINTR);
  						break;
  					}
  					waits++;
  					continue;
  				}
  				else {
  					if ((STp->device)->scsi_level >= SCSI_2 &&
  					    cmdstatp->sense_hdr.asc == 0x3a)	/* Check ASC */
  						retval = CHKRES_NO_TAPE;
  					else
  						retval = CHKRES_NOT_READY;
  					break;
  				}
  			}
  		}
  
  		retval = (STp->buffer)->syscall_result;
  		if (!retval)
  			retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
  		break;
  	}
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
954
955
  	if (SRpnt != NULL)
  		st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
956
957
958
959
960
961
962
963
964
965
966
967
968
969
  	return retval;
  }
  
  
  /* See if the drive is ready and gather information about the tape. Return values:
     < 0   negative error code from errno.h
     0     drive ready
     1     drive not ready (possibly no tape)
  */
  static int check_tape(struct scsi_tape *STp, struct file *filp)
  {
  	int i, retval, new_session = 0, do_wait;
  	unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
  	unsigned short st_flags = filp->f_flags;
8b05b773b   Mike Christie   [SCSI] convert st...
970
  	struct st_request *SRpnt = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
972
  	struct st_modedef *STm;
  	struct st_partstat *STps;
496ad9aa8   Al Viro   new helper: file_...
973
  	struct inode *inode = file_inode(filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
975
976
977
978
  	int mode = TAPE_MODE(inode);
  
  	STp->ready = ST_READY;
  
  	if (mode != STp->current_mode) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
979
980
981
  		DEBC_printk(STp, "Mode change from %d to %d.
  ",
  			    STp->current_mode, mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  		new_session = 1;
  		STp->current_mode = mode;
  	}
  	STm = &(STp->modes[STp->current_mode]);
  
  	saved_cleaning = STp->cleaning_req;
  	STp->cleaning_req = 0;
  
  	do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
  	retval = test_ready(STp, do_wait);
  
  	if (retval < 0)
  	    goto err_out;
  
  	if (retval == CHKRES_NEW_SESSION) {
  		STp->pos_unknown = 0;
  		STp->partition = STp->new_partition = 0;
  		if (STp->can_partitions)
  			STp->nbr_partitions = 1; /* This guess will be updated later
                                                      if necessary */
  		for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  			STps = &(STp->ps[i]);
  			STps->rw = ST_IDLE;
  			STps->eof = ST_NOEOF;
  			STps->at_sm = 0;
  			STps->last_block_valid = 0;
  			STps->drv_block = 0;
  			STps->drv_file = 0;
  		}
  		new_session = 1;
  	}
  	else {
  		STp->cleaning_req |= saved_cleaning;
  
  		if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
  			if (retval == CHKRES_NO_TAPE)
  				STp->ready = ST_NO_TAPE;
  			else
  				STp->ready = ST_NOT_READY;
  
  			STp->density = 0;	/* Clear the erroneous "residue" */
  			STp->write_prot = 0;
  			STp->block_size = 0;
  			STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
  			STp->partition = STp->new_partition = 0;
  			STp->door_locked = ST_UNLOCKED;
  			return CHKRES_NOT_READY;
  		}
  	}
  
  	if (STp->omit_blklims)
  		STp->min_block = STp->max_block = (-1);
  	else {
  		memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
  		cmd[0] = READ_BLOCK_LIMITS;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
1037
1038
1039
1040
1041
  		SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
  				   STp->device->request_queue->rq_timeout,
  				   MAX_READY_RETRIES, 1);
  		if (!SRpnt) {
  			retval = (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1042
1043
  			goto err_out;
  		}
8b05b773b   Mike Christie   [SCSI] convert st...
1044
  		if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1045
1046
1047
1048
1049
  			STp->max_block = ((STp->buffer)->b_data[1] << 16) |
  			    ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
  			STp->min_block = ((STp->buffer)->b_data[4] << 8) |
  			    (STp->buffer)->b_data[5];
  			if ( DEB( debugging || ) !STp->inited)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1050
1051
1052
1053
  				st_printk(KERN_INFO, STp,
  					  "Block limits %d - %d bytes.
  ",
  					  STp->min_block, STp->max_block);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
1055
  		} else {
  			STp->min_block = STp->max_block = (-1);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1056
1057
  			DEBC_printk(STp, "Can't read block limits.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1058
1059
1060
1061
1062
1063
  		}
  	}
  
  	memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
  	cmd[0] = MODE_SENSE;
  	cmd[4] = 12;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
1064
1065
1066
1067
1068
  	SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
  			   STp->device->request_queue->rq_timeout,
  			   MAX_READY_RETRIES, 1);
  	if (!SRpnt) {
  		retval = (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1069
1070
1071
1072
  		goto err_out;
  	}
  
  	if ((STp->buffer)->syscall_result != 0) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1073
1074
  		DEBC_printk(STp, "No Mode Sense.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
1076
1077
1078
  		STp->block_size = ST_DEFAULT_BLOCK;	/* Educated guess (?) */
  		(STp->buffer)->syscall_result = 0;	/* Prevent error propagation */
  		STp->drv_write_prot = 0;
  	} else {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1079
1080
1081
1082
1083
1084
1085
  		DEBC_printk(STp,"Mode sense. Length %d, "
  			    "medium %x, WBS %x, BLL %d
  ",
  			    (STp->buffer)->b_data[0],
  			    (STp->buffer)->b_data[1],
  			    (STp->buffer)->b_data[2],
  			    (STp->buffer)->b_data[3]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1086
1087
1088
1089
1090
1091
  
  		if ((STp->buffer)->b_data[3] >= 8) {
  			STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
  			STp->density = (STp->buffer)->b_data[4];
  			STp->block_size = (STp->buffer)->b_data[9] * 65536 +
  			    (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1092
1093
1094
1095
1096
1097
1098
1099
  			DEBC_printk(STp, "Density %x, tape length: %x, "
  				    "drv buffer: %d
  ",
  				    STp->density,
  				    (STp->buffer)->b_data[5] * 65536 +
  				    (STp->buffer)->b_data[6] * 256 +
  				    (STp->buffer)->b_data[7],
  				    STp->drv_buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100
1101
  		}
  		STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
c743e44fb   Lee Duncan   [SCSI] st: expand...
1102
  		if (!STp->drv_buffer && STp->immediate_filemark) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1103
1104
1105
1106
  			st_printk(KERN_WARNING, STp,
  				  "non-buffered tape: disabling "
  				  "writing immediate filemarks
  ");
c743e44fb   Lee Duncan   [SCSI] st: expand...
1107
1108
  			STp->immediate_filemark = 0;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1109
  	}
8b05b773b   Mike Christie   [SCSI] convert st...
1110
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
  	SRpnt = NULL;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1112
  	STp->inited = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113
1114
1115
  
  	if (STp->block_size > 0)
  		(STp->buffer)->buffer_blocks =
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1116
  			(STp->buffer)->buffer_size / STp->block_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1117
1118
1119
  	else
  		(STp->buffer)->buffer_blocks = 1;
  	(STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1120
1121
1122
1123
  	DEBC_printk(STp, "Block size: %d, buffer size: %d (%d blocks).
  ",
  		    STp->block_size, (STp->buffer)->buffer_size,
  		    (STp->buffer)->buffer_blocks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1124
1125
1126
  
  	if (STp->drv_write_prot) {
  		STp->write_prot = 1;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1127
1128
  		DEBC_printk(STp, "Write protected
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  
  		if (do_wait &&
  		    ((st_flags & O_ACCMODE) == O_WRONLY ||
  		     (st_flags & O_ACCMODE) == O_RDWR)) {
  			retval = (-EROFS);
  			goto err_out;
  		}
  	}
  
  	if (STp->can_partitions && STp->nbr_partitions < 1) {
  		/* This code is reached when the device is opened for the first time
  		   after the driver has been initialized with tape in the drive and the
  		   partition support has been enabled. */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1142
1143
  		DEBC_printk(STp, "Updating partition number in status.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
  		if ((STp->partition = find_partition(STp)) < 0) {
  			retval = STp->partition;
  			goto err_out;
  		}
  		STp->new_partition = STp->partition;
  		STp->nbr_partitions = 1; /* This guess will be updated when necessary */
  	}
  
  	if (new_session) {	/* Change the drive parameters for the new mode */
  		STp->density_changed = STp->blksize_changed = 0;
  		STp->compression_changed = 0;
  		if (!(STm->defaults_for_writes) &&
  		    (retval = set_mode_densblk(STp, STm)) < 0)
  		    goto err_out;
  
  		if (STp->default_drvbuffer != 0xff) {
  			if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1161
1162
1163
1164
1165
  				st_printk(KERN_WARNING, STp,
  					  "Can't set default drive "
  					  "buffering to %d.
  ",
  					  STp->default_drvbuffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
1167
1168
1169
1170
1171
1172
1173
  		}
  	}
  
  	return CHKRES_READY;
  
   err_out:
  	return retval;
  }
b3369c68b   Jonathan Corbet   st: cdev lock_ker...
1174
  /* Open the device. Needs to take the BKL only because of incrementing the SCSI host
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1175
1176
1177
1178
     module count. */
  static int st_open(struct inode *inode, struct file *filp)
  {
  	int i, retval = (-EIO);
46a243f72   Oliver Neukum   [SCSI] st: implem...
1179
  	int resumed = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1180
1181
1182
  	struct scsi_tape *STp;
  	struct st_partstat *STps;
  	int dev = TAPE_NR(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1183
1184
1185
1186
1187
1188
1189
  
  	/*
  	 * We really want to do nonseekable_open(inode, filp); here, but some
  	 * versions of tar incorrectly call lseek on tapes and bail out if that
  	 * fails.  So we disallow pread() and pwrite(), but permit lseeks.
  	 */
  	filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
b3369c68b   Jonathan Corbet   st: cdev lock_ker...
1190
  	if (!(STp = scsi_tape_get(dev))) {
f03a56705   Kai Makisara   [SCSI] drivers/sc...
1191
  		return -ENXIO;
b3369c68b   Jonathan Corbet   st: cdev lock_ker...
1192
  	}
f03a56705   Kai Makisara   [SCSI] drivers/sc...
1193

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1194
  	filp->private_data = STp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1195

6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
1196
  	spin_lock(&st_use_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1197
  	if (STp->in_use) {
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
1198
  		spin_unlock(&st_use_lock);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
1199
  		scsi_tape_put(STp);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1200
1201
  		DEBC_printk(STp, "Device already in use.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1202
1203
  		return (-EBUSY);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1204
  	STp->in_use = 1;
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
1205
  	spin_unlock(&st_use_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1206
  	STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
46a243f72   Oliver Neukum   [SCSI] st: implem...
1207
1208
1209
1210
1211
  	if (scsi_autopm_get_device(STp->device) < 0) {
  		retval = -EIO;
  		goto err_out;
  	}
  	resumed = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
1213
1214
1215
1216
1217
1218
  	if (!scsi_block_when_processing_errors(STp->device)) {
  		retval = (-ENXIO);
  		goto err_out;
  	}
  
  	/* See that we have at least a one page buffer available */
  	if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1219
1220
1221
  		st_printk(KERN_WARNING, STp,
  			  "Can't allocate one page tape buffer.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
1223
1224
  		retval = (-EOVERFLOW);
  		goto err_out;
  	}
40f6b36c6   Kai Makisara   [SCSI] st: add op...
1225
  	(STp->buffer)->cleared = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
  	(STp->buffer)->writing = 0;
  	(STp->buffer)->syscall_result = 0;
  
  	STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
  
  	STp->dirty = 0;
  	for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  		STps = &(STp->ps[i]);
  		STps->rw = ST_IDLE;
  	}
9abe16c67   Kai Makisara   [SCSI] st: fix Ta...
1236
  	STp->try_dio_now = STp->try_dio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1237
1238
  	STp->recover_count = 0;
  	DEB( STp->nbr_waits = STp->nbr_finished = 0;
deee13dfd   Kai Makisara   [SCSI] st: compil...
1239
  	     STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1240
1241
1242
1243
1244
1245
  
  	retval = check_tape(STp, filp);
  	if (retval < 0)
  		goto err_out;
  	if ((filp->f_flags & O_NONBLOCK) == 0 &&
  	    retval != CHKRES_READY) {
413f73272   Kai Makisara   [SCSI] st: Fixup ...
1246
1247
1248
1249
  		if (STp->ready == NO_TAPE)
  			retval = (-ENOMEDIUM);
  		else
  			retval = (-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
1251
1252
1253
1254
1255
  		goto err_out;
  	}
  	return 0;
  
   err_out:
  	normalize_buffer(STp->buffer);
0644f5393   Hannes Reinecke   [SCSI] st: remove...
1256
  	spin_lock(&st_use_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1257
  	STp->in_use = 0;
0644f5393   Hannes Reinecke   [SCSI] st: remove...
1258
  	spin_unlock(&st_use_lock);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
1259
  	scsi_tape_put(STp);
46a243f72   Oliver Neukum   [SCSI] st: implem...
1260
1261
  	if (resumed)
  		scsi_autopm_put_device(STp->device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1262
1263
1264
1265
1266
1267
  	return retval;
  
  }
  
  
  /* Flush the tape buffer before close */
75e1fcc0b   Miklos Szeredi   [PATCH] vfs: add ...
1268
  static int st_flush(struct file *filp, fl_owner_t id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1269
1270
1271
  {
  	int result = 0, result2;
  	unsigned char cmd[MAX_COMMAND_SIZE];
8b05b773b   Mike Christie   [SCSI] convert st...
1272
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
1274
1275
  	struct scsi_tape *STp = filp->private_data;
  	struct st_modedef *STm = &(STp->modes[STp->current_mode]);
  	struct st_partstat *STps = &(STp->ps[STp->partition]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
1277
1278
1279
1280
  
  	if (file_count(filp) > 1)
  		return 0;
  
  	if (STps->rw == ST_WRITING && !STp->pos_unknown) {
8ef8d5941   Adrian Bunk   [SCSI] st: rename...
1281
  		result = st_flush_write_buffer(STp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1282
1283
1284
1285
1286
1287
  		if (result != 0 && result != (-ENOSPC))
  			goto out;
  	}
  
  	if (STp->can_partitions &&
  	    (result2 = switch_partition(STp)) < 0) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1288
1289
  		DEBC_printk(STp, "switch_partition at close failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
1291
1292
1293
1294
1295
  		if (result == 0)
  			result = result2;
  		goto out;
  	}
  
  	DEBC( if (STp->nbr_requests)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1296
1297
1298
1299
1300
  		st_printk(KERN_DEBUG, STp,
  			  "Number of r/w requests %d, dio used in %d, "
  			  "pages %d.
  ", STp->nbr_requests, STp->nbr_dio,
  			  STp->nbr_pages));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1301
1302
1303
  
  	if (STps->rw == ST_WRITING && !STp->pos_unknown) {
  		struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1304
1305
1306
1307
1308
  #if DEBUG
  		DEBC_printk(STp, "Async write waits %d, finished %d.
  ",
  			    STp->nbr_waits, STp->nbr_finished);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1309
1310
  		memset(cmd, 0, MAX_COMMAND_SIZE);
  		cmd[0] = WRITE_FILEMARKS;
c743e44fb   Lee Duncan   [SCSI] st: expand...
1311
1312
  		if (STp->immediate_filemark)
  			cmd[1] = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1313
  		cmd[4] = 1 + STp->two_fm;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
1314
1315
1316
  		SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
  				   STp->device->request_queue->rq_timeout,
  				   MAX_WRITE_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1317
  		if (!SRpnt) {
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
1318
  			result = (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
  			goto out;
  		}
  
  		if (STp->buffer->syscall_result == 0 ||
  		    (cmdstatp->have_sense && !cmdstatp->deferred &&
  		     (cmdstatp->flags & SENSE_EOM) &&
  		     (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  		      cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
  		     (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
  			/* Write successful at EOM */
8b05b773b   Mike Christie   [SCSI] convert st...
1329
  			st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1330
1331
1332
1333
1334
1335
1336
1337
1338
  			SRpnt = NULL;
  			if (STps->drv_file >= 0)
  				STps->drv_file++;
  			STps->drv_block = 0;
  			if (STp->two_fm)
  				cross_eof(STp, 0);
  			STps->eof = ST_FM;
  		}
  		else { /* Write error */
8b05b773b   Mike Christie   [SCSI] convert st...
1339
  			st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1340
  			SRpnt = NULL;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1341
1342
1343
  			st_printk(KERN_ERR, STp,
  				  "Error on write filemark.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344
1345
1346
  			if (result == 0)
  				result = (-EIO);
  		}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1347
1348
  		DEBC_printk(STp, "Buffer flushed, %d EOF(s) written
  ", cmd[4]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
  	} else if (!STp->rew_at_close) {
  		STps = &(STp->ps[STp->partition]);
  		if (!STm->sysv || STps->rw != ST_READING) {
  			if (STp->can_bsr)
  				result = flush_buffer(STp, 0);
  			else if (STps->eof == ST_FM_HIT) {
  				result = cross_eof(STp, 0);
  				if (result) {
  					if (STps->drv_file >= 0)
  						STps->drv_file++;
  					STps->drv_block = 0;
  					STps->eof = ST_FM;
  				} else
  					STps->eof = ST_NOEOF;
  			}
  		} else if ((STps->eof == ST_NOEOF &&
  			    !(result = cross_eof(STp, 1))) ||
  			   STps->eof == ST_FM_HIT) {
  			if (STps->drv_file >= 0)
  				STps->drv_file++;
  			STps->drv_block = 0;
  			STps->eof = ST_FM;
  		}
  	}
  
        out:
  	if (STp->rew_at_close) {
  		result2 = st_int_ioctl(STp, MTREW, 1);
  		if (result == 0)
  			result = result2;
  	}
  	return result;
  }
  
  
  /* Close the device and release it. BKL is not needed: this is the only thread
     accessing this tape. */
  static int st_release(struct inode *inode, struct file *filp)
  {
  	int result = 0;
  	struct scsi_tape *STp = filp->private_data;
  
  	if (STp->door_locked == ST_LOCKED_AUTO)
  		do_door_lock(STp, 0);
  
  	normalize_buffer(STp->buffer);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
1395
  	spin_lock(&st_use_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1396
  	STp->in_use = 0;
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
1397
  	spin_unlock(&st_use_lock);
46a243f72   Oliver Neukum   [SCSI] st: implem...
1398
  	scsi_autopm_put_device(STp->device);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
1399
  	scsi_tape_put(STp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
  
  	return result;
  }
  
  /* The checks common to both reading and writing */
  static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
  {
  	ssize_t retval = 0;
  
  	/*
  	 * If we are in the middle of error recovery, don't let anyone
  	 * else try and use this device.  Also, if error recovery fails, it
  	 * may try and take the device offline, in which case all further
  	 * access to the device is prohibited.
  	 */
  	if (!scsi_block_when_processing_errors(STp->device)) {
  		retval = (-ENXIO);
  		goto out;
  	}
  
  	if (STp->ready != ST_READY) {
  		if (STp->ready == ST_NO_TAPE)
  			retval = (-ENOMEDIUM);
  		else
  			retval = (-EIO);
  		goto out;
  	}
  
  	if (! STp->modes[STp->current_mode].defined) {
  		retval = (-ENXIO);
  		goto out;
  	}
  
  
  	/*
  	 * If there was a bus reset, block further access
  	 * to this device.
  	 */
  	if (STp->pos_unknown) {
  		retval = (-EIO);
  		goto out;
  	}
  
  	if (count == 0)
  		goto out;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1445
  	DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1446
  	if (!STp->in_use) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1447
1448
1449
  		st_printk(ST_DEB_MSG, STp,
  			  "Incorrect device.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
  		retval = (-EIO);
  		goto out;
  	} ) /* end DEB */
  
  	if (STp->can_partitions &&
  	    (retval = switch_partition(STp)) < 0)
  		goto out;
  
  	if (STp->block_size == 0 && STp->max_block > 0 &&
  	    (count < STp->min_block || count > STp->max_block)) {
  		retval = (-EINVAL);
  		goto out;
  	}
  
  	if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
  	    !do_door_lock(STp, 1))
  		STp->door_locked = ST_LOCKED_AUTO;
  
   out:
  	return retval;
  }
  
  
  static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
  			   size_t count, int is_read)
  {
  	int i, bufsize, retval = 0;
  	struct st_buffer *STbp = STp->buffer;
  
  	if (is_read)
9abe16c67   Kai Makisara   [SCSI] st: fix Ta...
1480
  		i = STp->try_dio_now && try_rdio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1481
  	else
9abe16c67   Kai Makisara   [SCSI] st: fix Ta...
1482
  		i = STp->try_dio_now && try_wdio;
8b05b773b   Mike Christie   [SCSI] convert st...
1483

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484
1485
  	if (i && ((unsigned long)buf & queue_dma_alignment(
  					STp->device->request_queue)) == 0) {
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
1486
1487
  		i = sgl_map_user_pages(STbp, STbp->use_sg, (unsigned long)buf,
  				       count, (is_read ? READ : WRITE));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1488
1489
1490
1491
1492
1493
1494
  		if (i > 0) {
  			STbp->do_dio = i;
  			STbp->buffer_bytes = 0;   /* can be used as transfer counter */
  		}
  		else
  			STbp->do_dio = 0;  /* fall back to buffering with any error */
  		STbp->sg_segs = STbp->do_dio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1495
1496
1497
1498
  		DEB(
  		     if (STbp->do_dio) {
  			STp->nbr_dio++;
  			STp->nbr_pages += STbp->do_dio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
  		     }
  		)
  	} else
  		STbp->do_dio = 0;
  	DEB( STp->nbr_requests++; )
  
  	if (!STbp->do_dio) {
  		if (STp->block_size)
  			bufsize = STp->block_size > st_fixed_buffer_size ?
  				STp->block_size : st_fixed_buffer_size;
40f6b36c6   Kai Makisara   [SCSI] st: add op...
1509
  		else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1510
  			bufsize = count;
40f6b36c6   Kai Makisara   [SCSI] st: add op...
1511
1512
1513
1514
1515
  			/* Make sure that data from previous user is not leaked even if
  			   HBA does not return correct residual */
  			if (is_read && STp->sili && !STbp->cleared)
  				clear_buffer(STbp);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1516
1517
  		if (bufsize > STbp->buffer_size &&
  		    !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1518
1519
1520
1521
  			st_printk(KERN_WARNING, STp,
  				  "Can't allocate %d byte tape buffer.
  ",
  				  bufsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
  			retval = (-EOVERFLOW);
  			goto out;
  		}
  		if (STp->block_size)
  			STbp->buffer_blocks = bufsize / STp->block_size;
  	}
  
   out:
  	return retval;
  }
  
  
  /* Can be called more than once after each setup_buffer() */
787926b1b   Kai Makisara   [SCSI] Fix st oop...
1535
  static void release_buffering(struct scsi_tape *STp, int is_read)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1536
1537
1538
1539
1540
  {
  	struct st_buffer *STbp;
  
  	STbp = STp->buffer;
  	if (STbp->do_dio) {
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
1541
  		sgl_unmap_user_pages(STbp, STbp->do_dio, is_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1542
  		STbp->do_dio = 0;
787926b1b   Kai Makisara   [SCSI] Fix st oop...
1543
  		STbp->sg_segs = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
  	}
  }
  
  
  /* Write command */
  static ssize_t
  st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
  {
  	ssize_t total;
  	ssize_t i, do_count, blks, transfer;
  	ssize_t retval;
  	int undone, retry_eot = 0, scode;
  	int async_write;
  	unsigned char cmd[MAX_COMMAND_SIZE];
  	const char __user *b_point;
8b05b773b   Mike Christie   [SCSI] convert st...
1559
  	struct st_request *SRpnt = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1560
1561
1562
1563
  	struct scsi_tape *STp = filp->private_data;
  	struct st_modedef *STm;
  	struct st_partstat *STps;
  	struct st_buffer *STbp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564

28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
1565
  	if (mutex_lock_interruptible(&STp->lock))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1566
1567
1568
1569
1570
1571
1572
1573
  		return -ERESTARTSYS;
  
  	retval = rw_checks(STp, filp, count);
  	if (retval || count == 0)
  		goto out;
  
  	/* Write must be integral number of blocks */
  	if (STp->block_size != 0 && (count % STp->block_size) != 0) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1574
1575
1576
  		st_printk(KERN_WARNING, STp,
  			  "Write not multiple of tape block size.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
  		retval = (-EINVAL);
  		goto out;
  	}
  
  	STm = &(STp->modes[STp->current_mode]);
  	STps = &(STp->ps[STp->partition]);
  
  	if (STp->write_prot) {
  		retval = (-EACCES);
  		goto out;
  	}
  
  
  	if (STps->rw == ST_READING) {
  		retval = flush_buffer(STp, 0);
  		if (retval)
  			goto out;
  		STps->rw = ST_WRITING;
  	} else if (STps->rw != ST_WRITING &&
  		   STps->drv_file == 0 && STps->drv_block == 0) {
  		if ((retval = set_mode_densblk(STp, STm)) < 0)
  			goto out;
  		if (STm->default_compression != ST_DONT_TOUCH &&
  		    !(STp->compression_changed)) {
  			if (st_compression(STp, (STm->default_compression == ST_YES))) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1602
1603
1604
  				st_printk(KERN_WARNING, STp,
  					  "Can't set default compression.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
  				if (modes_defined) {
  					retval = (-EINVAL);
  					goto out;
  				}
  			}
  		}
  	}
  
  	STbp = STp->buffer;
  	i = write_behind_check(STp);
  	if (i) {
  		if (i == -ENOSPC)
  			STps->eof = ST_EOM_OK;
  		else
  			STps->eof = ST_EOM_ERROR;
  	}
  
  	if (STps->eof == ST_EOM_OK) {
  		STps->eof = ST_EOD_1;  /* allow next write */
  		retval = (-ENOSPC);
  		goto out;
  	}
  	else if (STps->eof == ST_EOM_ERROR) {
  		retval = (-EIO);
  		goto out;
  	}
  
  	/* Check the buffer readability in cases where copy_user might catch
  	   the problems after some tape movement. */
  	if (STp->block_size != 0 &&
  	    !STbp->do_dio &&
  	    (copy_from_user(&i, buf, 1) != 0 ||
  	     copy_from_user(&i, buf + count - 1, 1) != 0)) {
  		retval = (-EFAULT);
  		goto out;
  	}
  
  	retval = setup_buffering(STp, buf, count, 0);
  	if (retval)
  		goto out;
  
  	total = count;
  
  	memset(cmd, 0, MAX_COMMAND_SIZE);
  	cmd[0] = WRITE_6;
  	cmd[1] = (STp->block_size != 0);
  
  	STps->rw = ST_WRITING;
  
  	b_point = buf;
  	while (count > 0 && !retry_eot) {
  
  		if (STbp->do_dio) {
  			do_count = count;
  		}
  		else {
  			if (STp->block_size == 0)
  				do_count = count;
  			else {
  				do_count = STbp->buffer_blocks * STp->block_size -
  					STbp->buffer_bytes;
  				if (do_count > count)
  					do_count = count;
  			}
  
  			i = append_to_buffer(b_point, STbp, do_count);
  			if (i) {
  				retval = i;
  				goto out;
  			}
  		}
  		count -= do_count;
  		b_point += do_count;
  
  		async_write = STp->block_size == 0 && !STbp->do_dio &&
  			STm->do_async_writes && STps->eof < ST_EOM_OK;
  
  		if (STp->block_size != 0 && STm->do_buffer_writes &&
9abe16c67   Kai Makisara   [SCSI] st: fix Ta...
1683
  		    !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
  		    STbp->buffer_bytes < STbp->buffer_size) {
  			STp->dirty = 1;
  			/* Don't write a buffer that is not full enough. */
  			if (!async_write && count == 0)
  				break;
  		}
  
  	retry_write:
  		if (STp->block_size == 0)
  			blks = transfer = do_count;
  		else {
  			if (!STbp->do_dio)
  				blks = STbp->buffer_bytes;
  			else
  				blks = do_count;
  			blks /= STp->block_size;
  			transfer = blks * STp->block_size;
  		}
  		cmd[2] = blks >> 16;
  		cmd[3] = blks >> 8;
  		cmd[4] = blks;
  
  		SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
a02488ed7   James Bottomley   [SCSI] st: update...
1707
1708
  				   STp->device->request_queue->rq_timeout,
  				   MAX_WRITE_RETRIES, !async_write);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1709
1710
1711
1712
  		if (!SRpnt) {
  			retval = STbp->syscall_result;
  			goto out;
  		}
8b05b773b   Mike Christie   [SCSI] convert st...
1713
  		if (async_write && !STbp->syscall_result) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
  			STbp->writing = transfer;
  			STp->dirty = !(STbp->writing ==
  				       STbp->buffer_bytes);
  			SRpnt = NULL;  /* Prevent releasing this request! */
  			DEB( STp->write_pending = 1; )
  			break;
  		}
  
  		if (STbp->syscall_result != 0) {
  			struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1724
1725
  			DEBC_printk(STp, "Error on write:
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
  			if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
  				scode = cmdstatp->sense_hdr.sense_key;
  				if (cmdstatp->remainder_valid)
  					undone = (int)cmdstatp->uremainder64;
  				else if (STp->block_size == 0 &&
  					 scode == VOLUME_OVERFLOW)
  					undone = transfer;
  				else
  					undone = 0;
  				if (STp->block_size != 0)
  					undone *= STp->block_size;
  				if (undone <= do_count) {
  					/* Only data from this write is not written */
  					count += undone;
626dcb1ee   Kai Makisara   [SCSI] st: Move b...
1740
  					b_point -= undone;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
  					do_count -= undone;
  					if (STp->block_size)
  						blks = (transfer - undone) / STp->block_size;
  					STps->eof = ST_EOM_OK;
  					/* Continue in fixed block mode if all written
  					   in this request but still something left to write
  					   (retval left to zero)
  					*/
  					if (STp->block_size == 0 ||
  					    undone > 0 || count == 0)
  						retval = (-ENOSPC); /* EOM within current request */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1752
1753
1754
1755
  					DEBC_printk(STp, "EOM with %d "
  						    "bytes unwritten.
  ",
  						    (int)count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
  				} else {
  					/* EOT within data buffered earlier (possible only
  					   in fixed block mode without direct i/o) */
  					if (!retry_eot && !cmdstatp->deferred &&
  					    (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
  						move_buffer_data(STp->buffer, transfer - undone);
  						retry_eot = 1;
  						if (STps->drv_block >= 0) {
  							STps->drv_block += (transfer - undone) /
  								STp->block_size;
  						}
  						STps->eof = ST_EOM_OK;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1768
1769
1770
1771
1772
  						DEBC_printk(STp, "Retry "
  							    "write of %d "
  							    "bytes at EOM.
  ",
  							    STp->buffer->buffer_bytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
  						goto retry_write;
  					}
  					else {
  						/* Either error within data buffered by driver or
  						   failed retry */
  						count -= do_count;
  						blks = do_count = 0;
  						STps->eof = ST_EOM_ERROR;
  						STps->drv_block = (-1); /* Too cautious? */
  						retval = (-EIO);	/* EOM for old data */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1783
1784
1785
  						DEBC_printk(STp, "EOM with "
  							    "lost data.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1786
1787
1788
1789
1790
  					}
  				}
  			} else {
  				count += do_count;
  				STps->drv_block = (-1);		/* Too cautious? */
8b05b773b   Mike Christie   [SCSI] convert st...
1791
  				retval = STbp->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
  			}
  
  		}
  
  		if (STps->drv_block >= 0) {
  			if (STp->block_size == 0)
  				STps->drv_block += (do_count > 0);
  			else
  				STps->drv_block += blks;
  		}
  
  		STbp->buffer_bytes = 0;
  		STp->dirty = 0;
  
  		if (retval || retry_eot) {
  			if (count < total)
  				retval = total - count;
  			goto out;
  		}
  	}
  
  	if (STps->eof == ST_EOD_1)
  		STps->eof = ST_EOM_OK;
  	else if (STps->eof != ST_EOM_OK)
  		STps->eof = ST_NOEOF;
  	retval = total - count;
  
   out:
  	if (SRpnt != NULL)
8b05b773b   Mike Christie   [SCSI] convert st...
1821
  		st_release_request(SRpnt);
787926b1b   Kai Makisara   [SCSI] Fix st oop...
1822
  	release_buffering(STp, 0);
28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
1823
  	mutex_unlock(&STp->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
  
  	return retval;
  }
  
  /* Read data from the tape. Returns zero in the normal case, one if the
     eof status has changed, and the negative error code in case of a
     fatal error. Otherwise updates the buffer and the eof state.
  
     Does release user buffer mapping if it is set.
  */
  static long read_tape(struct scsi_tape *STp, long count,
8b05b773b   Mike Christie   [SCSI] convert st...
1835
  		      struct st_request ** aSRpnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1836
1837
1838
  {
  	int transfer, blks, bytes;
  	unsigned char cmd[MAX_COMMAND_SIZE];
8b05b773b   Mike Christie   [SCSI] convert st...
1839
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1840
1841
1842
1843
  	struct st_modedef *STm;
  	struct st_partstat *STps;
  	struct st_buffer *STbp;
  	int retval = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
  
  	if (count == 0)
  		return 0;
  
  	STm = &(STp->modes[STp->current_mode]);
  	STps = &(STp->ps[STp->partition]);
  	if (STps->eof == ST_FM_HIT)
  		return 1;
  	STbp = STp->buffer;
  
  	if (STp->block_size == 0)
  		blks = bytes = count;
  	else {
9abe16c67   Kai Makisara   [SCSI] st: fix Ta...
1857
  		if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
  			blks = (STp->buffer)->buffer_blocks;
  			bytes = blks * STp->block_size;
  		} else {
  			bytes = count;
  			if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
  				bytes = (STp->buffer)->buffer_size;
  			blks = bytes / STp->block_size;
  			bytes = blks * STp->block_size;
  		}
  	}
  
  	memset(cmd, 0, MAX_COMMAND_SIZE);
  	cmd[0] = READ_6;
  	cmd[1] = (STp->block_size != 0);
40f6b36c6   Kai Makisara   [SCSI] st: add op...
1872
1873
  	if (!cmd[1] && STp->sili)
  		cmd[1] |= 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1874
1875
1876
1877
1878
1879
  	cmd[2] = blks >> 16;
  	cmd[3] = blks >> 8;
  	cmd[4] = blks;
  
  	SRpnt = *aSRpnt;
  	SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
a02488ed7   James Bottomley   [SCSI] st: update...
1880
1881
  			   STp->device->request_queue->rq_timeout,
  			   MAX_RETRIES, 1);
787926b1b   Kai Makisara   [SCSI] Fix st oop...
1882
  	release_buffering(STp, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
  	*aSRpnt = SRpnt;
  	if (!SRpnt)
  		return STbp->syscall_result;
  
  	STbp->read_pointer = 0;
  	STps->at_sm = 0;
  
  	/* Something to check */
  	if (STbp->syscall_result) {
  		struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  
  		retval = 1;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1895
1896
1897
1898
1899
1900
1901
  		DEBC_printk(STp,
  			    "Sense: %2x %2x %2x %2x %2x %2x %2x %2x
  ",
  			    SRpnt->sense[0], SRpnt->sense[1],
  			    SRpnt->sense[2], SRpnt->sense[3],
  			    SRpnt->sense[4], SRpnt->sense[5],
  			    SRpnt->sense[6], SRpnt->sense[7]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
  		if (cmdstatp->have_sense) {
  
  			if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
  				cmdstatp->flags &= 0xcf;	/* No need for EOM in this case */
  
  			if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
  				/* Compute the residual count */
  				if (cmdstatp->remainder_valid)
  					transfer = (int)cmdstatp->uremainder64;
  				else
  					transfer = 0;
  				if (STp->block_size == 0 &&
  				    cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
  					transfer = bytes;
  
  				if (cmdstatp->flags & SENSE_ILI) {	/* ILI */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
  					if (STp->block_size == 0 &&
  					    transfer < 0) {
  						st_printk(KERN_NOTICE, STp,
  							  "Failed to read %d "
  							  "byte block with %d "
  							  "byte transfer.
  ",
  							  bytes - transfer,
  							  bytes);
  						if (STps->drv_block >= 0)
  							STps->drv_block += 1;
  						STbp->buffer_bytes = 0;
  						return (-ENOMEM);
  					} else if (STp->block_size == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1932
1933
  						STbp->buffer_bytes = bytes - transfer;
  					} else {
8b05b773b   Mike Christie   [SCSI] convert st...
1934
  						st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1935
1936
  						SRpnt = *aSRpnt = NULL;
  						if (transfer == blks) {	/* We did not get anything, error */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1937
1938
1939
1940
  							st_printk(KERN_NOTICE, STp,
  								  "Incorrect "
  								  "block size.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1941
1942
1943
1944
1945
1946
1947
1948
  							if (STps->drv_block >= 0)
  								STps->drv_block += blks - transfer + 1;
  							st_int_ioctl(STp, MTBSR, 1);
  							return (-EIO);
  						}
  						/* We have some data, deliver it */
  						STbp->buffer_bytes = (blks - transfer) *
  						    STp->block_size;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1949
1950
1951
1952
1953
1954
  						DEBC_printk(STp, "ILI but "
  							    "enough data "
  							    "received %ld "
  							    "%d.
  ", count,
  							    STbp->buffer_bytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
  						if (STps->drv_block >= 0)
  							STps->drv_block += 1;
  						if (st_int_ioctl(STp, MTBSR, 1))
  							return (-EIO);
  					}
  				} else if (cmdstatp->flags & SENSE_FMK) {	/* FM overrides EOM */
  					if (STps->eof != ST_FM_HIT)
  						STps->eof = ST_FM_HIT;
  					else
  						STps->eof = ST_EOD_2;
  					if (STp->block_size == 0)
  						STbp->buffer_bytes = 0;
  					else
  						STbp->buffer_bytes =
  						    bytes - transfer * STp->block_size;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1970
1971
1972
1973
  					DEBC_printk(STp, "EOF detected (%d "
  						    "bytes read).
  ",
  						    STbp->buffer_bytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
  				} else if (cmdstatp->flags & SENSE_EOM) {
  					if (STps->eof == ST_FM)
  						STps->eof = ST_EOD_1;
  					else
  						STps->eof = ST_EOM_OK;
  					if (STp->block_size == 0)
  						STbp->buffer_bytes = bytes - transfer;
  					else
  						STbp->buffer_bytes =
  						    bytes - transfer * STp->block_size;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1984
1985
1986
1987
  					DEBC_printk(STp, "EOM detected (%d "
  						    "bytes read).
  ",
  						    STbp->buffer_bytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1988
1989
  				}
  			}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1990
  			/* end of EOF, EOM, ILI test */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1991
  			else {	/* nonzero sense key */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1992
1993
  				DEBC_printk(STp, "Tape error while reading.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1994
1995
1996
  				STps->drv_block = (-1);
  				if (STps->eof == ST_FM &&
  				    cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
1997
1998
1999
2000
  					DEBC_printk(STp, "Zero returned for "
  						    "first BLANK CHECK "
  						    "after EOF.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2001
2002
2003
2004
2005
2006
2007
2008
  					STps->eof = ST_EOD_2;	/* First BLANK_CHECK after FM */
  				} else	/* Some other extended sense code */
  					retval = (-EIO);
  			}
  
  			if (STbp->buffer_bytes < 0)  /* Caused by bogus sense data */
  				STbp->buffer_bytes = 0;
  		}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2009
  		/* End of extended sense test */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2010
2011
2012
2013
2014
  		else {		/* Non-extended sense */
  			retval = STbp->syscall_result;
  		}
  
  	}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2015
  	/* End of error handling */
40f6b36c6   Kai Makisara   [SCSI] st: add op...
2016
  	else {			/* Read successful */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2017
  		STbp->buffer_bytes = bytes;
40f6b36c6   Kai Makisara   [SCSI] st: add op...
2018
2019
2020
  		if (STp->sili) /* In fixed block mode residual is always zero here */
  			STbp->buffer_bytes -= STp->buffer->cmdstat.residual;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
  
  	if (STps->drv_block >= 0) {
  		if (STp->block_size == 0)
  			STps->drv_block++;
  		else
  			STps->drv_block += STbp->buffer_bytes / STp->block_size;
  	}
  	return retval;
  }
  
  
  /* Read command */
  static ssize_t
  st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
  {
  	ssize_t total;
  	ssize_t retval = 0;
  	ssize_t i, transfer;
  	int special, do_dio = 0;
8b05b773b   Mike Christie   [SCSI] convert st...
2040
  	struct st_request *SRpnt = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2041
2042
2043
2044
  	struct scsi_tape *STp = filp->private_data;
  	struct st_modedef *STm;
  	struct st_partstat *STps;
  	struct st_buffer *STbp = STp->buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2045

28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
2046
  	if (mutex_lock_interruptible(&STp->lock))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2047
2048
2049
2050
2051
2052
2053
  		return -ERESTARTSYS;
  
  	retval = rw_checks(STp, filp, count);
  	if (retval || count == 0)
  		goto out;
  
  	STm = &(STp->modes[STp->current_mode]);
9abe16c67   Kai Makisara   [SCSI] st: fix Ta...
2054
2055
2056
2057
2058
2059
  	if (STp->block_size != 0 && (count % STp->block_size) != 0) {
  		if (!STm->do_read_ahead) {
  			retval = (-EINVAL);	/* Read must be integral number of blocks */
  			goto out;
  		}
  		STp->try_dio_now = 0;  /* Direct i/o can't handle split blocks */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2060
2061
2062
2063
2064
2065
2066
2067
2068
  	}
  
  	STps = &(STp->ps[STp->partition]);
  	if (STps->rw == ST_WRITING) {
  		retval = flush_buffer(STp, 0);
  		if (retval)
  			goto out;
  		STps->rw = ST_READING;
  	}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2069
  	DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2070
  	if (debugging && STps->eof != ST_NOEOF)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2071
2072
2073
2074
2075
  		st_printk(ST_DEB_MSG, STp,
  			  "EOF/EOM flag up (%d). Bytes %d
  ",
  			  STps->eof, STbp->buffer_bytes);
  	) /* end DEB */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
  
  	retval = setup_buffering(STp, buf, count, 1);
  	if (retval)
  		goto out;
  	do_dio = STbp->do_dio;
  
  	if (STbp->buffer_bytes == 0 &&
  	    STps->eof >= ST_EOD_1) {
  		if (STps->eof < ST_EOD) {
  			STps->eof += 1;
  			retval = 0;
  			goto out;
  		}
  		retval = (-EIO);	/* EOM or Blank Check */
  		goto out;
  	}
  
  	if (do_dio) {
  		/* Check the buffer writability before any tape movement. Don't alter
  		   buffer data. */
  		if (copy_from_user(&i, buf, 1) != 0 ||
  		    copy_to_user(buf, &i, 1) != 0 ||
  		    copy_from_user(&i, buf + count - 1, 1) != 0 ||
  		    copy_to_user(buf + count - 1, &i, 1) != 0) {
  			retval = (-EFAULT);
  			goto out;
  		}
  	}
  
  	STps->rw = ST_READING;
  
  
  	/* Loop until enough data in buffer or a special condition found */
  	for (total = 0, special = 0; total < count && !special;) {
  
  		/* Get new data if the buffer is empty */
  		if (STbp->buffer_bytes == 0) {
  			special = read_tape(STp, count - total, &SRpnt);
  			if (special < 0) {	/* No need to continue read */
  				retval = special;
  				goto out;
  			}
  		}
  
  		/* Move the data from driver buffer to user buffer */
  		if (STbp->buffer_bytes > 0) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2122
  			DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2123
  			if (debugging && STps->eof != ST_NOEOF)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2124
2125
2126
2127
2128
2129
  				st_printk(ST_DEB_MSG, STp,
  					  "EOF up (%d). Left %d, needed %d.
  ",
  					  STps->eof, STbp->buffer_bytes,
  					  (int)(count - total));
  			) /* end DEB */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
  			transfer = STbp->buffer_bytes < count - total ?
  			    STbp->buffer_bytes : count - total;
  			if (!do_dio) {
  				i = from_buffer(STbp, buf, transfer);
  				if (i) {
  					retval = i;
  					goto out;
  				}
  			}
  			buf += transfer;
  			total += transfer;
  		}
  
  		if (STp->block_size == 0)
  			break;	/* Read only one variable length block */
  
  	}			/* for (total = 0, special = 0;
                                     total < count && !special; ) */
  
  	/* Change the eof state if no data from tape or buffer */
  	if (total == 0) {
  		if (STps->eof == ST_FM_HIT) {
  			STps->eof = ST_FM;
  			STps->drv_block = 0;
  			if (STps->drv_file >= 0)
  				STps->drv_file++;
  		} else if (STps->eof == ST_EOD_1) {
  			STps->eof = ST_EOD_2;
  			STps->drv_block = 0;
  			if (STps->drv_file >= 0)
  				STps->drv_file++;
  		} else if (STps->eof == ST_EOD_2)
  			STps->eof = ST_EOD;
  	} else if (STps->eof == ST_FM)
  		STps->eof = ST_NOEOF;
  	retval = total;
  
   out:
  	if (SRpnt != NULL) {
8b05b773b   Mike Christie   [SCSI] convert st...
2169
  		st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2170
2171
2172
  		SRpnt = NULL;
  	}
  	if (do_dio) {
787926b1b   Kai Makisara   [SCSI] Fix st oop...
2173
  		release_buffering(STp, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2174
2175
  		STbp->buffer_bytes = 0;
  	}
28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
2176
  	mutex_unlock(&STp->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2177
2178
2179
2180
2181
2182
2183
2184
  
  	return retval;
  }
  
  
  
  DEB(
  /* Set the driver options */
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2185
  static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2186
2187
  {
  	if (debugging) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
  		st_printk(KERN_INFO, STp,
  			  "Mode %d options: buffer writes: %d, "
  			  "async writes: %d, read ahead: %d
  ",
  			  STp->current_mode, STm->do_buffer_writes,
  			  STm->do_async_writes, STm->do_read_ahead);
  		st_printk(KERN_INFO, STp,
  			  "    can bsr: %d, two FMs: %d, "
  			  "fast mteom: %d, auto lock: %d,
  ",
  			  STp->can_bsr, STp->two_fm, STp->fast_mteom,
  			  STp->do_auto_lock);
  		st_printk(KERN_INFO, STp,
  			  "    defs for wr: %d, no block limits: %d, "
  			  "partitions: %d, s2 log: %d
  ",
  			  STm->defaults_for_writes, STp->omit_blklims,
  			  STp->can_partitions, STp->scsi2_logical);
  		st_printk(KERN_INFO, STp,
  			  "    sysv: %d nowait: %d sili: %d "
  			  "nowait_filemark: %d
  ",
  			  STm->sysv, STp->immediate, STp->sili,
  			  STp->immediate_filemark);
  		st_printk(KERN_INFO, STp, "    debugging: %d
  ", debugging);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
  	}
  }
  	)
  
  
  static int st_set_options(struct scsi_tape *STp, long options)
  {
  	int value;
  	long code;
  	struct st_modedef *STm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2224
  	struct cdev *cd0, *cd1;
d6216c473   Maurizio Lombardi   [SCSI] st: fix co...
2225
  	struct device *d0, *d1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2226
2227
2228
  
  	STm = &(STp->modes[STp->current_mode]);
  	if (!STm->defined) {
d6216c473   Maurizio Lombardi   [SCSI] st: fix co...
2229
2230
2231
2232
  		cd0 = STm->cdevs[0];
  		cd1 = STm->cdevs[1];
  		d0  = STm->devs[0];
  		d1  = STm->devs[1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2233
  		memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
d6216c473   Maurizio Lombardi   [SCSI] st: fix co...
2234
2235
2236
2237
  		STm->cdevs[0] = cd0;
  		STm->cdevs[1] = cd1;
  		STm->devs[0]  = d0;
  		STm->devs[1]  = d1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2238
  		modes_defined = 1;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2239
2240
2241
  		DEBC_printk(STp, "Initialized mode %d definition from mode 0
  ",
  			    STp->current_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
  	}
  
  	code = options & MT_ST_OPTIONS;
  	if (code == MT_ST_BOOLEANS) {
  		STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
  		STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
  		STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
  		STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
  		STp->two_fm = (options & MT_ST_TWO_FM) != 0;
  		STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
  		STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
  		STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
  		STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
  		if ((STp->device)->scsi_level >= SCSI_2)
  			STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
  		STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
  		STp->immediate = (options & MT_ST_NOWAIT) != 0;
c743e44fb   Lee Duncan   [SCSI] st: expand...
2259
  		STp->immediate_filemark = (options & MT_ST_NOWAIT_EOF) != 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2260
  		STm->sysv = (options & MT_ST_SYSV) != 0;
40f6b36c6   Kai Makisara   [SCSI] st: add op...
2261
  		STp->sili = (options & MT_ST_SILI) != 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2262
  		DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2263
  		     st_log_options(STp, STm); )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
  	} else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
  		value = (code == MT_ST_SETBOOLEANS);
  		if ((options & MT_ST_BUFFER_WRITES) != 0)
  			STm->do_buffer_writes = value;
  		if ((options & MT_ST_ASYNC_WRITES) != 0)
  			STm->do_async_writes = value;
  		if ((options & MT_ST_DEF_WRITES) != 0)
  			STm->defaults_for_writes = value;
  		if ((options & MT_ST_READ_AHEAD) != 0)
  			STm->do_read_ahead = value;
  		if ((options & MT_ST_TWO_FM) != 0)
  			STp->two_fm = value;
  		if ((options & MT_ST_FAST_MTEOM) != 0)
  			STp->fast_mteom = value;
  		if ((options & MT_ST_AUTO_LOCK) != 0)
  			STp->do_auto_lock = value;
  		if ((options & MT_ST_CAN_BSR) != 0)
  			STp->can_bsr = value;
  		if ((options & MT_ST_NO_BLKLIMS) != 0)
  			STp->omit_blklims = value;
  		if ((STp->device)->scsi_level >= SCSI_2 &&
  		    (options & MT_ST_CAN_PARTITIONS) != 0)
  			STp->can_partitions = value;
  		if ((options & MT_ST_SCSI2LOGICAL) != 0)
  			STp->scsi2_logical = value;
  		if ((options & MT_ST_NOWAIT) != 0)
  			STp->immediate = value;
c743e44fb   Lee Duncan   [SCSI] st: expand...
2291
2292
  		if ((options & MT_ST_NOWAIT_EOF) != 0)
  			STp->immediate_filemark = value;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2293
2294
  		if ((options & MT_ST_SYSV) != 0)
  			STm->sysv = value;
40f6b36c6   Kai Makisara   [SCSI] st: add op...
2295
2296
  		if ((options & MT_ST_SILI) != 0)
  			STp->sili = value;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2297
  		DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2298
2299
  		if ((options & MT_ST_DEBUGGING) != 0)
  			debugging = value;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2300
  			st_log_options(STp, STm); )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2301
2302
2303
2304
2305
2306
  	} else if (code == MT_ST_WRITE_THRESHOLD) {
  		/* Retained for compatibility */
  	} else if (code == MT_ST_DEF_BLKSIZE) {
  		value = (options & ~MT_ST_OPTIONS);
  		if (value == ~MT_ST_OPTIONS) {
  			STm->default_blksize = (-1);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2307
2308
  			DEBC_printk(STp, "Default block size disabled.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2309
2310
  		} else {
  			STm->default_blksize = value;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2311
2312
2313
  			DEBC_printk(STp,"Default block size set to "
  				    "%d bytes.
  ", STm->default_blksize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2314
2315
2316
2317
2318
2319
2320
2321
2322
  			if (STp->ready == ST_READY) {
  				STp->blksize_changed = 0;
  				set_mode_densblk(STp, STm);
  			}
  		}
  	} else if (code == MT_ST_TIMEOUTS) {
  		value = (options & ~MT_ST_OPTIONS);
  		if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
  			STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2323
2324
2325
  			DEBC_printk(STp, "Long timeout set to %d seconds.
  ",
  				    (value & ~MT_ST_SET_LONG_TIMEOUT));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2326
  		} else {
a02488ed7   James Bottomley   [SCSI] st: update...
2327
2328
  			blk_queue_rq_timeout(STp->device->request_queue,
  					     value * HZ);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2329
2330
2331
  			DEBC_printk(STp, "Normal timeout set to %d seconds.
  ",
  				    value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2332
2333
2334
2335
  		}
  	} else if (code == MT_ST_SET_CLN) {
  		value = (options & ~MT_ST_OPTIONS) & 0xff;
  		if (value != 0 &&
832151f45   Roel Kluin   [SCSI] st: fix te...
2336
2337
  			(value < EXTENDED_SENSE_START ||
  				value >= SCSI_SENSE_BUFFERSIZE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2338
2339
2340
2341
  			return (-EINVAL);
  		STp->cln_mode = value;
  		STp->cln_sense_mask = (options >> 8) & 0xff;
  		STp->cln_sense_value = (options >> 16) & 0xff;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2342
2343
2344
2345
  		st_printk(KERN_INFO, STp,
  			  "Cleaning request mode %d, mask %02x, value %02x
  ",
  			  value, STp->cln_sense_mask, STp->cln_sense_value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2346
2347
2348
2349
2350
2351
  	} else if (code == MT_ST_DEF_OPTIONS) {
  		code = (options & ~MT_ST_CLEAR_DEFAULT);
  		value = (options & MT_ST_CLEAR_DEFAULT);
  		if (code == MT_ST_DEF_DENSITY) {
  			if (value == MT_ST_CLEAR_DEFAULT) {
  				STm->default_density = (-1);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2352
2353
2354
  				DEBC_printk(STp,
  					    "Density default disabled.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2355
2356
  			} else {
  				STm->default_density = value & 0xff;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2357
2358
2359
  				DEBC_printk(STp, "Density default set to %x
  ",
  					    STm->default_density);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2360
2361
2362
2363
2364
2365
2366
2367
  				if (STp->ready == ST_READY) {
  					STp->density_changed = 0;
  					set_mode_densblk(STp, STm);
  				}
  			}
  		} else if (code == MT_ST_DEF_DRVBUFFER) {
  			if (value == MT_ST_CLEAR_DEFAULT) {
  				STp->default_drvbuffer = 0xff;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2368
2369
2370
  				DEBC_printk(STp,
  					    "Drive buffer default disabled.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2371
2372
  			} else {
  				STp->default_drvbuffer = value & 7;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2373
2374
2375
2376
  				DEBC_printk(STp,
  					    "Drive buffer default set to %x
  ",
  					    STp->default_drvbuffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2377
2378
2379
2380
2381
2382
  				if (STp->ready == ST_READY)
  					st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
  			}
  		} else if (code == MT_ST_DEF_COMPRESSION) {
  			if (value == MT_ST_CLEAR_DEFAULT) {
  				STm->default_compression = ST_DONT_TOUCH;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2383
2384
2385
  				DEBC_printk(STp,
  					    "Compression default disabled.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2386
2387
2388
  			} else {
  				if ((value & 0xff00) != 0) {
  					STp->c_algo = (value & 0xff00) >> 8;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2389
2390
2391
2392
  					DEBC_printk(STp, "Compression "
  						    "algorithm set to 0x%x.
  ",
  						    STp->c_algo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2393
2394
2395
  				}
  				if ((value & 0xff) != 0xff) {
  					STm->default_compression = (value & 1 ? ST_YES : ST_NO);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2396
2397
2398
2399
  					DEBC_printk(STp, "Compression default "
  						    "set to %x
  ",
  						    (value & 1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
  					if (STp->ready == ST_READY) {
  						STp->compression_changed = 0;
  						st_compression(STp, (STm->default_compression == ST_YES));
  					}
  				}
  			}
  		}
  	} else
  		return (-EIO);
  
  	return 0;
  }
  
  #define MODE_HEADER_LENGTH  4
  
  /* Mode header and page byte offsets */
  #define MH_OFF_DATA_LENGTH     0
  #define MH_OFF_MEDIUM_TYPE     1
  #define MH_OFF_DEV_SPECIFIC    2
  #define MH_OFF_BDESCS_LENGTH   3
  #define MP_OFF_PAGE_NBR        0
  #define MP_OFF_PAGE_LENGTH     1
  
  /* Mode header and page bit masks */
  #define MH_BIT_WP              0x80
  #define MP_MSK_PAGE_NBR        0x3f
  
  /* Don't return block descriptors */
  #define MODE_SENSE_OMIT_BDESCS 0x08
  
  #define MODE_SELECT_PAGE_FORMAT 0x10
  
  /* Read a mode page into the tape buffer. The block descriptors are included
     if incl_block_descs is true. The page control is ored to the page number
     parameter, if necessary. */
  static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
  {
  	unsigned char cmd[MAX_COMMAND_SIZE];
8ecf0d994   FUJITA Tomonori   [SCSI] st: conver...
2438
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2439
2440
2441
2442
2443
2444
2445
  
  	memset(cmd, 0, MAX_COMMAND_SIZE);
  	cmd[0] = MODE_SENSE;
  	if (omit_block_descs)
  		cmd[1] = MODE_SENSE_OMIT_BDESCS;
  	cmd[2] = page;
  	cmd[4] = 255;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2446
2447
2448
2449
  	SRpnt = st_do_scsi(NULL, STp, cmd, cmd[4], DMA_FROM_DEVICE,
  			   STp->device->request_queue->rq_timeout, 0, 1);
  	if (SRpnt == NULL)
  		return (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2450

8b05b773b   Mike Christie   [SCSI] convert st...
2451
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2452

02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2453
  	return STp->buffer->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2454
2455
2456
2457
2458
2459
2460
  }
  
  
  /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
     in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
  static int write_mode_page(struct scsi_tape *STp, int page, int slow)
  {
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2461
  	int pgo;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2462
  	unsigned char cmd[MAX_COMMAND_SIZE];
18c870157   FUJITA Tomonori   [SCSI] st: conver...
2463
  	struct st_request *SRpnt;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2464
  	int timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
  
  	memset(cmd, 0, MAX_COMMAND_SIZE);
  	cmd[0] = MODE_SELECT;
  	cmd[1] = MODE_SELECT_PAGE_FORMAT;
  	pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
  	cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
  
  	/* Clear reserved fields */
  	(STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
  	(STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
  	(STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
  	(STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2477
2478
2479
2480
2481
2482
  	timeout = slow ?
  		STp->long_timeout : STp->device->request_queue->rq_timeout;
  	SRpnt = st_do_scsi(NULL, STp, cmd, cmd[4], DMA_TO_DEVICE,
  			   timeout, 0, 1);
  	if (SRpnt == NULL)
  		return (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2483

8b05b773b   Mike Christie   [SCSI] convert st...
2484
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2485

02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2486
  	return STp->buffer->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
  }
  
  
  #define COMPRESSION_PAGE        0x0f
  #define COMPRESSION_PAGE_LENGTH 16
  
  #define CP_OFF_DCE_DCC          2
  #define CP_OFF_C_ALGO           7
  
  #define DCE_MASK  0x80
  #define DCC_MASK  0x40
  #define RED_MASK  0x60
  
  
  /* Control the compression with mode page 15. Algorithm not changed if zero.
  
     The block descriptors are read and written because Sony SDT-7000 does not
     work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
     Including block descriptors should not cause any harm to other drives. */
  
  static int st_compression(struct scsi_tape * STp, int state)
  {
  	int retval;
  	int mpoffs;  /* Offset to mode page start */
  	unsigned char *b_data = (STp->buffer)->b_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2512
2513
2514
2515
2516
2517
2518
  
  	if (STp->ready != ST_READY)
  		return (-EIO);
  
  	/* Read the current page contents */
  	retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
  	if (retval) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2519
2520
  		DEBC_printk(STp, "Compression mode page not supported.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2521
2522
2523
2524
  		return (-EIO);
  	}
  
  	mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2525
2526
2527
  	DEBC_printk(STp, "Compression state is %d.
  ",
  		    (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2528
2529
2530
  
  	/* Check if compression can be changed */
  	if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2531
2532
  		DEBC_printk(STp, "Compression not supported.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
  		return (-EIO);
  	}
  
  	/* Do the change */
  	if (state) {
  		b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
  		if (STp->c_algo != 0)
  			b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
  	}
  	else {
  		b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
  		if (STp->c_algo != 0)
  			b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
  	}
  
  	retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
  	if (retval) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2550
2551
  		DEBC_printk(STp, "Compression change failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2552
2553
  		return (-EIO);
  	}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2554
2555
  	DEBC_printk(STp, "Compression state changed to %d.
  ", state);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
  
  	STp->compression_changed = 1;
  	return 0;
  }
  
  
  /* Process the load and unload commands (does unload if the load code is zero) */
  static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
  {
  	int retval = (-EIO), timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2566
2567
  	unsigned char cmd[MAX_COMMAND_SIZE];
  	struct st_partstat *STps;
8b05b773b   Mike Christie   [SCSI] convert st...
2568
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
  
  	if (STp->ready != ST_READY && !load_code) {
  		if (STp->ready == ST_NO_TAPE)
  			return (-ENOMEDIUM);
  		else
  			return (-EIO);
  	}
  
  	memset(cmd, 0, MAX_COMMAND_SIZE);
  	cmd[0] = START_STOP;
  	if (load_code)
  		cmd[4] |= 1;
  	/*
  	 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
  	 */
  	if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
  	    && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2586
2587
2588
2589
  		DEBC_printk(STp, " Enhanced %sload slot %2d.
  ",
  			    (cmd[4]) ? "" : "un",
  			    load_code - MT_ST_HPLOADER_OFFSET);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2590
2591
2592
2593
  		cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
  	}
  	if (STp->immediate) {
  		cmd[1] = 1;	/* Don't wait for completion */
a02488ed7   James Bottomley   [SCSI] st: update...
2594
  		timeout = STp->device->request_queue->rq_timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2595
2596
2597
2598
2599
2600
  	}
  	else
  		timeout = STp->long_timeout;
  
  	DEBC(
  		if (!load_code)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2601
2602
  			st_printk(ST_DEB_MSG, STp, "Unloading tape.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2603
  		else
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2604
2605
  			st_printk(ST_DEB_MSG, STp, "Loading tape.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2606
  		);
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2607
2608
  	SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
  			   timeout, MAX_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2609
  	if (!SRpnt)
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2610
  		return (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2611
2612
  
  	retval = (STp->buffer)->syscall_result;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2613
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
  
  	if (!retval) {	/* SCSI command successful */
  
  		if (!load_code) {
  			STp->rew_at_close = 0;
  			STp->ready = ST_NO_TAPE;
  		}
  		else {
  			STp->rew_at_close = STp->autorew_dev;
  			retval = check_tape(STp, filp);
  			if (retval > 0)
  				retval = 0;
  		}
  	}
  	else {
  		STps = &(STp->ps[STp->partition]);
  		STps->drv_file = STps->drv_block = (-1);
  	}
  
  	return retval;
  }
  
  #if DEBUG
  #define ST_DEB_FORWARD  0
  #define ST_DEB_BACKWARD 1
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2639
  static void deb_space_print(struct scsi_tape *STp, int direction, char *units, unsigned char *cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2640
2641
  {
  	s32 sc;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2642
2643
  	if (!debugging)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2644
2645
2646
2647
  	sc = cmd[2] & 0x80 ? 0xff000000 : 0;
  	sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
  	if (direction)
  		sc = -sc;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2648
2649
2650
  	st_printk(ST_DEB_MSG, STp, "Spacing tape %s over %d %s.
  ",
  		  direction ? "backward" : "forward", sc, units);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2651
  }
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2652
2653
2654
2655
  #else
  #define ST_DEB_FORWARD  0
  #define ST_DEB_BACKWARD 1
  static void deb_space_print(struct scsi_tape *STp, int direction, char *units, unsigned char *cmd) {}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
  #endif
  
  
  /* Internal ioctl function */
  static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
  {
  	int timeout;
  	long ltmp;
  	int ioctl_result;
  	int chg_eof = 1;
  	unsigned char cmd[MAX_COMMAND_SIZE];
8b05b773b   Mike Christie   [SCSI] convert st...
2667
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2668
2669
2670
  	struct st_partstat *STps;
  	int fileno, blkno, at_sm, undone;
  	int datalen = 0, direction = DMA_NONE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
  
  	WARN_ON(STp->buffer->do_dio != 0);
  	if (STp->ready != ST_READY) {
  		if (STp->ready == ST_NO_TAPE)
  			return (-ENOMEDIUM);
  		else
  			return (-EIO);
  	}
  	timeout = STp->long_timeout;
  	STps = &(STp->ps[STp->partition]);
  	fileno = STps->drv_file;
  	blkno = STps->drv_block;
  	at_sm = STps->at_sm;
  
  	memset(cmd, 0, MAX_COMMAND_SIZE);
  	switch (cmd_in) {
  	case MTFSFM:
  		chg_eof = 0;	/* Changed from the FSF after this */
  	case MTFSF:
  		cmd[0] = SPACE;
  		cmd[1] = 0x01;	/* Space FileMarks */
  		cmd[2] = (arg >> 16);
  		cmd[3] = (arg >> 8);
  		cmd[4] = arg;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2695
  		deb_space_print(STp, ST_DEB_FORWARD, "filemarks", cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
  		if (fileno >= 0)
  			fileno += arg;
  		blkno = 0;
  		at_sm &= (arg == 0);
  		break;
  	case MTBSFM:
  		chg_eof = 0;	/* Changed from the FSF after this */
  	case MTBSF:
  		cmd[0] = SPACE;
  		cmd[1] = 0x01;	/* Space FileMarks */
  		ltmp = (-arg);
  		cmd[2] = (ltmp >> 16);
  		cmd[3] = (ltmp >> 8);
  		cmd[4] = ltmp;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2710
  		deb_space_print(STp, ST_DEB_BACKWARD, "filemarks", cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
  		if (fileno >= 0)
  			fileno -= arg;
  		blkno = (-1);	/* We can't know the block number */
  		at_sm &= (arg == 0);
  		break;
  	case MTFSR:
  		cmd[0] = SPACE;
  		cmd[1] = 0x00;	/* Space Blocks */
  		cmd[2] = (arg >> 16);
  		cmd[3] = (arg >> 8);
  		cmd[4] = arg;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2722
  		deb_space_print(STp, ST_DEB_FORWARD, "blocks", cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
  		if (blkno >= 0)
  			blkno += arg;
  		at_sm &= (arg == 0);
  		break;
  	case MTBSR:
  		cmd[0] = SPACE;
  		cmd[1] = 0x00;	/* Space Blocks */
  		ltmp = (-arg);
  		cmd[2] = (ltmp >> 16);
  		cmd[3] = (ltmp >> 8);
  		cmd[4] = ltmp;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2734
  		deb_space_print(STp, ST_DEB_BACKWARD, "blocks", cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
  		if (blkno >= 0)
  			blkno -= arg;
  		at_sm &= (arg == 0);
  		break;
  	case MTFSS:
  		cmd[0] = SPACE;
  		cmd[1] = 0x04;	/* Space Setmarks */
  		cmd[2] = (arg >> 16);
  		cmd[3] = (arg >> 8);
  		cmd[4] = arg;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2745
  		deb_space_print(STp, ST_DEB_FORWARD, "setmarks", cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
  		if (arg != 0) {
  			blkno = fileno = (-1);
  			at_sm = 1;
  		}
  		break;
  	case MTBSS:
  		cmd[0] = SPACE;
  		cmd[1] = 0x04;	/* Space Setmarks */
  		ltmp = (-arg);
  		cmd[2] = (ltmp >> 16);
  		cmd[3] = (ltmp >> 8);
  		cmd[4] = ltmp;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2758
  		deb_space_print(STp, ST_DEB_BACKWARD, "setmarks", cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2759
2760
2761
2762
2763
2764
  		if (arg != 0) {
  			blkno = fileno = (-1);
  			at_sm = 1;
  		}
  		break;
  	case MTWEOF:
3e51d3c92   Kai Makisara   [SCSI] st: add MT...
2765
  	case MTWEOFI:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2766
2767
2768
2769
2770
2771
  	case MTWSM:
  		if (STp->write_prot)
  			return (-EACCES);
  		cmd[0] = WRITE_FILEMARKS;
  		if (cmd_in == MTWSM)
  			cmd[1] = 2;
c743e44fb   Lee Duncan   [SCSI] st: expand...
2772
2773
  		if (cmd_in == MTWEOFI ||
  		    (cmd_in == MTWEOF && STp->immediate_filemark))
3e51d3c92   Kai Makisara   [SCSI] st: add MT...
2774
  			cmd[1] |= 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2775
2776
2777
  		cmd[2] = (arg >> 16);
  		cmd[3] = (arg >> 8);
  		cmd[4] = arg;
a02488ed7   James Bottomley   [SCSI] st: update...
2778
  		timeout = STp->device->request_queue->rq_timeout;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
  		DEBC(
  			if (cmd_in != MTWSM)
  				st_printk(ST_DEB_MSG, STp,
  					  "Writing %d filemarks.
  ",
  					  cmd[2] * 65536 +
  					  cmd[3] * 256 +
  					  cmd[4]);
  			else
  				st_printk(ST_DEB_MSG, STp,
  					  "Writing %d setmarks.
  ",
  					  cmd[2] * 65536 +
  					  cmd[3] * 256 +
  					  cmd[4]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
  		)
  		if (fileno >= 0)
  			fileno += arg;
  		blkno = 0;
  		at_sm = (cmd_in == MTWSM);
  		break;
  	case MTREW:
  		cmd[0] = REZERO_UNIT;
  		if (STp->immediate) {
  			cmd[1] = 1;	/* Don't wait for completion */
a02488ed7   James Bottomley   [SCSI] st: update...
2804
  			timeout = STp->device->request_queue->rq_timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2805
  		}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2806
2807
  		DEBC_printk(STp, "Rewinding tape.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2808
2809
2810
  		fileno = blkno = at_sm = 0;
  		break;
  	case MTNOP:
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2811
2812
  		DEBC_printk(STp, "No op on tape.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2813
2814
2815
2816
2817
2818
  		return 0;	/* Should do something ? */
  		break;
  	case MTRETEN:
  		cmd[0] = START_STOP;
  		if (STp->immediate) {
  			cmd[1] = 1;	/* Don't wait for completion */
a02488ed7   James Bottomley   [SCSI] st: update...
2819
  			timeout = STp->device->request_queue->rq_timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2820
2821
  		}
  		cmd[4] = 3;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2822
2823
  		DEBC_printk(STp, "Retensioning tape.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
  		fileno = blkno = at_sm = 0;
  		break;
  	case MTEOM:
  		if (!STp->fast_mteom) {
  			/* space to the end of tape */
  			ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
  			fileno = STps->drv_file;
  			if (STps->eof >= ST_EOD_1)
  				return 0;
  			/* The next lines would hide the number of spaced FileMarks
  			   That's why I inserted the previous lines. I had no luck
  			   with detecting EOM with FSF, so we go now to EOM.
  			   Joerg Weule */
  		} else
  			fileno = (-1);
  		cmd[0] = SPACE;
  		cmd[1] = 3;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2841
2842
  		DEBC_printk(STp, "Spacing to end of recorded medium.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
  		blkno = -1;
  		at_sm = 0;
  		break;
  	case MTERASE:
  		if (STp->write_prot)
  			return (-EACCES);
  		cmd[0] = ERASE;
  		cmd[1] = (arg ? 1 : 0);	/* Long erase with non-zero argument */
  		if (STp->immediate) {
  			cmd[1] |= 2;	/* Don't wait for completion */
a02488ed7   James Bottomley   [SCSI] st: update...
2853
  			timeout = STp->device->request_queue->rq_timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2854
2855
2856
  		}
  		else
  			timeout = STp->long_timeout * 8;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2857
2858
  		DEBC_printk(STp, "Erasing tape.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
  		fileno = blkno = at_sm = 0;
  		break;
  	case MTSETBLK:		/* Set block length */
  	case MTSETDENSITY:	/* Set tape density */
  	case MTSETDRVBUFFER:	/* Set drive buffering */
  	case SET_DENS_AND_BLK:	/* Set density and block size */
  		chg_eof = 0;
  		if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
  			return (-EIO);	/* Not allowed if data in buffer */
  		if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
  		    (arg & MT_ST_BLKSIZE_MASK) != 0 &&
  		    STp->max_block > 0 &&
  		    ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
  		     (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2873
2874
  			st_printk(KERN_WARNING, STp, "Illegal block size.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
  			return (-EINVAL);
  		}
  		cmd[0] = MODE_SELECT;
  		if ((STp->use_pf & USE_PF))
  			cmd[1] = MODE_SELECT_PAGE_FORMAT;
  		cmd[4] = datalen = 12;
  		direction = DMA_TO_DEVICE;
  
  		memset((STp->buffer)->b_data, 0, 12);
  		if (cmd_in == MTSETDRVBUFFER)
  			(STp->buffer)->b_data[2] = (arg & 7) << 4;
  		else
  			(STp->buffer)->b_data[2] =
  			    STp->drv_buffer << 4;
  		(STp->buffer)->b_data[3] = 8;	/* block descriptor length */
  		if (cmd_in == MTSETDENSITY) {
  			(STp->buffer)->b_data[4] = arg;
  			STp->density_changed = 1;	/* At least we tried ;-) */
  		} else if (cmd_in == SET_DENS_AND_BLK)
  			(STp->buffer)->b_data[4] = arg >> 24;
  		else
  			(STp->buffer)->b_data[4] = STp->density;
  		if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
  			ltmp = arg & MT_ST_BLKSIZE_MASK;
  			if (cmd_in == MTSETBLK)
  				STp->blksize_changed = 1; /* At least we tried ;-) */
  		} else
  			ltmp = STp->block_size;
  		(STp->buffer)->b_data[9] = (ltmp >> 16);
  		(STp->buffer)->b_data[10] = (ltmp >> 8);
  		(STp->buffer)->b_data[11] = ltmp;
a02488ed7   James Bottomley   [SCSI] st: update...
2906
  		timeout = STp->device->request_queue->rq_timeout;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2907
  		DEBC(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2908
  			if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2909
2910
2911
2912
2913
2914
  				st_printk(ST_DEB_MSG, STp,
  					  "Setting block size to %d bytes.
  ",
  					  (STp->buffer)->b_data[9] * 65536 +
  					  (STp->buffer)->b_data[10] * 256 +
  					  (STp->buffer)->b_data[11]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2915
  			if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2916
2917
2918
2919
  				st_printk(ST_DEB_MSG, STp,
  					  "Setting density code to %x.
  ",
  					  (STp->buffer)->b_data[4]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2920
  			if (cmd_in == MTSETDRVBUFFER)
b30d8bca5   Hannes Reinecke   scsi: Implement s...
2921
2922
2923
2924
  				st_printk(ST_DEB_MSG, STp,
  					  "Setting drive buffer code to %d.
  ",
  					  ((STp->buffer)->b_data[2] >> 4) & 7);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2925
2926
2927
2928
2929
  		)
  		break;
  	default:
  		return (-ENOSYS);
  	}
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2930
2931
  	SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
  			   timeout, MAX_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2932
2933
  	if (!SRpnt)
  		return (STp->buffer)->syscall_result;
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
2934
  	ioctl_result = (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2935
2936
  
  	if (!ioctl_result) {	/* SCSI command successful */
8b05b773b   Mike Christie   [SCSI] convert st...
2937
  		st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
  		SRpnt = NULL;
  		STps->drv_block = blkno;
  		STps->drv_file = fileno;
  		STps->at_sm = at_sm;
  
  		if (cmd_in == MTBSFM)
  			ioctl_result = st_int_ioctl(STp, MTFSF, 1);
  		else if (cmd_in == MTFSFM)
  			ioctl_result = st_int_ioctl(STp, MTBSF, 1);
  
  		if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2949
2950
  			STp->block_size = arg & MT_ST_BLKSIZE_MASK;
  			if (STp->block_size != 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
  				(STp->buffer)->buffer_blocks =
  				    (STp->buffer)->buffer_size / STp->block_size;
  			}
  			(STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
  			if (cmd_in == SET_DENS_AND_BLK)
  				STp->density = arg >> MT_ST_DENSITY_SHIFT;
  		} else if (cmd_in == MTSETDRVBUFFER)
  			STp->drv_buffer = (arg & 7);
  		else if (cmd_in == MTSETDENSITY)
  			STp->density = arg;
  
  		if (cmd_in == MTEOM)
  			STps->eof = ST_EOD;
  		else if (cmd_in == MTFSF)
  			STps->eof = ST_FM;
  		else if (chg_eof)
  			STps->eof = ST_NOEOF;
3e51d3c92   Kai Makisara   [SCSI] st: add MT...
2968
2969
  		if (cmd_in == MTWEOF || cmd_in == MTWEOFI)
  			STps->rw = ST_IDLE;  /* prevent automatic WEOF at close */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
  	} else { /* SCSI command was not completely successful. Don't return
                      from this block without releasing the SCSI command block! */
  		struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
  
  		if (cmdstatp->flags & SENSE_EOM) {
  			if (cmd_in != MTBSF && cmd_in != MTBSFM &&
  			    cmd_in != MTBSR && cmd_in != MTBSS)
  				STps->eof = ST_EOM_OK;
  			STps->drv_block = 0;
  		}
  
  		if (cmdstatp->remainder_valid)
  			undone = (int)cmdstatp->uremainder64;
  		else
  			undone = 0;
3e51d3c92   Kai Makisara   [SCSI] st: add MT...
2985
  		if ((cmd_in == MTWEOF || cmd_in == MTWEOFI) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2986
  		    cmdstatp->have_sense &&
91614c054   Kai Makisara   [SCSI] st: A MTIO...
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
  		    (cmdstatp->flags & SENSE_EOM)) {
  			if (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
  			    cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) {
  				ioctl_result = 0;	/* EOF(s) written successfully at EOM */
  				STps->eof = ST_NOEOF;
  			} else {  /* Writing EOF(s) failed */
  				if (fileno >= 0)
  					fileno -= undone;
  				if (undone < arg)
  					STps->eof = ST_NOEOF;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2998
  			STps->drv_file = fileno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
  		} else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
  			if (fileno >= 0)
  				STps->drv_file = fileno - undone;
  			else
  				STps->drv_file = fileno;
  			STps->drv_block = -1;
  			STps->eof = ST_NOEOF;
  		} else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
  			if (arg > 0 && undone < 0)  /* Some drives get this wrong */
  				undone = (-undone);
  			if (STps->drv_file >= 0)
  				STps->drv_file = fileno + undone;
  			STps->drv_block = 0;
  			STps->eof = ST_NOEOF;
  		} else if (cmd_in == MTFSR) {
  			if (cmdstatp->flags & SENSE_FMK) {	/* Hit filemark */
  				if (STps->drv_file >= 0)
  					STps->drv_file++;
  				STps->drv_block = 0;
  				STps->eof = ST_FM;
  			} else {
  				if (blkno >= undone)
  					STps->drv_block = blkno - undone;
  				else
  					STps->drv_block = (-1);
  				STps->eof = ST_NOEOF;
  			}
  		} else if (cmd_in == MTBSR) {
  			if (cmdstatp->flags & SENSE_FMK) {	/* Hit filemark */
  				STps->drv_file--;
  				STps->drv_block = (-1);
  			} else {
  				if (arg > 0 && undone < 0)  /* Some drives get this wrong */
  					undone = (-undone);
  				if (STps->drv_block >= 0)
  					STps->drv_block = blkno + undone;
  			}
  			STps->eof = ST_NOEOF;
  		} else if (cmd_in == MTEOM) {
  			STps->drv_file = (-1);
  			STps->drv_block = (-1);
  			STps->eof = ST_EOD;
  		} else if (cmd_in == MTSETBLK ||
  			   cmd_in == MTSETDENSITY ||
  			   cmd_in == MTSETDRVBUFFER ||
  			   cmd_in == SET_DENS_AND_BLK) {
  			if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
  			    !(STp->use_pf & PF_TESTED)) {
  				/* Try the other possible state of Page Format if not
  				   already tried */
1da2019ff   Kai Makisara   [SCSI] st: fix gc...
3049
  				STp->use_pf = (STp->use_pf ^ USE_PF) | PF_TESTED;
8b05b773b   Mike Christie   [SCSI] convert st...
3050
  				st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3051
3052
3053
3054
3055
3056
3057
3058
  				SRpnt = NULL;
  				return st_int_ioctl(STp, cmd_in, arg);
  			}
  		} else if (chg_eof)
  			STps->eof = ST_NOEOF;
  
  		if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
  			STps->eof = ST_EOD;
8b05b773b   Mike Christie   [SCSI] convert st...
3059
  		st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
  		SRpnt = NULL;
  	}
  
  	return ioctl_result;
  }
  
  
  /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
     structure. */
  
  static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
  			int logical)
  {
  	int result;
  	unsigned char scmd[MAX_COMMAND_SIZE];
8b05b773b   Mike Christie   [SCSI] convert st...
3075
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
  
  	if (STp->ready != ST_READY)
  		return (-EIO);
  
  	memset(scmd, 0, MAX_COMMAND_SIZE);
  	if ((STp->device)->scsi_level < SCSI_2) {
  		scmd[0] = QFA_REQUEST_BLOCK;
  		scmd[4] = 3;
  	} else {
  		scmd[0] = READ_POSITION;
  		if (!logical && !STp->scsi2_logical)
  			scmd[1] = 1;
  	}
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
3089
3090
3091
  	SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
  			   STp->device->request_queue->rq_timeout,
  			   MAX_READY_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3092
  	if (!SRpnt)
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
3093
  		return (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3094
3095
3096
3097
3098
  
  	if ((STp->buffer)->syscall_result != 0 ||
  	    (STp->device->scsi_level >= SCSI_2 &&
  	     ((STp->buffer)->b_data[0] & 4) != 0)) {
  		*block = *partition = 0;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3099
3100
  		DEBC_printk(STp, " Can't read tape position.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
  		result = (-EIO);
  	} else {
  		result = 0;
  		if ((STp->device)->scsi_level < SCSI_2) {
  			*block = ((STp->buffer)->b_data[0] << 16)
  			    + ((STp->buffer)->b_data[1] << 8)
  			    + (STp->buffer)->b_data[2];
  			*partition = 0;
  		} else {
  			*block = ((STp->buffer)->b_data[4] << 24)
  			    + ((STp->buffer)->b_data[5] << 16)
  			    + ((STp->buffer)->b_data[6] << 8)
  			    + (STp->buffer)->b_data[7];
  			*partition = (STp->buffer)->b_data[1];
  			if (((STp->buffer)->b_data[0] & 0x80) &&
  			    (STp->buffer)->b_data[1] == 0)	/* BOP of partition 0 */
  				STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
  		}
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3119
3120
3121
  		DEBC_printk(STp, "Got tape pos. blk %d part %d.
  ",
  			    *block, *partition);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3122
  	}
8b05b773b   Mike Christie   [SCSI] convert st...
3123
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
  	SRpnt = NULL;
  
  	return result;
  }
  
  
  /* Set the tape block and partition. Negative partition means that only the
     block should be set in vendor specific way. */
  static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
  			int logical)
  {
  	struct st_partstat *STps;
  	int result, p;
  	unsigned int blk;
  	int timeout;
  	unsigned char scmd[MAX_COMMAND_SIZE];
8b05b773b   Mike Christie   [SCSI] convert st...
3140
  	struct st_request *SRpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3141
3142
3143
3144
3145
  
  	if (STp->ready != ST_READY)
  		return (-EIO);
  	timeout = STp->long_timeout;
  	STps = &(STp->ps[STp->partition]);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3146
3147
3148
  	DEBC_printk(STp, "Setting block to %d and partition to %d.
  ",
  		    block, partition);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
  	DEB(if (partition < 0)
  		return (-EIO); )
  
  	/* Update the location at the partition we are leaving */
  	if ((!STp->can_partitions && partition != 0) ||
  	    partition >= ST_NBR_PARTITIONS)
  		return (-EINVAL);
  	if (partition != STp->partition) {
  		if (get_location(STp, &blk, &p, 1))
  			STps->last_block_valid = 0;
  		else {
  			STps->last_block_valid = 1;
  			STps->last_block_visited = blk;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3162
3163
3164
3165
  			DEBC_printk(STp, "Visited block %d for "
  				    "partition %d saved.
  ",
  				    blk, STp->partition);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
  		}
  	}
  
  	memset(scmd, 0, MAX_COMMAND_SIZE);
  	if ((STp->device)->scsi_level < SCSI_2) {
  		scmd[0] = QFA_SEEK_BLOCK;
  		scmd[2] = (block >> 16);
  		scmd[3] = (block >> 8);
  		scmd[4] = block;
  		scmd[5] = 0;
  	} else {
  		scmd[0] = SEEK_10;
  		scmd[3] = (block >> 24);
  		scmd[4] = (block >> 16);
  		scmd[5] = (block >> 8);
  		scmd[6] = block;
  		if (!logical && !STp->scsi2_logical)
  			scmd[1] = 4;
  		if (STp->partition != partition) {
  			scmd[1] |= 2;
  			scmd[8] = partition;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3187
3188
3189
3190
  			DEBC_printk(STp, "Trying to change partition "
  				    "from %d to %d
  ", STp->partition,
  				    partition);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3191
3192
3193
3194
  		}
  	}
  	if (STp->immediate) {
  		scmd[1] |= 1;		/* Don't wait for completion */
a02488ed7   James Bottomley   [SCSI] st: update...
3195
  		timeout = STp->device->request_queue->rq_timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3196
  	}
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
3197
3198
  	SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
  			   timeout, MAX_READY_RETRIES, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3199
  	if (!SRpnt)
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
3200
  		return (STp->buffer)->syscall_result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
  
  	STps->drv_block = STps->drv_file = (-1);
  	STps->eof = ST_NOEOF;
  	if ((STp->buffer)->syscall_result != 0) {
  		result = (-EIO);
  		if (STp->can_partitions &&
  		    (STp->device)->scsi_level >= SCSI_2 &&
  		    (p = find_partition(STp)) >= 0)
  			STp->partition = p;
  	} else {
  		if (STp->can_partitions) {
  			STp->partition = partition;
  			STps = &(STp->ps[partition]);
  			if (!STps->last_block_valid ||
  			    STps->last_block_visited != block) {
  				STps->at_sm = 0;
  				STps->rw = ST_IDLE;
  			}
  		} else
  			STps->at_sm = 0;
  		if (block == 0)
  			STps->drv_block = STps->drv_file = 0;
  		result = 0;
  	}
02ae2c0e8   Kai Makisara   [SCSI] st: integr...
3225

8b05b773b   Mike Christie   [SCSI] convert st...
3226
  	st_release_request(SRpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
  	SRpnt = NULL;
  
  	return result;
  }
  
  
  /* Find the current partition number for the drive status. Called from open and
     returns either partition number of negative error code. */
  static int find_partition(struct scsi_tape *STp)
  {
  	int i, partition;
  	unsigned int block;
  
  	if ((i = get_location(STp, &block, &partition, 1)) < 0)
  		return i;
  	if (partition >= ST_NBR_PARTITIONS)
  		return (-EIO);
  	return partition;
  }
  
  
  /* Change the partition if necessary */
  static int switch_partition(struct scsi_tape *STp)
  {
  	struct st_partstat *STps;
  
  	if (STp->partition == STp->new_partition)
  		return 0;
  	STps = &(STp->ps[STp->new_partition]);
  	if (!STps->last_block_valid)
  		STps->last_block_visited = 0;
  	return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
  }
  
  /* Functions for reading and writing the medium partition mode page. */
  
  #define PART_PAGE   0x11
  #define PART_PAGE_FIXED_LENGTH 8
  
  #define PP_OFF_MAX_ADD_PARTS   2
  #define PP_OFF_NBR_ADD_PARTS   3
  #define PP_OFF_FLAGS           4
  #define PP_OFF_PART_UNITS      6
  #define PP_OFF_RESERVED        7
  
  #define PP_BIT_IDP             0x20
  #define PP_MSK_PSUM_MB         0x10
  
  /* Get the number of partitions on the tape. As a side effect reads the
     mode page into the tape buffer. */
  static int nbr_partitions(struct scsi_tape *STp)
  {
  	int result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3280
3281
3282
3283
3284
3285
3286
  
  	if (STp->ready != ST_READY)
  		return (-EIO);
  
  	result = read_mode_page(STp, PART_PAGE, 1);
  
  	if (result) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3287
3288
  		DEBC_printk(STp, "Can't read medium partition page.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3289
3290
3291
3292
  		result = (-EIO);
  	} else {
  		result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
  					      PP_OFF_NBR_ADD_PARTS] + 1;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3293
3294
  		DEBC_printk(STp, "Number of partitions %d.
  ", result);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
  	}
  
  	return result;
  }
  
  
  /* Partition the tape into two partitions if size > 0 or one partition if
     size == 0.
  
     The block descriptors are read and written because Sony SDT-7000 does not
     work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
  
     My HP C1533A drive returns only one partition size field. This is used to
     set the size of partition 1. There is no size field for the default partition.
     Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
     used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
     The following algorithm is used to accommodate both drives: if the number of
     partition size fields is greater than the maximum number of additional partitions
     in the mode page, the second field is used. Otherwise the first field is used.
  
     For Seagate DDS drives the page length must be 8 when no partitions is defined
     and 10 when 1 partition is defined (information from Eric Lee Green). This is
     is acceptable also to some other old drives and enforced if the first partition
     size field is used for the first additional partition size.
   */
  static int partition_tape(struct scsi_tape *STp, int size)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3322
3323
3324
3325
3326
3327
  	int result;
  	int pgo, psd_cnt, psdo;
  	unsigned char *bp;
  
  	result = read_mode_page(STp, PART_PAGE, 0);
  	if (result) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3328
3329
  		DEBC_printk(STp, "Can't read partition mode page.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3330
3331
3332
3333
3334
  		return result;
  	}
  	/* The mode page is in the buffer. Let's modify it and write it. */
  	bp = (STp->buffer)->b_data;
  	pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3335
3336
3337
  	DEBC_printk(STp, "Partition page length is %d bytes.
  ",
  		    bp[pgo + MP_OFF_PAGE_LENGTH] + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3338
3339
3340
3341
3342
3343
3344
3345
  
  	psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
  	psdo = pgo + PART_PAGE_FIXED_LENGTH;
  	if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
  		bp[psdo] = bp[psdo + 1] = 0xff;  /* Rest of the tape */
  		psdo += 2;
  	}
  	memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3346
3347
  	DEBC_printk(STp, "psd_cnt %d, max.parts %d, nbr_parts %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3348
  		    psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3349
  		    bp[pgo + PP_OFF_NBR_ADD_PARTS]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3350
3351
3352
3353
3354
  
  	if (size <= 0) {
  		bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
  		if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
  		    bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3355
3356
  		DEBC_printk(STp, "Formatting tape with one partition.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3357
3358
3359
3360
3361
3362
  	} else {
  		bp[psdo] = (size >> 8) & 0xff;
  		bp[psdo + 1] = size & 0xff;
  		bp[pgo + 3] = 1;
  		if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
  		    bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3363
3364
3365
  		DEBC_printk(STp, "Formatting tape with two partitions "
  			    "(1 = %d MB).
  ", size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3366
3367
3368
3369
3370
3371
3372
  	}
  	bp[pgo + PP_OFF_PART_UNITS] = 0;
  	bp[pgo + PP_OFF_RESERVED] = 0;
  	bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
  
  	result = write_mode_page(STp, PART_PAGE, 1);
  	if (result) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3373
3374
  		st_printk(KERN_INFO, STp, "Partitioning of tape failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3375
3376
3377
3378
3379
3380
3381
3382
3383
  		result = (-EIO);
  	}
  
  	return result;
  }
  
  
  
  /* The ioctl command */
fd66c1b4e   Kai Makisara   [SCSI] st: conver...
3384
  static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3385
3386
3387
3388
3389
3390
3391
  {
  	int i, cmd_nr, cmd_type, bt;
  	int retval = 0;
  	unsigned int blk;
  	struct scsi_tape *STp = file->private_data;
  	struct st_modedef *STm;
  	struct st_partstat *STps;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3392
  	void __user *p = (void __user *)arg;
28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
3393
  	if (mutex_lock_interruptible(&STp->lock))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3394
  		return -ERESTARTSYS;
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3395
  	DEB(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3396
  	if (debugging && !STp->in_use) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3397
3398
  		st_printk(ST_DEB_MSG, STp, "Incorrect device.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
  		retval = (-EIO);
  		goto out;
  	} ) /* end DEB */
  
  	STm = &(STp->modes[STp->current_mode]);
  	STps = &(STp->ps[STp->partition]);
  
  	/*
  	 * If we are in the middle of error recovery, don't let anyone
  	 * else try and use this device.  Also, if error recovery fails, it
  	 * may try and take the device offline, in which case all further
  	 * access to the device is prohibited.
  	 */
906d15fbd   Christoph Hellwig   scsi: split scsi_...
3412
3413
3414
  	retval = scsi_ioctl_block_when_processing_errors(STp->device, cmd_in,
  			file->f_flags & O_NDELAY);
  	if (retval)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3415
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
  
  	cmd_type = _IOC_TYPE(cmd_in);
  	cmd_nr = _IOC_NR(cmd_in);
  
  	if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
  		struct mtop mtc;
  
  		if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
  			retval = (-EINVAL);
  			goto out;
  		}
  
  		i = copy_from_user(&mtc, p, sizeof(struct mtop));
  		if (i) {
  			retval = (-EFAULT);
  			goto out;
  		}
  
  		if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
3435
3436
3437
  			st_printk(KERN_WARNING, STp,
  				  "MTSETDRVBUFFER only allowed for root.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
  			retval = (-EPERM);
  			goto out;
  		}
  		if (!STm->defined &&
  		    (mtc.mt_op != MTSETDRVBUFFER &&
  		     (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
  			retval = (-ENXIO);
  			goto out;
  		}
  
  		if (!STp->pos_unknown) {
  
  			if (STps->eof == ST_FM_HIT) {
  				if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
                                      mtc.mt_op == MTEOM) {
  					mtc.mt_count -= 1;
  					if (STps->drv_file >= 0)
  						STps->drv_file += 1;
  				} else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
  					mtc.mt_count += 1;
  					if (STps->drv_file >= 0)
  						STps->drv_file += 1;
  				}
  			}
  
  			if (mtc.mt_op == MTSEEK) {
  				/* Old position must be restored if partition will be
                                     changed */
  				i = !STp->can_partitions ||
  				    (STp->new_partition != STp->partition);
  			} else {
  				i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
  				    mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
  				    mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
  				    mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
  				    mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
  				    mtc.mt_op == MTCOMPRESSION;
  			}
  			i = flush_buffer(STp, i);
  			if (i < 0) {
  				retval = i;
  				goto out;
  			}
  			if (STps->rw == ST_WRITING &&
  			    (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
  			     mtc.mt_op == MTSEEK ||
  			     mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
  				i = st_int_ioctl(STp, MTWEOF, 1);
  				if (i < 0) {
  					retval = i;
  					goto out;
  				}
  				if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
  					mtc.mt_count++;
  				STps->rw = ST_IDLE;
  			     }
  
  		} else {
  			/*
  			 * If there was a bus reset, block further access
  			 * to this device.  If the user wants to rewind the tape,
  			 * then reset the flag and allow access again.
  			 */
  			if (mtc.mt_op != MTREW &&
  			    mtc.mt_op != MTOFFL &&
  			    mtc.mt_op != MTRETEN &&
  			    mtc.mt_op != MTERASE &&
  			    mtc.mt_op != MTSEEK &&
  			    mtc.mt_op != MTEOM) {
  				retval = (-EIO);
  				goto out;
  			}
  			reset_state(STp);
  			/* remove this when the midlevel properly clears was_reset */
  			STp->device->was_reset = 0;
  		}
  
  		if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
  		    mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
  		    mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
  			STps->rw = ST_IDLE;	/* Prevent automatic WEOF and fsf */
  
  		if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
  			do_door_lock(STp, 0);	/* Ignore result! */
  
  		if (mtc.mt_op == MTSETDRVBUFFER &&
  		    (mtc.mt_count & MT_ST_OPTIONS) != 0) {
  			retval = st_set_options(STp, mtc.mt_count);
  			goto out;
  		}
  
  		if (mtc.mt_op == MTSETPART) {
  			if (!STp->can_partitions ||
  			    mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
  				retval = (-EINVAL);
  				goto out;
  			}
  			if (mtc.mt_count >= STp->nbr_partitions &&
  			    (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
  				retval = (-EIO);
  				goto out;
  			}
  			if (mtc.mt_count >= STp->nbr_partitions) {
  				retval = (-EINVAL);
  				goto out;
  			}
  			STp->new_partition = mtc.mt_count;
  			retval = 0;
  			goto out;
  		}
  
  		if (mtc.mt_op == MTMKPART) {
  			if (!STp->can_partitions) {
  				retval = (-EINVAL);
  				goto out;
  			}
  			if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
  			    (i = partition_tape(STp, mtc.mt_count)) < 0) {
  				retval = i;
  				goto out;
  			}
  			for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  				STp->ps[i].rw = ST_IDLE;
  				STp->ps[i].at_sm = 0;
  				STp->ps[i].last_block_valid = 0;
  			}
  			STp->partition = STp->new_partition = 0;
  			STp->nbr_partitions = 1;	/* Bad guess ?-) */
  			STps->drv_block = STps->drv_file = 0;
  			retval = 0;
  			goto out;
  		}
  
  		if (mtc.mt_op == MTSEEK) {
  			i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
  			if (!STp->can_partitions)
  				STp->ps[0].rw = ST_IDLE;
  			retval = i;
  			goto out;
  		}
  
  		if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
  			retval = do_load_unload(STp, file, 0);
  			goto out;
  		}
  
  		if (mtc.mt_op == MTLOAD) {
  			retval = do_load_unload(STp, file, max(1, mtc.mt_count));
  			goto out;
  		}
  
  		if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
  			retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
  			goto out;
  		}
  
  		if (STp->can_partitions && STp->ready == ST_READY &&
  		    (i = switch_partition(STp)) < 0) {
  			retval = i;
  			goto out;
  		}
  
  		if (mtc.mt_op == MTCOMPRESSION)
  			retval = st_compression(STp, (mtc.mt_count & 1));
  		else
  			retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
  		goto out;
  	}
  	if (!STm->defined) {
  		retval = (-ENXIO);
  		goto out;
  	}
  
  	if ((i = flush_buffer(STp, 0)) < 0) {
  		retval = i;
  		goto out;
  	}
  	if (STp->can_partitions &&
  	    (i = switch_partition(STp)) < 0) {
  		retval = i;
  		goto out;
  	}
  
  	if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
  		struct mtget mt_status;
  
  		if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
  			 retval = (-EINVAL);
  			 goto out;
  		}
  
  		mt_status.mt_type = STp->tape_type;
  		mt_status.mt_dsreg =
  		    ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
  		    ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
  		mt_status.mt_blkno = STps->drv_block;
  		mt_status.mt_fileno = STps->drv_file;
  		if (STp->block_size != 0) {
  			if (STps->rw == ST_WRITING)
  				mt_status.mt_blkno +=
  				    (STp->buffer)->buffer_bytes / STp->block_size;
  			else if (STps->rw == ST_READING)
  				mt_status.mt_blkno -=
                                          ((STp->buffer)->buffer_bytes +
                                           STp->block_size - 1) / STp->block_size;
  		}
  
  		mt_status.mt_gstat = 0;
  		if (STp->drv_write_prot)
  			mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
  		if (mt_status.mt_blkno == 0) {
  			if (mt_status.mt_fileno == 0)
  				mt_status.mt_gstat |= GMT_BOT(0xffffffff);
  			else
  				mt_status.mt_gstat |= GMT_EOF(0xffffffff);
  		}
  		mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
  		mt_status.mt_resid = STp->partition;
  		if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
  			mt_status.mt_gstat |= GMT_EOT(0xffffffff);
  		else if (STps->eof >= ST_EOM_OK)
  			mt_status.mt_gstat |= GMT_EOD(0xffffffff);
  		if (STp->density == 1)
  			mt_status.mt_gstat |= GMT_D_800(0xffffffff);
  		else if (STp->density == 2)
  			mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
  		else if (STp->density == 3)
  			mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
  		if (STp->ready == ST_READY)
  			mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
  		if (STp->ready == ST_NO_TAPE)
  			mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
  		if (STps->at_sm)
  			mt_status.mt_gstat |= GMT_SM(0xffffffff);
  		if (STm->do_async_writes ||
                      (STm->do_buffer_writes && STp->block_size != 0) ||
  		    STp->drv_buffer != 0)
  			mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
  		if (STp->cleaning_req)
  			mt_status.mt_gstat |= GMT_CLN(0xffffffff);
  
  		i = copy_to_user(p, &mt_status, sizeof(struct mtget));
  		if (i) {
  			retval = (-EFAULT);
  			goto out;
  		}
  
  		STp->recover_reg = 0;		/* Clear after read */
  		retval = 0;
  		goto out;
  	}			/* End of MTIOCGET */
  	if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
  		struct mtpos mt_pos;
  		if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
  			 retval = (-EINVAL);
  			 goto out;
  		}
  		if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
  			retval = i;
  			goto out;
  		}
  		mt_pos.mt_blkno = blk;
  		i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
  		if (i)
  			retval = (-EFAULT);
  		goto out;
  	}
28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
3705
  	mutex_unlock(&STp->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3706
3707
3708
3709
3710
  	switch (cmd_in) {
  		case SCSI_IOCTL_GET_IDLUN:
  		case SCSI_IOCTL_GET_BUS_NUMBER:
  			break;
  		default:
16c4b3e20   Kai Makisara   [SCSI] SCSI tape:...
3711
3712
3713
3714
  			if ((cmd_in == SG_IO ||
  			     cmd_in == SCSI_IOCTL_SEND_COMMAND ||
  			     cmd_in == CDROM_SEND_PACKET) &&
  			    !capable(CAP_SYS_RAWIO))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3715
3716
  				i = -EPERM;
  			else
74f3c8aff   Al Viro   [PATCH] switch sc...
3717
3718
  				i = scsi_cmd_ioctl(STp->disk->queue, STp->disk,
  						   file->f_mode, cmd_in, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3719
3720
3721
3722
  			if (i != -ENOTTY)
  				return i;
  			break;
  	}
16c4b3e20   Kai Makisara   [SCSI] SCSI tape:...
3723
3724
3725
3726
3727
3728
  	retval = scsi_ioctl(STp->device, cmd_in, p);
  	if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
  		STp->rew_at_close = 0;
  		STp->ready = ST_NO_TAPE;
  	}
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3729
3730
  
   out:
28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
3731
  	mutex_unlock(&STp->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
  	return retval;
  }
  
  #ifdef CONFIG_COMPAT
  static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  {
  	struct scsi_tape *STp = file->private_data;
  	struct scsi_device *sdev = STp->device;
  	int ret = -ENOIOCTLCMD;
  	if (sdev->host->hostt->compat_ioctl) { 
  
  		ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  
  	}
  	return ret;
  }
  #endif
  
  
  
  /* Try to allocate a new tape buffer. Calling function must not hold
     dev_arr_lock. */
f409d6cc6   FUJITA Tomonori   [SCSI] st: simpli...
3754
  static struct st_buffer *new_tape_buffer(int need_dma, int max_sg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3755
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3756
  	struct st_buffer *tb;
f409d6cc6   FUJITA Tomonori   [SCSI] st: simpli...
3757
  	tb = kzalloc(sizeof(struct st_buffer), GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3758
3759
3760
3761
3762
  	if (!tb) {
  		printk(KERN_NOTICE "st: Can't allocate new tape buffer.
  ");
  		return NULL;
  	}
1ac63cf5c   FUJITA Tomonori   [SCSI] st: remove...
3763
  	tb->frp_segs = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3764
  	tb->use_sg = max_sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3765
  	tb->dma = need_dma;
f409d6cc6   FUJITA Tomonori   [SCSI] st: simpli...
3766
  	tb->buffer_size = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3767

f409d6cc6   FUJITA Tomonori   [SCSI] st: simpli...
3768
3769
  	tb->reserved_pages = kzalloc(max_sg * sizeof(struct page *),
  				     GFP_ATOMIC);
d0e1ae31b   FUJITA Tomonori   [SCSI] st: add st...
3770
3771
3772
3773
  	if (!tb->reserved_pages) {
  		kfree(tb);
  		return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3774
3775
3776
3777
3778
  	return tb;
  }
  
  
  /* Try to allocate enough space in the tape buffer */
8f78fc5eb   Kai Makisara   [SCSI] st: retry ...
3779
  #define ST_MAX_ORDER 6
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3780
3781
  static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
  {
769989a4a   Bodo Stroesser   [SCSI] st: fix en...
3782
  	int segs, max_segs, b_size, order, got;
c53033f6b   Al Viro   [PATCH] gfp_t: dr...
3783
  	gfp_t priority;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3784
3785
3786
3787
3788
3789
3790
3791
  
  	if (new_size <= STbuffer->buffer_size)
  		return 1;
  
  	if (STbuffer->buffer_size <= PAGE_SIZE)
  		normalize_buffer(STbuffer);  /* Avoid extra segment */
  
  	max_segs = STbuffer->use_sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3792
3793
3794
3795
  
  	priority = GFP_KERNEL | __GFP_NOWARN;
  	if (need_dma)
  		priority |= GFP_DMA;
9c905966c   FUJITA Tomonori   [SCSI] st: make a...
3796

08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3797
3798
  	if (STbuffer->cleared)
  		priority |= __GFP_ZERO;
9c905966c   FUJITA Tomonori   [SCSI] st: make a...
3799
  	if (STbuffer->frp_segs) {
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3800
  		order = STbuffer->reserved_page_order;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3801
  		b_size = PAGE_SIZE << order;
9c905966c   FUJITA Tomonori   [SCSI] st: make a...
3802
3803
  	} else {
  		for (b_size = PAGE_SIZE, order = 0;
46081b166   FUJITA Tomonori   [SCSI] st: Increa...
3804
3805
  		     order < ST_MAX_ORDER &&
  			     max_segs * (PAGE_SIZE << order) < new_size;
8f78fc5eb   Kai Makisara   [SCSI] st: retry ...
3806
  		     order++, b_size *= 2)
9c905966c   FUJITA Tomonori   [SCSI] st: make a...
3807
  			;  /* empty */
373daacfc   Kai Makisara   [SCSI] st: Store ...
3808
  		STbuffer->reserved_page_order = order;
9c905966c   FUJITA Tomonori   [SCSI] st: make a...
3809
  	}
8f78fc5eb   Kai Makisara   [SCSI] st: retry ...
3810
3811
3812
3813
3814
3815
  	if (max_segs * (PAGE_SIZE << order) < new_size) {
  		if (order == ST_MAX_ORDER)
  			return 0;
  		normalize_buffer(STbuffer);
  		return enlarge_buffer(STbuffer, new_size, need_dma);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3816
3817
3818
  
  	for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
  	     segs < max_segs && got < new_size;) {
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3819
3820
3821
3822
  		struct page *page;
  
  		page = alloc_pages(priority, order);
  		if (!page) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3823
3824
3825
3826
  			DEB(STbuffer->buffer_size = got);
  			normalize_buffer(STbuffer);
  			return 0;
  		}
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3827

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3828
3829
3830
  		STbuffer->frp_segs += 1;
  		got += b_size;
  		STbuffer->buffer_size = got;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3831
  		STbuffer->reserved_pages[segs] = page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3832
3833
  		segs++;
  	}
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3834
  	STbuffer->b_data = page_address(STbuffer->reserved_pages[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3835
3836
3837
  
  	return 1;
  }
40f6b36c6   Kai Makisara   [SCSI] st: add op...
3838
3839
3840
3841
3842
3843
  /* Make sure that no data from previous user is in the internal buffer */
  static void clear_buffer(struct st_buffer * st_bp)
  {
  	int i;
  
  	for (i=0; i < st_bp->frp_segs; i++)
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3844
  		memset(page_address(st_bp->reserved_pages[i]), 0,
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3845
  		       PAGE_SIZE << st_bp->reserved_page_order);
40f6b36c6   Kai Makisara   [SCSI] st: add op...
3846
3847
  	st_bp->cleared = 1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3848
3849
3850
  /* Release the extra buffer */
  static void normalize_buffer(struct st_buffer * STbuffer)
  {
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3851
  	int i, order = STbuffer->reserved_page_order;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3852

1ac63cf5c   FUJITA Tomonori   [SCSI] st: remove...
3853
  	for (i = 0; i < STbuffer->frp_segs; i++) {
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3854
3855
  		__free_pages(STbuffer->reserved_pages[i], order);
  		STbuffer->buffer_size -= (PAGE_SIZE << order);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3856
  	}
1ac63cf5c   FUJITA Tomonori   [SCSI] st: remove...
3857
  	STbuffer->frp_segs = 0;
8b05b773b   Mike Christie   [SCSI] convert st...
3858
  	STbuffer->sg_segs = 0;
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3859
  	STbuffer->reserved_page_order = 0;
d0e1ae31b   FUJITA Tomonori   [SCSI] st: add st...
3860
  	STbuffer->map_data.offset = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3861
3862
3863
3864
3865
3866
3867
3868
  }
  
  
  /* Move data from the user buffer to the tape buffer. Returns zero (success) or
     negative error code. */
  static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
  {
  	int i, cnt, res, offset;
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3869
  	int length = PAGE_SIZE << st_bp->reserved_page_order;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3870
3871
  
  	for (i = 0, offset = st_bp->buffer_bytes;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3872
3873
  	     i < st_bp->frp_segs && offset >= length; i++)
  		offset -= length;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3874
3875
3876
3877
3878
3879
  	if (i == st_bp->frp_segs) {	/* Should never happen */
  		printk(KERN_WARNING "st: append_to_buffer offset overflow.
  ");
  		return (-EIO);
  	}
  	for (; i < st_bp->frp_segs && do_count > 0; i++) {
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3880
3881
3882
  		struct page *page = st_bp->reserved_pages[i];
  		cnt = length - offset < do_count ? length - offset : do_count;
  		res = copy_from_user(page_address(page) + offset, ubp, cnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
  		if (res)
  			return (-EFAULT);
  		do_count -= cnt;
  		st_bp->buffer_bytes += cnt;
  		ubp += cnt;
  		offset = 0;
  	}
  	if (do_count) /* Should never happen */
  		return (-EIO);
  
  	return 0;
  }
  
  
  /* Move data from the tape buffer to the user buffer. Returns zero (success) or
     negative error code. */
  static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
  {
  	int i, cnt, res, offset;
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3902
  	int length = PAGE_SIZE << st_bp->reserved_page_order;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3903
3904
  
  	for (i = 0, offset = st_bp->read_pointer;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3905
3906
  	     i < st_bp->frp_segs && offset >= length; i++)
  		offset -= length;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3907
3908
3909
3910
3911
3912
  	if (i == st_bp->frp_segs) {	/* Should never happen */
  		printk(KERN_WARNING "st: from_buffer offset overflow.
  ");
  		return (-EIO);
  	}
  	for (; i < st_bp->frp_segs && do_count > 0; i++) {
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3913
3914
3915
  		struct page *page = st_bp->reserved_pages[i];
  		cnt = length - offset < do_count ? length - offset : do_count;
  		res = copy_to_user(ubp, page_address(page) + offset, cnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
  		if (res)
  			return (-EFAULT);
  		do_count -= cnt;
  		st_bp->buffer_bytes -= cnt;
  		st_bp->read_pointer += cnt;
  		ubp += cnt;
  		offset = 0;
  	}
  	if (do_count) /* Should never happen */
  		return (-EIO);
  
  	return 0;
  }
  
  
  /* Move data towards start of buffer */
  static void move_buffer_data(struct st_buffer * st_bp, int offset)
  {
  	int src_seg, dst_seg, src_offset = 0, dst_offset;
  	int count, total;
c982c368b   FUJITA Tomonori   [SCSI] st: fix md...
3936
  	int length = PAGE_SIZE << st_bp->reserved_page_order;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3937
3938
3939
3940
3941
3942
3943
  
  	if (offset == 0)
  		return;
  
  	total=st_bp->buffer_bytes - offset;
  	for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
  		src_offset = offset;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3944
  		if (src_offset < length)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3945
  			break;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3946
  		offset -= length;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3947
3948
3949
3950
  	}
  
  	st_bp->buffer_bytes = st_bp->read_pointer = total;
  	for (dst_seg=dst_offset=0; total > 0; ) {
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3951
3952
3953
3954
3955
3956
  		struct page *dpage = st_bp->reserved_pages[dst_seg];
  		struct page *spage = st_bp->reserved_pages[src_seg];
  
  		count = min(length - dst_offset, length - src_offset);
  		memmove(page_address(dpage) + dst_offset,
  			page_address(spage) + src_offset, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3957
  		src_offset += count;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3958
  		if (src_offset >= length) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3959
3960
3961
3962
  			src_seg++;
  			src_offset = 0;
  		}
  		dst_offset += count;
08c958324   FUJITA Tomonori   [SCSI] st: kill s...
3963
  		if (dst_offset >= length) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3964
3965
3966
3967
3968
3969
  			dst_seg++;
  			dst_offset = 0;
  		}
  		total -= count;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
  /* Validate the options from command line or module parameters */
  static void validate_options(void)
  {
  	if (buffer_kbs > 0)
  		st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
  	if (max_sg_segs >= ST_FIRST_SG)
  		st_max_sg_segs = max_sg_segs;
  }
  
  #ifndef MODULE
  /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
   */
  static int __init st_setup(char *str)
  {
  	int i, len, ints[5];
  	char *stp;
  
  	stp = get_options(str, ARRAY_SIZE(ints), ints);
  
  	if (ints[0] > 0) {
  		for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
  			if (parms[i].val)
  				*parms[i].val = ints[i + 1];
  	} else {
  		while (stp != NULL) {
  			for (i = 0; i < ARRAY_SIZE(parms); i++) {
  				len = strlen(parms[i].name);
  				if (!strncmp(stp, parms[i].name, len) &&
  				    (*(stp + len) == ':' || *(stp + len) == '=')) {
  					if (parms[i].val)
  						*parms[i].val =
  							simple_strtoul(stp + len + 1, NULL, 0);
  					else
  						printk(KERN_WARNING "st: Obsolete parameter %s
  ",
  						       parms[i].name);
  					break;
  				}
  			}
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
4009
  			if (i >= ARRAY_SIZE(parms))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
  				 printk(KERN_WARNING "st: invalid parameter in '%s'
  ",
  					stp);
  			stp = strchr(stp, ',');
  			if (stp)
  				stp++;
  		}
  	}
  
  	validate_options();
  
  	return 1;
  }
  
  __setup("st=", st_setup);
  
  #endif
00977a59b   Arjan van de Ven   [PATCH] mark stru...
4027
  static const struct file_operations st_fops =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4028
4029
4030
4031
  {
  	.owner =	THIS_MODULE,
  	.read =		st_read,
  	.write =	st_write,
fd66c1b4e   Kai Makisara   [SCSI] st: conver...
4032
  	.unlocked_ioctl = st_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4033
4034
4035
4036
4037
4038
  #ifdef CONFIG_COMPAT
  	.compat_ioctl = st_compat_ioctl,
  #endif
  	.open =		st_open,
  	.flush =	st_flush,
  	.release =	st_release,
b4d878e23   Jan Blunck   st: use noop_llse...
4039
  	.llseek =	noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4040
  };
26898afd6   Jeff Mahoney   [SCSI] st: clean ...
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
  static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
  {
  	int i, error;
  	dev_t cdev_devno;
  	struct cdev *cdev;
  	struct device *dev;
  	struct st_modedef *STm = &(tape->modes[mode]);
  	char name[10];
  	int dev_num = tape->index;
  
  	cdev_devno = MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, rew));
  
  	cdev = cdev_alloc();
  	if (!cdev) {
  		pr_err("st%d: out of memory. Device not attached.
  ", dev_num);
  		error = -ENOMEM;
  		goto out;
  	}
  	cdev->owner = THIS_MODULE;
  	cdev->ops = &st_fops;
  
  	error = cdev_add(cdev, cdev_devno, 1);
  	if (error) {
  		pr_err("st%d: Can't add %s-rewind mode %d
  ", dev_num,
  		       rew ? "non" : "auto", mode);
  		pr_err("st%d: Device not attached.
  ", dev_num);
  		goto out_free;
  	}
  	STm->cdevs[rew] = cdev;
  
  	i = mode << (4 - ST_NBR_MODE_BITS);
  	snprintf(name, 10, "%s%s%s", rew ? "n" : "",
  		 tape->disk->disk_name, st_formats[i]);
  
  	dev = device_create(&st_sysfs_class, &tape->device->sdev_gendev,
  			    cdev_devno, &tape->modes[mode], "%s", name);
  	if (IS_ERR(dev)) {
  		pr_err("st%d: device_create failed
  ", dev_num);
  		error = PTR_ERR(dev);
  		goto out_free;
  	}
  
  	STm->devs[rew] = dev;
  
  	return 0;
  out_free:
  	cdev_del(STm->cdevs[rew]);
  	STm->cdevs[rew] = NULL;
  out:
  	return error;
  }
  
  static int create_cdevs(struct scsi_tape *tape)
  {
  	int mode, error;
  	for (mode = 0; mode < ST_NBR_MODES; ++mode) {
  		error = create_one_cdev(tape, mode, 0);
  		if (error)
  			return error;
  		error = create_one_cdev(tape, mode, 1);
  		if (error)
  			return error;
  	}
  
  	return sysfs_create_link(&tape->device->sdev_gendev.kobj,
  				 &tape->modes[0].devs[0]->kobj, "tape");
  }
  
  static void remove_cdevs(struct scsi_tape *tape)
  {
  	int mode, rew;
  	sysfs_remove_link(&tape->device->sdev_gendev.kobj, "tape");
  	for (mode = 0; mode < ST_NBR_MODES; mode++) {
  		struct st_modedef *STm = &(tape->modes[mode]);
  		for (rew = 0; rew < 2; rew++) {
  			if (STm->cdevs[rew])
  				cdev_del(STm->cdevs[rew]);
  			if (STm->devs[rew])
  				device_unregister(STm->devs[rew]);
  		}
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4127
4128
4129
4130
  static int st_probe(struct device *dev)
  {
  	struct scsi_device *SDp = to_scsi_device(dev);
  	struct gendisk *disk = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4131
4132
4133
4134
  	struct scsi_tape *tpnt = NULL;
  	struct st_modedef *STm;
  	struct st_partstat *STps;
  	struct st_buffer *buffer;
b98c52b57   Tejun Heo   scsi: convert to ...
4135
  	int i, error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4136
  	char *stp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4137
4138
4139
4140
  
  	if (SDp->type != TYPE_TAPE)
  		return -ENODEV;
  	if ((stp = st_incompatible(SDp))) {
3bf743e7c   Jeff Garzik   [SCSI] use {sdev,...
4141
4142
  		sdev_printk(KERN_INFO, SDp, "Found incompatible tape
  ");
b30d8bca5   Hannes Reinecke   scsi: Implement s...
4143
4144
4145
  		sdev_printk(KERN_INFO, SDp,
  			    "st: The suggested driver is %s.
  ", stp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4146
4147
  		return -ENODEV;
  	}
6fe8c1dbe   Subhash Jadavani   scsi: balance out...
4148
  	scsi_autopm_get_device(SDp);
8a78362c4   Martin K. Petersen   block: Consolidat...
4149
  	i = queue_max_segments(SDp->request_queue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4150
4151
  	if (st_max_sg_segs < i)
  		i = st_max_sg_segs;
f409d6cc6   FUJITA Tomonori   [SCSI] st: simpli...
4152
  	buffer = new_tape_buffer((SDp->host)->unchecked_isa_dma, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4153
  	if (buffer == NULL) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
4154
4155
4156
4157
  		sdev_printk(KERN_ERR, SDp,
  			    "st: Can't allocate new tape buffer. "
  			    "Device not attached.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4158
4159
4160
4161
4162
  		goto out;
  	}
  
  	disk = alloc_disk(1);
  	if (!disk) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
4163
4164
4165
  		sdev_printk(KERN_ERR, SDp,
  			    "st: out of memory. Device not attached.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4166
4167
  		goto out_buffer_free;
  	}
24669f75a   Jes Sorensen   [SCSI] SCSI core ...
4168
  	tpnt = kzalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4169
  	if (tpnt == NULL) {
b30d8bca5   Hannes Reinecke   scsi: Implement s...
4170
4171
4172
  		sdev_printk(KERN_ERR, SDp,
  			    "st: Can't allocate device descriptor.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4173
4174
  		goto out_put_disk;
  	}
f03a56705   Kai Makisara   [SCSI] drivers/sc...
4175
  	kref_init(&tpnt->kref);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4176
  	tpnt->disk = disk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4177
4178
  	disk->private_data = &tpnt->driver;
  	disk->queue = SDp->request_queue;
2b5bebccd   Joe Lawrence   [SCSI] st: Take a...
4179
4180
4181
4182
  	/* SCSI tape doesn't register this gendisk via add_disk().  Manually
  	 * take queue reference that release_disk() expects. */
  	if (!blk_get_queue(disk->queue))
  		goto out_put_disk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4183
  	tpnt->driver = &st_template;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4184
4185
4186
4187
4188
4189
4190
4191
  
  	tpnt->device = SDp;
  	if (SDp->scsi_level <= 2)
  		tpnt->tape_type = MT_ISSCSI1;
  	else
  		tpnt->tape_type = MT_ISSCSI2;
  
  	tpnt->buffer = buffer;
f03a56705   Kai Makisara   [SCSI] drivers/sc...
4192
  	tpnt->buffer->last_SRpnt = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
  
  	tpnt->inited = 0;
  	tpnt->dirty = 0;
  	tpnt->in_use = 0;
  	tpnt->drv_buffer = 1;	/* Try buffering if no mode sense */
  	tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
  	tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
  	tpnt->density = 0;
  	tpnt->do_auto_lock = ST_AUTO_LOCK;
  	tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
  	tpnt->can_partitions = 0;
  	tpnt->two_fm = ST_TWO_FM;
  	tpnt->fast_mteom = ST_FAST_MTEOM;
  	tpnt->scsi2_logical = ST_SCSI2LOGICAL;
40f6b36c6   Kai Makisara   [SCSI] st: add op...
4207
  	tpnt->sili = ST_SILI;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4208
  	tpnt->immediate = ST_NOWAIT;
c743e44fb   Lee Duncan   [SCSI] st: expand...
4209
  	tpnt->immediate_filemark = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4210
4211
4212
4213
  	tpnt->default_drvbuffer = 0xff;		/* No forced buffering */
  	tpnt->partition = 0;
  	tpnt->new_partition = 0;
  	tpnt->nbr_partitions = 0;
a02488ed7   James Bottomley   [SCSI] st: update...
4214
  	blk_queue_rq_timeout(tpnt->device->request_queue, ST_TIMEOUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4215
4216
  	tpnt->long_timeout = ST_LONG_TIMEOUT;
  	tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
  	for (i = 0; i < ST_NBR_MODES; i++) {
  		STm = &(tpnt->modes[i]);
  		STm->defined = 0;
  		STm->sysv = ST_SYSV;
  		STm->defaults_for_writes = 0;
  		STm->do_async_writes = ST_ASYNC_WRITES;
  		STm->do_buffer_writes = ST_BUFFER_WRITES;
  		STm->do_read_ahead = ST_READ_AHEAD;
  		STm->default_compression = ST_DONT_TOUCH;
  		STm->default_blksize = (-1);	/* No forced size */
  		STm->default_density = (-1);	/* No forced density */
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4228
  		STm->tape = tpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
  	}
  
  	for (i = 0; i < ST_NBR_PARTITIONS; i++) {
  		STps = &(tpnt->ps[i]);
  		STps->rw = ST_IDLE;
  		STps->eof = ST_NOEOF;
  		STps->at_sm = 0;
  		STps->last_block_valid = 0;
  		STps->drv_block = (-1);
  		STps->drv_file = (-1);
  	}
  
  	tpnt->current_mode = 0;
  	tpnt->modes[0].defined = 1;
  
  	tpnt->density_changed = tpnt->compression_changed =
  	    tpnt->blksize_changed = 0;
28f85009e   Matthias Kaehlcke   [SCSI] st: Use mu...
4246
  	mutex_init(&tpnt->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4247

b98c52b57   Tejun Heo   scsi: convert to ...
4248
  	idr_preload(GFP_KERNEL);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4249
  	spin_lock(&st_index_lock);
b98c52b57   Tejun Heo   scsi: convert to ...
4250
  	error = idr_alloc(&st_index_idr, tpnt, 0, ST_MAX_TAPES + 1, GFP_NOWAIT);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4251
  	spin_unlock(&st_index_lock);
b98c52b57   Tejun Heo   scsi: convert to ...
4252
4253
  	idr_preload_end();
  	if (error < 0) {
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4254
4255
  		pr_warn("st: idr allocation failed: %d
  ", error);
2b5bebccd   Joe Lawrence   [SCSI] st: Take a...
4256
  		goto out_put_queue;
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4257
  	}
b98c52b57   Tejun Heo   scsi: convert to ...
4258
4259
  	tpnt->index = error;
  	sprintf(disk->disk_name, "st%d", tpnt->index);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4260
4261
  
  	dev_set_drvdata(dev, tpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4262

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4263

26898afd6   Jeff Mahoney   [SCSI] st: clean ...
4264
4265
4266
  	error = create_cdevs(tpnt);
  	if (error)
  		goto out_remove_devs;
46a243f72   Oliver Neukum   [SCSI] st: implem...
4267
  	scsi_autopm_put_device(SDp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4268

422528540   Kai Makisara   [SCSI] st: log me...
4269
  	sdev_printk(KERN_NOTICE, SDp,
8b1ea24c6   Rene Herman   [PATCH] missing n...
4270
4271
  		    "Attached scsi tape %s
  ", tape_name(tpnt));
422528540   Kai Makisara   [SCSI] st: log me...
4272
4273
4274
4275
  	sdev_printk(KERN_INFO, SDp, "%s: try direct i/o: %s (alignment %d B)
  ",
  		    tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
  		    queue_dma_alignment(SDp->request_queue) + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4276
4277
  
  	return 0;
26898afd6   Jeff Mahoney   [SCSI] st: clean ...
4278
4279
  out_remove_devs:
  	remove_cdevs(tpnt);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4280
  	spin_lock(&st_index_lock);
b98c52b57   Tejun Heo   scsi: convert to ...
4281
  	idr_remove(&st_index_idr, tpnt->index);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4282
  	spin_unlock(&st_index_lock);
2b5bebccd   Joe Lawrence   [SCSI] st: Take a...
4283
4284
  out_put_queue:
  	blk_put_queue(disk->queue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4285
4286
  out_put_disk:
  	put_disk(disk);
c9475cb0c   Jesper Juhl   [PATCH] kfree cle...
4287
  	kfree(tpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4288
4289
4290
  out_buffer_free:
  	kfree(buffer);
  out:
6fe8c1dbe   Subhash Jadavani   scsi: balance out...
4291
  	scsi_autopm_put_device(SDp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4292
4293
4294
4295
4296
4297
  	return -ENODEV;
  };
  
  
  static int st_remove(struct device *dev)
  {
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4298
  	struct scsi_tape *tpnt = dev_get_drvdata(dev);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4299
4300
4301
  	int index = tpnt->index;
  
  	scsi_autopm_get_device(to_scsi_device(dev));
26898afd6   Jeff Mahoney   [SCSI] st: clean ...
4302
  	remove_cdevs(tpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4303

6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4304
4305
4306
4307
4308
4309
  	mutex_lock(&st_ref_mutex);
  	kref_put(&tpnt->kref, scsi_tape_release);
  	mutex_unlock(&st_ref_mutex);
  	spin_lock(&st_index_lock);
  	idr_remove(&st_index_idr, index);
  	spin_unlock(&st_index_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4310
4311
  	return 0;
  }
f03a56705   Kai Makisara   [SCSI] drivers/sc...
4312
4313
4314
4315
  /**
   *      scsi_tape_release - Called to free the Scsi_Tape structure
   *      @kref: pointer to embedded kref
   *
0b9506723   Arjan van de Ven   [SCSI] turn most ...
4316
   *      st_ref_mutex must be held entering this routine.  Because it is
f03a56705   Kai Makisara   [SCSI] drivers/sc...
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
   *      called on last put, you should always use the scsi_tape_get()
   *      scsi_tape_put() helpers which manipulate the semaphore directly
   *      and never do a direct kref_put().
   **/
  static void scsi_tape_release(struct kref *kref)
  {
  	struct scsi_tape *tpnt = to_scsi_tape(kref);
  	struct gendisk *disk = tpnt->disk;
  
  	tpnt->device = NULL;
  
  	if (tpnt->buffer) {
f03a56705   Kai Makisara   [SCSI] drivers/sc...
4329
  		normalize_buffer(tpnt->buffer);
d0e1ae31b   FUJITA Tomonori   [SCSI] st: add st...
4330
  		kfree(tpnt->buffer->reserved_pages);
f03a56705   Kai Makisara   [SCSI] drivers/sc...
4331
4332
4333
4334
4335
4336
4337
4338
  		kfree(tpnt->buffer);
  	}
  
  	disk->private_data = NULL;
  	put_disk(disk);
  	kfree(tpnt);
  	return;
  }
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4339
4340
  static struct class st_sysfs_class = {
  	.name = "scsi_tape",
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4341
  	.dev_groups = st_dev_groups,
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4342
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4343
4344
  static int __init init_st(void)
  {
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4345
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4346
  	validate_options();
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4347
4348
  	printk(KERN_INFO "st: Version %s, fixed bufsize %d, s/g segs %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4349
  		verstr, st_fixed_buffer_size, st_max_sg_segs);
2bec708a8   Laurence Oberman   st: add a debug_f...
4350
4351
4352
4353
4354
4355
  	debugging = (debug_flag > 0) ? debug_flag : NO_DEBUG;
  	if (debugging) {
  		printk(KERN_INFO "st: Debugging enabled debug_flag = %d
  ",
  			debugging);
  	}
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4356
4357
4358
4359
4360
  	err = class_register(&st_sysfs_class);
  	if (err) {
  		pr_err("Unable register sysfs class for SCSI tapes
  ");
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4361
  	}
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4362
4363
4364
4365
4366
4367
4368
  	err = register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
  				     ST_MAX_TAPE_ENTRIES, "st");
  	if (err) {
  		printk(KERN_ERR "Unable to get major %d for SCSI tapes
  ",
  		       SCSI_TAPE_MAJOR);
  		goto err_class;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4369
  	}
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4370
4371
4372
  	err = scsi_register_driver(&st_template.gendrv);
  	if (err)
  		goto err_chrdev;
405ae7d38   Robert P. J. Day   Replace remaining...
4373
  	err = do_create_sysfs_files();
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
  	if (err)
  		goto err_scsidrv;
  
  	return 0;
  
  err_scsidrv:
  	scsi_unregister_driver(&st_template.gendrv);
  err_chrdev:
  	unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
  				 ST_MAX_TAPE_ENTRIES);
  err_class:
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4385
  	class_unregister(&st_sysfs_class);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4386
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4387
4388
4389
4390
  }
  
  static void __exit exit_st(void)
  {
405ae7d38   Robert P. J. Day   Replace remaining...
4391
  	do_remove_sysfs_files();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4392
4393
4394
  	scsi_unregister_driver(&st_template.gendrv);
  	unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
  				 ST_MAX_TAPE_ENTRIES);
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4395
  	class_unregister(&st_sysfs_class);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
  	printk(KERN_INFO "st: Unloaded.
  ");
  }
  
  module_init(init_st);
  module_exit(exit_st);
  
  
  /* The sysfs driver interface. Read-only at the moment */
  static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
  {
  	return snprintf(buf, PAGE_SIZE, "%d
  ", try_direct_io);
  }
  static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
  
  static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
  {
  	return snprintf(buf, PAGE_SIZE, "%d
  ", st_fixed_buffer_size);
  }
  static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
  
  static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
  {
  	return snprintf(buf, PAGE_SIZE, "%d
  ", st_max_sg_segs);
  }
  static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
  
  static ssize_t st_version_show(struct device_driver *ddd, char *buf)
  {
  	return snprintf(buf, PAGE_SIZE, "[%s]
  ", verstr);
  }
  static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
405ae7d38   Robert P. J. Day   Replace remaining...
4432
  static int do_create_sysfs_files(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4433
  {
405ae7d38   Robert P. J. Day   Replace remaining...
4434
  	struct device_driver *sysfs = &st_template.gendrv;
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4435
  	int err;
405ae7d38   Robert P. J. Day   Replace remaining...
4436
  	err = driver_create_file(sysfs, &driver_attr_try_direct_io);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4437
4438
  	if (err)
  		return err;
405ae7d38   Robert P. J. Day   Replace remaining...
4439
  	err = driver_create_file(sysfs, &driver_attr_fixed_buffer_size);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4440
4441
  	if (err)
  		goto err_try_direct_io;
405ae7d38   Robert P. J. Day   Replace remaining...
4442
  	err = driver_create_file(sysfs, &driver_attr_max_sg_segs);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4443
4444
  	if (err)
  		goto err_attr_fixed_buf;
405ae7d38   Robert P. J. Day   Replace remaining...
4445
  	err = driver_create_file(sysfs, &driver_attr_version);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4446
4447
  	if (err)
  		goto err_attr_max_sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4448

13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4449
4450
4451
  	return 0;
  
  err_attr_max_sg:
405ae7d38   Robert P. J. Day   Replace remaining...
4452
  	driver_remove_file(sysfs, &driver_attr_max_sg_segs);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4453
  err_attr_fixed_buf:
405ae7d38   Robert P. J. Day   Replace remaining...
4454
  	driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4455
  err_try_direct_io:
405ae7d38   Robert P. J. Day   Replace remaining...
4456
  	driver_remove_file(sysfs, &driver_attr_try_direct_io);
13026a6b9   Jeff Garzik   [SCSI] SCSI st: f...
4457
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4458
  }
405ae7d38   Robert P. J. Day   Replace remaining...
4459
  static void do_remove_sysfs_files(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4460
  {
405ae7d38   Robert P. J. Day   Replace remaining...
4461
  	struct device_driver *sysfs = &st_template.gendrv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4462

405ae7d38   Robert P. J. Day   Replace remaining...
4463
4464
4465
4466
  	driver_remove_file(sysfs, &driver_attr_version);
  	driver_remove_file(sysfs, &driver_attr_max_sg_segs);
  	driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
  	driver_remove_file(sysfs, &driver_attr_try_direct_io);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4467
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4468
  /* The sysfs simple class interface */
ee959b00c   Tony Jones   SCSI: convert str...
4469
  static ssize_t
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4470
  defined_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4471
  {
7d15d6a4d   James Bottomley   [SCSI] st: fix up...
4472
  	struct st_modedef *STm = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4473
4474
4475
4476
4477
4478
  	ssize_t l = 0;
  
  	l = snprintf(buf, PAGE_SIZE, "%d
  ", STm->defined);
  	return l;
  }
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4479
  static DEVICE_ATTR_RO(defined);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4480

ee959b00c   Tony Jones   SCSI: convert str...
4481
  static ssize_t
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4482
4483
  default_blksize_show(struct device *dev, struct device_attribute *attr,
  		     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4484
  {
7d15d6a4d   James Bottomley   [SCSI] st: fix up...
4485
  	struct st_modedef *STm = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4486
4487
4488
4489
4490
4491
  	ssize_t l = 0;
  
  	l = snprintf(buf, PAGE_SIZE, "%d
  ", STm->default_blksize);
  	return l;
  }
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4492
  static DEVICE_ATTR_RO(default_blksize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4493

ee959b00c   Tony Jones   SCSI: convert str...
4494
  static ssize_t
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4495
4496
  default_density_show(struct device *dev, struct device_attribute *attr,
  		     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4497
  {
7d15d6a4d   James Bottomley   [SCSI] st: fix up...
4498
  	struct st_modedef *STm = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4499
4500
4501
4502
4503
4504
4505
4506
4507
  	ssize_t l = 0;
  	char *fmt;
  
  	fmt = STm->default_density >= 0 ? "0x%02x
  " : "%d
  ";
  	l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
  	return l;
  }
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4508
  static DEVICE_ATTR_RO(default_density);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4509

ee959b00c   Tony Jones   SCSI: convert str...
4510
  static ssize_t
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4511
4512
  default_compression_show(struct device *dev, struct device_attribute *attr,
  			 char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4513
  {
7d15d6a4d   James Bottomley   [SCSI] st: fix up...
4514
  	struct st_modedef *STm = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4515
4516
4517
4518
4519
4520
  	ssize_t l = 0;
  
  	l = snprintf(buf, PAGE_SIZE, "%d
  ", STm->default_compression - 1);
  	return l;
  }
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4521
  static DEVICE_ATTR_RO(default_compression);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4522

ee959b00c   Tony Jones   SCSI: convert str...
4523
  static ssize_t
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4524
  options_show(struct device *dev, struct device_attribute *attr, char *buf)
b174be02f   Kai Makisara   [SCSI] st: show o...
4525
  {
7d15d6a4d   James Bottomley   [SCSI] st: fix up...
4526
  	struct st_modedef *STm = dev_get_drvdata(dev);
6c648d95a   Jeff Mahoney   [SCSI] st: get ri...
4527
4528
  	struct scsi_tape *STp = STm->tape;
  	int options;
b174be02f   Kai Makisara   [SCSI] st: show o...
4529
  	ssize_t l = 0;
b174be02f   Kai Makisara   [SCSI] st: show o...
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
  	options = STm->do_buffer_writes ? MT_ST_BUFFER_WRITES : 0;
  	options |= STm->do_async_writes ? MT_ST_ASYNC_WRITES : 0;
  	options |= STm->do_read_ahead ? MT_ST_READ_AHEAD : 0;
  	DEB( options |= debugging ? MT_ST_DEBUGGING : 0 );
  	options |= STp->two_fm ? MT_ST_TWO_FM : 0;
  	options |= STp->fast_mteom ? MT_ST_FAST_MTEOM : 0;
  	options |= STm->defaults_for_writes ? MT_ST_DEF_WRITES : 0;
  	options |= STp->can_bsr ? MT_ST_CAN_BSR : 0;
  	options |= STp->omit_blklims ? MT_ST_NO_BLKLIMS : 0;
  	options |= STp->can_partitions ? MT_ST_CAN_PARTITIONS : 0;
  	options |= STp->scsi2_logical ? MT_ST_SCSI2LOGICAL : 0;
  	options |= STm->sysv ? MT_ST_SYSV : 0;
  	options |= STp->immediate ? MT_ST_NOWAIT : 0;
c743e44fb   Lee Duncan   [SCSI] st: expand...
4543
  	options |= STp->immediate_filemark ? MT_ST_NOWAIT_EOF : 0;
b174be02f   Kai Makisara   [SCSI] st: show o...
4544
4545
4546
4547
4548
4549
  	options |= STp->sili ? MT_ST_SILI : 0;
  
  	l = snprintf(buf, PAGE_SIZE, "0x%08x
  ", options);
  	return l;
  }
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4550
4551
4552
4553
4554
4555
4556
4557
4558
  static DEVICE_ATTR_RO(options);
  
  static struct attribute *st_dev_attrs[] = {
  	&dev_attr_defined.attr,
  	&dev_attr_default_blksize.attr,
  	&dev_attr_default_density.attr,
  	&dev_attr_default_compression.attr,
  	&dev_attr_options.attr,
  	NULL,
af23782be   Jeff Mahoney   [SCSI] st: Use st...
4559
  };
c69c6be5a   Greg Kroah-Hartman   [SCSI] st: conver...
4560
  ATTRIBUTE_GROUPS(st_dev);
b174be02f   Kai Makisara   [SCSI] st: show o...
4561

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4562
  /* The following functions may be useful for a larger audience. */
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4563
4564
4565
  static int sgl_map_user_pages(struct st_buffer *STbp,
  			      const unsigned int max_pages, unsigned long uaddr,
  			      size_t count, int rw)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4566
  {
07542b832   James Bottomley   This patch fixes ...
4567
4568
4569
  	unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
  	unsigned long start = uaddr >> PAGE_SHIFT;
  	const int nr_pages = end - start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4570
  	int res, i, j;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4571
  	struct page **pages;
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4572
  	struct rq_map_data *mdata = &STbp->map_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4573

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
  	/* User attempted Overflow! */
  	if ((uaddr + count) < uaddr)
  		return -EINVAL;
  
  	/* Too big */
          if (nr_pages > max_pages)
  		return -ENOMEM;
  
  	/* Hmm? */
  	if (count == 0)
  		return 0;
  
  	if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
  		return -ENOMEM;
  
          /* Try to fault in all of the necessary pages */
  	down_read(&current->mm->mmap_sem);
          /* rw==READ means read from drive, write into memory area */
  	res = get_user_pages(
  		current,
  		current->mm,
  		uaddr,
  		nr_pages,
  		rw == READ,
  		0, /* don't force */
  		pages,
  		NULL);
  	up_read(&current->mm->mmap_sem);
  
  	/* Errors and no page mapped should return here */
  	if (res < nr_pages)
  		goto out_unmap;
  
          for (i=0; i < nr_pages; i++) {
                  /* FIXME: flush superflous for rw==READ,
                   * probably wrong function for rw==WRITE
                   */
  		flush_dcache_page(pages[i]);
          }
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4613
  	mdata->offset = uaddr & ~PAGE_MASK;
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4614
  	STbp->mapped_pages = pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4615

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4616
  	return nr_pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4617
4618
4619
4620
   out_unmap:
  	if (res > 0) {
  		for (j=0; j < res; j++)
  			page_cache_release(pages[j]);
6bc733e9f   Hugh Dickins   [SCSI] st: fix a ...
4621
  		res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4622
4623
4624
4625
4626
4627
4628
  	}
  	kfree(pages);
  	return res;
  }
  
  
  /* And unmap them... */
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4629
4630
  static int sgl_unmap_user_pages(struct st_buffer *STbp,
  				const unsigned int nr_pages, int dirtied)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4631
4632
4633
4634
  {
  	int i;
  
  	for (i=0; i < nr_pages; i++) {
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4635
  		struct page *page = STbp->mapped_pages[i];
b5810039a   Nick Piggin   [PATCH] core remo...
4636

b5810039a   Nick Piggin   [PATCH] core remo...
4637
4638
  		if (dirtied)
  			SetPageDirty(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4639
4640
4641
  		/* FIXME: cache flush missing for rw==READ
  		 * FIXME: call the correct reference counting function
  		 */
b5810039a   Nick Piggin   [PATCH] core remo...
4642
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4643
  	}
6620742f7   FUJITA Tomonori   [SCSI] st: conver...
4644
4645
  	kfree(STbp->mapped_pages);
  	STbp->mapped_pages = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4646
4647
4648
  
  	return 0;
  }