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