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