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