blob: fec5568c711a881554bf009881a79ca2be9cd510 [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 Makisara626dcb12008-07-11 15:05:25 +030020static const char *verstr = "20080504";
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>
Jonathan Corbetb3369c62008-05-15 16:08:15 -060041#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
44#include <asm/dma.h>
45#include <asm/system.h>
46
47#include <scsi/scsi.h>
48#include <scsi/scsi_dbg.h>
49#include <scsi/scsi_device.h>
50#include <scsi/scsi_driver.h>
51#include <scsi/scsi_eh.h>
52#include <scsi/scsi_host.h>
53#include <scsi/scsi_ioctl.h>
Kai Makisara 16c4b3e2005-05-01 18:11:55 +030054#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57/* The driver prints some debugging information on the console if DEBUG
58 is defined and non-zero. */
59#define DEBUG 0
60
61#if DEBUG
62/* The message level for the debug messages is currently set to KERN_NOTICE
63 so that people can easily see the messages. Later when the debugging messages
64 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
65#define ST_DEB_MSG KERN_NOTICE
66#define DEB(a) a
67#define DEBC(a) if (debugging) { a ; }
68#else
69#define DEB(a)
70#define DEBC(a)
71#endif
72
73#define ST_KILOBYTE 1024
74
75#include "st_options.h"
76#include "st.h"
77
78static int buffer_kbs;
79static int max_sg_segs;
80static int try_direct_io = TRY_DIRECT_IO;
81static int try_rdio = 1;
82static int try_wdio = 1;
83
84static int st_dev_max;
85static int st_nr_dev;
86
gregkh@suse.ded2538782005-03-23 09:55:22 -080087static struct class *st_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89MODULE_AUTHOR("Kai Makisara");
Rene Hermanf018fa52006-03-08 00:14:20 -080090MODULE_DESCRIPTION("SCSI tape (st) driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091MODULE_LICENSE("GPL");
Rene Hermanf018fa52006-03-08 00:14:20 -080092MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +040093MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* Set 'perm' (4th argument) to 0 to disable module_param's definition
96 * of sysfs parameters (which module_param doesn't yet support).
97 * Sysfs parameters defined explicitly later.
98 */
99module_param_named(buffer_kbs, buffer_kbs, int, 0);
100MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
101module_param_named(max_sg_segs, max_sg_segs, int, 0);
102MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
103module_param_named(try_direct_io, try_direct_io, int, 0);
104MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
105
106/* Extra parameters for testing */
107module_param_named(try_rdio, try_rdio, int, 0);
108MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
109module_param_named(try_wdio, try_wdio, int, 0);
110MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
111
112#ifndef MODULE
113static int write_threshold_kbs; /* retained for compatibility */
114static struct st_dev_parm {
115 char *name;
116 int *val;
117} parms[] __initdata = {
118 {
119 "buffer_kbs", &buffer_kbs
120 },
121 { /* Retained for compatibility with 2.4 */
122 "write_threshold_kbs", &write_threshold_kbs
123 },
124 {
125 "max_sg_segs", NULL
126 },
127 {
128 "try_direct_io", &try_direct_io
129 }
130};
131#endif
132
133/* Restrict the number of modes so that names for all are assigned */
134#if ST_NBR_MODES > 16
135#error "Maximum number of modes is 16"
136#endif
137/* Bit reversed order to get same names for same minors with all
138 mode counts */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100139static const char *st_formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 "", "r", "k", "s", "l", "t", "o", "u",
141 "m", "v", "p", "x", "a", "y", "q", "z"};
142
143/* The default definitions have been moved to st_options.h */
144
145#define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
146
147/* The buffer size should fit into the 24 bits for length in the
148 6-byte SCSI read and write commands. */
149#if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
150#error "Buffer size should not exceed (2 << 24 - 1) bytes!"
151#endif
152
153static int debugging = DEBUG;
154
155#define MAX_RETRIES 0
156#define MAX_WRITE_RETRIES 0
157#define MAX_READY_RETRIES 0
158#define NO_TAPE NOT_READY
159
160#define ST_TIMEOUT (900 * HZ)
161#define ST_LONG_TIMEOUT (14000 * HZ)
162
163/* Remove mode bits and auto-rewind bit (7) */
164#define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
165 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
166#define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
167
168/* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
169#define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
170 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
171
172/* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
173 24 bits) */
174#define SET_DENS_AND_BLK 0x10001
175
176static DEFINE_RWLOCK(st_dev_arr_lock);
177
178static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
179static int st_max_sg_segs = ST_MAX_SG;
180
181static struct scsi_tape **scsi_tapes = NULL;
182
183static int modes_defined;
184
185static struct st_buffer *new_tape_buffer(int, int, int);
186static int enlarge_buffer(struct st_buffer *, int, int);
Kai Makisara40f6b362008-02-24 22:23:24 +0200187static void clear_buffer(struct st_buffer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static void normalize_buffer(struct st_buffer *);
189static int append_to_buffer(const char __user *, struct st_buffer *, int);
190static int from_buffer(struct st_buffer *, char __user *, int);
191static void move_buffer_data(struct st_buffer *, int);
192static void buf_to_sg(struct st_buffer *, unsigned int);
193
FUJITA Tomonori66207422008-12-18 14:49:43 +0900194static int sgl_map_user_pages(struct st_buffer *, const unsigned int,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned long, size_t, int);
FUJITA Tomonori66207422008-12-18 14:49:43 +0900196static int sgl_unmap_user_pages(struct st_buffer *, const unsigned int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198static int st_probe(struct device *);
199static int st_remove(struct device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100201static int do_create_sysfs_files(void);
202static void do_remove_sysfs_files(void);
Jeff Garzik13026a62006-10-04 06:00:38 -0400203static int do_create_class_files(struct scsi_tape *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static struct scsi_driver st_template = {
206 .owner = THIS_MODULE,
207 .gendrv = {
208 .name = "st",
209 .probe = st_probe,
210 .remove = st_remove,
211 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214static int st_compression(struct scsi_tape *, int);
215
216static int find_partition(struct scsi_tape *);
217static int switch_partition(struct scsi_tape *);
218
219static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
220
Kai Makisaraf03a5672005-08-02 13:40:47 +0300221static void scsi_tape_release(struct kref *);
222
223#define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
224
Arjan van de Ven0b950672006-01-11 13:16:10 +0100225static DEFINE_MUTEX(st_ref_mutex);
Kai Makisaraf03a5672005-08-02 13:40:47 +0300226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228#include "osst_detect.h"
229#ifndef SIGS_FROM_OSST
230#define SIGS_FROM_OSST \
231 {"OnStream", "SC-", "", "osst"}, \
232 {"OnStream", "DI-", "", "osst"}, \
233 {"OnStream", "DP-", "", "osst"}, \
234 {"OnStream", "USB", "", "osst"}, \
235 {"OnStream", "FW-", "", "osst"}
236#endif
237
Kai Makisaraf03a5672005-08-02 13:40:47 +0300238static struct scsi_tape *scsi_tape_get(int dev)
239{
240 struct scsi_tape *STp = NULL;
241
Arjan van de Ven0b950672006-01-11 13:16:10 +0100242 mutex_lock(&st_ref_mutex);
Kai Makisaraf03a5672005-08-02 13:40:47 +0300243 write_lock(&st_dev_arr_lock);
244
245 if (dev < st_dev_max && scsi_tapes != NULL)
246 STp = scsi_tapes[dev];
247 if (!STp) goto out;
248
249 kref_get(&STp->kref);
250
251 if (!STp->device)
252 goto out_put;
253
254 if (scsi_device_get(STp->device))
255 goto out_put;
256
257 goto out;
258
259out_put:
260 kref_put(&STp->kref, scsi_tape_release);
261 STp = NULL;
262out:
263 write_unlock(&st_dev_arr_lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100264 mutex_unlock(&st_ref_mutex);
Kai Makisaraf03a5672005-08-02 13:40:47 +0300265 return STp;
266}
267
268static void scsi_tape_put(struct scsi_tape *STp)
269{
270 struct scsi_device *sdev = STp->device;
271
Arjan van de Ven0b950672006-01-11 13:16:10 +0100272 mutex_lock(&st_ref_mutex);
Kai Makisaraf03a5672005-08-02 13:40:47 +0300273 kref_put(&STp->kref, scsi_tape_release);
274 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100275 mutex_unlock(&st_ref_mutex);
Kai Makisaraf03a5672005-08-02 13:40:47 +0300276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278struct st_reject_data {
279 char *vendor;
280 char *model;
281 char *rev;
282 char *driver_hint; /* Name of the correct driver, NULL if unknown */
283};
284
285static struct st_reject_data reject_list[] = {
286 /* {"XXX", "Yy-", "", NULL}, example */
287 SIGS_FROM_OSST,
288 {NULL, }};
289
290/* If the device signature is on the list of incompatible drives, the
291 function returns a pointer to the name of the correct driver (if known) */
292static char * st_incompatible(struct scsi_device* SDp)
293{
294 struct st_reject_data *rp;
295
296 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
297 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
298 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
299 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
300 if (rp->driver_hint)
301 return rp->driver_hint;
302 else
303 return "unknown";
304 }
305 return NULL;
306}
307
308
309static inline char *tape_name(struct scsi_tape *tape)
310{
311 return tape->disk->disk_name;
312}
313
314
Mike Christie8b05b772005-11-08 04:06:44 -0600315static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 const u8 *ucp;
Mike Christie8b05b772005-11-08 04:06:44 -0600318 const u8 *sense = SRpnt->sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Mike Christie8b05b772005-11-08 04:06:44 -0600320 s->have_sense = scsi_normalize_sense(SRpnt->sense,
321 SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 s->flags = 0;
323
324 if (s->have_sense) {
325 s->deferred = 0;
326 s->remainder_valid =
327 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
328 switch (sense[0] & 0x7f) {
329 case 0x71:
330 s->deferred = 1;
331 case 0x70:
332 s->fixed_format = 1;
333 s->flags = sense[2] & 0xe0;
334 break;
335 case 0x73:
336 s->deferred = 1;
337 case 0x72:
338 s->fixed_format = 0;
339 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
340 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
341 break;
342 }
343 }
344}
345
346
347/* Convert the result to success code */
Mike Christie8b05b772005-11-08 04:06:44 -0600348static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Mike Christie8b05b772005-11-08 04:06:44 -0600350 int result = SRpnt->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 u8 scode;
352 DEB(const char *stp;)
353 char *name = tape_name(STp);
354 struct st_cmdstatus *cmdstatp;
355
356 if (!result)
357 return 0;
358
359 cmdstatp = &STp->buffer->cmdstat;
Kai Makisaraf03a5672005-08-02 13:40:47 +0300360 st_analyze_sense(SRpnt, cmdstatp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (cmdstatp->have_sense)
363 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
364 else
365 scode = 0;
366
367 DEB(
368 if (debugging) {
Mike Christie8b05b772005-11-08 04:06:44 -0600369 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 name, result,
Mike Christie8b05b772005-11-08 04:06:44 -0600371 SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
372 SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (cmdstatp->have_sense)
Luben Tuikov4e73ea72006-07-07 00:02:18 -0700374 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } ) /* end DEB */
376 if (!debugging) { /* Abnormal conditions for tape */
377 if (!cmdstatp->have_sense)
378 printk(KERN_WARNING
379 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
380 name, result, suggestion(result),
381 driver_byte(result) & DRIVER_MASK, host_byte(result));
382 else if (cmdstatp->have_sense &&
383 scode != NO_SENSE &&
384 scode != RECOVERED_ERROR &&
385 /* scode != UNIT_ATTENTION && */
386 scode != BLANK_CHECK &&
387 scode != VOLUME_OVERFLOW &&
Mike Christie8b05b772005-11-08 04:06:44 -0600388 SRpnt->cmd[0] != MODE_SENSE &&
389 SRpnt->cmd[0] != TEST_UNIT_READY) {
Luben Tuikov4e73ea72006-07-07 00:02:18 -0700390
391 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 }
394
395 if (cmdstatp->fixed_format &&
396 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
397 if (STp->cln_sense_value)
Mike Christie8b05b772005-11-08 04:06:44 -0600398 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 STp->cln_sense_mask) == STp->cln_sense_value);
400 else
Mike Christie8b05b772005-11-08 04:06:44 -0600401 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 STp->cln_sense_mask) != 0);
403 }
404 if (cmdstatp->have_sense &&
405 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
406 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
407
408 STp->pos_unknown |= STp->device->was_reset;
409
410 if (cmdstatp->have_sense &&
411 scode == RECOVERED_ERROR
412#if ST_RECOVERED_WRITE_FATAL
Mike Christie8b05b772005-11-08 04:06:44 -0600413 && SRpnt->cmd[0] != WRITE_6
414 && SRpnt->cmd[0] != WRITE_FILEMARKS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#endif
416 ) {
417 STp->recover_count++;
418 STp->recover_reg++;
419
420 DEB(
421 if (debugging) {
Mike Christie8b05b772005-11-08 04:06:44 -0600422 if (SRpnt->cmd[0] == READ_6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 stp = "read";
Mike Christie8b05b772005-11-08 04:06:44 -0600424 else if (SRpnt->cmd[0] == WRITE_6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 stp = "write";
426 else
427 stp = "ioctl";
428 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
429 STp->recover_count);
430 } ) /* end DEB */
431
432 if (cmdstatp->flags == 0)
433 return 0;
434 }
435 return (-EIO);
436}
437
FUJITA Tomonori4deba242008-12-05 15:25:20 +0900438static struct st_request *st_allocate_request(struct scsi_tape *stp)
Mike Christie8b05b772005-11-08 04:06:44 -0600439{
FUJITA Tomonori4deba242008-12-05 15:25:20 +0900440 struct st_request *streq;
441
442 streq = kzalloc(sizeof(*streq), GFP_KERNEL);
443 if (streq)
444 streq->stp = stp;
445 else {
446 DEBC(printk(KERN_ERR "%s: Can't get SCSI request.\n",
447 tape_name(stp)););
448 if (signal_pending(current))
449 stp->buffer->syscall_result = -EINTR;
450 else
451 stp->buffer->syscall_result = -EBUSY;
452 }
453
454 return streq;
Mike Christie8b05b772005-11-08 04:06:44 -0600455}
456
457static void st_release_request(struct st_request *streq)
458{
459 kfree(streq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
FUJITA Tomonori13b53b42008-12-18 14:49:41 +0900462static void st_scsi_execute_end(struct request *req, int uptodate)
463{
464 struct st_request *SRpnt = req->end_io_data;
465 struct scsi_tape *STp = SRpnt->stp;
466
467 STp->buffer->cmdstat.midlevel_result = SRpnt->result = req->errors;
468 STp->buffer->cmdstat.residual = req->data_len;
469
470 if (SRpnt->waiting)
471 complete(SRpnt->waiting);
472
473 blk_rq_unmap_user(SRpnt->bio);
474 __blk_put_request(req->q, req);
475}
476
477static int st_scsi_execute(struct st_request *SRpnt, const unsigned char *cmd,
478 int data_direction, void *buffer, unsigned bufflen,
479 int timeout, int retries)
480{
481 struct request *req;
482 struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data;
483 int err = 0;
484 int write = (data_direction == DMA_TO_DEVICE);
485
486 req = blk_get_request(SRpnt->stp->device->request_queue, write,
487 GFP_KERNEL);
488 if (!req)
489 return DRIVER_ERROR << 24;
490
491 req->cmd_type = REQ_TYPE_BLOCK_PC;
492 req->cmd_flags |= REQ_QUIET;
493
494 mdata->null_mapped = 1;
495
496 err = blk_rq_map_user(req->q, req, mdata, NULL, bufflen, GFP_KERNEL);
497 if (err) {
498 blk_put_request(req);
499 return DRIVER_ERROR << 24;
500 }
501
502 SRpnt->bio = req->bio;
503 req->cmd_len = COMMAND_SIZE(cmd[0]);
504 memset(req->cmd, 0, BLK_MAX_CDB);
505 memcpy(req->cmd, cmd, req->cmd_len);
506 req->sense = SRpnt->sense;
507 req->sense_len = 0;
508 req->timeout = timeout;
509 req->retries = retries;
510 req->end_io_data = SRpnt;
511
512 blk_execute_rq_nowait(req->q, NULL, req, 1, st_scsi_execute_end);
513 return 0;
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/* Do the scsi command. Waits until command performed if do_wait is true.
517 Otherwise write_behind_check() is used to check that the command
518 has finished. */
Mike Christie8b05b772005-11-08 04:06:44 -0600519static struct st_request *
520st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int bytes, int direction, int timeout, int retries, int do_wait)
522{
Kai Makisaraf03a5672005-08-02 13:40:47 +0300523 struct completion *waiting;
FUJITA Tomonori6d476262008-12-18 14:49:42 +0900524 struct rq_map_data *mdata = &STp->buffer->map_data;
525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Kai Makisaraf03a5672005-08-02 13:40:47 +0300527 /* if async, make sure there's no command outstanding */
528 if (!do_wait && ((STp->buffer)->last_SRpnt)) {
529 printk(KERN_ERR "%s: Async command already active.\n",
530 tape_name(STp));
531 if (signal_pending(current))
532 (STp->buffer)->syscall_result = (-EINTR);
533 else
534 (STp->buffer)->syscall_result = (-EBUSY);
535 return NULL;
536 }
537
FUJITA Tomonori4deba242008-12-05 15:25:20 +0900538 if (!SRpnt) {
539 SRpnt = st_allocate_request(STp);
540 if (!SRpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Kai Makisaraf03a5672005-08-02 13:40:47 +0300544 /* If async IO, set last_SRpnt. This ptr tells write_behind_check
545 which IO is outstanding. It's nulled out when the IO completes. */
546 if (!do_wait)
547 (STp->buffer)->last_SRpnt = SRpnt;
548
549 waiting = &STp->wait;
550 init_completion(waiting);
Mike Christie8b05b772005-11-08 04:06:44 -0600551 SRpnt->waiting = waiting;
552
FUJITA Tomonori66207422008-12-18 14:49:43 +0900553 if (STp->buffer->do_dio) {
554 mdata->nr_entries = STp->buffer->sg_segs;
555 mdata->pages = STp->buffer->mapped_pages;
556 } else {
Mike Christie8b05b772005-11-08 04:06:44 -0600557 buf_to_sg(STp->buffer, bytes);
558
FUJITA Tomonori6d476262008-12-18 14:49:42 +0900559 mdata->nr_entries =
560 DIV_ROUND_UP(bytes, PAGE_SIZE << mdata->page_order);
561 STp->buffer->map_data.pages = STp->buffer->reserved_pages;
562 STp->buffer->map_data.offset = 0;
563 }
564
Mike Christie8b05b772005-11-08 04:06:44 -0600565 memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 STp->buffer->cmdstat.have_sense = 0;
Mike Christie8b05b772005-11-08 04:06:44 -0600567 STp->buffer->syscall_result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
FUJITA Tomonori66207422008-12-18 14:49:43 +0900569 ret = st_scsi_execute(SRpnt, cmd, direction, NULL, bytes, timeout,
570 retries);
FUJITA Tomonori6d476262008-12-18 14:49:42 +0900571 if (ret) {
Mike Christie8b05b772005-11-08 04:06:44 -0600572 /* could not allocate the buffer or request was too large */
573 (STp->buffer)->syscall_result = (-EBUSY);
Kai Makisara787926b2005-11-13 10:04:44 +0200574 (STp->buffer)->last_SRpnt = NULL;
FUJITA Tomonori6d476262008-12-18 14:49:42 +0900575 } else if (do_wait) {
Kai Makisaraf03a5672005-08-02 13:40:47 +0300576 wait_for_completion(waiting);
Mike Christie8b05b772005-11-08 04:06:44 -0600577 SRpnt->waiting = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
579 }
Mike Christie8b05b772005-11-08 04:06:44 -0600580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return SRpnt;
582}
583
FUJITA Tomonoriffb43492008-12-05 15:25:21 +0900584static int st_scsi_kern_execute(struct st_request *streq,
585 const unsigned char *cmd, int data_direction,
586 void *buffer, unsigned bufflen, int timeout,
587 int retries)
588{
589 struct scsi_tape *stp = streq->stp;
590 int ret, resid;
591
592 stp->buffer->cmdstat.have_sense = 0;
593 memcpy(streq->cmd, cmd, sizeof(streq->cmd));
594
595 ret = scsi_execute(stp->device, cmd, data_direction, buffer, bufflen,
596 streq->sense, timeout, retries, 0, &resid);
597 if (driver_byte(ret) & DRIVER_ERROR)
598 return -EBUSY;
599
600 stp->buffer->cmdstat.midlevel_result = streq->result = ret;
601 stp->buffer->cmdstat.residual = resid;
602 stp->buffer->syscall_result = st_chk_result(stp, streq);
603
604 return 0;
605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
608 write has been correct but EOM early warning reached, -EIO if write ended in
609 error or zero if write successful. Asynchronous writes are used only in
610 variable block mode. */
611static int write_behind_check(struct scsi_tape * STp)
612{
613 int retval = 0;
614 struct st_buffer *STbuffer;
615 struct st_partstat *STps;
616 struct st_cmdstatus *cmdstatp;
Mike Christie8b05b772005-11-08 04:06:44 -0600617 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 STbuffer = STp->buffer;
620 if (!STbuffer->writing)
621 return 0;
622
623 DEB(
624 if (STp->write_pending)
625 STp->nbr_waits++;
626 else
627 STp->nbr_finished++;
628 ) /* end DEB */
629
630 wait_for_completion(&(STp->wait));
Kai Makisaraf03a5672005-08-02 13:40:47 +0300631 SRpnt = STbuffer->last_SRpnt;
632 STbuffer->last_SRpnt = NULL;
Mike Christie8b05b772005-11-08 04:06:44 -0600633 SRpnt->waiting = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Kai Makisaraf03a5672005-08-02 13:40:47 +0300635 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
Mike Christie8b05b772005-11-08 04:06:44 -0600636 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 STbuffer->buffer_bytes -= STbuffer->writing;
639 STps = &(STp->ps[STp->partition]);
640 if (STps->drv_block >= 0) {
641 if (STp->block_size == 0)
642 STps->drv_block++;
643 else
644 STps->drv_block += STbuffer->writing / STp->block_size;
645 }
646
647 cmdstatp = &STbuffer->cmdstat;
648 if (STbuffer->syscall_result) {
649 retval = -EIO;
650 if (cmdstatp->have_sense && !cmdstatp->deferred &&
651 (cmdstatp->flags & SENSE_EOM) &&
652 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
653 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
654 /* EOM at write-behind, has all data been written? */
655 if (!cmdstatp->remainder_valid ||
656 cmdstatp->uremainder64 == 0)
657 retval = -ENOSPC;
658 }
659 if (retval == -EIO)
660 STps->drv_block = -1;
661 }
662 STbuffer->writing = 0;
663
664 DEB(if (debugging && retval)
665 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
666 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
667
668 return retval;
669}
670
671
672/* Step over EOF if it has been inadvertently crossed (ioctl not used because
673 it messes up the block number). */
674static int cross_eof(struct scsi_tape * STp, int forward)
675{
Mike Christie8b05b772005-11-08 04:06:44 -0600676 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned char cmd[MAX_COMMAND_SIZE];
FUJITA Tomonori39ade4b2008-12-05 15:25:25 +0900678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 cmd[0] = SPACE;
681 cmd[1] = 0x01; /* Space FileMarks */
682 if (forward) {
683 cmd[2] = cmd[3] = 0;
684 cmd[4] = 1;
685 } else
686 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
687 cmd[5] = 0;
688
689 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
690 tape_name(STp), forward ? "forward" : "backward"));
691
FUJITA Tomonori39ade4b2008-12-05 15:25:25 +0900692 SRpnt = st_allocate_request(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!SRpnt)
FUJITA Tomonori39ade4b2008-12-05 15:25:25 +0900694 return STp->buffer->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
FUJITA Tomonori39ade4b2008-12-05 15:25:25 +0900696 ret = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0,
697 STp->device->request_queue->rq_timeout,
698 MAX_RETRIES);
699 if (ret)
700 goto out;
701
702 ret = STp->buffer->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if ((STp->buffer)->cmdstat.midlevel_result != 0)
705 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
706 tape_name(STp), forward ? "forward" : "backward");
707
FUJITA Tomonori39ade4b2008-12-05 15:25:25 +0900708out:
709 st_release_request(SRpnt);
710
711 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714
715/* Flush the write buffer (never need to write if variable blocksize). */
Adrian Bunk8ef8d592008-04-14 17:17:16 +0300716static int st_flush_write_buffer(struct scsi_tape * STp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Kai Makisara786231a2008-07-11 15:06:40 +0300718 int transfer, blks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int result;
720 unsigned char cmd[MAX_COMMAND_SIZE];
Mike Christie8b05b772005-11-08 04:06:44 -0600721 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct st_partstat *STps;
723
724 result = write_behind_check(STp);
725 if (result)
726 return result;
727
728 result = 0;
729 if (STp->dirty == 1) {
730
Kai Makisara786231a2008-07-11 15:06:40 +0300731 transfer = STp->buffer->buffer_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
733 tape_name(STp), transfer));
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 memset(cmd, 0, MAX_COMMAND_SIZE);
736 cmd[0] = WRITE_6;
737 cmd[1] = 1;
738 blks = transfer / STp->block_size;
739 cmd[2] = blks >> 16;
740 cmd[3] = blks >> 8;
741 cmd[4] = blks;
742
743 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
James Bottomleya02488e2008-11-30 10:36:26 -0600744 STp->device->request_queue->rq_timeout,
745 MAX_WRITE_RETRIES, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!SRpnt)
747 return (STp->buffer)->syscall_result;
748
749 STps = &(STp->ps[STp->partition]);
750 if ((STp->buffer)->syscall_result != 0) {
751 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
752
753 if (cmdstatp->have_sense && !cmdstatp->deferred &&
754 (cmdstatp->flags & SENSE_EOM) &&
755 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
756 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
757 (!cmdstatp->remainder_valid ||
758 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
759 STp->dirty = 0;
760 (STp->buffer)->buffer_bytes = 0;
761 if (STps->drv_block >= 0)
762 STps->drv_block += blks;
763 result = (-ENOSPC);
764 } else {
765 printk(KERN_ERR "%s: Error on flush.\n",
766 tape_name(STp));
767 STps->drv_block = (-1);
768 result = (-EIO);
769 }
770 } else {
771 if (STps->drv_block >= 0)
772 STps->drv_block += blks;
773 STp->dirty = 0;
774 (STp->buffer)->buffer_bytes = 0;
775 }
Mike Christie8b05b772005-11-08 04:06:44 -0600776 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 SRpnt = NULL;
778 }
779 return result;
780}
781
782
783/* Flush the tape buffer. The tape will be positioned correctly unless
784 seek_next is true. */
785static int flush_buffer(struct scsi_tape *STp, int seek_next)
786{
787 int backspace, result;
788 struct st_buffer *STbuffer;
789 struct st_partstat *STps;
790
791 STbuffer = STp->buffer;
792
793 /*
794 * If there was a bus reset, block further access
795 * to this device.
796 */
797 if (STp->pos_unknown)
798 return (-EIO);
799
800 if (STp->ready != ST_READY)
801 return 0;
802 STps = &(STp->ps[STp->partition]);
803 if (STps->rw == ST_WRITING) /* Writing */
Adrian Bunk8ef8d592008-04-14 17:17:16 +0300804 return st_flush_write_buffer(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (STp->block_size == 0)
807 return 0;
808
809 backspace = ((STp->buffer)->buffer_bytes +
810 (STp->buffer)->read_pointer) / STp->block_size -
811 ((STp->buffer)->read_pointer + STp->block_size - 1) /
812 STp->block_size;
813 (STp->buffer)->buffer_bytes = 0;
814 (STp->buffer)->read_pointer = 0;
815 result = 0;
816 if (!seek_next) {
817 if (STps->eof == ST_FM_HIT) {
818 result = cross_eof(STp, 0); /* Back over the EOF hit */
819 if (!result)
820 STps->eof = ST_NOEOF;
821 else {
822 if (STps->drv_file >= 0)
823 STps->drv_file++;
824 STps->drv_block = 0;
825 }
826 }
827 if (!result && backspace > 0)
828 result = st_int_ioctl(STp, MTBSR, backspace);
829 } else if (STps->eof == ST_FM_HIT) {
830 if (STps->drv_file >= 0)
831 STps->drv_file++;
832 STps->drv_block = 0;
833 STps->eof = ST_NOEOF;
834 }
835 return result;
836
837}
838
839/* Set the mode parameters */
840static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
841{
842 int set_it = 0;
843 unsigned long arg;
844 char *name = tape_name(STp);
845
846 if (!STp->density_changed &&
847 STm->default_density >= 0 &&
848 STm->default_density != STp->density) {
849 arg = STm->default_density;
850 set_it = 1;
851 } else
852 arg = STp->density;
853 arg <<= MT_ST_DENSITY_SHIFT;
854 if (!STp->blksize_changed &&
855 STm->default_blksize >= 0 &&
856 STm->default_blksize != STp->block_size) {
857 arg |= STm->default_blksize;
858 set_it = 1;
859 } else
860 arg |= STp->block_size;
861 if (set_it &&
862 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
863 printk(KERN_WARNING
864 "%s: Can't set default block size to %d bytes and density %x.\n",
865 name, STm->default_blksize, STm->default_density);
866 if (modes_defined)
867 return (-EINVAL);
868 }
869 return 0;
870}
871
872
Mike Christie8b05b772005-11-08 04:06:44 -0600873/* Lock or unlock the drive door. Don't use when st_request allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static int do_door_lock(struct scsi_tape * STp, int do_lock)
875{
876 int retval, cmd;
877 DEB(char *name = tape_name(STp);)
878
879
880 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
881 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
882 do_lock ? "L" : "Unl"));
883 retval = scsi_ioctl(STp->device, cmd, NULL);
884 if (!retval) {
885 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
886 }
887 else {
888 STp->door_locked = ST_LOCK_FAILS;
889 }
890 return retval;
891}
892
893
894/* Set the internal state after reset */
895static void reset_state(struct scsi_tape *STp)
896{
897 int i;
898 struct st_partstat *STps;
899
900 STp->pos_unknown = 0;
901 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
902 STps = &(STp->ps[i]);
903 STps->rw = ST_IDLE;
904 STps->eof = ST_NOEOF;
905 STps->at_sm = 0;
906 STps->last_block_valid = 0;
907 STps->drv_block = -1;
908 STps->drv_file = -1;
909 }
910 if (STp->can_partitions) {
911 STp->partition = find_partition(STp);
912 if (STp->partition < 0)
913 STp->partition = 0;
914 STp->new_partition = STp->partition;
915 }
916}
917
918/* Test if the drive is ready. Returns either one of the codes below or a negative system
919 error code. */
920#define CHKRES_READY 0
921#define CHKRES_NEW_SESSION 1
922#define CHKRES_NOT_READY 2
923#define CHKRES_NO_TAPE 3
924
925#define MAX_ATTENTIONS 10
926
927static int test_ready(struct scsi_tape *STp, int do_wait)
928{
929 int attentions, waits, max_wait, scode;
930 int retval = CHKRES_READY, new_session = 0;
931 unsigned char cmd[MAX_COMMAND_SIZE];
FUJITA Tomonori0944a722008-12-05 15:25:22 +0900932 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
934
FUJITA Tomonori0944a722008-12-05 15:25:22 +0900935 SRpnt = st_allocate_request(STp);
936 if (!SRpnt)
937 return STp->buffer->syscall_result;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
940
941 for (attentions=waits=0; ; ) {
942 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
943 cmd[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
FUJITA Tomonori0944a722008-12-05 15:25:22 +0900945 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0,
946 STp->long_timeout,
947 MAX_READY_RETRIES);
948 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (cmdstatp->have_sense) {
952
953 scode = cmdstatp->sense_hdr.sense_key;
954
955 if (scode == UNIT_ATTENTION) { /* New media? */
956 new_session = 1;
957 if (attentions < MAX_ATTENTIONS) {
958 attentions++;
959 continue;
960 }
961 else {
962 retval = (-EIO);
963 break;
964 }
965 }
966
967 if (scode == NOT_READY) {
968 if (waits < max_wait) {
969 if (msleep_interruptible(1000)) {
970 retval = (-EINTR);
971 break;
972 }
973 waits++;
974 continue;
975 }
976 else {
977 if ((STp->device)->scsi_level >= SCSI_2 &&
978 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
979 retval = CHKRES_NO_TAPE;
980 else
981 retval = CHKRES_NOT_READY;
982 break;
983 }
984 }
985 }
986
987 retval = (STp->buffer)->syscall_result;
988 if (!retval)
989 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
990 break;
991 }
992
FUJITA Tomonori0944a722008-12-05 15:25:22 +0900993 st_release_request(SRpnt);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return retval;
996}
997
998
999/* See if the drive is ready and gather information about the tape. Return values:
1000 < 0 negative error code from errno.h
1001 0 drive ready
1002 1 drive not ready (possibly no tape)
1003*/
1004static int check_tape(struct scsi_tape *STp, struct file *filp)
1005{
1006 int i, retval, new_session = 0, do_wait;
1007 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
1008 unsigned short st_flags = filp->f_flags;
Mike Christie8b05b772005-11-08 04:06:44 -06001009 struct st_request *SRpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct st_modedef *STm;
1011 struct st_partstat *STps;
1012 char *name = tape_name(STp);
Josef Sipek7ac62072006-12-08 02:37:37 -08001013 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int mode = TAPE_MODE(inode);
1015
1016 STp->ready = ST_READY;
1017
1018 if (mode != STp->current_mode) {
1019 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
1020 name, STp->current_mode, mode));
1021 new_session = 1;
1022 STp->current_mode = mode;
1023 }
1024 STm = &(STp->modes[STp->current_mode]);
1025
1026 saved_cleaning = STp->cleaning_req;
1027 STp->cleaning_req = 0;
1028
1029 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
1030 retval = test_ready(STp, do_wait);
1031
1032 if (retval < 0)
1033 goto err_out;
1034
1035 if (retval == CHKRES_NEW_SESSION) {
1036 STp->pos_unknown = 0;
1037 STp->partition = STp->new_partition = 0;
1038 if (STp->can_partitions)
1039 STp->nbr_partitions = 1; /* This guess will be updated later
1040 if necessary */
1041 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1042 STps = &(STp->ps[i]);
1043 STps->rw = ST_IDLE;
1044 STps->eof = ST_NOEOF;
1045 STps->at_sm = 0;
1046 STps->last_block_valid = 0;
1047 STps->drv_block = 0;
1048 STps->drv_file = 0;
1049 }
1050 new_session = 1;
1051 }
1052 else {
1053 STp->cleaning_req |= saved_cleaning;
1054
1055 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
1056 if (retval == CHKRES_NO_TAPE)
1057 STp->ready = ST_NO_TAPE;
1058 else
1059 STp->ready = ST_NOT_READY;
1060
1061 STp->density = 0; /* Clear the erroneous "residue" */
1062 STp->write_prot = 0;
1063 STp->block_size = 0;
1064 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
1065 STp->partition = STp->new_partition = 0;
1066 STp->door_locked = ST_UNLOCKED;
1067 return CHKRES_NOT_READY;
1068 }
1069 }
1070
FUJITA Tomonori52107b22008-12-05 15:25:27 +09001071 SRpnt = st_allocate_request(STp);
1072 if (!SRpnt) {
1073 retval = STp->buffer->syscall_result;
1074 goto err_out;
1075 }
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (STp->omit_blklims)
1078 STp->min_block = STp->max_block = (-1);
1079 else {
1080 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1081 cmd[0] = READ_BLOCK_LIMITS;
1082
FUJITA Tomonori52107b22008-12-05 15:25:27 +09001083 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_FROM_DEVICE,
1084 STp->buffer->b_data, 6,
1085 STp->device->request_queue->rq_timeout,
1086 MAX_READY_RETRIES);
1087 if (retval) {
1088 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 goto err_out;
1090 }
1091
Mike Christie8b05b772005-11-08 04:06:44 -06001092 if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
1094 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
1095 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
1096 (STp->buffer)->b_data[5];
1097 if ( DEB( debugging || ) !STp->inited)
Kai Makisara42252852006-11-07 21:56:38 +02001098 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 "%s: Block limits %d - %d bytes.\n", name,
1100 STp->min_block, STp->max_block);
1101 } else {
1102 STp->min_block = STp->max_block = (-1);
1103 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
1104 name));
1105 }
1106 }
1107
1108 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1109 cmd[0] = MODE_SENSE;
1110 cmd[4] = 12;
1111
FUJITA Tomonori52107b22008-12-05 15:25:27 +09001112 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_FROM_DEVICE,
1113 STp->buffer->b_data, 12,
1114 STp->device->request_queue->rq_timeout,
1115 MAX_READY_RETRIES);
1116 if (retval) {
1117 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto err_out;
1119 }
1120
1121 if ((STp->buffer)->syscall_result != 0) {
1122 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
1123 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
1124 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
1125 STp->drv_write_prot = 0;
1126 } else {
1127 DEBC(printk(ST_DEB_MSG
1128 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
1129 name,
1130 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
1131 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
1132
1133 if ((STp->buffer)->b_data[3] >= 8) {
1134 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
1135 STp->density = (STp->buffer)->b_data[4];
1136 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
1137 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
1138 DEBC(printk(ST_DEB_MSG
1139 "%s: Density %x, tape length: %x, drv buffer: %d\n",
1140 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
1141 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
1142 STp->drv_buffer));
1143 }
1144 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
1145 }
Mike Christie8b05b772005-11-08 04:06:44 -06001146 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 SRpnt = NULL;
1148 STp->inited = 1;
1149
1150 if (STp->block_size > 0)
1151 (STp->buffer)->buffer_blocks =
1152 (STp->buffer)->buffer_size / STp->block_size;
1153 else
1154 (STp->buffer)->buffer_blocks = 1;
1155 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
1156
1157 DEBC(printk(ST_DEB_MSG
1158 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
1159 STp->block_size, (STp->buffer)->buffer_size,
1160 (STp->buffer)->buffer_blocks));
1161
1162 if (STp->drv_write_prot) {
1163 STp->write_prot = 1;
1164
1165 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
1166
1167 if (do_wait &&
1168 ((st_flags & O_ACCMODE) == O_WRONLY ||
1169 (st_flags & O_ACCMODE) == O_RDWR)) {
1170 retval = (-EROFS);
1171 goto err_out;
1172 }
1173 }
1174
1175 if (STp->can_partitions && STp->nbr_partitions < 1) {
1176 /* This code is reached when the device is opened for the first time
1177 after the driver has been initialized with tape in the drive and the
1178 partition support has been enabled. */
1179 DEBC(printk(ST_DEB_MSG
1180 "%s: Updating partition number in status.\n", name));
1181 if ((STp->partition = find_partition(STp)) < 0) {
1182 retval = STp->partition;
1183 goto err_out;
1184 }
1185 STp->new_partition = STp->partition;
1186 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1187 }
1188
1189 if (new_session) { /* Change the drive parameters for the new mode */
1190 STp->density_changed = STp->blksize_changed = 0;
1191 STp->compression_changed = 0;
1192 if (!(STm->defaults_for_writes) &&
1193 (retval = set_mode_densblk(STp, STm)) < 0)
1194 goto err_out;
1195
1196 if (STp->default_drvbuffer != 0xff) {
1197 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1198 printk(KERN_WARNING
1199 "%s: Can't set default drive buffering to %d.\n",
1200 name, STp->default_drvbuffer);
1201 }
1202 }
1203
1204 return CHKRES_READY;
1205
1206 err_out:
1207 return retval;
1208}
1209
1210
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001211 /* Open the device. Needs to take the BKL only because of incrementing the SCSI host
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 module count. */
1213static int st_open(struct inode *inode, struct file *filp)
1214{
1215 int i, retval = (-EIO);
1216 struct scsi_tape *STp;
1217 struct st_partstat *STps;
1218 int dev = TAPE_NR(inode);
1219 char *name;
1220
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001221 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * We really want to do nonseekable_open(inode, filp); here, but some
1224 * versions of tar incorrectly call lseek on tapes and bail out if that
1225 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1226 */
1227 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1228
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001229 if (!(STp = scsi_tape_get(dev))) {
1230 unlock_kernel();
Kai Makisaraf03a5672005-08-02 13:40:47 +03001231 return -ENXIO;
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001232 }
Kai Makisaraf03a5672005-08-02 13:40:47 +03001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 write_lock(&st_dev_arr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 filp->private_data = STp;
1236 name = tape_name(STp);
1237
1238 if (STp->in_use) {
1239 write_unlock(&st_dev_arr_lock);
Kai Makisaraf03a5672005-08-02 13:40:47 +03001240 scsi_tape_put(STp);
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001241 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1243 return (-EBUSY);
1244 }
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 STp->in_use = 1;
1247 write_unlock(&st_dev_arr_lock);
1248 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1249
1250 if (!scsi_block_when_processing_errors(STp->device)) {
1251 retval = (-ENXIO);
1252 goto err_out;
1253 }
1254
1255 /* See that we have at least a one page buffer available */
1256 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1257 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1258 name);
1259 retval = (-EOVERFLOW);
1260 goto err_out;
1261 }
1262
Kai Makisara40f6b362008-02-24 22:23:24 +02001263 (STp->buffer)->cleared = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 (STp->buffer)->writing = 0;
1265 (STp->buffer)->syscall_result = 0;
1266
1267 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1268
1269 STp->dirty = 0;
1270 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1271 STps = &(STp->ps[i]);
1272 STps->rw = ST_IDLE;
1273 }
Kai Makisara9abe16c2007-02-03 13:21:29 +02001274 STp->try_dio_now = STp->try_dio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 STp->recover_count = 0;
1276 DEB( STp->nbr_waits = STp->nbr_finished = 0;
Kai Makisaradeee13d2008-02-22 20:11:21 +02001277 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 retval = check_tape(STp, filp);
1280 if (retval < 0)
1281 goto err_out;
1282 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1283 retval != CHKRES_READY) {
Kai Makisara413f7322006-10-05 22:59:46 +03001284 if (STp->ready == NO_TAPE)
1285 retval = (-ENOMEDIUM);
1286 else
1287 retval = (-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto err_out;
1289 }
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001290 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292
1293 err_out:
1294 normalize_buffer(STp->buffer);
1295 STp->in_use = 0;
Kai Makisaraf03a5672005-08-02 13:40:47 +03001296 scsi_tape_put(STp);
Jonathan Corbetb3369c62008-05-15 16:08:15 -06001297 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return retval;
1299
1300}
1301
1302
1303/* Flush the tape buffer before close */
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07001304static int st_flush(struct file *filp, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 int result = 0, result2;
1307 unsigned char cmd[MAX_COMMAND_SIZE];
Mike Christie8b05b772005-11-08 04:06:44 -06001308 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct scsi_tape *STp = filp->private_data;
1310 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1311 struct st_partstat *STps = &(STp->ps[STp->partition]);
1312 char *name = tape_name(STp);
1313
1314 if (file_count(filp) > 1)
1315 return 0;
1316
1317 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
Adrian Bunk8ef8d592008-04-14 17:17:16 +03001318 result = st_flush_write_buffer(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (result != 0 && result != (-ENOSPC))
1320 goto out;
1321 }
1322
1323 if (STp->can_partitions &&
1324 (result2 = switch_partition(STp)) < 0) {
1325 DEBC(printk(ST_DEB_MSG
1326 "%s: switch_partition at close failed.\n", name));
1327 if (result == 0)
1328 result = result2;
1329 goto out;
1330 }
1331
1332 DEBC( if (STp->nbr_requests)
Kai Makisaradeee13d2008-02-22 20:11:21 +02001333 printk(KERN_DEBUG "%s: Number of r/w requests %d, dio used in %d, pages %d.\n",
1334 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1337 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1338
1339 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1340 name, STp->nbr_waits, STp->nbr_finished);
1341 )
1342
1343 memset(cmd, 0, MAX_COMMAND_SIZE);
1344 cmd[0] = WRITE_FILEMARKS;
1345 cmd[4] = 1 + STp->two_fm;
1346
FUJITA Tomonori212cd8b2008-12-05 15:25:26 +09001347 SRpnt = st_allocate_request(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (!SRpnt) {
FUJITA Tomonori212cd8b2008-12-05 15:25:26 +09001349 result = STp->buffer->syscall_result;
1350 goto out;
1351 }
1352
1353 result = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0,
1354 STp->device->request_queue->rq_timeout,
1355 MAX_WRITE_RETRIES);
1356 if (result) {
1357 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto out;
1359 }
1360
1361 if (STp->buffer->syscall_result == 0 ||
1362 (cmdstatp->have_sense && !cmdstatp->deferred &&
1363 (cmdstatp->flags & SENSE_EOM) &&
1364 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1365 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1366 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1367 /* Write successful at EOM */
Mike Christie8b05b772005-11-08 04:06:44 -06001368 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 SRpnt = NULL;
1370 if (STps->drv_file >= 0)
1371 STps->drv_file++;
1372 STps->drv_block = 0;
1373 if (STp->two_fm)
1374 cross_eof(STp, 0);
1375 STps->eof = ST_FM;
1376 }
1377 else { /* Write error */
Mike Christie8b05b772005-11-08 04:06:44 -06001378 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 SRpnt = NULL;
1380 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1381 if (result == 0)
1382 result = (-EIO);
1383 }
1384
1385 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1386 name, cmd[4]));
1387 } else if (!STp->rew_at_close) {
1388 STps = &(STp->ps[STp->partition]);
1389 if (!STm->sysv || STps->rw != ST_READING) {
1390 if (STp->can_bsr)
1391 result = flush_buffer(STp, 0);
1392 else if (STps->eof == ST_FM_HIT) {
1393 result = cross_eof(STp, 0);
1394 if (result) {
1395 if (STps->drv_file >= 0)
1396 STps->drv_file++;
1397 STps->drv_block = 0;
1398 STps->eof = ST_FM;
1399 } else
1400 STps->eof = ST_NOEOF;
1401 }
1402 } else if ((STps->eof == ST_NOEOF &&
1403 !(result = cross_eof(STp, 1))) ||
1404 STps->eof == ST_FM_HIT) {
1405 if (STps->drv_file >= 0)
1406 STps->drv_file++;
1407 STps->drv_block = 0;
1408 STps->eof = ST_FM;
1409 }
1410 }
1411
1412 out:
1413 if (STp->rew_at_close) {
1414 result2 = st_int_ioctl(STp, MTREW, 1);
1415 if (result == 0)
1416 result = result2;
1417 }
1418 return result;
1419}
1420
1421
1422/* Close the device and release it. BKL is not needed: this is the only thread
1423 accessing this tape. */
1424static int st_release(struct inode *inode, struct file *filp)
1425{
1426 int result = 0;
1427 struct scsi_tape *STp = filp->private_data;
1428
1429 if (STp->door_locked == ST_LOCKED_AUTO)
1430 do_door_lock(STp, 0);
1431
1432 normalize_buffer(STp->buffer);
1433 write_lock(&st_dev_arr_lock);
1434 STp->in_use = 0;
1435 write_unlock(&st_dev_arr_lock);
Kai Makisaraf03a5672005-08-02 13:40:47 +03001436 scsi_tape_put(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 return result;
1439}
1440
1441/* The checks common to both reading and writing */
1442static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1443{
1444 ssize_t retval = 0;
1445
1446 /*
1447 * If we are in the middle of error recovery, don't let anyone
1448 * else try and use this device. Also, if error recovery fails, it
1449 * may try and take the device offline, in which case all further
1450 * access to the device is prohibited.
1451 */
1452 if (!scsi_block_when_processing_errors(STp->device)) {
1453 retval = (-ENXIO);
1454 goto out;
1455 }
1456
1457 if (STp->ready != ST_READY) {
1458 if (STp->ready == ST_NO_TAPE)
1459 retval = (-ENOMEDIUM);
1460 else
1461 retval = (-EIO);
1462 goto out;
1463 }
1464
1465 if (! STp->modes[STp->current_mode].defined) {
1466 retval = (-ENXIO);
1467 goto out;
1468 }
1469
1470
1471 /*
1472 * If there was a bus reset, block further access
1473 * to this device.
1474 */
1475 if (STp->pos_unknown) {
1476 retval = (-EIO);
1477 goto out;
1478 }
1479
1480 if (count == 0)
1481 goto out;
1482
1483 DEB(
1484 if (!STp->in_use) {
1485 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1486 retval = (-EIO);
1487 goto out;
1488 } ) /* end DEB */
1489
1490 if (STp->can_partitions &&
1491 (retval = switch_partition(STp)) < 0)
1492 goto out;
1493
1494 if (STp->block_size == 0 && STp->max_block > 0 &&
1495 (count < STp->min_block || count > STp->max_block)) {
1496 retval = (-EINVAL);
1497 goto out;
1498 }
1499
1500 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1501 !do_door_lock(STp, 1))
1502 STp->door_locked = ST_LOCKED_AUTO;
1503
1504 out:
1505 return retval;
1506}
1507
1508
1509static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1510 size_t count, int is_read)
1511{
1512 int i, bufsize, retval = 0;
1513 struct st_buffer *STbp = STp->buffer;
1514
1515 if (is_read)
Kai Makisara9abe16c2007-02-03 13:21:29 +02001516 i = STp->try_dio_now && try_rdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 else
Kai Makisara9abe16c2007-02-03 13:21:29 +02001518 i = STp->try_dio_now && try_wdio;
Mike Christie8b05b772005-11-08 04:06:44 -06001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (i && ((unsigned long)buf & queue_dma_alignment(
1521 STp->device->request_queue)) == 0) {
FUJITA Tomonori66207422008-12-18 14:49:43 +09001522 i = sgl_map_user_pages(STbp, STbp->use_sg, (unsigned long)buf,
1523 count, (is_read ? READ : WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (i > 0) {
1525 STbp->do_dio = i;
1526 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1527 }
1528 else
1529 STbp->do_dio = 0; /* fall back to buffering with any error */
1530 STbp->sg_segs = STbp->do_dio;
1531 STbp->frp_sg_current = 0;
1532 DEB(
1533 if (STbp->do_dio) {
1534 STp->nbr_dio++;
1535 STp->nbr_pages += STbp->do_dio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 )
1538 } else
1539 STbp->do_dio = 0;
1540 DEB( STp->nbr_requests++; )
1541
1542 if (!STbp->do_dio) {
1543 if (STp->block_size)
1544 bufsize = STp->block_size > st_fixed_buffer_size ?
1545 STp->block_size : st_fixed_buffer_size;
Kai Makisara40f6b362008-02-24 22:23:24 +02001546 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 bufsize = count;
Kai Makisara40f6b362008-02-24 22:23:24 +02001548 /* Make sure that data from previous user is not leaked even if
1549 HBA does not return correct residual */
1550 if (is_read && STp->sili && !STbp->cleared)
1551 clear_buffer(STbp);
1552 }
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (bufsize > STbp->buffer_size &&
1555 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1556 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1557 tape_name(STp), bufsize);
1558 retval = (-EOVERFLOW);
1559 goto out;
1560 }
1561 if (STp->block_size)
1562 STbp->buffer_blocks = bufsize / STp->block_size;
1563 }
1564
1565 out:
1566 return retval;
1567}
1568
1569
1570/* Can be called more than once after each setup_buffer() */
Kai Makisara787926b2005-11-13 10:04:44 +02001571static void release_buffering(struct scsi_tape *STp, int is_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 struct st_buffer *STbp;
1574
1575 STbp = STp->buffer;
1576 if (STbp->do_dio) {
FUJITA Tomonori66207422008-12-18 14:49:43 +09001577 sgl_unmap_user_pages(STbp, STbp->do_dio, is_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 STbp->do_dio = 0;
Kai Makisara787926b2005-11-13 10:04:44 +02001579 STbp->sg_segs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581}
1582
1583
1584/* Write command */
1585static ssize_t
1586st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1587{
1588 ssize_t total;
1589 ssize_t i, do_count, blks, transfer;
1590 ssize_t retval;
1591 int undone, retry_eot = 0, scode;
1592 int async_write;
1593 unsigned char cmd[MAX_COMMAND_SIZE];
1594 const char __user *b_point;
Mike Christie8b05b772005-11-08 04:06:44 -06001595 struct st_request *SRpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 struct scsi_tape *STp = filp->private_data;
1597 struct st_modedef *STm;
1598 struct st_partstat *STps;
1599 struct st_buffer *STbp;
1600 char *name = tape_name(STp);
1601
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02001602 if (mutex_lock_interruptible(&STp->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -ERESTARTSYS;
1604
1605 retval = rw_checks(STp, filp, count);
1606 if (retval || count == 0)
1607 goto out;
1608
1609 /* Write must be integral number of blocks */
1610 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1611 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1612 name);
1613 retval = (-EINVAL);
1614 goto out;
1615 }
1616
1617 STm = &(STp->modes[STp->current_mode]);
1618 STps = &(STp->ps[STp->partition]);
1619
1620 if (STp->write_prot) {
1621 retval = (-EACCES);
1622 goto out;
1623 }
1624
1625
1626 if (STps->rw == ST_READING) {
1627 retval = flush_buffer(STp, 0);
1628 if (retval)
1629 goto out;
1630 STps->rw = ST_WRITING;
1631 } else if (STps->rw != ST_WRITING &&
1632 STps->drv_file == 0 && STps->drv_block == 0) {
1633 if ((retval = set_mode_densblk(STp, STm)) < 0)
1634 goto out;
1635 if (STm->default_compression != ST_DONT_TOUCH &&
1636 !(STp->compression_changed)) {
1637 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1638 printk(KERN_WARNING "%s: Can't set default compression.\n",
1639 name);
1640 if (modes_defined) {
1641 retval = (-EINVAL);
1642 goto out;
1643 }
1644 }
1645 }
1646 }
1647
1648 STbp = STp->buffer;
1649 i = write_behind_check(STp);
1650 if (i) {
1651 if (i == -ENOSPC)
1652 STps->eof = ST_EOM_OK;
1653 else
1654 STps->eof = ST_EOM_ERROR;
1655 }
1656
1657 if (STps->eof == ST_EOM_OK) {
1658 STps->eof = ST_EOD_1; /* allow next write */
1659 retval = (-ENOSPC);
1660 goto out;
1661 }
1662 else if (STps->eof == ST_EOM_ERROR) {
1663 retval = (-EIO);
1664 goto out;
1665 }
1666
1667 /* Check the buffer readability in cases where copy_user might catch
1668 the problems after some tape movement. */
1669 if (STp->block_size != 0 &&
1670 !STbp->do_dio &&
1671 (copy_from_user(&i, buf, 1) != 0 ||
1672 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1673 retval = (-EFAULT);
1674 goto out;
1675 }
1676
1677 retval = setup_buffering(STp, buf, count, 0);
1678 if (retval)
1679 goto out;
1680
1681 total = count;
1682
1683 memset(cmd, 0, MAX_COMMAND_SIZE);
1684 cmd[0] = WRITE_6;
1685 cmd[1] = (STp->block_size != 0);
1686
1687 STps->rw = ST_WRITING;
1688
1689 b_point = buf;
1690 while (count > 0 && !retry_eot) {
1691
1692 if (STbp->do_dio) {
1693 do_count = count;
1694 }
1695 else {
1696 if (STp->block_size == 0)
1697 do_count = count;
1698 else {
1699 do_count = STbp->buffer_blocks * STp->block_size -
1700 STbp->buffer_bytes;
1701 if (do_count > count)
1702 do_count = count;
1703 }
1704
1705 i = append_to_buffer(b_point, STbp, do_count);
1706 if (i) {
1707 retval = i;
1708 goto out;
1709 }
1710 }
1711 count -= do_count;
1712 b_point += do_count;
1713
1714 async_write = STp->block_size == 0 && !STbp->do_dio &&
1715 STm->do_async_writes && STps->eof < ST_EOM_OK;
1716
1717 if (STp->block_size != 0 && STm->do_buffer_writes &&
Kai Makisara9abe16c2007-02-03 13:21:29 +02001718 !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 STbp->buffer_bytes < STbp->buffer_size) {
1720 STp->dirty = 1;
1721 /* Don't write a buffer that is not full enough. */
1722 if (!async_write && count == 0)
1723 break;
1724 }
1725
1726 retry_write:
1727 if (STp->block_size == 0)
1728 blks = transfer = do_count;
1729 else {
1730 if (!STbp->do_dio)
1731 blks = STbp->buffer_bytes;
1732 else
1733 blks = do_count;
1734 blks /= STp->block_size;
1735 transfer = blks * STp->block_size;
1736 }
1737 cmd[2] = blks >> 16;
1738 cmd[3] = blks >> 8;
1739 cmd[4] = blks;
1740
1741 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
James Bottomleya02488e2008-11-30 10:36:26 -06001742 STp->device->request_queue->rq_timeout,
1743 MAX_WRITE_RETRIES, !async_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (!SRpnt) {
1745 retval = STbp->syscall_result;
1746 goto out;
1747 }
Mike Christie8b05b772005-11-08 04:06:44 -06001748 if (async_write && !STbp->syscall_result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 STbp->writing = transfer;
1750 STp->dirty = !(STbp->writing ==
1751 STbp->buffer_bytes);
1752 SRpnt = NULL; /* Prevent releasing this request! */
1753 DEB( STp->write_pending = 1; )
1754 break;
1755 }
1756
1757 if (STbp->syscall_result != 0) {
1758 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1759
1760 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1761 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1762 scode = cmdstatp->sense_hdr.sense_key;
1763 if (cmdstatp->remainder_valid)
1764 undone = (int)cmdstatp->uremainder64;
1765 else if (STp->block_size == 0 &&
1766 scode == VOLUME_OVERFLOW)
1767 undone = transfer;
1768 else
1769 undone = 0;
1770 if (STp->block_size != 0)
1771 undone *= STp->block_size;
1772 if (undone <= do_count) {
1773 /* Only data from this write is not written */
1774 count += undone;
Kai Makisara626dcb12008-07-11 15:05:25 +03001775 b_point -= undone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 do_count -= undone;
1777 if (STp->block_size)
1778 blks = (transfer - undone) / STp->block_size;
1779 STps->eof = ST_EOM_OK;
1780 /* Continue in fixed block mode if all written
1781 in this request but still something left to write
1782 (retval left to zero)
1783 */
1784 if (STp->block_size == 0 ||
1785 undone > 0 || count == 0)
1786 retval = (-ENOSPC); /* EOM within current request */
1787 DEBC(printk(ST_DEB_MSG
1788 "%s: EOM with %d bytes unwritten.\n",
1789 name, (int)count));
1790 } else {
1791 /* EOT within data buffered earlier (possible only
1792 in fixed block mode without direct i/o) */
1793 if (!retry_eot && !cmdstatp->deferred &&
1794 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1795 move_buffer_data(STp->buffer, transfer - undone);
1796 retry_eot = 1;
1797 if (STps->drv_block >= 0) {
1798 STps->drv_block += (transfer - undone) /
1799 STp->block_size;
1800 }
1801 STps->eof = ST_EOM_OK;
1802 DEBC(printk(ST_DEB_MSG
1803 "%s: Retry write of %d bytes at EOM.\n",
1804 name, STp->buffer->buffer_bytes));
1805 goto retry_write;
1806 }
1807 else {
1808 /* Either error within data buffered by driver or
1809 failed retry */
1810 count -= do_count;
1811 blks = do_count = 0;
1812 STps->eof = ST_EOM_ERROR;
1813 STps->drv_block = (-1); /* Too cautious? */
1814 retval = (-EIO); /* EOM for old data */
1815 DEBC(printk(ST_DEB_MSG
1816 "%s: EOM with lost data.\n",
1817 name));
1818 }
1819 }
1820 } else {
1821 count += do_count;
1822 STps->drv_block = (-1); /* Too cautious? */
Mike Christie8b05b772005-11-08 04:06:44 -06001823 retval = STbp->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
1826 }
1827
1828 if (STps->drv_block >= 0) {
1829 if (STp->block_size == 0)
1830 STps->drv_block += (do_count > 0);
1831 else
1832 STps->drv_block += blks;
1833 }
1834
1835 STbp->buffer_bytes = 0;
1836 STp->dirty = 0;
1837
1838 if (retval || retry_eot) {
1839 if (count < total)
1840 retval = total - count;
1841 goto out;
1842 }
1843 }
1844
1845 if (STps->eof == ST_EOD_1)
1846 STps->eof = ST_EOM_OK;
1847 else if (STps->eof != ST_EOM_OK)
1848 STps->eof = ST_NOEOF;
1849 retval = total - count;
1850
1851 out:
1852 if (SRpnt != NULL)
Mike Christie8b05b772005-11-08 04:06:44 -06001853 st_release_request(SRpnt);
Kai Makisara787926b2005-11-13 10:04:44 +02001854 release_buffering(STp, 0);
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02001855 mutex_unlock(&STp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 return retval;
1858}
1859
1860/* Read data from the tape. Returns zero in the normal case, one if the
1861 eof status has changed, and the negative error code in case of a
1862 fatal error. Otherwise updates the buffer and the eof state.
1863
1864 Does release user buffer mapping if it is set.
1865*/
1866static long read_tape(struct scsi_tape *STp, long count,
Mike Christie8b05b772005-11-08 04:06:44 -06001867 struct st_request ** aSRpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 int transfer, blks, bytes;
1870 unsigned char cmd[MAX_COMMAND_SIZE];
Mike Christie8b05b772005-11-08 04:06:44 -06001871 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct st_modedef *STm;
1873 struct st_partstat *STps;
1874 struct st_buffer *STbp;
1875 int retval = 0;
1876 char *name = tape_name(STp);
1877
1878 if (count == 0)
1879 return 0;
1880
1881 STm = &(STp->modes[STp->current_mode]);
1882 STps = &(STp->ps[STp->partition]);
1883 if (STps->eof == ST_FM_HIT)
1884 return 1;
1885 STbp = STp->buffer;
1886
1887 if (STp->block_size == 0)
1888 blks = bytes = count;
1889 else {
Kai Makisara9abe16c2007-02-03 13:21:29 +02001890 if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 blks = (STp->buffer)->buffer_blocks;
1892 bytes = blks * STp->block_size;
1893 } else {
1894 bytes = count;
1895 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1896 bytes = (STp->buffer)->buffer_size;
1897 blks = bytes / STp->block_size;
1898 bytes = blks * STp->block_size;
1899 }
1900 }
1901
1902 memset(cmd, 0, MAX_COMMAND_SIZE);
1903 cmd[0] = READ_6;
1904 cmd[1] = (STp->block_size != 0);
Kai Makisara40f6b362008-02-24 22:23:24 +02001905 if (!cmd[1] && STp->sili)
1906 cmd[1] |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 cmd[2] = blks >> 16;
1908 cmd[3] = blks >> 8;
1909 cmd[4] = blks;
1910
1911 SRpnt = *aSRpnt;
1912 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
James Bottomleya02488e2008-11-30 10:36:26 -06001913 STp->device->request_queue->rq_timeout,
1914 MAX_RETRIES, 1);
Kai Makisara787926b2005-11-13 10:04:44 +02001915 release_buffering(STp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *aSRpnt = SRpnt;
1917 if (!SRpnt)
1918 return STbp->syscall_result;
1919
1920 STbp->read_pointer = 0;
1921 STps->at_sm = 0;
1922
1923 /* Something to check */
1924 if (STbp->syscall_result) {
1925 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1926
1927 retval = 1;
1928 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1929 name,
Mike Christie8b05b772005-11-08 04:06:44 -06001930 SRpnt->sense[0], SRpnt->sense[1],
1931 SRpnt->sense[2], SRpnt->sense[3],
1932 SRpnt->sense[4], SRpnt->sense[5],
1933 SRpnt->sense[6], SRpnt->sense[7]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (cmdstatp->have_sense) {
1935
1936 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1937 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1938
1939 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1940 /* Compute the residual count */
1941 if (cmdstatp->remainder_valid)
1942 transfer = (int)cmdstatp->uremainder64;
1943 else
1944 transfer = 0;
1945 if (STp->block_size == 0 &&
1946 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1947 transfer = bytes;
1948
1949 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1950 if (STp->block_size == 0) {
1951 if (transfer <= 0) {
1952 if (transfer < 0)
1953 printk(KERN_NOTICE
1954 "%s: Failed to read %d byte block with %d byte transfer.\n",
1955 name, bytes - transfer, bytes);
1956 if (STps->drv_block >= 0)
1957 STps->drv_block += 1;
1958 STbp->buffer_bytes = 0;
1959 return (-ENOMEM);
1960 }
1961 STbp->buffer_bytes = bytes - transfer;
1962 } else {
Mike Christie8b05b772005-11-08 04:06:44 -06001963 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 SRpnt = *aSRpnt = NULL;
1965 if (transfer == blks) { /* We did not get anything, error */
1966 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1967 if (STps->drv_block >= 0)
1968 STps->drv_block += blks - transfer + 1;
1969 st_int_ioctl(STp, MTBSR, 1);
1970 return (-EIO);
1971 }
1972 /* We have some data, deliver it */
1973 STbp->buffer_bytes = (blks - transfer) *
1974 STp->block_size;
1975 DEBC(printk(ST_DEB_MSG
1976 "%s: ILI but enough data received %ld %d.\n",
1977 name, count, STbp->buffer_bytes));
1978 if (STps->drv_block >= 0)
1979 STps->drv_block += 1;
1980 if (st_int_ioctl(STp, MTBSR, 1))
1981 return (-EIO);
1982 }
1983 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1984 if (STps->eof != ST_FM_HIT)
1985 STps->eof = ST_FM_HIT;
1986 else
1987 STps->eof = ST_EOD_2;
1988 if (STp->block_size == 0)
1989 STbp->buffer_bytes = 0;
1990 else
1991 STbp->buffer_bytes =
1992 bytes - transfer * STp->block_size;
1993 DEBC(printk(ST_DEB_MSG
1994 "%s: EOF detected (%d bytes read).\n",
1995 name, STbp->buffer_bytes));
1996 } else if (cmdstatp->flags & SENSE_EOM) {
1997 if (STps->eof == ST_FM)
1998 STps->eof = ST_EOD_1;
1999 else
2000 STps->eof = ST_EOM_OK;
2001 if (STp->block_size == 0)
2002 STbp->buffer_bytes = bytes - transfer;
2003 else
2004 STbp->buffer_bytes =
2005 bytes - transfer * STp->block_size;
2006
2007 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
2008 name, STbp->buffer_bytes));
2009 }
2010 }
2011 /* end of EOF, EOM, ILI test */
2012 else { /* nonzero sense key */
2013 DEBC(printk(ST_DEB_MSG
2014 "%s: Tape error while reading.\n", name));
2015 STps->drv_block = (-1);
2016 if (STps->eof == ST_FM &&
2017 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
2018 DEBC(printk(ST_DEB_MSG
2019 "%s: Zero returned for first BLANK CHECK after EOF.\n",
2020 name));
2021 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
2022 } else /* Some other extended sense code */
2023 retval = (-EIO);
2024 }
2025
2026 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
2027 STbp->buffer_bytes = 0;
2028 }
2029 /* End of extended sense test */
2030 else { /* Non-extended sense */
2031 retval = STbp->syscall_result;
2032 }
2033
2034 }
2035 /* End of error handling */
Kai Makisara40f6b362008-02-24 22:23:24 +02002036 else { /* Read successful */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 STbp->buffer_bytes = bytes;
Kai Makisara40f6b362008-02-24 22:23:24 +02002038 if (STp->sili) /* In fixed block mode residual is always zero here */
2039 STbp->buffer_bytes -= STp->buffer->cmdstat.residual;
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 if (STps->drv_block >= 0) {
2043 if (STp->block_size == 0)
2044 STps->drv_block++;
2045 else
2046 STps->drv_block += STbp->buffer_bytes / STp->block_size;
2047 }
2048 return retval;
2049}
2050
2051
2052/* Read command */
2053static ssize_t
2054st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
2055{
2056 ssize_t total;
2057 ssize_t retval = 0;
2058 ssize_t i, transfer;
2059 int special, do_dio = 0;
Mike Christie8b05b772005-11-08 04:06:44 -06002060 struct st_request *SRpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct scsi_tape *STp = filp->private_data;
2062 struct st_modedef *STm;
2063 struct st_partstat *STps;
2064 struct st_buffer *STbp = STp->buffer;
2065 DEB( char *name = tape_name(STp); )
2066
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02002067 if (mutex_lock_interruptible(&STp->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -ERESTARTSYS;
2069
2070 retval = rw_checks(STp, filp, count);
2071 if (retval || count == 0)
2072 goto out;
2073
2074 STm = &(STp->modes[STp->current_mode]);
Kai Makisara9abe16c2007-02-03 13:21:29 +02002075 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
2076 if (!STm->do_read_ahead) {
2077 retval = (-EINVAL); /* Read must be integral number of blocks */
2078 goto out;
2079 }
2080 STp->try_dio_now = 0; /* Direct i/o can't handle split blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082
2083 STps = &(STp->ps[STp->partition]);
2084 if (STps->rw == ST_WRITING) {
2085 retval = flush_buffer(STp, 0);
2086 if (retval)
2087 goto out;
2088 STps->rw = ST_READING;
2089 }
2090 DEB(
2091 if (debugging && STps->eof != ST_NOEOF)
2092 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
2093 STps->eof, STbp->buffer_bytes);
2094 ) /* end DEB */
2095
2096 retval = setup_buffering(STp, buf, count, 1);
2097 if (retval)
2098 goto out;
2099 do_dio = STbp->do_dio;
2100
2101 if (STbp->buffer_bytes == 0 &&
2102 STps->eof >= ST_EOD_1) {
2103 if (STps->eof < ST_EOD) {
2104 STps->eof += 1;
2105 retval = 0;
2106 goto out;
2107 }
2108 retval = (-EIO); /* EOM or Blank Check */
2109 goto out;
2110 }
2111
2112 if (do_dio) {
2113 /* Check the buffer writability before any tape movement. Don't alter
2114 buffer data. */
2115 if (copy_from_user(&i, buf, 1) != 0 ||
2116 copy_to_user(buf, &i, 1) != 0 ||
2117 copy_from_user(&i, buf + count - 1, 1) != 0 ||
2118 copy_to_user(buf + count - 1, &i, 1) != 0) {
2119 retval = (-EFAULT);
2120 goto out;
2121 }
2122 }
2123
2124 STps->rw = ST_READING;
2125
2126
2127 /* Loop until enough data in buffer or a special condition found */
2128 for (total = 0, special = 0; total < count && !special;) {
2129
2130 /* Get new data if the buffer is empty */
2131 if (STbp->buffer_bytes == 0) {
2132 special = read_tape(STp, count - total, &SRpnt);
2133 if (special < 0) { /* No need to continue read */
2134 retval = special;
2135 goto out;
2136 }
2137 }
2138
2139 /* Move the data from driver buffer to user buffer */
2140 if (STbp->buffer_bytes > 0) {
2141 DEB(
2142 if (debugging && STps->eof != ST_NOEOF)
2143 printk(ST_DEB_MSG
2144 "%s: EOF up (%d). Left %d, needed %d.\n", name,
2145 STps->eof, STbp->buffer_bytes,
2146 (int)(count - total));
2147 ) /* end DEB */
2148 transfer = STbp->buffer_bytes < count - total ?
2149 STbp->buffer_bytes : count - total;
2150 if (!do_dio) {
2151 i = from_buffer(STbp, buf, transfer);
2152 if (i) {
2153 retval = i;
2154 goto out;
2155 }
2156 }
2157 buf += transfer;
2158 total += transfer;
2159 }
2160
2161 if (STp->block_size == 0)
2162 break; /* Read only one variable length block */
2163
2164 } /* for (total = 0, special = 0;
2165 total < count && !special; ) */
2166
2167 /* Change the eof state if no data from tape or buffer */
2168 if (total == 0) {
2169 if (STps->eof == ST_FM_HIT) {
2170 STps->eof = ST_FM;
2171 STps->drv_block = 0;
2172 if (STps->drv_file >= 0)
2173 STps->drv_file++;
2174 } else if (STps->eof == ST_EOD_1) {
2175 STps->eof = ST_EOD_2;
2176 STps->drv_block = 0;
2177 if (STps->drv_file >= 0)
2178 STps->drv_file++;
2179 } else if (STps->eof == ST_EOD_2)
2180 STps->eof = ST_EOD;
2181 } else if (STps->eof == ST_FM)
2182 STps->eof = ST_NOEOF;
2183 retval = total;
2184
2185 out:
2186 if (SRpnt != NULL) {
Mike Christie8b05b772005-11-08 04:06:44 -06002187 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 SRpnt = NULL;
2189 }
2190 if (do_dio) {
Kai Makisara787926b2005-11-13 10:04:44 +02002191 release_buffering(STp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 STbp->buffer_bytes = 0;
2193 }
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02002194 mutex_unlock(&STp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 return retval;
2197}
2198
2199
2200
2201DEB(
2202/* Set the driver options */
2203static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2204{
2205 if (debugging) {
2206 printk(KERN_INFO
2207 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2208 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2209 STm->do_read_ahead);
2210 printk(KERN_INFO
2211 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2212 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2213 printk(KERN_INFO
2214 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2215 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2216 STp->scsi2_logical);
2217 printk(KERN_INFO
Kai Makisara40f6b362008-02-24 22:23:24 +02002218 "%s: sysv: %d nowait: %d sili: %d\n", name, STm->sysv, STp->immediate,
2219 STp->sili);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 printk(KERN_INFO "%s: debugging: %d\n",
2221 name, debugging);
2222 }
2223}
2224 )
2225
2226
2227static int st_set_options(struct scsi_tape *STp, long options)
2228{
2229 int value;
2230 long code;
2231 struct st_modedef *STm;
2232 char *name = tape_name(STp);
2233 struct cdev *cd0, *cd1;
2234
2235 STm = &(STp->modes[STp->current_mode]);
2236 if (!STm->defined) {
2237 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2238 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2239 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2240 modes_defined = 1;
2241 DEBC(printk(ST_DEB_MSG
2242 "%s: Initialized mode %d definition from mode 0\n",
2243 name, STp->current_mode));
2244 }
2245
2246 code = options & MT_ST_OPTIONS;
2247 if (code == MT_ST_BOOLEANS) {
2248 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2249 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2250 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2251 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2252 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2253 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2254 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2255 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2256 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2257 if ((STp->device)->scsi_level >= SCSI_2)
2258 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2259 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2260 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2261 STm->sysv = (options & MT_ST_SYSV) != 0;
Kai Makisara40f6b362008-02-24 22:23:24 +02002262 STp->sili = (options & MT_ST_SILI) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2264 st_log_options(STp, STm, name); )
2265 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2266 value = (code == MT_ST_SETBOOLEANS);
2267 if ((options & MT_ST_BUFFER_WRITES) != 0)
2268 STm->do_buffer_writes = value;
2269 if ((options & MT_ST_ASYNC_WRITES) != 0)
2270 STm->do_async_writes = value;
2271 if ((options & MT_ST_DEF_WRITES) != 0)
2272 STm->defaults_for_writes = value;
2273 if ((options & MT_ST_READ_AHEAD) != 0)
2274 STm->do_read_ahead = value;
2275 if ((options & MT_ST_TWO_FM) != 0)
2276 STp->two_fm = value;
2277 if ((options & MT_ST_FAST_MTEOM) != 0)
2278 STp->fast_mteom = value;
2279 if ((options & MT_ST_AUTO_LOCK) != 0)
2280 STp->do_auto_lock = value;
2281 if ((options & MT_ST_CAN_BSR) != 0)
2282 STp->can_bsr = value;
2283 if ((options & MT_ST_NO_BLKLIMS) != 0)
2284 STp->omit_blklims = value;
2285 if ((STp->device)->scsi_level >= SCSI_2 &&
2286 (options & MT_ST_CAN_PARTITIONS) != 0)
2287 STp->can_partitions = value;
2288 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2289 STp->scsi2_logical = value;
2290 if ((options & MT_ST_NOWAIT) != 0)
2291 STp->immediate = value;
2292 if ((options & MT_ST_SYSV) != 0)
2293 STm->sysv = value;
Kai Makisara40f6b362008-02-24 22:23:24 +02002294 if ((options & MT_ST_SILI) != 0)
2295 STp->sili = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 DEB(
2297 if ((options & MT_ST_DEBUGGING) != 0)
2298 debugging = value;
2299 st_log_options(STp, STm, name); )
2300 } else if (code == MT_ST_WRITE_THRESHOLD) {
2301 /* Retained for compatibility */
2302 } else if (code == MT_ST_DEF_BLKSIZE) {
2303 value = (options & ~MT_ST_OPTIONS);
2304 if (value == ~MT_ST_OPTIONS) {
2305 STm->default_blksize = (-1);
2306 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2307 } else {
2308 STm->default_blksize = value;
2309 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2310 name, STm->default_blksize));
2311 if (STp->ready == ST_READY) {
2312 STp->blksize_changed = 0;
2313 set_mode_densblk(STp, STm);
2314 }
2315 }
2316 } else if (code == MT_ST_TIMEOUTS) {
2317 value = (options & ~MT_ST_OPTIONS);
2318 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2319 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2320 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2321 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2322 } else {
James Bottomleya02488e2008-11-30 10:36:26 -06002323 blk_queue_rq_timeout(STp->device->request_queue,
2324 value * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2326 name, value) );
2327 }
2328 } else if (code == MT_ST_SET_CLN) {
2329 value = (options & ~MT_ST_OPTIONS) & 0xff;
2330 if (value != 0 &&
2331 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2332 return (-EINVAL);
2333 STp->cln_mode = value;
2334 STp->cln_sense_mask = (options >> 8) & 0xff;
2335 STp->cln_sense_value = (options >> 16) & 0xff;
2336 printk(KERN_INFO
2337 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2338 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2339 } else if (code == MT_ST_DEF_OPTIONS) {
2340 code = (options & ~MT_ST_CLEAR_DEFAULT);
2341 value = (options & MT_ST_CLEAR_DEFAULT);
2342 if (code == MT_ST_DEF_DENSITY) {
2343 if (value == MT_ST_CLEAR_DEFAULT) {
2344 STm->default_density = (-1);
2345 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2346 name));
2347 } else {
2348 STm->default_density = value & 0xff;
2349 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2350 name, STm->default_density));
2351 if (STp->ready == ST_READY) {
2352 STp->density_changed = 0;
2353 set_mode_densblk(STp, STm);
2354 }
2355 }
2356 } else if (code == MT_ST_DEF_DRVBUFFER) {
2357 if (value == MT_ST_CLEAR_DEFAULT) {
2358 STp->default_drvbuffer = 0xff;
2359 DEBC( printk(KERN_INFO
2360 "%s: Drive buffer default disabled.\n", name));
2361 } else {
2362 STp->default_drvbuffer = value & 7;
2363 DEBC( printk(KERN_INFO
2364 "%s: Drive buffer default set to %x\n",
2365 name, STp->default_drvbuffer));
2366 if (STp->ready == ST_READY)
2367 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2368 }
2369 } else if (code == MT_ST_DEF_COMPRESSION) {
2370 if (value == MT_ST_CLEAR_DEFAULT) {
2371 STm->default_compression = ST_DONT_TOUCH;
2372 DEBC( printk(KERN_INFO
2373 "%s: Compression default disabled.\n", name));
2374 } else {
2375 if ((value & 0xff00) != 0) {
2376 STp->c_algo = (value & 0xff00) >> 8;
2377 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2378 name, STp->c_algo));
2379 }
2380 if ((value & 0xff) != 0xff) {
2381 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2382 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2383 name, (value & 1)));
2384 if (STp->ready == ST_READY) {
2385 STp->compression_changed = 0;
2386 st_compression(STp, (STm->default_compression == ST_YES));
2387 }
2388 }
2389 }
2390 }
2391 } else
2392 return (-EIO);
2393
2394 return 0;
2395}
2396
2397#define MODE_HEADER_LENGTH 4
2398
2399/* Mode header and page byte offsets */
2400#define MH_OFF_DATA_LENGTH 0
2401#define MH_OFF_MEDIUM_TYPE 1
2402#define MH_OFF_DEV_SPECIFIC 2
2403#define MH_OFF_BDESCS_LENGTH 3
2404#define MP_OFF_PAGE_NBR 0
2405#define MP_OFF_PAGE_LENGTH 1
2406
2407/* Mode header and page bit masks */
2408#define MH_BIT_WP 0x80
2409#define MP_MSK_PAGE_NBR 0x3f
2410
2411/* Don't return block descriptors */
2412#define MODE_SENSE_OMIT_BDESCS 0x08
2413
2414#define MODE_SELECT_PAGE_FORMAT 0x10
2415
2416/* Read a mode page into the tape buffer. The block descriptors are included
2417 if incl_block_descs is true. The page control is ored to the page number
2418 parameter, if necessary. */
2419static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2420{
2421 unsigned char cmd[MAX_COMMAND_SIZE];
FUJITA Tomonori8ecf0d92008-12-05 15:25:28 +09002422 struct st_request *SRpnt;
2423 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 memset(cmd, 0, MAX_COMMAND_SIZE);
2426 cmd[0] = MODE_SENSE;
2427 if (omit_block_descs)
2428 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2429 cmd[2] = page;
2430 cmd[4] = 255;
2431
FUJITA Tomonori8ecf0d92008-12-05 15:25:28 +09002432 SRpnt = st_allocate_request(STp);
2433 if (!SRpnt)
2434 return STp->buffer->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
FUJITA Tomonori8ecf0d92008-12-05 15:25:28 +09002436 ret = st_scsi_kern_execute(SRpnt, cmd, DMA_FROM_DEVICE,
2437 STp->buffer->b_data, cmd[4],
2438 STp->device->request_queue->rq_timeout,
2439 MAX_RETRIES);
Mike Christie8b05b772005-11-08 04:06:44 -06002440 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
FUJITA Tomonori8ecf0d92008-12-05 15:25:28 +09002442 return ret ? : STp->buffer->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
2445
2446/* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2447 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2448static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2449{
FUJITA Tomonori18c87012008-12-05 15:25:29 +09002450 int pgo, timeout, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 unsigned char cmd[MAX_COMMAND_SIZE];
FUJITA Tomonori18c87012008-12-05 15:25:29 +09002452 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 memset(cmd, 0, MAX_COMMAND_SIZE);
2455 cmd[0] = MODE_SELECT;
2456 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2457 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2458 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2459
2460 /* Clear reserved fields */
2461 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2462 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2463 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2464 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2465
FUJITA Tomonori18c87012008-12-05 15:25:29 +09002466 SRpnt = st_allocate_request(STp);
2467 if (!SRpnt)
2468 return ret;
2469
2470 timeout = slow ? STp->long_timeout :
2471 STp->device->request_queue->rq_timeout;
2472
2473 ret = st_scsi_kern_execute(SRpnt, cmd, DMA_TO_DEVICE,
2474 STp->buffer->b_data, cmd[4], timeout, 0);
2475 if (!ret)
2476 ret = STp->buffer->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Mike Christie8b05b772005-11-08 04:06:44 -06002478 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
FUJITA Tomonori18c87012008-12-05 15:25:29 +09002480 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
2483
2484#define COMPRESSION_PAGE 0x0f
2485#define COMPRESSION_PAGE_LENGTH 16
2486
2487#define CP_OFF_DCE_DCC 2
2488#define CP_OFF_C_ALGO 7
2489
2490#define DCE_MASK 0x80
2491#define DCC_MASK 0x40
2492#define RED_MASK 0x60
2493
2494
2495/* Control the compression with mode page 15. Algorithm not changed if zero.
2496
2497 The block descriptors are read and written because Sony SDT-7000 does not
2498 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2499 Including block descriptors should not cause any harm to other drives. */
2500
2501static int st_compression(struct scsi_tape * STp, int state)
2502{
2503 int retval;
2504 int mpoffs; /* Offset to mode page start */
2505 unsigned char *b_data = (STp->buffer)->b_data;
2506 DEB( char *name = tape_name(STp); )
2507
2508 if (STp->ready != ST_READY)
2509 return (-EIO);
2510
2511 /* Read the current page contents */
2512 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2513 if (retval) {
2514 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2515 name));
2516 return (-EIO);
2517 }
2518
2519 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2520 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2521 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2522
2523 /* Check if compression can be changed */
2524 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2525 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2526 return (-EIO);
2527 }
2528
2529 /* Do the change */
2530 if (state) {
2531 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2532 if (STp->c_algo != 0)
2533 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2534 }
2535 else {
2536 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2537 if (STp->c_algo != 0)
2538 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2539 }
2540
2541 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2542 if (retval) {
2543 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2544 return (-EIO);
2545 }
2546 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2547 name, state));
2548
2549 STp->compression_changed = 1;
2550 return 0;
2551}
2552
2553
2554/* Process the load and unload commands (does unload if the load code is zero) */
2555static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2556{
2557 int retval = (-EIO), timeout;
2558 DEB( char *name = tape_name(STp); )
2559 unsigned char cmd[MAX_COMMAND_SIZE];
2560 struct st_partstat *STps;
Mike Christie8b05b772005-11-08 04:06:44 -06002561 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 if (STp->ready != ST_READY && !load_code) {
2564 if (STp->ready == ST_NO_TAPE)
2565 return (-ENOMEDIUM);
2566 else
2567 return (-EIO);
2568 }
2569
2570 memset(cmd, 0, MAX_COMMAND_SIZE);
2571 cmd[0] = START_STOP;
2572 if (load_code)
2573 cmd[4] |= 1;
2574 /*
2575 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2576 */
2577 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2578 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2579 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2580 name, (cmd[4]) ? "" : "un",
2581 load_code - MT_ST_HPLOADER_OFFSET));
2582 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2583 }
2584 if (STp->immediate) {
2585 cmd[1] = 1; /* Don't wait for completion */
James Bottomleya02488e2008-11-30 10:36:26 -06002586 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 else
2589 timeout = STp->long_timeout;
2590
2591 DEBC(
2592 if (!load_code)
2593 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2594 else
2595 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2596 );
2597
FUJITA Tomonori15c920a2008-12-05 15:25:24 +09002598 SRpnt = st_allocate_request(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (!SRpnt)
FUJITA Tomonori15c920a2008-12-05 15:25:24 +09002600 return STp->buffer->syscall_result;
2601
2602 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0, timeout,
2603 MAX_RETRIES);
2604 if (retval)
2605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 retval = (STp->buffer)->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 if (!retval) { /* SCSI command successful */
2610
2611 if (!load_code) {
2612 STp->rew_at_close = 0;
2613 STp->ready = ST_NO_TAPE;
2614 }
2615 else {
2616 STp->rew_at_close = STp->autorew_dev;
2617 retval = check_tape(STp, filp);
2618 if (retval > 0)
2619 retval = 0;
2620 }
2621 }
2622 else {
2623 STps = &(STp->ps[STp->partition]);
2624 STps->drv_file = STps->drv_block = (-1);
2625 }
FUJITA Tomonori15c920a2008-12-05 15:25:24 +09002626out:
2627 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 return retval;
2630}
2631
2632#if DEBUG
2633#define ST_DEB_FORWARD 0
2634#define ST_DEB_BACKWARD 1
2635static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2636{
2637 s32 sc;
2638
2639 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2640 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2641 if (direction)
2642 sc = -sc;
2643 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2644 direction ? "backward" : "forward", sc, units);
2645}
2646#endif
2647
2648
2649/* Internal ioctl function */
2650static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2651{
2652 int timeout;
2653 long ltmp;
2654 int ioctl_result;
2655 int chg_eof = 1;
2656 unsigned char cmd[MAX_COMMAND_SIZE];
Mike Christie8b05b772005-11-08 04:06:44 -06002657 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 struct st_partstat *STps;
2659 int fileno, blkno, at_sm, undone;
2660 int datalen = 0, direction = DMA_NONE;
2661 char *name = tape_name(STp);
2662
2663 WARN_ON(STp->buffer->do_dio != 0);
2664 if (STp->ready != ST_READY) {
2665 if (STp->ready == ST_NO_TAPE)
2666 return (-ENOMEDIUM);
2667 else
2668 return (-EIO);
2669 }
2670 timeout = STp->long_timeout;
2671 STps = &(STp->ps[STp->partition]);
2672 fileno = STps->drv_file;
2673 blkno = STps->drv_block;
2674 at_sm = STps->at_sm;
2675
2676 memset(cmd, 0, MAX_COMMAND_SIZE);
2677 switch (cmd_in) {
2678 case MTFSFM:
2679 chg_eof = 0; /* Changed from the FSF after this */
2680 case MTFSF:
2681 cmd[0] = SPACE;
2682 cmd[1] = 0x01; /* Space FileMarks */
2683 cmd[2] = (arg >> 16);
2684 cmd[3] = (arg >> 8);
2685 cmd[4] = arg;
2686 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2687 if (fileno >= 0)
2688 fileno += arg;
2689 blkno = 0;
2690 at_sm &= (arg == 0);
2691 break;
2692 case MTBSFM:
2693 chg_eof = 0; /* Changed from the FSF after this */
2694 case MTBSF:
2695 cmd[0] = SPACE;
2696 cmd[1] = 0x01; /* Space FileMarks */
2697 ltmp = (-arg);
2698 cmd[2] = (ltmp >> 16);
2699 cmd[3] = (ltmp >> 8);
2700 cmd[4] = ltmp;
2701 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2702 if (fileno >= 0)
2703 fileno -= arg;
2704 blkno = (-1); /* We can't know the block number */
2705 at_sm &= (arg == 0);
2706 break;
2707 case MTFSR:
2708 cmd[0] = SPACE;
2709 cmd[1] = 0x00; /* Space Blocks */
2710 cmd[2] = (arg >> 16);
2711 cmd[3] = (arg >> 8);
2712 cmd[4] = arg;
2713 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2714 if (blkno >= 0)
2715 blkno += arg;
2716 at_sm &= (arg == 0);
2717 break;
2718 case MTBSR:
2719 cmd[0] = SPACE;
2720 cmd[1] = 0x00; /* Space Blocks */
2721 ltmp = (-arg);
2722 cmd[2] = (ltmp >> 16);
2723 cmd[3] = (ltmp >> 8);
2724 cmd[4] = ltmp;
2725 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2726 if (blkno >= 0)
2727 blkno -= arg;
2728 at_sm &= (arg == 0);
2729 break;
2730 case MTFSS:
2731 cmd[0] = SPACE;
2732 cmd[1] = 0x04; /* Space Setmarks */
2733 cmd[2] = (arg >> 16);
2734 cmd[3] = (arg >> 8);
2735 cmd[4] = arg;
2736 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2737 if (arg != 0) {
2738 blkno = fileno = (-1);
2739 at_sm = 1;
2740 }
2741 break;
2742 case MTBSS:
2743 cmd[0] = SPACE;
2744 cmd[1] = 0x04; /* Space Setmarks */
2745 ltmp = (-arg);
2746 cmd[2] = (ltmp >> 16);
2747 cmd[3] = (ltmp >> 8);
2748 cmd[4] = ltmp;
2749 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2750 if (arg != 0) {
2751 blkno = fileno = (-1);
2752 at_sm = 1;
2753 }
2754 break;
2755 case MTWEOF:
2756 case MTWSM:
2757 if (STp->write_prot)
2758 return (-EACCES);
2759 cmd[0] = WRITE_FILEMARKS;
2760 if (cmd_in == MTWSM)
2761 cmd[1] = 2;
2762 cmd[2] = (arg >> 16);
2763 cmd[3] = (arg >> 8);
2764 cmd[4] = arg;
James Bottomleya02488e2008-11-30 10:36:26 -06002765 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 DEBC(
2767 if (cmd_in == MTWEOF)
2768 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2769 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2770 else
2771 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2772 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2773 )
2774 if (fileno >= 0)
2775 fileno += arg;
2776 blkno = 0;
2777 at_sm = (cmd_in == MTWSM);
2778 break;
2779 case MTREW:
2780 cmd[0] = REZERO_UNIT;
2781 if (STp->immediate) {
2782 cmd[1] = 1; /* Don't wait for completion */
James Bottomleya02488e2008-11-30 10:36:26 -06002783 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2786 fileno = blkno = at_sm = 0;
2787 break;
2788 case MTNOP:
2789 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2790 return 0; /* Should do something ? */
2791 break;
2792 case MTRETEN:
2793 cmd[0] = START_STOP;
2794 if (STp->immediate) {
2795 cmd[1] = 1; /* Don't wait for completion */
James Bottomleya02488e2008-11-30 10:36:26 -06002796 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798 cmd[4] = 3;
2799 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2800 fileno = blkno = at_sm = 0;
2801 break;
2802 case MTEOM:
2803 if (!STp->fast_mteom) {
2804 /* space to the end of tape */
2805 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2806 fileno = STps->drv_file;
2807 if (STps->eof >= ST_EOD_1)
2808 return 0;
2809 /* The next lines would hide the number of spaced FileMarks
2810 That's why I inserted the previous lines. I had no luck
2811 with detecting EOM with FSF, so we go now to EOM.
2812 Joerg Weule */
2813 } else
2814 fileno = (-1);
2815 cmd[0] = SPACE;
2816 cmd[1] = 3;
2817 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2818 name));
2819 blkno = -1;
2820 at_sm = 0;
2821 break;
2822 case MTERASE:
2823 if (STp->write_prot)
2824 return (-EACCES);
2825 cmd[0] = ERASE;
2826 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2827 if (STp->immediate) {
2828 cmd[1] |= 2; /* Don't wait for completion */
James Bottomleya02488e2008-11-30 10:36:26 -06002829 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
2831 else
2832 timeout = STp->long_timeout * 8;
2833
2834 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2835 fileno = blkno = at_sm = 0;
2836 break;
2837 case MTSETBLK: /* Set block length */
2838 case MTSETDENSITY: /* Set tape density */
2839 case MTSETDRVBUFFER: /* Set drive buffering */
2840 case SET_DENS_AND_BLK: /* Set density and block size */
2841 chg_eof = 0;
2842 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2843 return (-EIO); /* Not allowed if data in buffer */
2844 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2845 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2846 STp->max_block > 0 &&
2847 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2848 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2849 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2850 return (-EINVAL);
2851 }
2852 cmd[0] = MODE_SELECT;
2853 if ((STp->use_pf & USE_PF))
2854 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2855 cmd[4] = datalen = 12;
2856 direction = DMA_TO_DEVICE;
2857
2858 memset((STp->buffer)->b_data, 0, 12);
2859 if (cmd_in == MTSETDRVBUFFER)
2860 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2861 else
2862 (STp->buffer)->b_data[2] =
2863 STp->drv_buffer << 4;
2864 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2865 if (cmd_in == MTSETDENSITY) {
2866 (STp->buffer)->b_data[4] = arg;
2867 STp->density_changed = 1; /* At least we tried ;-) */
2868 } else if (cmd_in == SET_DENS_AND_BLK)
2869 (STp->buffer)->b_data[4] = arg >> 24;
2870 else
2871 (STp->buffer)->b_data[4] = STp->density;
2872 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2873 ltmp = arg & MT_ST_BLKSIZE_MASK;
2874 if (cmd_in == MTSETBLK)
2875 STp->blksize_changed = 1; /* At least we tried ;-) */
2876 } else
2877 ltmp = STp->block_size;
2878 (STp->buffer)->b_data[9] = (ltmp >> 16);
2879 (STp->buffer)->b_data[10] = (ltmp >> 8);
2880 (STp->buffer)->b_data[11] = ltmp;
James Bottomleya02488e2008-11-30 10:36:26 -06002881 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 DEBC(
2883 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2884 printk(ST_DEB_MSG
2885 "%s: Setting block size to %d bytes.\n", name,
2886 (STp->buffer)->b_data[9] * 65536 +
2887 (STp->buffer)->b_data[10] * 256 +
2888 (STp->buffer)->b_data[11]);
2889 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2890 printk(ST_DEB_MSG
2891 "%s: Setting density code to %x.\n", name,
2892 (STp->buffer)->b_data[4]);
2893 if (cmd_in == MTSETDRVBUFFER)
2894 printk(ST_DEB_MSG
2895 "%s: Setting drive buffer code to %d.\n", name,
2896 ((STp->buffer)->b_data[2] >> 4) & 7);
2897 )
2898 break;
2899 default:
2900 return (-ENOSYS);
2901 }
2902
FUJITA Tomonoriccc607f2008-12-05 15:25:31 +09002903 SRpnt = st_allocate_request(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (!SRpnt)
2905 return (STp->buffer)->syscall_result;
2906
FUJITA Tomonoriccc607f2008-12-05 15:25:31 +09002907 ioctl_result = st_scsi_kern_execute(SRpnt, cmd, direction,
2908 STp->buffer->b_data, datalen,
2909 timeout, MAX_RETRIES);
2910 if (!ioctl_result)
2911 ioctl_result = (STp->buffer)->syscall_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 if (!ioctl_result) { /* SCSI command successful */
Mike Christie8b05b772005-11-08 04:06:44 -06002914 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 SRpnt = NULL;
2916 STps->drv_block = blkno;
2917 STps->drv_file = fileno;
2918 STps->at_sm = at_sm;
2919
2920 if (cmd_in == MTBSFM)
2921 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2922 else if (cmd_in == MTFSFM)
2923 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2924
2925 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2926 int old_block_size = STp->block_size;
2927 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2928 if (STp->block_size != 0) {
2929 if (old_block_size == 0)
2930 normalize_buffer(STp->buffer);
2931 (STp->buffer)->buffer_blocks =
2932 (STp->buffer)->buffer_size / STp->block_size;
2933 }
2934 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2935 if (cmd_in == SET_DENS_AND_BLK)
2936 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2937 } else if (cmd_in == MTSETDRVBUFFER)
2938 STp->drv_buffer = (arg & 7);
2939 else if (cmd_in == MTSETDENSITY)
2940 STp->density = arg;
2941
2942 if (cmd_in == MTEOM)
2943 STps->eof = ST_EOD;
2944 else if (cmd_in == MTFSF)
2945 STps->eof = ST_FM;
2946 else if (chg_eof)
2947 STps->eof = ST_NOEOF;
2948
2949 if (cmd_in == MTWEOF)
2950 STps->rw = ST_IDLE;
2951 } else { /* SCSI command was not completely successful. Don't return
2952 from this block without releasing the SCSI command block! */
2953 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2954
2955 if (cmdstatp->flags & SENSE_EOM) {
2956 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2957 cmd_in != MTBSR && cmd_in != MTBSS)
2958 STps->eof = ST_EOM_OK;
2959 STps->drv_block = 0;
2960 }
2961
2962 if (cmdstatp->remainder_valid)
2963 undone = (int)cmdstatp->uremainder64;
2964 else
2965 undone = 0;
2966
2967 if (cmd_in == MTWEOF &&
2968 cmdstatp->have_sense &&
Kai Makisara91614c02007-01-26 00:38:39 +02002969 (cmdstatp->flags & SENSE_EOM)) {
2970 if (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2971 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) {
2972 ioctl_result = 0; /* EOF(s) written successfully at EOM */
2973 STps->eof = ST_NOEOF;
2974 } else { /* Writing EOF(s) failed */
2975 if (fileno >= 0)
2976 fileno -= undone;
2977 if (undone < arg)
2978 STps->eof = ST_NOEOF;
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 STps->drv_file = fileno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2982 if (fileno >= 0)
2983 STps->drv_file = fileno - undone;
2984 else
2985 STps->drv_file = fileno;
2986 STps->drv_block = -1;
2987 STps->eof = ST_NOEOF;
2988 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2989 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2990 undone = (-undone);
2991 if (STps->drv_file >= 0)
2992 STps->drv_file = fileno + undone;
2993 STps->drv_block = 0;
2994 STps->eof = ST_NOEOF;
2995 } else if (cmd_in == MTFSR) {
2996 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2997 if (STps->drv_file >= 0)
2998 STps->drv_file++;
2999 STps->drv_block = 0;
3000 STps->eof = ST_FM;
3001 } else {
3002 if (blkno >= undone)
3003 STps->drv_block = blkno - undone;
3004 else
3005 STps->drv_block = (-1);
3006 STps->eof = ST_NOEOF;
3007 }
3008 } else if (cmd_in == MTBSR) {
3009 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
3010 STps->drv_file--;
3011 STps->drv_block = (-1);
3012 } else {
3013 if (arg > 0 && undone < 0) /* Some drives get this wrong */
3014 undone = (-undone);
3015 if (STps->drv_block >= 0)
3016 STps->drv_block = blkno + undone;
3017 }
3018 STps->eof = ST_NOEOF;
3019 } else if (cmd_in == MTEOM) {
3020 STps->drv_file = (-1);
3021 STps->drv_block = (-1);
3022 STps->eof = ST_EOD;
3023 } else if (cmd_in == MTSETBLK ||
3024 cmd_in == MTSETDENSITY ||
3025 cmd_in == MTSETDRVBUFFER ||
3026 cmd_in == SET_DENS_AND_BLK) {
3027 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
3028 !(STp->use_pf & PF_TESTED)) {
3029 /* Try the other possible state of Page Format if not
3030 already tried */
3031 STp->use_pf = !STp->use_pf | PF_TESTED;
Mike Christie8b05b772005-11-08 04:06:44 -06003032 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 SRpnt = NULL;
3034 return st_int_ioctl(STp, cmd_in, arg);
3035 }
3036 } else if (chg_eof)
3037 STps->eof = ST_NOEOF;
3038
3039 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
3040 STps->eof = ST_EOD;
3041
Mike Christie8b05b772005-11-08 04:06:44 -06003042 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 SRpnt = NULL;
3044 }
3045
3046 return ioctl_result;
3047}
3048
3049
3050/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
3051 structure. */
3052
3053static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
3054 int logical)
3055{
3056 int result;
3057 unsigned char scmd[MAX_COMMAND_SIZE];
Mike Christie8b05b772005-11-08 04:06:44 -06003058 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 DEB( char *name = tape_name(STp); )
3060
3061 if (STp->ready != ST_READY)
3062 return (-EIO);
3063
3064 memset(scmd, 0, MAX_COMMAND_SIZE);
3065 if ((STp->device)->scsi_level < SCSI_2) {
3066 scmd[0] = QFA_REQUEST_BLOCK;
3067 scmd[4] = 3;
3068 } else {
3069 scmd[0] = READ_POSITION;
3070 if (!logical && !STp->scsi2_logical)
3071 scmd[1] = 1;
3072 }
FUJITA Tomonori7a31ec32008-12-05 15:25:30 +09003073
3074 SRpnt = st_allocate_request(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (!SRpnt)
FUJITA Tomonori7a31ec32008-12-05 15:25:30 +09003076 return STp->buffer->syscall_result;
3077
3078 result = st_scsi_kern_execute(SRpnt, scmd, DMA_FROM_DEVICE,
3079 STp->buffer->b_data, 20,
3080 STp->device->request_queue->rq_timeout,
3081 MAX_READY_RETRIES);
3082 if (result)
3083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 if ((STp->buffer)->syscall_result != 0 ||
3086 (STp->device->scsi_level >= SCSI_2 &&
3087 ((STp->buffer)->b_data[0] & 4) != 0)) {
3088 *block = *partition = 0;
3089 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
3090 result = (-EIO);
3091 } else {
3092 result = 0;
3093 if ((STp->device)->scsi_level < SCSI_2) {
3094 *block = ((STp->buffer)->b_data[0] << 16)
3095 + ((STp->buffer)->b_data[1] << 8)
3096 + (STp->buffer)->b_data[2];
3097 *partition = 0;
3098 } else {
3099 *block = ((STp->buffer)->b_data[4] << 24)
3100 + ((STp->buffer)->b_data[5] << 16)
3101 + ((STp->buffer)->b_data[6] << 8)
3102 + (STp->buffer)->b_data[7];
3103 *partition = (STp->buffer)->b_data[1];
3104 if (((STp->buffer)->b_data[0] & 0x80) &&
3105 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
3106 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
3107 }
3108 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
3109 *block, *partition));
3110 }
FUJITA Tomonori7a31ec32008-12-05 15:25:30 +09003111out:
Mike Christie8b05b772005-11-08 04:06:44 -06003112 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 SRpnt = NULL;
3114
3115 return result;
3116}
3117
3118
3119/* Set the tape block and partition. Negative partition means that only the
3120 block should be set in vendor specific way. */
3121static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
3122 int logical)
3123{
3124 struct st_partstat *STps;
3125 int result, p;
3126 unsigned int blk;
3127 int timeout;
3128 unsigned char scmd[MAX_COMMAND_SIZE];
Mike Christie8b05b772005-11-08 04:06:44 -06003129 struct st_request *SRpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 DEB( char *name = tape_name(STp); )
3131
3132 if (STp->ready != ST_READY)
3133 return (-EIO);
3134 timeout = STp->long_timeout;
3135 STps = &(STp->ps[STp->partition]);
3136
3137 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
3138 name, block, partition));
3139 DEB(if (partition < 0)
3140 return (-EIO); )
3141
3142 /* Update the location at the partition we are leaving */
3143 if ((!STp->can_partitions && partition != 0) ||
3144 partition >= ST_NBR_PARTITIONS)
3145 return (-EINVAL);
3146 if (partition != STp->partition) {
3147 if (get_location(STp, &blk, &p, 1))
3148 STps->last_block_valid = 0;
3149 else {
3150 STps->last_block_valid = 1;
3151 STps->last_block_visited = blk;
3152 DEBC(printk(ST_DEB_MSG
3153 "%s: Visited block %d for partition %d saved.\n",
3154 name, blk, STp->partition));
3155 }
3156 }
3157
3158 memset(scmd, 0, MAX_COMMAND_SIZE);
3159 if ((STp->device)->scsi_level < SCSI_2) {
3160 scmd[0] = QFA_SEEK_BLOCK;
3161 scmd[2] = (block >> 16);
3162 scmd[3] = (block >> 8);
3163 scmd[4] = block;
3164 scmd[5] = 0;
3165 } else {
3166 scmd[0] = SEEK_10;
3167 scmd[3] = (block >> 24);
3168 scmd[4] = (block >> 16);
3169 scmd[5] = (block >> 8);
3170 scmd[6] = block;
3171 if (!logical && !STp->scsi2_logical)
3172 scmd[1] = 4;
3173 if (STp->partition != partition) {
3174 scmd[1] |= 2;
3175 scmd[8] = partition;
3176 DEBC(printk(ST_DEB_MSG
3177 "%s: Trying to change partition from %d to %d\n",
3178 name, STp->partition, partition));
3179 }
3180 }
3181 if (STp->immediate) {
3182 scmd[1] |= 1; /* Don't wait for completion */
James Bottomleya02488e2008-11-30 10:36:26 -06003183 timeout = STp->device->request_queue->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185
FUJITA Tomonori3c0bf162008-12-05 15:25:23 +09003186 SRpnt = st_allocate_request(STp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (!SRpnt)
FUJITA Tomonori3c0bf162008-12-05 15:25:23 +09003188 return STp->buffer->syscall_result;
3189
3190 result = st_scsi_kern_execute(SRpnt, scmd, DMA_NONE, NULL, 0,
3191 timeout, MAX_READY_RETRIES);
3192 if (result)
3193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 STps->drv_block = STps->drv_file = (-1);
3196 STps->eof = ST_NOEOF;
3197 if ((STp->buffer)->syscall_result != 0) {
3198 result = (-EIO);
3199 if (STp->can_partitions &&
3200 (STp->device)->scsi_level >= SCSI_2 &&
3201 (p = find_partition(STp)) >= 0)
3202 STp->partition = p;
3203 } else {
3204 if (STp->can_partitions) {
3205 STp->partition = partition;
3206 STps = &(STp->ps[partition]);
3207 if (!STps->last_block_valid ||
3208 STps->last_block_visited != block) {
3209 STps->at_sm = 0;
3210 STps->rw = ST_IDLE;
3211 }
3212 } else
3213 STps->at_sm = 0;
3214 if (block == 0)
3215 STps->drv_block = STps->drv_file = 0;
3216 result = 0;
3217 }
FUJITA Tomonori3c0bf162008-12-05 15:25:23 +09003218out:
Mike Christie8b05b772005-11-08 04:06:44 -06003219 st_release_request(SRpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 SRpnt = NULL;
3221
3222 return result;
3223}
3224
3225
3226/* Find the current partition number for the drive status. Called from open and
3227 returns either partition number of negative error code. */
3228static int find_partition(struct scsi_tape *STp)
3229{
3230 int i, partition;
3231 unsigned int block;
3232
3233 if ((i = get_location(STp, &block, &partition, 1)) < 0)
3234 return i;
3235 if (partition >= ST_NBR_PARTITIONS)
3236 return (-EIO);
3237 return partition;
3238}
3239
3240
3241/* Change the partition if necessary */
3242static int switch_partition(struct scsi_tape *STp)
3243{
3244 struct st_partstat *STps;
3245
3246 if (STp->partition == STp->new_partition)
3247 return 0;
3248 STps = &(STp->ps[STp->new_partition]);
3249 if (!STps->last_block_valid)
3250 STps->last_block_visited = 0;
3251 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3252}
3253
3254/* Functions for reading and writing the medium partition mode page. */
3255
3256#define PART_PAGE 0x11
3257#define PART_PAGE_FIXED_LENGTH 8
3258
3259#define PP_OFF_MAX_ADD_PARTS 2
3260#define PP_OFF_NBR_ADD_PARTS 3
3261#define PP_OFF_FLAGS 4
3262#define PP_OFF_PART_UNITS 6
3263#define PP_OFF_RESERVED 7
3264
3265#define PP_BIT_IDP 0x20
3266#define PP_MSK_PSUM_MB 0x10
3267
3268/* Get the number of partitions on the tape. As a side effect reads the
3269 mode page into the tape buffer. */
3270static int nbr_partitions(struct scsi_tape *STp)
3271{
3272 int result;
3273 DEB( char *name = tape_name(STp); )
3274
3275 if (STp->ready != ST_READY)
3276 return (-EIO);
3277
3278 result = read_mode_page(STp, PART_PAGE, 1);
3279
3280 if (result) {
3281 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3282 name));
3283 result = (-EIO);
3284 } else {
3285 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3286 PP_OFF_NBR_ADD_PARTS] + 1;
3287 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3288 }
3289
3290 return result;
3291}
3292
3293
3294/* Partition the tape into two partitions if size > 0 or one partition if
3295 size == 0.
3296
3297 The block descriptors are read and written because Sony SDT-7000 does not
3298 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3299
3300 My HP C1533A drive returns only one partition size field. This is used to
3301 set the size of partition 1. There is no size field for the default partition.
3302 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3303 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3304 The following algorithm is used to accommodate both drives: if the number of
3305 partition size fields is greater than the maximum number of additional partitions
3306 in the mode page, the second field is used. Otherwise the first field is used.
3307
3308 For Seagate DDS drives the page length must be 8 when no partitions is defined
3309 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3310 is acceptable also to some other old drives and enforced if the first partition
3311 size field is used for the first additional partition size.
3312 */
3313static int partition_tape(struct scsi_tape *STp, int size)
3314{
3315 char *name = tape_name(STp);
3316 int result;
3317 int pgo, psd_cnt, psdo;
3318 unsigned char *bp;
3319
3320 result = read_mode_page(STp, PART_PAGE, 0);
3321 if (result) {
3322 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3323 return result;
3324 }
3325 /* The mode page is in the buffer. Let's modify it and write it. */
3326 bp = (STp->buffer)->b_data;
3327 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3328 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3329 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3330
3331 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3332 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3333 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3334 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3335 psdo += 2;
3336 }
3337 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3338
3339 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3340 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3341 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3342
3343 if (size <= 0) {
3344 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3345 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3346 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3347 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3348 name));
3349 } else {
3350 bp[psdo] = (size >> 8) & 0xff;
3351 bp[psdo + 1] = size & 0xff;
3352 bp[pgo + 3] = 1;
3353 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3354 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3355 DEBC(printk(ST_DEB_MSG
3356 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3357 name, size));
3358 }
3359 bp[pgo + PP_OFF_PART_UNITS] = 0;
3360 bp[pgo + PP_OFF_RESERVED] = 0;
3361 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3362
3363 result = write_mode_page(STp, PART_PAGE, 1);
3364 if (result) {
3365 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3366 result = (-EIO);
3367 }
3368
3369 return result;
3370}
3371
3372
3373
3374/* The ioctl command */
Kai Makisarafd66c1b2008-01-17 22:45:22 +02003375static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
3377 int i, cmd_nr, cmd_type, bt;
3378 int retval = 0;
3379 unsigned int blk;
3380 struct scsi_tape *STp = file->private_data;
3381 struct st_modedef *STm;
3382 struct st_partstat *STps;
3383 char *name = tape_name(STp);
3384 void __user *p = (void __user *)arg;
3385
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02003386 if (mutex_lock_interruptible(&STp->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return -ERESTARTSYS;
3388
3389 DEB(
3390 if (debugging && !STp->in_use) {
3391 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3392 retval = (-EIO);
3393 goto out;
3394 } ) /* end DEB */
3395
3396 STm = &(STp->modes[STp->current_mode]);
3397 STps = &(STp->ps[STp->partition]);
3398
3399 /*
3400 * If we are in the middle of error recovery, don't let anyone
3401 * else try and use this device. Also, if error recovery fails, it
3402 * may try and take the device offline, in which case all further
3403 * access to the device is prohibited.
3404 */
Al Viro83ff6fe2008-03-02 08:15:49 -05003405 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p,
3406 file->f_flags & O_NDELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3408 goto out;
3409 retval = 0;
3410
3411 cmd_type = _IOC_TYPE(cmd_in);
3412 cmd_nr = _IOC_NR(cmd_in);
3413
3414 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3415 struct mtop mtc;
3416
3417 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3418 retval = (-EINVAL);
3419 goto out;
3420 }
3421
3422 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3423 if (i) {
3424 retval = (-EFAULT);
3425 goto out;
3426 }
3427
3428 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3429 printk(KERN_WARNING
3430 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3431 retval = (-EPERM);
3432 goto out;
3433 }
3434 if (!STm->defined &&
3435 (mtc.mt_op != MTSETDRVBUFFER &&
3436 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3437 retval = (-ENXIO);
3438 goto out;
3439 }
3440
3441 if (!STp->pos_unknown) {
3442
3443 if (STps->eof == ST_FM_HIT) {
3444 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3445 mtc.mt_op == MTEOM) {
3446 mtc.mt_count -= 1;
3447 if (STps->drv_file >= 0)
3448 STps->drv_file += 1;
3449 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3450 mtc.mt_count += 1;
3451 if (STps->drv_file >= 0)
3452 STps->drv_file += 1;
3453 }
3454 }
3455
3456 if (mtc.mt_op == MTSEEK) {
3457 /* Old position must be restored if partition will be
3458 changed */
3459 i = !STp->can_partitions ||
3460 (STp->new_partition != STp->partition);
3461 } else {
3462 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3463 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3464 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3465 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3466 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3467 mtc.mt_op == MTCOMPRESSION;
3468 }
3469 i = flush_buffer(STp, i);
3470 if (i < 0) {
3471 retval = i;
3472 goto out;
3473 }
3474 if (STps->rw == ST_WRITING &&
3475 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3476 mtc.mt_op == MTSEEK ||
3477 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3478 i = st_int_ioctl(STp, MTWEOF, 1);
3479 if (i < 0) {
3480 retval = i;
3481 goto out;
3482 }
3483 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3484 mtc.mt_count++;
3485 STps->rw = ST_IDLE;
3486 }
3487
3488 } else {
3489 /*
3490 * If there was a bus reset, block further access
3491 * to this device. If the user wants to rewind the tape,
3492 * then reset the flag and allow access again.
3493 */
3494 if (mtc.mt_op != MTREW &&
3495 mtc.mt_op != MTOFFL &&
3496 mtc.mt_op != MTRETEN &&
3497 mtc.mt_op != MTERASE &&
3498 mtc.mt_op != MTSEEK &&
3499 mtc.mt_op != MTEOM) {
3500 retval = (-EIO);
3501 goto out;
3502 }
3503 reset_state(STp);
3504 /* remove this when the midlevel properly clears was_reset */
3505 STp->device->was_reset = 0;
3506 }
3507
3508 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3509 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3510 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3511 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3512
3513 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3514 do_door_lock(STp, 0); /* Ignore result! */
3515
3516 if (mtc.mt_op == MTSETDRVBUFFER &&
3517 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3518 retval = st_set_options(STp, mtc.mt_count);
3519 goto out;
3520 }
3521
3522 if (mtc.mt_op == MTSETPART) {
3523 if (!STp->can_partitions ||
3524 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3525 retval = (-EINVAL);
3526 goto out;
3527 }
3528 if (mtc.mt_count >= STp->nbr_partitions &&
3529 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3530 retval = (-EIO);
3531 goto out;
3532 }
3533 if (mtc.mt_count >= STp->nbr_partitions) {
3534 retval = (-EINVAL);
3535 goto out;
3536 }
3537 STp->new_partition = mtc.mt_count;
3538 retval = 0;
3539 goto out;
3540 }
3541
3542 if (mtc.mt_op == MTMKPART) {
3543 if (!STp->can_partitions) {
3544 retval = (-EINVAL);
3545 goto out;
3546 }
3547 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3548 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3549 retval = i;
3550 goto out;
3551 }
3552 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3553 STp->ps[i].rw = ST_IDLE;
3554 STp->ps[i].at_sm = 0;
3555 STp->ps[i].last_block_valid = 0;
3556 }
3557 STp->partition = STp->new_partition = 0;
3558 STp->nbr_partitions = 1; /* Bad guess ?-) */
3559 STps->drv_block = STps->drv_file = 0;
3560 retval = 0;
3561 goto out;
3562 }
3563
3564 if (mtc.mt_op == MTSEEK) {
3565 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3566 if (!STp->can_partitions)
3567 STp->ps[0].rw = ST_IDLE;
3568 retval = i;
3569 goto out;
3570 }
3571
3572 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3573 retval = do_load_unload(STp, file, 0);
3574 goto out;
3575 }
3576
3577 if (mtc.mt_op == MTLOAD) {
3578 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3579 goto out;
3580 }
3581
3582 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3583 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3584 goto out;
3585 }
3586
3587 if (STp->can_partitions && STp->ready == ST_READY &&
3588 (i = switch_partition(STp)) < 0) {
3589 retval = i;
3590 goto out;
3591 }
3592
3593 if (mtc.mt_op == MTCOMPRESSION)
3594 retval = st_compression(STp, (mtc.mt_count & 1));
3595 else
3596 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3597 goto out;
3598 }
3599 if (!STm->defined) {
3600 retval = (-ENXIO);
3601 goto out;
3602 }
3603
3604 if ((i = flush_buffer(STp, 0)) < 0) {
3605 retval = i;
3606 goto out;
3607 }
3608 if (STp->can_partitions &&
3609 (i = switch_partition(STp)) < 0) {
3610 retval = i;
3611 goto out;
3612 }
3613
3614 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3615 struct mtget mt_status;
3616
3617 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3618 retval = (-EINVAL);
3619 goto out;
3620 }
3621
3622 mt_status.mt_type = STp->tape_type;
3623 mt_status.mt_dsreg =
3624 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3625 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3626 mt_status.mt_blkno = STps->drv_block;
3627 mt_status.mt_fileno = STps->drv_file;
3628 if (STp->block_size != 0) {
3629 if (STps->rw == ST_WRITING)
3630 mt_status.mt_blkno +=
3631 (STp->buffer)->buffer_bytes / STp->block_size;
3632 else if (STps->rw == ST_READING)
3633 mt_status.mt_blkno -=
3634 ((STp->buffer)->buffer_bytes +
3635 STp->block_size - 1) / STp->block_size;
3636 }
3637
3638 mt_status.mt_gstat = 0;
3639 if (STp->drv_write_prot)
3640 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3641 if (mt_status.mt_blkno == 0) {
3642 if (mt_status.mt_fileno == 0)
3643 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3644 else
3645 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3646 }
3647 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3648 mt_status.mt_resid = STp->partition;
3649 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3650 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3651 else if (STps->eof >= ST_EOM_OK)
3652 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3653 if (STp->density == 1)
3654 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3655 else if (STp->density == 2)
3656 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3657 else if (STp->density == 3)
3658 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3659 if (STp->ready == ST_READY)
3660 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3661 if (STp->ready == ST_NO_TAPE)
3662 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3663 if (STps->at_sm)
3664 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3665 if (STm->do_async_writes ||
3666 (STm->do_buffer_writes && STp->block_size != 0) ||
3667 STp->drv_buffer != 0)
3668 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3669 if (STp->cleaning_req)
3670 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3671
3672 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3673 if (i) {
3674 retval = (-EFAULT);
3675 goto out;
3676 }
3677
3678 STp->recover_reg = 0; /* Clear after read */
3679 retval = 0;
3680 goto out;
3681 } /* End of MTIOCGET */
3682 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3683 struct mtpos mt_pos;
3684 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3685 retval = (-EINVAL);
3686 goto out;
3687 }
3688 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3689 retval = i;
3690 goto out;
3691 }
3692 mt_pos.mt_blkno = blk;
3693 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3694 if (i)
3695 retval = (-EFAULT);
3696 goto out;
3697 }
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02003698 mutex_unlock(&STp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 switch (cmd_in) {
3700 case SCSI_IOCTL_GET_IDLUN:
3701 case SCSI_IOCTL_GET_BUS_NUMBER:
3702 break;
3703 default:
Kai Makisara 16c4b3e2005-05-01 18:11:55 +03003704 if ((cmd_in == SG_IO ||
3705 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
3706 cmd_in == CDROM_SEND_PACKET) &&
3707 !capable(CAP_SYS_RAWIO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 i = -EPERM;
3709 else
Al Viro74f3c8a2007-08-27 15:38:10 -04003710 i = scsi_cmd_ioctl(STp->disk->queue, STp->disk,
3711 file->f_mode, cmd_in, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (i != -ENOTTY)
3713 return i;
3714 break;
3715 }
Kai Makisara 16c4b3e2005-05-01 18:11:55 +03003716 retval = scsi_ioctl(STp->device, cmd_in, p);
3717 if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3718 STp->rew_at_close = 0;
3719 STp->ready = ST_NO_TAPE;
3720 }
3721 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723 out:
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02003724 mutex_unlock(&STp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return retval;
3726}
3727
3728#ifdef CONFIG_COMPAT
3729static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3730{
3731 struct scsi_tape *STp = file->private_data;
3732 struct scsi_device *sdev = STp->device;
3733 int ret = -ENOIOCTLCMD;
3734 if (sdev->host->hostt->compat_ioctl) {
3735
3736 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3737
3738 }
3739 return ret;
3740}
3741#endif
3742
3743
3744
3745/* Try to allocate a new tape buffer. Calling function must not hold
3746 dev_arr_lock. */
3747static struct st_buffer *
3748 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3749{
Mike Christie8b05b772005-11-08 04:06:44 -06003750 int i, got = 0;
Al Viroc53033f2005-10-21 03:22:08 -04003751 gfp_t priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 struct st_buffer *tb;
3753
3754 if (from_initialization)
3755 priority = GFP_ATOMIC;
3756 else
3757 priority = GFP_KERNEL;
3758
3759 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3760 max_sg * sizeof(struct st_buf_fragment);
Jes Sorensen24669f752006-01-16 10:31:18 -05003761 tb = kzalloc(i, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 if (!tb) {
3763 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3764 return NULL;
3765 }
Mike Christie8b05b772005-11-08 04:06:44 -06003766 tb->frp_segs = tb->orig_frp_segs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 tb->use_sg = max_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 tb->dma = need_dma;
3771 tb->buffer_size = got;
FUJITA Tomonoricd816212007-12-15 15:51:55 +09003772 sg_init_table(tb->sg, max_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
FUJITA Tomonorid0e1ae32008-12-18 14:49:40 +09003774 tb->reserved_pages = kzalloc(max_sg * sizeof(struct page *), priority);
3775 if (!tb->reserved_pages) {
3776 kfree(tb);
3777 return NULL;
3778 }
3779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return tb;
3781}
3782
3783
3784/* Try to allocate enough space in the tape buffer */
3785static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3786{
Al Viroc53033f2005-10-21 03:22:08 -04003787 int segs, nbr, max_segs, b_size, order, got;
3788 gfp_t priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 if (new_size <= STbuffer->buffer_size)
3791 return 1;
3792
3793 if (STbuffer->buffer_size <= PAGE_SIZE)
3794 normalize_buffer(STbuffer); /* Avoid extra segment */
3795
3796 max_segs = STbuffer->use_sg;
3797 nbr = max_segs - STbuffer->frp_segs;
3798 if (nbr <= 0)
3799 return 0;
3800
3801 priority = GFP_KERNEL | __GFP_NOWARN;
3802 if (need_dma)
3803 priority |= GFP_DMA;
FUJITA Tomonori9c905962008-12-18 14:49:39 +09003804
3805 if (STbuffer->frp_segs) {
3806 b_size = STbuffer->frp[0].length;
3807 order = get_order(b_size);
3808 } else {
3809 for (b_size = PAGE_SIZE, order = 0;
3810 order <= 6 && b_size < new_size; order++, b_size *= 2)
3811 ; /* empty */
3812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
3814 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3815 segs < max_segs && got < new_size;) {
3816 STbuffer->frp[segs].page = alloc_pages(priority, order);
3817 if (STbuffer->frp[segs].page == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 DEB(STbuffer->buffer_size = got);
3819 normalize_buffer(STbuffer);
3820 return 0;
3821 }
3822 STbuffer->frp[segs].length = b_size;
3823 STbuffer->frp_segs += 1;
3824 got += b_size;
3825 STbuffer->buffer_size = got;
Kai Makisara40f6b362008-02-24 22:23:24 +02003826 if (STbuffer->cleared)
3827 memset(page_address(STbuffer->frp[segs].page), 0, b_size);
FUJITA Tomonorid0e1ae32008-12-18 14:49:40 +09003828 STbuffer->reserved_pages[segs] = STbuffer->frp[segs].page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 segs++;
3830 }
3831 STbuffer->b_data = page_address(STbuffer->frp[0].page);
FUJITA Tomonorid0e1ae32008-12-18 14:49:40 +09003832 STbuffer->map_data.page_order = order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
3834 return 1;
3835}
3836
3837
Kai Makisara40f6b362008-02-24 22:23:24 +02003838/* Make sure that no data from previous user is in the internal buffer */
3839static void clear_buffer(struct st_buffer * st_bp)
3840{
3841 int i;
3842
3843 for (i=0; i < st_bp->frp_segs; i++)
3844 memset(page_address(st_bp->frp[i].page), 0, st_bp->frp[i].length);
3845 st_bp->cleared = 1;
3846}
3847
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849/* Release the extra buffer */
3850static void normalize_buffer(struct st_buffer * STbuffer)
3851{
3852 int i, order;
3853
3854 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3855 order = get_order(STbuffer->frp[i].length);
3856 __free_pages(STbuffer->frp[i].page, order);
3857 STbuffer->buffer_size -= STbuffer->frp[i].length;
3858 }
3859 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3860 STbuffer->frp_sg_current = 0;
Mike Christie8b05b772005-11-08 04:06:44 -06003861 STbuffer->sg_segs = 0;
FUJITA Tomonorid0e1ae32008-12-18 14:49:40 +09003862 STbuffer->map_data.page_order = 0;
3863 STbuffer->map_data.offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864}
3865
3866
3867/* Move data from the user buffer to the tape buffer. Returns zero (success) or
3868 negative error code. */
3869static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3870{
3871 int i, cnt, res, offset;
3872
3873 for (i = 0, offset = st_bp->buffer_bytes;
3874 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3875 offset -= st_bp->frp[i].length;
3876 if (i == st_bp->frp_segs) { /* Should never happen */
3877 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3878 return (-EIO);
3879 }
3880 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3881 cnt = st_bp->frp[i].length - offset < do_count ?
3882 st_bp->frp[i].length - offset : do_count;
3883 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3884 if (res)
3885 return (-EFAULT);
3886 do_count -= cnt;
3887 st_bp->buffer_bytes += cnt;
3888 ubp += cnt;
3889 offset = 0;
3890 }
3891 if (do_count) /* Should never happen */
3892 return (-EIO);
3893
3894 return 0;
3895}
3896
3897
3898/* Move data from the tape buffer to the user buffer. Returns zero (success) or
3899 negative error code. */
3900static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3901{
3902 int i, cnt, res, offset;
3903
3904 for (i = 0, offset = st_bp->read_pointer;
3905 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3906 offset -= st_bp->frp[i].length;
3907 if (i == st_bp->frp_segs) { /* Should never happen */
3908 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3909 return (-EIO);
3910 }
3911 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3912 cnt = st_bp->frp[i].length - offset < do_count ?
3913 st_bp->frp[i].length - offset : do_count;
3914 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3915 if (res)
3916 return (-EFAULT);
3917 do_count -= cnt;
3918 st_bp->buffer_bytes -= cnt;
3919 st_bp->read_pointer += cnt;
3920 ubp += cnt;
3921 offset = 0;
3922 }
3923 if (do_count) /* Should never happen */
3924 return (-EIO);
3925
3926 return 0;
3927}
3928
3929
3930/* Move data towards start of buffer */
3931static void move_buffer_data(struct st_buffer * st_bp, int offset)
3932{
3933 int src_seg, dst_seg, src_offset = 0, dst_offset;
3934 int count, total;
3935
3936 if (offset == 0)
3937 return;
3938
3939 total=st_bp->buffer_bytes - offset;
3940 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3941 src_offset = offset;
3942 if (src_offset < st_bp->frp[src_seg].length)
3943 break;
3944 offset -= st_bp->frp[src_seg].length;
3945 }
3946
3947 st_bp->buffer_bytes = st_bp->read_pointer = total;
3948 for (dst_seg=dst_offset=0; total > 0; ) {
3949 count = min(st_bp->frp[dst_seg].length - dst_offset,
3950 st_bp->frp[src_seg].length - src_offset);
3951 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3952 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3953 src_offset += count;
3954 if (src_offset >= st_bp->frp[src_seg].length) {
3955 src_seg++;
3956 src_offset = 0;
3957 }
3958 dst_offset += count;
3959 if (dst_offset >= st_bp->frp[dst_seg].length) {
3960 dst_seg++;
3961 dst_offset = 0;
3962 }
3963 total -= count;
3964 }
3965}
3966
3967
3968/* Fill the s/g list up to the length required for this transfer */
3969static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3970{
3971 int i;
3972 unsigned int count;
3973 struct scatterlist *sg;
3974 struct st_buf_fragment *frp;
3975
3976 if (length == STbp->frp_sg_current)
3977 return; /* work already done */
3978
3979 sg = &(STbp->sg[0]);
3980 frp = STbp->frp;
3981 for (i=count=0; count < length; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 if (length - count > frp[i].length)
Jens Axboe642f1492007-10-24 11:20:47 +02003983 sg_set_page(&sg[i], frp[i].page, frp[i].length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 else
Jens Axboe642f1492007-10-24 11:20:47 +02003985 sg_set_page(&sg[i], frp[i].page, length - count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 count += sg[i].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 }
3988 STbp->sg_segs = i;
3989 STbp->frp_sg_current = length;
3990}
3991
3992
3993/* Validate the options from command line or module parameters */
3994static void validate_options(void)
3995{
3996 if (buffer_kbs > 0)
3997 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3998 if (max_sg_segs >= ST_FIRST_SG)
3999 st_max_sg_segs = max_sg_segs;
4000}
4001
4002#ifndef MODULE
4003/* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
4004 */
4005static int __init st_setup(char *str)
4006{
4007 int i, len, ints[5];
4008 char *stp;
4009
4010 stp = get_options(str, ARRAY_SIZE(ints), ints);
4011
4012 if (ints[0] > 0) {
4013 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
4014 if (parms[i].val)
4015 *parms[i].val = ints[i + 1];
4016 } else {
4017 while (stp != NULL) {
4018 for (i = 0; i < ARRAY_SIZE(parms); i++) {
4019 len = strlen(parms[i].name);
4020 if (!strncmp(stp, parms[i].name, len) &&
4021 (*(stp + len) == ':' || *(stp + len) == '=')) {
4022 if (parms[i].val)
4023 *parms[i].val =
4024 simple_strtoul(stp + len + 1, NULL, 0);
4025 else
4026 printk(KERN_WARNING "st: Obsolete parameter %s\n",
4027 parms[i].name);
4028 break;
4029 }
4030 }
Tobias Klauser6391a112006-06-08 22:23:48 -07004031 if (i >= ARRAY_SIZE(parms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
4033 stp);
4034 stp = strchr(stp, ',');
4035 if (stp)
4036 stp++;
4037 }
4038 }
4039
4040 validate_options();
4041
4042 return 1;
4043}
4044
4045__setup("st=", st_setup);
4046
4047#endif
4048
Arjan van de Ven00977a52007-02-12 00:55:34 -08004049static const struct file_operations st_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
4051 .owner = THIS_MODULE,
4052 .read = st_read,
4053 .write = st_write,
Kai Makisarafd66c1b2008-01-17 22:45:22 +02004054 .unlocked_ioctl = st_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055#ifdef CONFIG_COMPAT
4056 .compat_ioctl = st_compat_ioctl,
4057#endif
4058 .open = st_open,
4059 .flush = st_flush,
4060 .release = st_release,
4061};
4062
4063static int st_probe(struct device *dev)
4064{
4065 struct scsi_device *SDp = to_scsi_device(dev);
4066 struct gendisk *disk = NULL;
4067 struct cdev *cdev = NULL;
4068 struct scsi_tape *tpnt = NULL;
4069 struct st_modedef *STm;
4070 struct st_partstat *STps;
4071 struct st_buffer *buffer;
4072 int i, j, mode, dev_num, error;
4073 char *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 if (SDp->type != TYPE_TAPE)
4076 return -ENODEV;
4077 if ((stp = st_incompatible(SDp))) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -04004078 sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
4080 return -ENODEV;
4081 }
4082
Mike Christie8b05b772005-11-08 04:06:44 -06004083 i = min(SDp->request_queue->max_hw_segments,
4084 SDp->request_queue->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 if (st_max_sg_segs < i)
4086 i = st_max_sg_segs;
4087 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
4088 if (buffer == NULL) {
4089 printk(KERN_ERR
4090 "st: Can't allocate new tape buffer. Device not attached.\n");
4091 goto out;
4092 }
4093
4094 disk = alloc_disk(1);
4095 if (!disk) {
4096 printk(KERN_ERR "st: out of memory. Device not attached.\n");
4097 goto out_buffer_free;
4098 }
4099
4100 write_lock(&st_dev_arr_lock);
4101 if (st_nr_dev >= st_dev_max) {
4102 struct scsi_tape **tmp_da;
4103 int tmp_dev_max;
4104
4105 tmp_dev_max = max(st_nr_dev * 2, 8);
4106 if (tmp_dev_max > ST_MAX_TAPES)
4107 tmp_dev_max = ST_MAX_TAPES;
4108 if (tmp_dev_max <= st_nr_dev) {
4109 write_unlock(&st_dev_arr_lock);
4110 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
4111 ST_MAX_TAPES);
4112 goto out_put_disk;
4113 }
4114
Jes Sorensen24669f752006-01-16 10:31:18 -05004115 tmp_da = kzalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (tmp_da == NULL) {
4117 write_unlock(&st_dev_arr_lock);
4118 printk(KERN_ERR "st: Can't extend device array.\n");
4119 goto out_put_disk;
4120 }
4121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 if (scsi_tapes != NULL) {
4123 memcpy(tmp_da, scsi_tapes,
4124 st_dev_max * sizeof(struct scsi_tape *));
4125 kfree(scsi_tapes);
4126 }
4127 scsi_tapes = tmp_da;
4128
4129 st_dev_max = tmp_dev_max;
4130 }
4131
4132 for (i = 0; i < st_dev_max; i++)
4133 if (scsi_tapes[i] == NULL)
4134 break;
4135 if (i >= st_dev_max)
4136 panic("scsi_devices corrupt (st)");
4137
Jes Sorensen24669f752006-01-16 10:31:18 -05004138 tpnt = kzalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 if (tpnt == NULL) {
4140 write_unlock(&st_dev_arr_lock);
4141 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
4142 goto out_put_disk;
4143 }
Kai Makisaraf03a5672005-08-02 13:40:47 +03004144 kref_init(&tpnt->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 tpnt->disk = disk;
4146 sprintf(disk->disk_name, "st%d", i);
4147 disk->private_data = &tpnt->driver;
4148 disk->queue = SDp->request_queue;
4149 tpnt->driver = &st_template;
4150 scsi_tapes[i] = tpnt;
4151 dev_num = i;
4152
4153 tpnt->device = SDp;
4154 if (SDp->scsi_level <= 2)
4155 tpnt->tape_type = MT_ISSCSI1;
4156 else
4157 tpnt->tape_type = MT_ISSCSI2;
4158
4159 tpnt->buffer = buffer;
Kai Makisaraf03a5672005-08-02 13:40:47 +03004160 tpnt->buffer->last_SRpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162 tpnt->inited = 0;
4163 tpnt->dirty = 0;
4164 tpnt->in_use = 0;
4165 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
4166 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
4167 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
4168 tpnt->density = 0;
4169 tpnt->do_auto_lock = ST_AUTO_LOCK;
4170 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
4171 tpnt->can_partitions = 0;
4172 tpnt->two_fm = ST_TWO_FM;
4173 tpnt->fast_mteom = ST_FAST_MTEOM;
4174 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
Kai Makisara40f6b362008-02-24 22:23:24 +02004175 tpnt->sili = ST_SILI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 tpnt->immediate = ST_NOWAIT;
4177 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
4178 tpnt->partition = 0;
4179 tpnt->new_partition = 0;
4180 tpnt->nbr_partitions = 0;
James Bottomleya02488e2008-11-30 10:36:26 -06004181 blk_queue_rq_timeout(tpnt->device->request_queue, ST_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 tpnt->long_timeout = ST_LONG_TIMEOUT;
4183 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 for (i = 0; i < ST_NBR_MODES; i++) {
4186 STm = &(tpnt->modes[i]);
4187 STm->defined = 0;
4188 STm->sysv = ST_SYSV;
4189 STm->defaults_for_writes = 0;
4190 STm->do_async_writes = ST_ASYNC_WRITES;
4191 STm->do_buffer_writes = ST_BUFFER_WRITES;
4192 STm->do_read_ahead = ST_READ_AHEAD;
4193 STm->default_compression = ST_DONT_TOUCH;
4194 STm->default_blksize = (-1); /* No forced size */
4195 STm->default_density = (-1); /* No forced density */
4196 }
4197
4198 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
4199 STps = &(tpnt->ps[i]);
4200 STps->rw = ST_IDLE;
4201 STps->eof = ST_NOEOF;
4202 STps->at_sm = 0;
4203 STps->last_block_valid = 0;
4204 STps->drv_block = (-1);
4205 STps->drv_file = (-1);
4206 }
4207
4208 tpnt->current_mode = 0;
4209 tpnt->modes[0].defined = 1;
4210
4211 tpnt->density_changed = tpnt->compression_changed =
4212 tpnt->blksize_changed = 0;
Matthias Kaehlcke28f85002007-07-29 23:38:15 +02004213 mutex_init(&tpnt->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
4215 st_nr_dev++;
4216 write_unlock(&st_dev_arr_lock);
4217
4218 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4219 STm = &(tpnt->modes[mode]);
4220 for (j=0; j < 2; j++) {
4221 cdev = cdev_alloc();
4222 if (!cdev) {
4223 printk(KERN_ERR
4224 "st%d: out of memory. Device not attached.\n",
4225 dev_num);
4226 goto out_free_tape;
4227 }
4228 cdev->owner = THIS_MODULE;
4229 cdev->ops = &st_fops;
4230
4231 error = cdev_add(cdev,
4232 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
4233 1);
4234 if (error) {
4235 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
4236 dev_num, j ? "non" : "auto", mode);
4237 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
4238 goto out_free_tape;
4239 }
4240 STm->cdevs[j] = cdev;
4241
4242 }
Jeff Garzik13026a62006-10-04 06:00:38 -04004243 error = do_create_class_files(tpnt, dev_num, mode);
4244 if (error)
4245 goto out_free_tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247
Kai Makisara42252852006-11-07 21:56:38 +02004248 sdev_printk(KERN_NOTICE, SDp,
Rene Herman8b1ea242006-05-20 15:00:22 -07004249 "Attached scsi tape %s\n", tape_name(tpnt));
Kai Makisara42252852006-11-07 21:56:38 +02004250 sdev_printk(KERN_INFO, SDp, "%s: try direct i/o: %s (alignment %d B)\n",
4251 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4252 queue_dma_alignment(SDp->request_queue) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254 return 0;
4255
4256out_free_tape:
4257 for (mode=0; mode < ST_NBR_MODES; mode++) {
4258 STm = &(tpnt->modes[mode]);
4259 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4260 "tape");
4261 for (j=0; j < 2; j++) {
4262 if (STm->cdevs[j]) {
4263 if (cdev == STm->cdevs[j])
4264 cdev = NULL;
Tony Jonesee959b02008-02-22 00:13:36 +01004265 device_destroy(st_sysfs_class,
4266 MKDEV(SCSI_TAPE_MAJOR,
4267 TAPE_MINOR(i, mode, j)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 cdev_del(STm->cdevs[j]);
4269 }
4270 }
4271 }
4272 if (cdev)
4273 cdev_del(cdev);
4274 write_lock(&st_dev_arr_lock);
4275 scsi_tapes[dev_num] = NULL;
4276 st_nr_dev--;
4277 write_unlock(&st_dev_arr_lock);
4278out_put_disk:
4279 put_disk(disk);
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004280 kfree(tpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281out_buffer_free:
4282 kfree(buffer);
4283out:
4284 return -ENODEV;
4285};
4286
4287
4288static int st_remove(struct device *dev)
4289{
4290 struct scsi_device *SDp = to_scsi_device(dev);
4291 struct scsi_tape *tpnt;
4292 int i, j, mode;
4293
4294 write_lock(&st_dev_arr_lock);
4295 for (i = 0; i < st_dev_max; i++) {
4296 tpnt = scsi_tapes[i];
4297 if (tpnt != NULL && tpnt->device == SDp) {
4298 scsi_tapes[i] = NULL;
4299 st_nr_dev--;
4300 write_unlock(&st_dev_arr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4302 "tape");
4303 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 for (j=0; j < 2; j++) {
Tony Jonesee959b02008-02-22 00:13:36 +01004305 device_destroy(st_sysfs_class,
4306 MKDEV(SCSI_TAPE_MAJOR,
4307 TAPE_MINOR(i, mode, j)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 cdev_del(tpnt->modes[mode].cdevs[j]);
4309 tpnt->modes[mode].cdevs[j] = NULL;
4310 }
4311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Arjan van de Ven0b950672006-01-11 13:16:10 +01004313 mutex_lock(&st_ref_mutex);
Kai Makisaraf03a5672005-08-02 13:40:47 +03004314 kref_put(&tpnt->kref, scsi_tape_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +01004315 mutex_unlock(&st_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return 0;
4317 }
4318 }
4319
4320 write_unlock(&st_dev_arr_lock);
4321 return 0;
4322}
4323
Kai Makisaraf03a5672005-08-02 13:40:47 +03004324/**
4325 * scsi_tape_release - Called to free the Scsi_Tape structure
4326 * @kref: pointer to embedded kref
4327 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01004328 * st_ref_mutex must be held entering this routine. Because it is
Kai Makisaraf03a5672005-08-02 13:40:47 +03004329 * called on last put, you should always use the scsi_tape_get()
4330 * scsi_tape_put() helpers which manipulate the semaphore directly
4331 * and never do a direct kref_put().
4332 **/
4333static void scsi_tape_release(struct kref *kref)
4334{
4335 struct scsi_tape *tpnt = to_scsi_tape(kref);
4336 struct gendisk *disk = tpnt->disk;
4337
4338 tpnt->device = NULL;
4339
4340 if (tpnt->buffer) {
4341 tpnt->buffer->orig_frp_segs = 0;
4342 normalize_buffer(tpnt->buffer);
FUJITA Tomonorid0e1ae32008-12-18 14:49:40 +09004343 kfree(tpnt->buffer->reserved_pages);
Kai Makisaraf03a5672005-08-02 13:40:47 +03004344 kfree(tpnt->buffer);
4345 }
4346
4347 disk->private_data = NULL;
4348 put_disk(disk);
4349 kfree(tpnt);
4350 return;
4351}
4352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353static int __init init_st(void)
4354{
Jeff Garzik13026a62006-10-04 06:00:38 -04004355 int err;
4356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 validate_options();
4358
Jeff Garzik13026a62006-10-04 06:00:38 -04004359 printk(KERN_INFO "st: Version %s, fixed bufsize %d, s/g segs %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 verstr, st_fixed_buffer_size, st_max_sg_segs);
4361
gregkh@suse.ded2538782005-03-23 09:55:22 -08004362 st_sysfs_class = class_create(THIS_MODULE, "scsi_tape");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (IS_ERR(st_sysfs_class)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
Jeff Garzik13026a62006-10-04 06:00:38 -04004365 return PTR_ERR(st_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
4367
Jeff Garzik13026a62006-10-04 06:00:38 -04004368 err = register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4369 ST_MAX_TAPE_ENTRIES, "st");
4370 if (err) {
4371 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",
4372 SCSI_TAPE_MAJOR);
4373 goto err_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
Jeff Garzik13026a62006-10-04 06:00:38 -04004375
4376 err = scsi_register_driver(&st_template.gendrv);
4377 if (err)
4378 goto err_chrdev;
4379
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004380 err = do_create_sysfs_files();
Jeff Garzik13026a62006-10-04 06:00:38 -04004381 if (err)
4382 goto err_scsidrv;
4383
4384 return 0;
4385
4386err_scsidrv:
4387 scsi_unregister_driver(&st_template.gendrv);
4388err_chrdev:
4389 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4390 ST_MAX_TAPE_ENTRIES);
4391err_class:
Kai Makisarac2c96f42005-08-02 12:21:51 +03004392 class_destroy(st_sysfs_class);
Jeff Garzik13026a62006-10-04 06:00:38 -04004393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394}
4395
4396static void __exit exit_st(void)
4397{
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004398 do_remove_sysfs_files();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 scsi_unregister_driver(&st_template.gendrv);
4400 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4401 ST_MAX_TAPE_ENTRIES);
Kai Makisarac2c96f42005-08-02 12:21:51 +03004402 class_destroy(st_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 kfree(scsi_tapes);
4404 printk(KERN_INFO "st: Unloaded.\n");
4405}
4406
4407module_init(init_st);
4408module_exit(exit_st);
4409
4410
4411/* The sysfs driver interface. Read-only at the moment */
4412static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4413{
4414 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4415}
4416static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4417
4418static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4419{
4420 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4421}
4422static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4423
4424static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4425{
4426 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4427}
4428static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4429
4430static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4431{
4432 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4433}
4434static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4435
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004436static int do_create_sysfs_files(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004438 struct device_driver *sysfs = &st_template.gendrv;
Jeff Garzik13026a62006-10-04 06:00:38 -04004439 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004441 err = driver_create_file(sysfs, &driver_attr_try_direct_io);
Jeff Garzik13026a62006-10-04 06:00:38 -04004442 if (err)
4443 return err;
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004444 err = driver_create_file(sysfs, &driver_attr_fixed_buffer_size);
Jeff Garzik13026a62006-10-04 06:00:38 -04004445 if (err)
4446 goto err_try_direct_io;
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004447 err = driver_create_file(sysfs, &driver_attr_max_sg_segs);
Jeff Garzik13026a62006-10-04 06:00:38 -04004448 if (err)
4449 goto err_attr_fixed_buf;
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004450 err = driver_create_file(sysfs, &driver_attr_version);
Jeff Garzik13026a62006-10-04 06:00:38 -04004451 if (err)
4452 goto err_attr_max_sg;
4453
4454 return 0;
4455
4456err_attr_max_sg:
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004457 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
Jeff Garzik13026a62006-10-04 06:00:38 -04004458err_attr_fixed_buf:
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004459 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
Jeff Garzik13026a62006-10-04 06:00:38 -04004460err_try_direct_io:
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004461 driver_remove_file(sysfs, &driver_attr_try_direct_io);
Jeff Garzik13026a62006-10-04 06:00:38 -04004462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
4464
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004465static void do_remove_sysfs_files(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004467 struct device_driver *sysfs = &st_template.gendrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01004469 driver_remove_file(sysfs, &driver_attr_version);
4470 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4471 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4472 driver_remove_file(sysfs, &driver_attr_try_direct_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473}
4474
4475
4476/* The sysfs simple class interface */
Tony Jonesee959b02008-02-22 00:13:36 +01004477static ssize_t
4478st_defined_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
James Bottomley7d15d6a2008-03-14 14:12:43 -07004480 struct st_modedef *STm = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 ssize_t l = 0;
4482
4483 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4484 return l;
4485}
4486
Tony Jonesee959b02008-02-22 00:13:36 +01004487DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Tony Jonesee959b02008-02-22 00:13:36 +01004489static ssize_t
4490st_defblk_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
James Bottomley7d15d6a2008-03-14 14:12:43 -07004492 struct st_modedef *STm = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 ssize_t l = 0;
4494
4495 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4496 return l;
4497}
4498
Tony Jonesee959b02008-02-22 00:13:36 +01004499DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Tony Jonesee959b02008-02-22 00:13:36 +01004501static ssize_t
4502st_defdensity_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
James Bottomley7d15d6a2008-03-14 14:12:43 -07004504 struct st_modedef *STm = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 ssize_t l = 0;
4506 char *fmt;
4507
4508 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4509 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4510 return l;
4511}
4512
Tony Jonesee959b02008-02-22 00:13:36 +01004513DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Tony Jonesee959b02008-02-22 00:13:36 +01004515static ssize_t
4516st_defcompression_show(struct device *dev, struct device_attribute *attr,
4517 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
James Bottomley7d15d6a2008-03-14 14:12:43 -07004519 struct st_modedef *STm = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 ssize_t l = 0;
4521
4522 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4523 return l;
4524}
4525
Tony Jonesee959b02008-02-22 00:13:36 +01004526DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Tony Jonesee959b02008-02-22 00:13:36 +01004528static ssize_t
4529st_options_show(struct device *dev, struct device_attribute *attr, char *buf)
Kai Makisarab174be02008-02-24 22:29:12 +02004530{
James Bottomley7d15d6a2008-03-14 14:12:43 -07004531 struct st_modedef *STm = dev_get_drvdata(dev);
Kai Makisarab174be02008-02-24 22:29:12 +02004532 struct scsi_tape *STp;
4533 int i, j, options;
4534 ssize_t l = 0;
4535
4536 for (i=0; i < st_dev_max; i++) {
4537 for (j=0; j < ST_NBR_MODES; j++)
4538 if (&scsi_tapes[i]->modes[j] == STm)
4539 break;
4540 if (j < ST_NBR_MODES)
4541 break;
4542 }
4543 if (i == st_dev_max)
4544 return 0; /* should never happen */
4545
4546 STp = scsi_tapes[i];
4547
4548 options = STm->do_buffer_writes ? MT_ST_BUFFER_WRITES : 0;
4549 options |= STm->do_async_writes ? MT_ST_ASYNC_WRITES : 0;
4550 options |= STm->do_read_ahead ? MT_ST_READ_AHEAD : 0;
4551 DEB( options |= debugging ? MT_ST_DEBUGGING : 0 );
4552 options |= STp->two_fm ? MT_ST_TWO_FM : 0;
4553 options |= STp->fast_mteom ? MT_ST_FAST_MTEOM : 0;
4554 options |= STm->defaults_for_writes ? MT_ST_DEF_WRITES : 0;
4555 options |= STp->can_bsr ? MT_ST_CAN_BSR : 0;
4556 options |= STp->omit_blklims ? MT_ST_NO_BLKLIMS : 0;
4557 options |= STp->can_partitions ? MT_ST_CAN_PARTITIONS : 0;
4558 options |= STp->scsi2_logical ? MT_ST_SCSI2LOGICAL : 0;
4559 options |= STm->sysv ? MT_ST_SYSV : 0;
4560 options |= STp->immediate ? MT_ST_NOWAIT : 0;
4561 options |= STp->sili ? MT_ST_SILI : 0;
4562
4563 l = snprintf(buf, PAGE_SIZE, "0x%08x\n", options);
4564 return l;
4565}
4566
Tony Jonesee959b02008-02-22 00:13:36 +01004567DEVICE_ATTR(options, S_IRUGO, st_options_show, NULL);
Kai Makisarab174be02008-02-24 22:29:12 +02004568
Jeff Garzik13026a62006-10-04 06:00:38 -04004569static int do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
4571 int i, rew, error;
4572 char name[10];
Tony Jonesee959b02008-02-22 00:13:36 +01004573 struct device *st_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 for (rew=0; rew < 2; rew++) {
4576 /* Make sure that the minor numbers corresponding to the four
4577 first modes always get the same names */
4578 i = mode << (4 - ST_NBR_MODE_BITS);
4579 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4580 STp->disk->disk_name, st_formats[i]);
4581 st_class_member =
Greg Kroah-Hartmand73a1a62008-07-21 20:03:34 -07004582 device_create(st_sysfs_class, &STp->device->sdev_gendev,
4583 MKDEV(SCSI_TAPE_MAJOR,
4584 TAPE_MINOR(dev_num, mode, rew)),
4585 &STp->modes[mode], "%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (IS_ERR(st_class_member)) {
Tony Jonesee959b02008-02-22 00:13:36 +01004587 printk(KERN_WARNING "st%d: device_create failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 dev_num);
Jeff Garzik13026a62006-10-04 06:00:38 -04004589 error = PTR_ERR(st_class_member);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 goto out;
4591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Tony Jonesee959b02008-02-22 00:13:36 +01004593 error = device_create_file(st_class_member,
4594 &dev_attr_defined);
Jeff Garzik13026a62006-10-04 06:00:38 -04004595 if (error) goto out;
Tony Jonesee959b02008-02-22 00:13:36 +01004596 error = device_create_file(st_class_member,
4597 &dev_attr_default_blksize);
Jeff Garzik13026a62006-10-04 06:00:38 -04004598 if (error) goto out;
Tony Jonesee959b02008-02-22 00:13:36 +01004599 error = device_create_file(st_class_member,
4600 &dev_attr_default_density);
Jeff Garzik13026a62006-10-04 06:00:38 -04004601 if (error) goto out;
Tony Jonesee959b02008-02-22 00:13:36 +01004602 error = device_create_file(st_class_member,
4603 &dev_attr_default_compression);
Jeff Garzik13026a62006-10-04 06:00:38 -04004604 if (error) goto out;
Tony Jonesee959b02008-02-22 00:13:36 +01004605 error = device_create_file(st_class_member,
4606 &dev_attr_options);
Kai Makisarab174be02008-02-24 22:29:12 +02004607 if (error) goto out;
Jeff Garzik13026a62006-10-04 06:00:38 -04004608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 if (mode == 0 && rew == 0) {
4610 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4611 &st_class_member->kobj,
4612 "tape");
4613 if (error) {
4614 printk(KERN_ERR
4615 "st%d: Can't create sysfs link from SCSI device.\n",
4616 dev_num);
Jeff Garzik13026a62006-10-04 06:00:38 -04004617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
4619 }
4620 }
Jeff Garzik13026a62006-10-04 06:00:38 -04004621
4622 return 0;
4623
4624out:
4625 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628/* The following functions may be useful for a larger audience. */
FUJITA Tomonori66207422008-12-18 14:49:43 +09004629static int sgl_map_user_pages(struct st_buffer *STbp,
4630 const unsigned int max_pages, unsigned long uaddr,
4631 size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632{
James Bottomley07542b82005-08-31 20:27:22 -04004633 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
4634 unsigned long start = uaddr >> PAGE_SHIFT;
4635 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 struct page **pages;
FUJITA Tomonori66207422008-12-18 14:49:43 +09004638 struct rq_map_data *mdata = &STbp->map_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 /* User attempted Overflow! */
4641 if ((uaddr + count) < uaddr)
4642 return -EINVAL;
4643
4644 /* Too big */
4645 if (nr_pages > max_pages)
4646 return -ENOMEM;
4647
4648 /* Hmm? */
4649 if (count == 0)
4650 return 0;
4651
4652 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4653 return -ENOMEM;
4654
4655 /* Try to fault in all of the necessary pages */
4656 down_read(&current->mm->mmap_sem);
4657 /* rw==READ means read from drive, write into memory area */
4658 res = get_user_pages(
4659 current,
4660 current->mm,
4661 uaddr,
4662 nr_pages,
4663 rw == READ,
4664 0, /* don't force */
4665 pages,
4666 NULL);
4667 up_read(&current->mm->mmap_sem);
4668
4669 /* Errors and no page mapped should return here */
4670 if (res < nr_pages)
4671 goto out_unmap;
4672
4673 for (i=0; i < nr_pages; i++) {
4674 /* FIXME: flush superflous for rw==READ,
4675 * probably wrong function for rw==WRITE
4676 */
4677 flush_dcache_page(pages[i]);
4678 }
4679
FUJITA Tomonori66207422008-12-18 14:49:43 +09004680 mdata->offset = uaddr & ~PAGE_MASK;
4681 mdata->page_order = 0;
4682 STbp->mapped_pages = pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 out_unmap:
4686 if (res > 0) {
4687 for (j=0; j < res; j++)
4688 page_cache_release(pages[j]);
Hugh Dickins6bc733e2005-12-01 20:21:57 +00004689 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
4691 kfree(pages);
4692 return res;
4693}
4694
4695
4696/* And unmap them... */
FUJITA Tomonori66207422008-12-18 14:49:43 +09004697static int sgl_unmap_user_pages(struct st_buffer *STbp,
4698 const unsigned int nr_pages, int dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
4700 int i;
4701
4702 for (i=0; i < nr_pages; i++) {
FUJITA Tomonori66207422008-12-18 14:49:43 +09004703 struct page *page = STbp->mapped_pages[i];
Nick Pigginb5810032005-10-29 18:16:12 -07004704
Nick Pigginb5810032005-10-29 18:16:12 -07004705 if (dirtied)
4706 SetPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 /* FIXME: cache flush missing for rw==READ
4708 * FIXME: call the correct reference counting function
4709 */
Nick Pigginb5810032005-10-29 18:16:12 -07004710 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 }
FUJITA Tomonori66207422008-12-18 14:49:43 +09004712 kfree(STbp->mapped_pages);
4713 STbp->mapped_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 return 0;
4716}