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