3 * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
4 * to allow user process control of SCSI devices.
5 * Development Sponsored by Killy Corp. NY NY
7 * Original driver (sg.c):
8 * Copyright (C) 1992 Lawrence Foard
9 * Version 2 and 3 extensions to driver:
10 * Copyright (C) 1998 - 2005 Douglas Gilbert
12 * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
21 static int sg_version_num
= 30533; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.33"
25 * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
26 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
27 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
28 * (otherwise the macros compile to empty statements).
31 #include <linux/config.h>
32 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/mtio.h>
41 #include <linux/ioctl.h>
42 #include <linux/fcntl.h>
43 #include <linux/init.h>
44 #include <linux/poll.h>
45 #include <linux/smp_lock.h>
46 #include <linux/moduleparam.h>
47 #include <linux/devfs_fs_kernel.h>
48 #include <linux/cdev.h>
49 #include <linux/seq_file.h>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_driver.h>
56 #include <scsi/scsi_ioctl.h>
59 #include "scsi_logging.h"
61 #ifdef CONFIG_SCSI_PROC_FS
62 #include <linux/proc_fs.h>
63 static char *sg_version_date
= "20050328";
65 static int sg_proc_init(void);
66 static void sg_proc_cleanup(void);
69 #ifndef LINUX_VERSION_CODE
70 #include <linux/version.h>
71 #endif /* LINUX_VERSION_CODE */
73 #define SG_ALLOW_DIO_DEF 0
74 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
76 #define SG_MAX_DEVS 32768
79 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
80 * Then when using 32 bit integers x * m may overflow during the calculation.
81 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
82 * calculates the same, but prevents the overflow when both m and d
83 * are "small" numbers (like HZ and USER_HZ).
84 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
87 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
89 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
91 int sg_big_buff
= SG_DEF_RESERVED_SIZE
;
92 /* N.B. This variable is readable and writeable via
93 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
94 of this size (or less if there is not enough memory) will be reserved
95 for use by this file descriptor. [Deprecated usage: this variable is also
96 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
97 the kernel (i.e. it is not a module).] */
98 static int def_reserved_size
= -1; /* picks up init parameter */
99 static int sg_allow_dio
= SG_ALLOW_DIO_DEF
;
101 #define SG_SECTOR_SZ 512
102 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
104 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
106 static int sg_add(struct class_device
*);
107 static void sg_remove(struct class_device
*);
109 static Scsi_Request
*dummy_cmdp
; /* only used for sizeof */
111 static DEFINE_RWLOCK(sg_dev_arr_lock
); /* Also used to lock
112 file descriptor list for device */
114 static struct class_interface sg_interface
= {
119 typedef struct sg_scatter_hold
{ /* holding area for scsi scatter gather info */
120 unsigned short k_use_sg
; /* Count of kernel scatter-gather pieces */
121 unsigned short sglist_len
; /* size of malloc'd scatter-gather list ++ */
122 unsigned bufflen
; /* Size of (aggregate) data buffer */
123 unsigned b_malloc_len
; /* actual len malloc'ed in buffer */
124 void *buffer
; /* Data buffer or scatter list (k_use_sg>0) */
125 char dio_in_use
; /* 0->indirect IO (or mmap), 1->dio */
126 unsigned char cmd_opcode
; /* first byte of command */
129 struct sg_device
; /* forward declarations */
132 typedef struct sg_request
{ /* SG_MAX_QUEUE requests outstanding per file */
133 Scsi_Request
*my_cmdp
; /* != 0 when request with lower levels */
134 struct sg_request
*nextrp
; /* NULL -> tail request (slist) */
135 struct sg_fd
*parentfp
; /* NULL -> not in use */
136 Sg_scatter_hold data
; /* hold buffer, perhaps scatter list */
137 sg_io_hdr_t header
; /* scsi command+info, see <scsi/sg.h> */
138 unsigned char sense_b
[sizeof (dummy_cmdp
->sr_sense_buffer
)];
139 char res_used
; /* 1 -> using reserve buffer, 0 -> not ... */
140 char orphan
; /* 1 -> drop on sight, 0 -> normal */
141 char sg_io_owned
; /* 1 -> packet belongs to SG_IO */
142 volatile char done
; /* 0->before bh, 1->before read, 2->read */
145 typedef struct sg_fd
{ /* holds the state of a file descriptor */
146 struct sg_fd
*nextfp
; /* NULL when last opened fd on this device */
147 struct sg_device
*parentdp
; /* owning device */
148 wait_queue_head_t read_wait
; /* queue read until command done */
149 rwlock_t rq_list_lock
; /* protect access to list in req_arr */
150 int timeout
; /* defaults to SG_DEFAULT_TIMEOUT */
151 int timeout_user
; /* defaults to SG_DEFAULT_TIMEOUT_USER */
152 Sg_scatter_hold reserve
; /* buffer held for this file descriptor */
153 unsigned save_scat_len
; /* original length of trunc. scat. element */
154 Sg_request
*headrp
; /* head of request slist, NULL->empty */
155 struct fasync_struct
*async_qp
; /* used by asynchronous notification */
156 Sg_request req_arr
[SG_MAX_QUEUE
]; /* used as singly-linked list */
157 char low_dma
; /* as in parent but possibly overridden to 1 */
158 char force_packid
; /* 1 -> pack_id input to read(), 0 -> ignored */
159 volatile char closed
; /* 1 -> fd closed but request(s) outstanding */
160 char cmd_q
; /* 1 -> allow command queuing, 0 -> don't */
161 char next_cmd_len
; /* 0 -> automatic (def), >0 -> use on next write() */
162 char keep_orphan
; /* 0 -> drop orphan (def), 1 -> keep for read() */
163 char mmap_called
; /* 0 -> mmap() never called on this fd */
166 typedef struct sg_device
{ /* holds the state of each scsi generic device */
167 struct scsi_device
*device
;
168 wait_queue_head_t o_excl_wait
; /* queue open() when O_EXCL in use */
169 int sg_tablesize
; /* adapter's max scatter-gather table size */
170 Sg_fd
*headfp
; /* first open fd belonging to this device */
171 volatile char detached
; /* 0->attached, 1->detached pending removal */
172 volatile char exclude
; /* opened for exclusive access */
173 char sgdebug
; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
174 struct gendisk
*disk
;
175 struct cdev
* cdev
; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
178 static int sg_fasync(int fd
, struct file
*filp
, int mode
);
179 static void sg_cmd_done(Scsi_Cmnd
* SCpnt
); /* tasklet or soft irq callback */
180 static int sg_start_req(Sg_request
* srp
);
181 static void sg_finish_rem_req(Sg_request
* srp
);
182 static int sg_build_indirect(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
);
183 static int sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
,
185 static ssize_t
sg_new_read(Sg_fd
* sfp
, char __user
*buf
, size_t count
,
187 static ssize_t
sg_new_write(Sg_fd
* sfp
, const char __user
*buf
, size_t count
,
188 int blocking
, int read_only
, Sg_request
** o_srp
);
189 static int sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
190 unsigned char *cmnd
, int timeout
, int blocking
);
191 static int sg_u_iovec(sg_io_hdr_t
* hp
, int sg_num
, int ind
,
192 int wr_xf
, int *countp
, unsigned char __user
**up
);
193 static int sg_write_xfer(Sg_request
* srp
);
194 static int sg_read_xfer(Sg_request
* srp
);
195 static int sg_read_oxfer(Sg_request
* srp
, char __user
*outp
, int num_read_xfer
);
196 static void sg_remove_scat(Sg_scatter_hold
* schp
);
197 static void sg_build_reserve(Sg_fd
* sfp
, int req_size
);
198 static void sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
);
199 static void sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
);
200 static char *sg_page_malloc(int rqSz
, int lowDma
, int *retSzp
);
201 static void sg_page_free(char *buff
, int size
);
202 static Sg_fd
*sg_add_sfp(Sg_device
* sdp
, int dev
);
203 static int sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
);
204 static void __sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
);
205 static Sg_request
*sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
);
206 static Sg_request
*sg_add_request(Sg_fd
* sfp
);
207 static int sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
);
208 static int sg_res_in_use(Sg_fd
* sfp
);
209 static int sg_allow_access(unsigned char opcode
, char dev_type
);
210 static int sg_build_direct(Sg_request
* srp
, Sg_fd
* sfp
, int dxfer_len
);
211 static Sg_device
*sg_get_dev(int dev
);
212 static inline unsigned char *sg_scatg2virt(const struct scatterlist
*sclp
);
213 #ifdef CONFIG_SCSI_PROC_FS
214 static int sg_last_dev(void);
217 static Sg_device
**sg_dev_arr
= NULL
;
218 static int sg_dev_max
;
219 static int sg_nr_dev
;
221 #define SZ_SG_HEADER sizeof(struct sg_header)
222 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
223 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
224 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
227 sg_open(struct inode
*inode
, struct file
*filp
)
229 int dev
= iminor(inode
);
230 int flags
= filp
->f_flags
;
236 nonseekable_open(inode
, filp
);
237 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev
, flags
));
238 sdp
= sg_get_dev(dev
);
239 if ((!sdp
) || (!sdp
->device
))
244 /* This driver's module count bumped by fops_get in <linux/fs.h> */
245 /* Prevent the device driver from vanishing while we sleep */
246 retval
= scsi_device_get(sdp
->device
);
250 if (!((flags
& O_NONBLOCK
) ||
251 scsi_block_when_processing_errors(sdp
->device
))) {
253 /* we are in error recovery for this device */
257 if (flags
& O_EXCL
) {
258 if (O_RDONLY
== (flags
& O_ACCMODE
)) {
259 retval
= -EPERM
; /* Can't lock it with read only access */
262 if (sdp
->headfp
&& (flags
& O_NONBLOCK
)) {
267 __wait_event_interruptible(sdp
->o_excl_wait
,
268 ((sdp
->headfp
|| sdp
->exclude
) ? 0 : (sdp
->exclude
= 1)), res
);
270 retval
= res
; /* -ERESTARTSYS because signal hit process */
273 } else if (sdp
->exclude
) { /* some other fd has an exclusive lock on dev */
274 if (flags
& O_NONBLOCK
) {
279 __wait_event_interruptible(sdp
->o_excl_wait
, (!sdp
->exclude
),
282 retval
= res
; /* -ERESTARTSYS because signal hit process */
290 if (!sdp
->headfp
) { /* no existing opens on this device */
292 sdp
->sg_tablesize
= sdp
->device
->host
->sg_tablesize
;
294 if ((sfp
= sg_add_sfp(sdp
, dev
)))
295 filp
->private_data
= sfp
;
298 sdp
->exclude
= 0; /* undo if error */
305 scsi_device_put(sdp
->device
);
309 /* Following function was formerly called 'sg_close' */
311 sg_release(struct inode
*inode
, struct file
*filp
)
316 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
318 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp
->disk
->disk_name
));
319 sg_fasync(-1, filp
, 0); /* remove filp from async notification list */
320 if (0 == sg_remove_sfp(sdp
, sfp
)) { /* Returns 1 when sdp gone */
321 if (!sdp
->detached
) {
322 scsi_device_put(sdp
->device
);
325 wake_up_interruptible(&sdp
->o_excl_wait
);
331 sg_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
* ppos
)
336 int req_pack_id
= -1;
338 struct sg_header
*old_hdr
= NULL
;
341 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
343 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
344 sdp
->disk
->disk_name
, (int) count
));
345 if (!access_ok(VERIFY_WRITE
, buf
, count
))
347 if (sfp
->force_packid
&& (count
>= SZ_SG_HEADER
)) {
348 old_hdr
= kmalloc(SZ_SG_HEADER
, GFP_KERNEL
);
351 if (__copy_from_user(old_hdr
, buf
, SZ_SG_HEADER
)) {
355 if (old_hdr
->reply_len
< 0) {
356 if (count
>= SZ_SG_IO_HDR
) {
357 sg_io_hdr_t
*new_hdr
;
358 new_hdr
= kmalloc(SZ_SG_IO_HDR
, GFP_KERNEL
);
363 retval
=__copy_from_user
364 (new_hdr
, buf
, SZ_SG_IO_HDR
);
365 req_pack_id
= new_hdr
->pack_id
;
373 req_pack_id
= old_hdr
->pack_id
;
375 srp
= sg_get_rq_mark(sfp
, req_pack_id
);
376 if (!srp
) { /* now wait on packet to arrive */
381 if (filp
->f_flags
& O_NONBLOCK
) {
386 retval
= 0; /* following macro beats race condition */
387 __wait_event_interruptible(sfp
->read_wait
,
389 (srp
= sg_get_rq_mark(sfp
, req_pack_id
))),
398 /* -ERESTARTSYS as signal hit process */
402 if (srp
->header
.interface_id
!= '\0') {
403 retval
= sg_new_read(sfp
, buf
, count
, srp
);
408 if (old_hdr
== NULL
) {
409 old_hdr
= kmalloc(SZ_SG_HEADER
, GFP_KERNEL
);
415 memset(old_hdr
, 0, SZ_SG_HEADER
);
416 old_hdr
->reply_len
= (int) hp
->timeout
;
417 old_hdr
->pack_len
= old_hdr
->reply_len
; /* old, strange behaviour */
418 old_hdr
->pack_id
= hp
->pack_id
;
419 old_hdr
->twelve_byte
=
420 ((srp
->data
.cmd_opcode
>= 0xc0) && (12 == hp
->cmd_len
)) ? 1 : 0;
421 old_hdr
->target_status
= hp
->masked_status
;
422 old_hdr
->host_status
= hp
->host_status
;
423 old_hdr
->driver_status
= hp
->driver_status
;
424 if ((CHECK_CONDITION
& hp
->masked_status
) ||
425 (DRIVER_SENSE
& hp
->driver_status
))
426 memcpy(old_hdr
->sense_buffer
, srp
->sense_b
,
427 sizeof (old_hdr
->sense_buffer
));
428 switch (hp
->host_status
) {
429 /* This setup of 'result' is for backward compatibility and is best
430 ignored by the user who should use target, host + driver status */
432 case DID_PASSTHROUGH
:
439 old_hdr
->result
= EBUSY
;
446 old_hdr
->result
= EIO
;
449 old_hdr
->result
= (srp
->sense_b
[0] == 0 &&
450 hp
->masked_status
== GOOD
) ? 0 : EIO
;
453 old_hdr
->result
= EIO
;
457 /* Now copy the result back to the user buffer. */
458 if (count
>= SZ_SG_HEADER
) {
459 if (__copy_to_user(buf
, old_hdr
, SZ_SG_HEADER
)) {
464 if (count
> old_hdr
->reply_len
)
465 count
= old_hdr
->reply_len
;
466 if (count
> SZ_SG_HEADER
) {
467 if (sg_read_oxfer(srp
, buf
, count
- SZ_SG_HEADER
)) {
473 count
= (old_hdr
->result
== 0) ? 0 : -EIO
;
474 sg_finish_rem_req(srp
);
483 sg_new_read(Sg_fd
* sfp
, char __user
*buf
, size_t count
, Sg_request
* srp
)
485 sg_io_hdr_t
*hp
= &srp
->header
;
489 if (count
< SZ_SG_IO_HDR
) {
494 if ((hp
->mx_sb_len
> 0) && hp
->sbp
) {
495 if ((CHECK_CONDITION
& hp
->masked_status
) ||
496 (DRIVER_SENSE
& hp
->driver_status
)) {
497 int sb_len
= sizeof (dummy_cmdp
->sr_sense_buffer
);
498 sb_len
= (hp
->mx_sb_len
> sb_len
) ? sb_len
: hp
->mx_sb_len
;
499 len
= 8 + (int) srp
->sense_b
[7]; /* Additional sense length field */
500 len
= (len
> sb_len
) ? sb_len
: len
;
501 if (copy_to_user(hp
->sbp
, srp
->sense_b
, len
)) {
508 if (hp
->masked_status
|| hp
->host_status
|| hp
->driver_status
)
509 hp
->info
|= SG_INFO_CHECK
;
510 if (copy_to_user(buf
, hp
, SZ_SG_IO_HDR
)) {
514 err
= sg_read_xfer(srp
);
516 sg_finish_rem_req(srp
);
517 return (0 == err
) ? count
: err
;
521 sg_write(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
* ppos
)
523 int mxsize
, cmd_size
, k
;
524 int input_size
, blocking
;
525 unsigned char opcode
;
529 struct sg_header old_hdr
;
531 unsigned char cmnd
[sizeof (dummy_cmdp
->sr_cmnd
)];
533 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
535 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
536 sdp
->disk
->disk_name
, (int) count
));
539 if (!((filp
->f_flags
& O_NONBLOCK
) ||
540 scsi_block_when_processing_errors(sdp
->device
)))
543 if (!access_ok(VERIFY_READ
, buf
, count
))
544 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
545 if (count
< SZ_SG_HEADER
)
547 if (__copy_from_user(&old_hdr
, buf
, SZ_SG_HEADER
))
549 blocking
= !(filp
->f_flags
& O_NONBLOCK
);
550 if (old_hdr
.reply_len
< 0)
551 return sg_new_write(sfp
, buf
, count
, blocking
, 0, NULL
);
552 if (count
< (SZ_SG_HEADER
+ 6))
553 return -EIO
; /* The minimum scsi command length is 6 bytes. */
555 if (!(srp
= sg_add_request(sfp
))) {
556 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
560 __get_user(opcode
, buf
);
561 if (sfp
->next_cmd_len
> 0) {
562 if (sfp
->next_cmd_len
> MAX_COMMAND_SIZE
) {
563 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
564 sfp
->next_cmd_len
= 0;
565 sg_remove_request(sfp
, srp
);
568 cmd_size
= sfp
->next_cmd_len
;
569 sfp
->next_cmd_len
= 0; /* reset so only this write() effected */
571 cmd_size
= COMMAND_SIZE(opcode
); /* based on SCSI command group */
572 if ((opcode
>= 0xc0) && old_hdr
.twelve_byte
)
575 SCSI_LOG_TIMEOUT(4, printk(
576 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode
, cmd_size
));
577 /* Determine buffer size. */
578 input_size
= count
- cmd_size
;
579 mxsize
= (input_size
> old_hdr
.reply_len
) ? input_size
: old_hdr
.reply_len
;
580 mxsize
-= SZ_SG_HEADER
;
581 input_size
-= SZ_SG_HEADER
;
582 if (input_size
< 0) {
583 sg_remove_request(sfp
, srp
);
584 return -EIO
; /* User did not pass enough bytes for this command. */
587 hp
->interface_id
= '\0'; /* indicator of old interface tunnelled */
588 hp
->cmd_len
= (unsigned char) cmd_size
;
592 hp
->dxfer_direction
= (old_hdr
.reply_len
> SZ_SG_HEADER
) ?
593 SG_DXFER_TO_FROM_DEV
: SG_DXFER_TO_DEV
;
595 hp
->dxfer_direction
= (mxsize
> 0) ? SG_DXFER_FROM_DEV
: SG_DXFER_NONE
;
596 hp
->dxfer_len
= mxsize
;
597 hp
->dxferp
= (char __user
*)buf
+ cmd_size
;
599 hp
->timeout
= old_hdr
.reply_len
; /* structure abuse ... */
600 hp
->flags
= input_size
; /* structure abuse ... */
601 hp
->pack_id
= old_hdr
.pack_id
;
603 if (__copy_from_user(cmnd
, buf
, cmd_size
))
606 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
607 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
608 * is a non-zero input_size, so emit a warning.
610 if (hp
->dxfer_direction
== SG_DXFER_TO_FROM_DEV
)
611 if (printk_ratelimit())
613 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
614 "guessing data in;\n" KERN_WARNING
" "
615 "program %s not setting count and/or reply_len properly\n",
616 old_hdr
.reply_len
- (int)SZ_SG_HEADER
,
617 input_size
, (unsigned int) cmnd
[0],
619 k
= sg_common_write(sfp
, srp
, cmnd
, sfp
->timeout
, blocking
);
620 return (k
< 0) ? k
: count
;
624 sg_new_write(Sg_fd
* sfp
, const char __user
*buf
, size_t count
,
625 int blocking
, int read_only
, Sg_request
** o_srp
)
630 unsigned char cmnd
[sizeof (dummy_cmdp
->sr_cmnd
)];
632 unsigned long ul_timeout
;
634 if (count
< SZ_SG_IO_HDR
)
636 if (!access_ok(VERIFY_READ
, buf
, count
))
637 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
639 sfp
->cmd_q
= 1; /* when sg_io_hdr seen, set command queuing on */
640 if (!(srp
= sg_add_request(sfp
))) {
641 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
645 if (__copy_from_user(hp
, buf
, SZ_SG_IO_HDR
)) {
646 sg_remove_request(sfp
, srp
);
649 if (hp
->interface_id
!= 'S') {
650 sg_remove_request(sfp
, srp
);
653 if (hp
->flags
& SG_FLAG_MMAP_IO
) {
654 if (hp
->dxfer_len
> sfp
->reserve
.bufflen
) {
655 sg_remove_request(sfp
, srp
);
656 return -ENOMEM
; /* MMAP_IO size must fit in reserve buffer */
658 if (hp
->flags
& SG_FLAG_DIRECT_IO
) {
659 sg_remove_request(sfp
, srp
);
660 return -EINVAL
; /* either MMAP_IO or DIRECT_IO (not both) */
662 if (sg_res_in_use(sfp
)) {
663 sg_remove_request(sfp
, srp
);
664 return -EBUSY
; /* reserve buffer already being used */
667 ul_timeout
= msecs_to_jiffies(srp
->header
.timeout
);
668 timeout
= (ul_timeout
< INT_MAX
) ? ul_timeout
: INT_MAX
;
669 if ((!hp
->cmdp
) || (hp
->cmd_len
< 6) || (hp
->cmd_len
> sizeof (cmnd
))) {
670 sg_remove_request(sfp
, srp
);
673 if (!access_ok(VERIFY_READ
, hp
->cmdp
, hp
->cmd_len
)) {
674 sg_remove_request(sfp
, srp
);
675 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
677 if (__copy_from_user(cmnd
, hp
->cmdp
, hp
->cmd_len
)) {
678 sg_remove_request(sfp
, srp
);
682 (!sg_allow_access(cmnd
[0], sfp
->parentdp
->device
->type
))) {
683 sg_remove_request(sfp
, srp
);
686 k
= sg_common_write(sfp
, srp
, cmnd
, timeout
, blocking
);
695 sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
696 unsigned char *cmnd
, int timeout
, int blocking
)
700 Sg_device
*sdp
= sfp
->parentdp
;
701 sg_io_hdr_t
*hp
= &srp
->header
;
704 srp
->data
.cmd_opcode
= cmnd
[0]; /* hold opcode of command */
706 hp
->masked_status
= 0;
710 hp
->driver_status
= 0;
712 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
713 (int) cmnd
[0], (int) hp
->cmd_len
));
715 if ((k
= sg_start_req(srp
))) {
716 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k
));
717 sg_finish_rem_req(srp
);
718 return k
; /* probably out of space --> ENOMEM */
720 if ((k
= sg_write_xfer(srp
))) {
721 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
722 sg_finish_rem_req(srp
);
726 sg_finish_rem_req(srp
);
729 SRpnt
= scsi_allocate_request(sdp
->device
, GFP_ATOMIC
);
731 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
732 sg_finish_rem_req(srp
);
736 srp
->my_cmdp
= SRpnt
;
737 q
= SRpnt
->sr_device
->request_queue
;
738 SRpnt
->sr_request
->rq_disk
= sdp
->disk
;
739 SRpnt
->sr_sense_buffer
[0] = 0;
740 SRpnt
->sr_cmd_len
= hp
->cmd_len
;
741 SRpnt
->sr_use_sg
= srp
->data
.k_use_sg
;
742 SRpnt
->sr_sglist_len
= srp
->data
.sglist_len
;
743 SRpnt
->sr_bufflen
= srp
->data
.bufflen
;
744 SRpnt
->sr_underflow
= 0;
745 SRpnt
->sr_buffer
= srp
->data
.buffer
;
746 switch (hp
->dxfer_direction
) {
747 case SG_DXFER_TO_FROM_DEV
:
748 case SG_DXFER_FROM_DEV
:
749 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
751 case SG_DXFER_TO_DEV
:
752 SRpnt
->sr_data_direction
= SCSI_DATA_WRITE
;
754 case SG_DXFER_UNKNOWN
:
755 SRpnt
->sr_data_direction
= SCSI_DATA_UNKNOWN
;
758 SRpnt
->sr_data_direction
= SCSI_DATA_NONE
;
761 SRpnt
->upper_private_data
= srp
;
762 srp
->data
.k_use_sg
= 0;
763 srp
->data
.sglist_len
= 0;
764 srp
->data
.bufflen
= 0;
765 srp
->data
.buffer
= NULL
;
766 hp
->duration
= jiffies_to_msecs(jiffies
);
767 /* Now send everything of to mid-level. The next time we hear about this
768 packet is when sg_cmd_done() is called (i.e. a callback). */
769 scsi_do_req(SRpnt
, (void *) cmnd
,
770 (void *) SRpnt
->sr_buffer
, hp
->dxfer_len
,
771 sg_cmd_done
, timeout
, SG_DEFAULT_RETRIES
);
772 /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
777 sg_srp_done(Sg_request
*srp
, Sg_fd
*sfp
)
779 unsigned long iflags
;
782 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
784 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
789 sg_ioctl(struct inode
*inode
, struct file
*filp
,
790 unsigned int cmd_in
, unsigned long arg
)
792 void __user
*p
= (void __user
*)arg
;
794 int result
, val
, read_only
;
798 unsigned long iflags
;
800 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
802 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
803 sdp
->disk
->disk_name
, (int) cmd_in
));
804 read_only
= (O_RDWR
!= (filp
->f_flags
& O_ACCMODE
));
809 int blocking
= 1; /* ignore O_NONBLOCK flag */
813 if (!scsi_block_when_processing_errors(sdp
->device
))
815 if (!access_ok(VERIFY_WRITE
, p
, SZ_SG_IO_HDR
))
818 sg_new_write(sfp
, p
, SZ_SG_IO_HDR
,
819 blocking
, read_only
, &srp
);
822 srp
->sg_io_owned
= 1;
824 result
= 0; /* following macro to beat race condition */
825 __wait_event_interruptible(sfp
->read_wait
,
826 (sdp
->detached
|| sfp
->closed
|| sg_srp_done(srp
, sfp
)),
831 return 0; /* request packet dropped already */
835 return result
; /* -ERESTARTSYS because signal hit process */
837 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
839 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
840 result
= sg_new_read(sfp
, p
, SZ_SG_IO_HDR
, srp
);
841 return (result
< 0) ? result
: 0;
844 result
= get_user(val
, ip
);
849 if (val
>= MULDIV (INT_MAX
, USER_HZ
, HZ
))
850 val
= MULDIV (INT_MAX
, USER_HZ
, HZ
);
851 sfp
->timeout_user
= val
;
852 sfp
->timeout
= MULDIV (val
, HZ
, USER_HZ
);
855 case SG_GET_TIMEOUT
: /* N.B. User receives timeout as return value */
856 /* strange ..., for backward compatibility */
857 return sfp
->timeout_user
;
858 case SG_SET_FORCE_LOW_DMA
:
859 result
= get_user(val
, ip
);
864 if ((0 == sfp
->low_dma
) && (0 == sg_res_in_use(sfp
))) {
865 val
= (int) sfp
->reserve
.bufflen
;
866 sg_remove_scat(&sfp
->reserve
);
867 sg_build_reserve(sfp
, val
);
872 sfp
->low_dma
= sdp
->device
->host
->unchecked_isa_dma
;
876 return put_user((int) sfp
->low_dma
, ip
);
878 if (!access_ok(VERIFY_WRITE
, p
, sizeof (sg_scsi_id_t
)))
881 sg_scsi_id_t __user
*sg_idp
= p
;
885 __put_user((int) sdp
->device
->host
->host_no
,
887 __put_user((int) sdp
->device
->channel
,
889 __put_user((int) sdp
->device
->id
, &sg_idp
->scsi_id
);
890 __put_user((int) sdp
->device
->lun
, &sg_idp
->lun
);
891 __put_user((int) sdp
->device
->type
, &sg_idp
->scsi_type
);
892 __put_user((short) sdp
->device
->host
->cmd_per_lun
,
893 &sg_idp
->h_cmd_per_lun
);
894 __put_user((short) sdp
->device
->queue_depth
,
895 &sg_idp
->d_queue_depth
);
896 __put_user(0, &sg_idp
->unused
[0]);
897 __put_user(0, &sg_idp
->unused
[1]);
900 case SG_SET_FORCE_PACK_ID
:
901 result
= get_user(val
, ip
);
904 sfp
->force_packid
= val
? 1 : 0;
907 if (!access_ok(VERIFY_WRITE
, ip
, sizeof (int)))
909 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
910 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
911 if ((1 == srp
->done
) && (!srp
->sg_io_owned
)) {
912 read_unlock_irqrestore(&sfp
->rq_list_lock
,
914 __put_user(srp
->header
.pack_id
, ip
);
918 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
921 case SG_GET_NUM_WAITING
:
922 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
923 for (val
= 0, srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
924 if ((1 == srp
->done
) && (!srp
->sg_io_owned
))
927 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
928 return put_user(val
, ip
);
929 case SG_GET_SG_TABLESIZE
:
930 return put_user(sdp
->sg_tablesize
, ip
);
931 case SG_SET_RESERVED_SIZE
:
932 result
= get_user(val
, ip
);
937 if (val
!= sfp
->reserve
.bufflen
) {
938 if (sg_res_in_use(sfp
) || sfp
->mmap_called
)
940 sg_remove_scat(&sfp
->reserve
);
941 sg_build_reserve(sfp
, val
);
944 case SG_GET_RESERVED_SIZE
:
945 val
= (int) sfp
->reserve
.bufflen
;
946 return put_user(val
, ip
);
947 case SG_SET_COMMAND_Q
:
948 result
= get_user(val
, ip
);
951 sfp
->cmd_q
= val
? 1 : 0;
953 case SG_GET_COMMAND_Q
:
954 return put_user((int) sfp
->cmd_q
, ip
);
955 case SG_SET_KEEP_ORPHAN
:
956 result
= get_user(val
, ip
);
959 sfp
->keep_orphan
= val
;
961 case SG_GET_KEEP_ORPHAN
:
962 return put_user((int) sfp
->keep_orphan
, ip
);
963 case SG_NEXT_CMD_LEN
:
964 result
= get_user(val
, ip
);
967 sfp
->next_cmd_len
= (val
> 0) ? val
: 0;
969 case SG_GET_VERSION_NUM
:
970 return put_user(sg_version_num
, ip
);
971 case SG_GET_ACCESS_COUNT
:
972 /* faked - we don't have a real access count anymore */
973 val
= (sdp
->device
? 1 : 0);
974 return put_user(val
, ip
);
975 case SG_GET_REQUEST_TABLE
:
976 if (!access_ok(VERIFY_WRITE
, p
, SZ_SG_REQ_INFO
* SG_MAX_QUEUE
))
979 sg_req_info_t
*rinfo
;
982 rinfo
= kmalloc(SZ_SG_REQ_INFO
* SG_MAX_QUEUE
,
986 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
987 for (srp
= sfp
->headrp
, val
= 0; val
< SG_MAX_QUEUE
;
988 ++val
, srp
= srp
? srp
->nextrp
: srp
) {
989 memset(&rinfo
[val
], 0, SZ_SG_REQ_INFO
);
991 rinfo
[val
].req_state
= srp
->done
+ 1;
993 srp
->header
.masked_status
&
994 srp
->header
.host_status
&
995 srp
->header
.driver_status
;
997 rinfo
[val
].duration
=
998 srp
->header
.duration
;
1000 ms
= jiffies_to_msecs(jiffies
);
1001 rinfo
[val
].duration
=
1002 (ms
> srp
->header
.duration
) ?
1003 (ms
- srp
->header
.duration
) : 0;
1005 rinfo
[val
].orphan
= srp
->orphan
;
1006 rinfo
[val
].sg_io_owned
=
1008 rinfo
[val
].pack_id
=
1009 srp
->header
.pack_id
;
1010 rinfo
[val
].usr_ptr
=
1011 srp
->header
.usr_ptr
;
1014 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1015 result
= __copy_to_user(p
, rinfo
,
1016 SZ_SG_REQ_INFO
* SG_MAX_QUEUE
);
1017 result
= result
? -EFAULT
: 0;
1021 case SG_EMULATED_HOST
:
1024 return put_user(sdp
->device
->host
->hostt
->emulated
, ip
);
1028 if (filp
->f_flags
& O_NONBLOCK
) {
1029 if (test_bit(SHOST_RECOVERY
,
1030 &sdp
->device
->host
->shost_state
))
1032 } else if (!scsi_block_when_processing_errors(sdp
->device
))
1034 result
= get_user(val
, ip
);
1037 if (SG_SCSI_RESET_NOTHING
== val
)
1040 case SG_SCSI_RESET_DEVICE
:
1041 val
= SCSI_TRY_RESET_DEVICE
;
1043 case SG_SCSI_RESET_BUS
:
1044 val
= SCSI_TRY_RESET_BUS
;
1046 case SG_SCSI_RESET_HOST
:
1047 val
= SCSI_TRY_RESET_HOST
;
1052 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
1054 return (scsi_reset_provider(sdp
->device
, val
) ==
1055 SUCCESS
) ? 0 : -EIO
;
1056 case SCSI_IOCTL_SEND_COMMAND
:
1060 unsigned char opcode
= WRITE_6
;
1061 Scsi_Ioctl_Command __user
*siocp
= p
;
1063 if (copy_from_user(&opcode
, siocp
->data
, 1))
1065 if (!sg_allow_access(opcode
, sdp
->device
->type
))
1068 return scsi_ioctl_send_command(sdp
->device
, p
);
1070 result
= get_user(val
, ip
);
1073 sdp
->sgdebug
= (char) val
;
1075 case SCSI_IOCTL_GET_IDLUN
:
1076 case SCSI_IOCTL_GET_BUS_NUMBER
:
1077 case SCSI_IOCTL_PROBE_HOST
:
1078 case SG_GET_TRANSFORM
:
1081 return scsi_ioctl(sdp
->device
, cmd_in
, p
);
1084 return -EPERM
; /* don't know so take safe approach */
1085 return scsi_ioctl(sdp
->device
, cmd_in
, p
);
1089 #ifdef CONFIG_COMPAT
1090 static long sg_compat_ioctl(struct file
*filp
, unsigned int cmd_in
, unsigned long arg
)
1094 struct scsi_device
*sdev
;
1096 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
1100 if (sdev
->host
->hostt
->compat_ioctl
) {
1103 ret
= sdev
->host
->hostt
->compat_ioctl(sdev
, cmd_in
, (void __user
*)arg
);
1108 return -ENOIOCTLCMD
;
1113 sg_poll(struct file
*filp
, poll_table
* wait
)
1115 unsigned int res
= 0;
1120 unsigned long iflags
;
1122 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
))
1125 poll_wait(filp
, &sfp
->read_wait
, wait
);
1126 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1127 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
1128 /* if any read waiting, flag it */
1129 if ((0 == res
) && (1 == srp
->done
) && (!srp
->sg_io_owned
))
1130 res
= POLLIN
| POLLRDNORM
;
1133 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1137 else if (!sfp
->cmd_q
) {
1139 res
|= POLLOUT
| POLLWRNORM
;
1140 } else if (count
< SG_MAX_QUEUE
)
1141 res
|= POLLOUT
| POLLWRNORM
;
1142 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1143 sdp
->disk
->disk_name
, (int) res
));
1148 sg_fasync(int fd
, struct file
*filp
, int mode
)
1154 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
1156 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1157 sdp
->disk
->disk_name
, mode
));
1159 retval
= fasync_helper(fd
, filp
, mode
, &sfp
->async_qp
);
1160 return (retval
< 0) ? retval
: 0;
1163 static inline unsigned char *
1164 sg_scatg2virt(const struct scatterlist
*sclp
)
1166 return (sclp
&& sclp
->page
) ?
1167 (unsigned char *) page_address(sclp
->page
) + sclp
->offset
: NULL
;
1170 /* When startFinish==1 increments page counts for pages other than the
1171 first of scatter gather elements obtained from __get_free_pages().
1172 When startFinish==0 decrements ... */
1174 sg_rb_correct4mmap(Sg_scatter_hold
* rsv_schp
, int startFinish
)
1180 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1181 startFinish
, rsv_schp
->k_use_sg
));
1182 /* N.B. correction _not_ applied to base page of each allocation */
1183 if (rsv_schp
->k_use_sg
) { /* reserve buffer is a scatter gather list */
1184 struct scatterlist
*sclp
= rsv_schp
->buffer
;
1186 for (k
= 0; k
< rsv_schp
->k_use_sg
; ++k
, ++sclp
) {
1187 for (m
= PAGE_SIZE
; m
< sclp
->length
; m
+= PAGE_SIZE
) {
1188 page_ptr
= sg_scatg2virt(sclp
) + m
;
1189 page
= virt_to_page(page_ptr
);
1193 if (page_count(page
) > 0)
1198 } else { /* reserve buffer is just a single allocation */
1199 for (m
= PAGE_SIZE
; m
< rsv_schp
->bufflen
; m
+= PAGE_SIZE
) {
1200 page_ptr
= (unsigned char *) rsv_schp
->buffer
+ m
;
1201 page
= virt_to_page(page_ptr
);
1205 if (page_count(page
) > 0)
1212 static struct page
*
1213 sg_vma_nopage(struct vm_area_struct
*vma
, unsigned long addr
, int *type
)
1216 struct page
*page
= NOPAGE_SIGBUS
;
1217 void *page_ptr
= NULL
;
1218 unsigned long offset
;
1219 Sg_scatter_hold
*rsv_schp
;
1221 if ((NULL
== vma
) || (!(sfp
= (Sg_fd
*) vma
->vm_private_data
)))
1223 rsv_schp
= &sfp
->reserve
;
1224 offset
= addr
- vma
->vm_start
;
1225 if (offset
>= rsv_schp
->bufflen
)
1227 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1228 offset
, rsv_schp
->k_use_sg
));
1229 if (rsv_schp
->k_use_sg
) { /* reserve buffer is a scatter gather list */
1231 unsigned long sa
= vma
->vm_start
;
1233 struct scatterlist
*sclp
= rsv_schp
->buffer
;
1235 for (k
= 0; (k
< rsv_schp
->k_use_sg
) && (sa
< vma
->vm_end
);
1237 len
= vma
->vm_end
- sa
;
1238 len
= (len
< sclp
->length
) ? len
: sclp
->length
;
1240 page_ptr
= sg_scatg2virt(sclp
) + offset
;
1241 page
= virt_to_page(page_ptr
);
1242 get_page(page
); /* increment page count */
1248 } else { /* reserve buffer is just a single allocation */
1249 page_ptr
= (unsigned char *) rsv_schp
->buffer
+ offset
;
1250 page
= virt_to_page(page_ptr
);
1251 get_page(page
); /* increment page count */
1254 *type
= VM_FAULT_MINOR
;
1258 static struct vm_operations_struct sg_mmap_vm_ops
= {
1259 .nopage
= sg_vma_nopage
,
1263 sg_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1266 unsigned long req_sz
;
1267 Sg_scatter_hold
*rsv_schp
;
1269 if ((!filp
) || (!vma
) || (!(sfp
= (Sg_fd
*) filp
->private_data
)))
1271 req_sz
= vma
->vm_end
- vma
->vm_start
;
1272 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1273 (void *) vma
->vm_start
, (int) req_sz
));
1275 return -EINVAL
; /* want no offset */
1276 rsv_schp
= &sfp
->reserve
;
1277 if (req_sz
> rsv_schp
->bufflen
)
1278 return -ENOMEM
; /* cannot map more than reserved buffer */
1280 if (rsv_schp
->k_use_sg
) { /* reserve buffer is a scatter gather list */
1282 unsigned long sa
= vma
->vm_start
;
1284 struct scatterlist
*sclp
= rsv_schp
->buffer
;
1286 for (k
= 0; (k
< rsv_schp
->k_use_sg
) && (sa
< vma
->vm_end
);
1288 if (0 != sclp
->offset
)
1289 return -EFAULT
; /* non page aligned memory ?? */
1290 len
= vma
->vm_end
- sa
;
1291 len
= (len
< sclp
->length
) ? len
: sclp
->length
;
1294 } else { /* reserve buffer is just a single allocation */
1295 if ((unsigned long) rsv_schp
->buffer
& (PAGE_SIZE
- 1))
1296 return -EFAULT
; /* non page aligned memory ?? */
1298 if (0 == sfp
->mmap_called
) {
1299 sg_rb_correct4mmap(rsv_schp
, 1); /* do only once per fd lifetime */
1300 sfp
->mmap_called
= 1;
1302 vma
->vm_flags
|= (VM_RESERVED
| VM_IO
);
1303 vma
->vm_private_data
= sfp
;
1304 vma
->vm_ops
= &sg_mmap_vm_ops
;
1308 /* This function is a "bottom half" handler that is called by the
1309 * mid level when a command is completed (or has failed). */
1311 sg_cmd_done(Scsi_Cmnd
* SCpnt
)
1313 Scsi_Request
*SRpnt
= NULL
;
1314 Sg_device
*sdp
= NULL
;
1316 Sg_request
*srp
= NULL
;
1317 unsigned long iflags
;
1320 if (SCpnt
&& (SRpnt
= SCpnt
->sc_request
))
1321 srp
= (Sg_request
*) SRpnt
->upper_private_data
;
1323 printk(KERN_ERR
"sg_cmd_done: NULL request\n");
1325 scsi_release_request(SRpnt
);
1328 sfp
= srp
->parentfp
;
1330 sdp
= sfp
->parentdp
;
1331 if ((NULL
== sdp
) || sdp
->detached
) {
1332 printk(KERN_INFO
"sg_cmd_done: device detached\n");
1333 scsi_release_request(SRpnt
);
1337 /* First transfer ownership of data buffers to sg_device object. */
1338 srp
->data
.k_use_sg
= SRpnt
->sr_use_sg
;
1339 srp
->data
.sglist_len
= SRpnt
->sr_sglist_len
;
1340 srp
->data
.bufflen
= SRpnt
->sr_bufflen
;
1341 srp
->data
.buffer
= SRpnt
->sr_buffer
;
1342 /* now clear out request structure */
1343 SRpnt
->sr_use_sg
= 0;
1344 SRpnt
->sr_sglist_len
= 0;
1345 SRpnt
->sr_bufflen
= 0;
1346 SRpnt
->sr_buffer
= NULL
;
1347 SRpnt
->sr_underflow
= 0;
1348 SRpnt
->sr_request
->rq_disk
= NULL
; /* "sg" _disowns_ request blk */
1350 srp
->my_cmdp
= NULL
;
1352 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1353 sdp
->disk
->disk_name
, srp
->header
.pack_id
, (int) SRpnt
->sr_result
));
1354 srp
->header
.resid
= SCpnt
->resid
;
1355 ms
= jiffies_to_msecs(jiffies
);
1356 srp
->header
.duration
= (ms
> srp
->header
.duration
) ?
1357 (ms
- srp
->header
.duration
) : 0;
1358 if (0 != SRpnt
->sr_result
) {
1359 struct scsi_sense_hdr sshdr
;
1361 memcpy(srp
->sense_b
, SRpnt
->sr_sense_buffer
,
1362 sizeof (srp
->sense_b
));
1363 srp
->header
.status
= 0xff & SRpnt
->sr_result
;
1364 srp
->header
.masked_status
= status_byte(SRpnt
->sr_result
);
1365 srp
->header
.msg_status
= msg_byte(SRpnt
->sr_result
);
1366 srp
->header
.host_status
= host_byte(SRpnt
->sr_result
);
1367 srp
->header
.driver_status
= driver_byte(SRpnt
->sr_result
);
1368 if ((sdp
->sgdebug
> 0) &&
1369 ((CHECK_CONDITION
== srp
->header
.masked_status
) ||
1370 (COMMAND_TERMINATED
== srp
->header
.masked_status
)))
1371 print_req_sense("sg_cmd_done", SRpnt
);
1373 /* Following if statement is a patch supplied by Eric Youngdale */
1374 if (driver_byte(SRpnt
->sr_result
) != 0
1375 && scsi_command_normalize_sense(SCpnt
, &sshdr
)
1376 && !scsi_sense_is_deferred(&sshdr
)
1377 && sshdr
.sense_key
== UNIT_ATTENTION
1378 && sdp
->device
->removable
) {
1379 /* Detected possible disc change. Set the bit - this */
1380 /* may be used if there are filesystems using this device */
1381 sdp
->device
->changed
= 1;
1384 /* Rely on write phase to clean out srp status values, so no "else" */
1386 scsi_release_request(SRpnt
);
1388 if (sfp
->closed
) { /* whoops this fd already released, cleanup */
1389 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1390 sg_finish_rem_req(srp
);
1392 if (NULL
== sfp
->headrp
) {
1393 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1394 if (0 == sg_remove_sfp(sdp
, sfp
)) { /* device still present */
1395 scsi_device_put(sdp
->device
);
1399 } else if (srp
&& srp
->orphan
) {
1400 if (sfp
->keep_orphan
)
1401 srp
->sg_io_owned
= 0;
1403 sg_finish_rem_req(srp
);
1408 /* Now wake up any sg_read() that is waiting for this packet. */
1409 kill_fasync(&sfp
->async_qp
, SIGPOLL
, POLL_IN
);
1410 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1412 wake_up_interruptible(&sfp
->read_wait
);
1413 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1417 static struct file_operations sg_fops
= {
1418 .owner
= THIS_MODULE
,
1423 #ifdef CONFIG_COMPAT
1424 .compat_ioctl
= sg_compat_ioctl
,
1428 .release
= sg_release
,
1429 .fasync
= sg_fasync
,
1432 static struct class_simple
* sg_sysfs_class
;
1434 static int sg_sysfs_valid
= 0;
1436 static int sg_alloc(struct gendisk
*disk
, struct scsi_device
*scsidp
)
1439 unsigned long iflags
;
1440 void *old_sg_dev_arr
= NULL
;
1443 sdp
= kmalloc(sizeof(Sg_device
), GFP_KERNEL
);
1445 printk(KERN_WARNING
"kmalloc Sg_device failure\n");
1449 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1450 if (unlikely(sg_nr_dev
>= sg_dev_max
)) { /* try to resize */
1452 int tmp_dev_max
= sg_nr_dev
+ SG_DEV_ARR_LUMP
;
1453 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1455 tmp_da
= kmalloc(tmp_dev_max
* sizeof(Sg_device
*), GFP_KERNEL
);
1456 if (unlikely(!tmp_da
))
1459 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1460 memset(tmp_da
, 0, tmp_dev_max
* sizeof(Sg_device
*));
1461 memcpy(tmp_da
, sg_dev_arr
, sg_dev_max
* sizeof(Sg_device
*));
1462 old_sg_dev_arr
= sg_dev_arr
;
1463 sg_dev_arr
= tmp_da
;
1464 sg_dev_max
= tmp_dev_max
;
1467 for (k
= 0; k
< sg_dev_max
; k
++)
1470 if (unlikely(k
>= SG_MAX_DEVS
))
1473 memset(sdp
, 0, sizeof(*sdp
));
1474 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k
));
1475 sprintf(disk
->disk_name
, "sg%d", k
);
1476 disk
->first_minor
= k
;
1478 sdp
->device
= scsidp
;
1479 init_waitqueue_head(&sdp
->o_excl_wait
);
1480 sdp
->sg_tablesize
= scsidp
->host
? scsidp
->host
->sg_tablesize
: 0;
1483 sg_dev_arr
[k
] = sdp
;
1484 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1490 kfree(old_sg_dev_arr
);
1494 printk(KERN_WARNING
"sg_alloc: device array cannot be resized\n");
1499 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1501 "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1502 "number exceeds %d\n", scsidp
->host
->host_no
, scsidp
->channel
,
1503 scsidp
->id
, scsidp
->lun
, scsidp
->type
, SG_MAX_DEVS
- 1);
1509 sg_add(struct class_device
*cl_dev
)
1511 struct scsi_device
*scsidp
= to_scsi_device(cl_dev
->dev
);
1512 struct gendisk
*disk
;
1513 Sg_device
*sdp
= NULL
;
1514 struct cdev
* cdev
= NULL
;
1517 disk
= alloc_disk(1);
1519 printk(KERN_WARNING
"alloc_disk failed\n");
1522 disk
->major
= SCSI_GENERIC_MAJOR
;
1525 cdev
= cdev_alloc();
1527 printk(KERN_WARNING
"cdev_alloc failed\n");
1530 cdev
->owner
= THIS_MODULE
;
1531 cdev
->ops
= &sg_fops
;
1533 error
= sg_alloc(disk
, scsidp
);
1535 printk(KERN_WARNING
"sg_alloc failed\n");
1539 sdp
= sg_dev_arr
[k
];
1541 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR
, k
),
1542 S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
,
1543 "%s/generic", scsidp
->devfs_name
);
1544 error
= cdev_add(cdev
, MKDEV(SCSI_GENERIC_MAJOR
, k
), 1);
1546 devfs_remove("%s/generic", scsidp
->devfs_name
);
1550 if (sg_sysfs_valid
) {
1551 struct class_device
* sg_class_member
;
1553 sg_class_member
= class_simple_device_add(sg_sysfs_class
,
1554 MKDEV(SCSI_GENERIC_MAJOR
, k
),
1557 if (IS_ERR(sg_class_member
))
1558 printk(KERN_WARNING
"sg_add: "
1559 "class_simple_device_add failed\n");
1560 class_set_devdata(sg_class_member
, sdp
);
1561 error
= sysfs_create_link(&scsidp
->sdev_gendev
.kobj
,
1562 &sg_class_member
->kobj
, "generic");
1564 printk(KERN_ERR
"sg_add: unable to make symlink "
1565 "'generic' back to sg%d\n", k
);
1567 printk(KERN_WARNING
"sg_add: sg_sys INvalid\n");
1570 "Attached scsi generic sg%d at scsi%d, channel"
1571 " %d, id %d, lun %d, type %d\n", k
,
1572 scsidp
->host
->host_no
, scsidp
->channel
, scsidp
->id
,
1573 scsidp
->lun
, scsidp
->type
);
1585 sg_remove(struct class_device
*cl_dev
)
1587 struct scsi_device
*scsidp
= to_scsi_device(cl_dev
->dev
);
1588 Sg_device
*sdp
= NULL
;
1589 unsigned long iflags
;
1596 if (NULL
== sg_dev_arr
)
1599 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1600 for (k
= 0; k
< sg_dev_max
; k
++) {
1601 sdp
= sg_dev_arr
[k
];
1602 if ((NULL
== sdp
) || (sdp
->device
!= scsidp
))
1603 continue; /* dirty but lowers nesting */
1606 for (sfp
= sdp
->headfp
; sfp
; sfp
= tsfp
) {
1608 for (srp
= sfp
->headrp
; srp
; srp
= tsrp
) {
1610 if (sfp
->closed
|| (0 == sg_srp_done(srp
, sfp
)))
1611 sg_finish_rem_req(srp
);
1614 scsi_device_put(sdp
->device
);
1615 __sg_remove_sfp(sdp
, sfp
);
1618 wake_up_interruptible(&sfp
->read_wait
);
1619 kill_fasync(&sfp
->async_qp
, SIGPOLL
,
1623 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k
));
1624 if (NULL
== sdp
->headfp
) {
1625 sg_dev_arr
[k
] = NULL
;
1627 } else { /* nothing active, simple case */
1628 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k
));
1629 sg_dev_arr
[k
] = NULL
;
1634 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1637 sysfs_remove_link(&scsidp
->sdev_gendev
.kobj
, "generic");
1638 class_simple_device_remove(MKDEV(SCSI_GENERIC_MAJOR
, k
));
1639 cdev_del(sdp
->cdev
);
1641 devfs_remove("%s/generic", scsidp
->devfs_name
);
1642 put_disk(sdp
->disk
);
1644 if (NULL
== sdp
->headfp
)
1645 kfree((char *) sdp
);
1649 msleep(10); /* dirty detach so delay device destruction */
1652 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1653 * of sysfs parameters (which module_param doesn't yet support).
1654 * Sysfs parameters defined explicitly below.
1656 module_param_named(def_reserved_size
, def_reserved_size
, int, S_IRUGO
);
1657 module_param_named(allow_dio
, sg_allow_dio
, int, S_IRUGO
| S_IWUSR
);
1659 MODULE_AUTHOR("Douglas Gilbert");
1660 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1661 MODULE_LICENSE("GPL");
1662 MODULE_VERSION(SG_VERSION_STR
);
1664 MODULE_PARM_DESC(def_reserved_size
, "size of buffer reserved for each fd");
1665 MODULE_PARM_DESC(allow_dio
, "allow direct I/O (default: 0 (disallow))");
1672 if (def_reserved_size
>= 0)
1673 sg_big_buff
= def_reserved_size
;
1675 rc
= register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0),
1679 sg_sysfs_class
= class_simple_create(THIS_MODULE
, "scsi_generic");
1680 if ( IS_ERR(sg_sysfs_class
) ) {
1681 rc
= PTR_ERR(sg_sysfs_class
);
1685 rc
= scsi_register_interface(&sg_interface
);
1687 #ifdef CONFIG_SCSI_PROC_FS
1689 #endif /* CONFIG_SCSI_PROC_FS */
1692 class_simple_destroy(sg_sysfs_class
);
1694 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0), SG_MAX_DEVS
);
1701 #ifdef CONFIG_SCSI_PROC_FS
1703 #endif /* CONFIG_SCSI_PROC_FS */
1704 scsi_unregister_interface(&sg_interface
);
1705 class_simple_destroy(sg_sysfs_class
);
1707 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0),
1709 if (sg_dev_arr
!= NULL
) {
1710 kfree((char *) sg_dev_arr
);
1717 sg_start_req(Sg_request
* srp
)
1720 Sg_fd
*sfp
= srp
->parentfp
;
1721 sg_io_hdr_t
*hp
= &srp
->header
;
1722 int dxfer_len
= (int) hp
->dxfer_len
;
1723 int dxfer_dir
= hp
->dxfer_direction
;
1724 Sg_scatter_hold
*req_schp
= &srp
->data
;
1725 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
1727 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len
));
1728 if ((dxfer_len
<= 0) || (dxfer_dir
== SG_DXFER_NONE
))
1730 if (sg_allow_dio
&& (hp
->flags
& SG_FLAG_DIRECT_IO
) &&
1731 (dxfer_dir
!= SG_DXFER_UNKNOWN
) && (0 == hp
->iovec_count
) &&
1732 (!sfp
->parentdp
->device
->host
->unchecked_isa_dma
)) {
1733 res
= sg_build_direct(srp
, sfp
, dxfer_len
);
1734 if (res
<= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1737 if ((!sg_res_in_use(sfp
)) && (dxfer_len
<= rsv_schp
->bufflen
))
1738 sg_link_reserve(sfp
, srp
, dxfer_len
);
1740 res
= sg_build_indirect(req_schp
, sfp
, dxfer_len
);
1742 sg_remove_scat(req_schp
);
1750 sg_finish_rem_req(Sg_request
* srp
)
1752 Sg_fd
*sfp
= srp
->parentfp
;
1753 Sg_scatter_hold
*req_schp
= &srp
->data
;
1755 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp
->res_used
));
1757 sg_unlink_reserve(sfp
, srp
);
1759 sg_remove_scat(req_schp
);
1760 sg_remove_request(sfp
, srp
);
1764 sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
, int tablesize
)
1767 int elem_sz
= sizeof (struct scatterlist
);
1768 int sg_bufflen
= tablesize
* elem_sz
;
1769 int mx_sc_elems
= tablesize
;
1771 schp
->buffer
= sg_page_malloc(sg_bufflen
, sfp
->low_dma
, &ret_sz
);
1774 else if (ret_sz
!= sg_bufflen
) {
1775 sg_bufflen
= ret_sz
;
1776 mx_sc_elems
= sg_bufflen
/ elem_sz
;
1778 schp
->sglist_len
= sg_bufflen
;
1779 memset(schp
->buffer
, 0, sg_bufflen
);
1780 return mx_sc_elems
; /* number of scat_gath elements allocated */
1783 #ifdef SG_ALLOW_DIO_CODE
1784 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1785 /* hopefully this generic code will moved to a library */
1787 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1788 - mapping of all pages not successful
1789 - any page is above max_pfn
1790 (i.e., either completely successful or fails)
1793 st_map_user_pages(struct scatterlist
*sgl
, const unsigned int max_pages
,
1794 unsigned long uaddr
, size_t count
, int rw
,
1795 unsigned long max_pfn
)
1798 unsigned int nr_pages
;
1799 struct page
**pages
;
1801 nr_pages
= ((uaddr
& ~PAGE_MASK
) + count
+ ~PAGE_MASK
) >> PAGE_SHIFT
;
1803 /* User attempted Overflow! */
1804 if ((uaddr
+ count
) < uaddr
)
1808 if (nr_pages
> max_pages
)
1815 if ((pages
= kmalloc(max_pages
* sizeof(*pages
), GFP_ATOMIC
)) == NULL
)
1818 /* Try to fault in all of the necessary pages */
1819 down_read(¤t
->mm
->mmap_sem
);
1820 /* rw==READ means read from drive, write into memory area */
1821 res
= get_user_pages(
1827 0, /* don't force */
1830 up_read(¤t
->mm
->mmap_sem
);
1832 /* Errors and no page mapped should return here */
1836 for (i
=0; i
< nr_pages
; i
++) {
1837 /* FIXME: flush superflous for rw==READ,
1838 * probably wrong function for rw==WRITE
1840 flush_dcache_page(pages
[i
]);
1841 if (page_to_pfn(pages
[i
]) > max_pfn
)
1843 /* ?? Is locking needed? I don't think so */
1844 /* if (TestSetPageLocked(pages[i]))
1848 /* Populate the scatter/gather list */
1849 sgl
[0].page
= pages
[0];
1850 sgl
[0].offset
= uaddr
& ~PAGE_MASK
;
1852 sgl
[0].length
= PAGE_SIZE
- sgl
[0].offset
;
1853 count
-= sgl
[0].length
;
1854 for (i
=1; i
< nr_pages
; i
++) {
1856 sgl
[i
].page
= pages
[i
];
1857 sgl
[i
].length
= count
< PAGE_SIZE
? count
: PAGE_SIZE
;
1862 sgl
[0].length
= count
;
1869 /* for (j=0; j < i; j++)
1870 unlock_page(pages[j]); */
1874 for (j
=0; j
< res
; j
++)
1875 page_cache_release(pages
[j
]);
1881 /* And unmap them... */
1883 st_unmap_user_pages(struct scatterlist
*sgl
, const unsigned int nr_pages
,
1888 for (i
=0; i
< nr_pages
; i
++) {
1889 if (dirtied
&& !PageReserved(sgl
[i
].page
))
1890 SetPageDirty(sgl
[i
].page
);
1891 /* unlock_page(sgl[i].page); */
1892 /* FIXME: cache flush missing for rw==READ
1893 * FIXME: call the correct reference counting function
1895 page_cache_release(sgl
[i
].page
);
1901 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1905 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1907 sg_build_direct(Sg_request
* srp
, Sg_fd
* sfp
, int dxfer_len
)
1909 #ifdef SG_ALLOW_DIO_CODE
1910 sg_io_hdr_t
*hp
= &srp
->header
;
1911 Sg_scatter_hold
*schp
= &srp
->data
;
1912 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1913 struct scatterlist
*sgl
;
1914 int mx_sc_elems
, res
;
1915 struct scsi_device
*sdev
= sfp
->parentdp
->device
;
1917 if (((unsigned long)hp
->dxferp
&
1918 queue_dma_alignment(sdev
->request_queue
)) != 0)
1920 mx_sc_elems
= sg_build_sgat(schp
, sfp
, sg_tablesize
);
1921 if (mx_sc_elems
<= 0) {
1924 sgl
= (struct scatterlist
*)schp
->buffer
;
1925 res
= st_map_user_pages(sgl
, mx_sc_elems
, (unsigned long)hp
->dxferp
, dxfer_len
,
1926 (SG_DXFER_TO_DEV
== hp
->dxfer_direction
) ? 1 : 0, ULONG_MAX
);
1929 schp
->k_use_sg
= res
;
1930 schp
->dio_in_use
= 1;
1931 hp
->info
|= SG_INFO_DIRECT_IO
;
1939 sg_build_indirect(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
)
1942 int blk_size
= buff_size
;
1943 unsigned char *p
= NULL
;
1945 if ((blk_size
< 0) || (!sfp
))
1948 ++blk_size
; /* don't know why */
1949 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1950 blk_size
= (blk_size
+ SG_SECTOR_MSK
) & (~SG_SECTOR_MSK
);
1951 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1952 buff_size
, blk_size
));
1953 if (blk_size
<= SG_SCATTER_SZ
) {
1954 p
= sg_page_malloc(blk_size
, sfp
->low_dma
, &ret_sz
);
1957 if (blk_size
== ret_sz
) { /* got it on the first attempt */
1960 schp
->bufflen
= blk_size
;
1961 schp
->b_malloc_len
= blk_size
;
1965 p
= sg_page_malloc(SG_SCATTER_SZ
, sfp
->low_dma
, &ret_sz
);
1969 /* Want some local declarations, so start new block ... */
1970 { /* lets try and build a scatter gather list */
1971 struct scatterlist
*sclp
;
1974 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1977 /* N.B. ret_sz carried into this block ... */
1978 mx_sc_elems
= sg_build_sgat(schp
, sfp
, sg_tablesize
);
1979 if (mx_sc_elems
< 0)
1980 return mx_sc_elems
; /* most likely -ENOMEM */
1982 for (k
= 0, sclp
= schp
->buffer
, rem_sz
= blk_size
;
1983 (rem_sz
> 0) && (k
< mx_sc_elems
);
1984 ++k
, rem_sz
-= ret_sz
, ++sclp
) {
1990 SG_SCATTER_SZ
) ? SG_SCATTER_SZ
: rem_sz
;
1991 p
= sg_page_malloc(num
, sfp
->low_dma
, &ret_sz
);
1995 sclp
->page
= virt_to_page(p
);
1996 sclp
->offset
= offset_in_page(p
);
1997 sclp
->length
= ret_sz
;
1999 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
2000 k
, sg_scatg2virt(sclp
), ret_sz
));
2001 } /* end of for loop */
2003 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k
, rem_sz
));
2004 schp
->bufflen
= blk_size
;
2005 if (rem_sz
> 0) /* must have failed */
2012 sg_write_xfer(Sg_request
* srp
)
2014 sg_io_hdr_t
*hp
= &srp
->header
;
2015 Sg_scatter_hold
*schp
= &srp
->data
;
2017 int j
, k
, onum
, usglen
, ksglen
, res
;
2018 int iovec_count
= (int) hp
->iovec_count
;
2019 int dxfer_dir
= hp
->dxfer_direction
;
2021 unsigned char __user
*up
;
2022 int new_interface
= ('\0' == hp
->interface_id
) ? 0 : 1;
2024 if ((SG_DXFER_UNKNOWN
== dxfer_dir
) || (SG_DXFER_TO_DEV
== dxfer_dir
) ||
2025 (SG_DXFER_TO_FROM_DEV
== dxfer_dir
)) {
2026 num_xfer
= (int) (new_interface
? hp
->dxfer_len
: hp
->flags
);
2027 if (schp
->bufflen
< num_xfer
)
2028 num_xfer
= schp
->bufflen
;
2030 if ((num_xfer
<= 0) || (schp
->dio_in_use
) ||
2032 && ((SG_FLAG_NO_DXFER
| SG_FLAG_MMAP_IO
) & hp
->flags
)))
2035 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2036 num_xfer
, iovec_count
, schp
->k_use_sg
));
2039 if (!access_ok(VERIFY_READ
, hp
->dxferp
, SZ_SG_IOVEC
* onum
))
2044 if (0 == schp
->k_use_sg
) { /* kernel has single buffer */
2045 for (j
= 0, p
= schp
->buffer
; j
< onum
; ++j
) {
2046 res
= sg_u_iovec(hp
, iovec_count
, j
, 1, &usglen
, &up
);
2049 usglen
= (num_xfer
> usglen
) ? usglen
: num_xfer
;
2050 if (__copy_from_user(p
, up
, usglen
))
2057 } else { /* kernel using scatter gather list */
2058 struct scatterlist
*sclp
= (struct scatterlist
*) schp
->buffer
;
2060 ksglen
= (int) sclp
->length
;
2061 p
= sg_scatg2virt(sclp
);
2062 for (j
= 0, k
= 0; j
< onum
; ++j
) {
2063 res
= sg_u_iovec(hp
, iovec_count
, j
, 1, &usglen
, &up
);
2067 for (; p
; ++sclp
, ksglen
= (int) sclp
->length
,
2068 p
= sg_scatg2virt(sclp
)) {
2071 if (ksglen
> usglen
) {
2072 if (usglen
>= num_xfer
) {
2073 if (__copy_from_user
2078 if (__copy_from_user(p
, up
, usglen
))
2084 if (ksglen
>= num_xfer
) {
2085 if (__copy_from_user
2090 if (__copy_from_user(p
, up
, ksglen
))
2096 if (k
>= schp
->k_use_sg
)
2105 sg_u_iovec(sg_io_hdr_t
* hp
, int sg_num
, int ind
,
2106 int wr_xf
, int *countp
, unsigned char __user
**up
)
2108 int num_xfer
= (int) hp
->dxfer_len
;
2109 unsigned char __user
*p
= hp
->dxferp
;
2113 if (wr_xf
&& ('\0' == hp
->interface_id
))
2114 count
= (int) hp
->flags
; /* holds "old" input_size */
2119 if (__copy_from_user(&iovec
, p
+ ind
*SZ_SG_IOVEC
, SZ_SG_IOVEC
))
2122 count
= (int) iovec
.iov_len
;
2124 if (!access_ok(wr_xf
? VERIFY_READ
: VERIFY_WRITE
, p
, count
))
2134 sg_remove_scat(Sg_scatter_hold
* schp
)
2136 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp
->k_use_sg
));
2137 if (schp
->buffer
&& (schp
->sglist_len
> 0)) {
2138 struct scatterlist
*sclp
= (struct scatterlist
*) schp
->buffer
;
2140 if (schp
->dio_in_use
) {
2141 #ifdef SG_ALLOW_DIO_CODE
2142 st_unmap_user_pages(sclp
, schp
->k_use_sg
, TRUE
);
2147 for (k
= 0; (k
< schp
->k_use_sg
) && sg_scatg2virt(sclp
);
2149 SCSI_LOG_TIMEOUT(5, printk(
2150 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2151 k
, sg_scatg2virt(sclp
), sclp
->length
));
2152 sg_page_free(sg_scatg2virt(sclp
), sclp
->length
);
2158 sg_page_free(schp
->buffer
, schp
->sglist_len
);
2159 } else if (schp
->buffer
)
2160 sg_page_free(schp
->buffer
, schp
->b_malloc_len
);
2161 memset(schp
, 0, sizeof (*schp
));
2165 sg_read_xfer(Sg_request
* srp
)
2167 sg_io_hdr_t
*hp
= &srp
->header
;
2168 Sg_scatter_hold
*schp
= &srp
->data
;
2170 int j
, k
, onum
, usglen
, ksglen
, res
;
2171 int iovec_count
= (int) hp
->iovec_count
;
2172 int dxfer_dir
= hp
->dxfer_direction
;
2174 unsigned char __user
*up
;
2175 int new_interface
= ('\0' == hp
->interface_id
) ? 0 : 1;
2177 if ((SG_DXFER_UNKNOWN
== dxfer_dir
) || (SG_DXFER_FROM_DEV
== dxfer_dir
)
2178 || (SG_DXFER_TO_FROM_DEV
== dxfer_dir
)) {
2179 num_xfer
= hp
->dxfer_len
;
2180 if (schp
->bufflen
< num_xfer
)
2181 num_xfer
= schp
->bufflen
;
2183 if ((num_xfer
<= 0) || (schp
->dio_in_use
) ||
2185 && ((SG_FLAG_NO_DXFER
| SG_FLAG_MMAP_IO
) & hp
->flags
)))
2188 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2189 num_xfer
, iovec_count
, schp
->k_use_sg
));
2192 if (!access_ok(VERIFY_READ
, hp
->dxferp
, SZ_SG_IOVEC
* onum
))
2197 if (0 == schp
->k_use_sg
) { /* kernel has single buffer */
2198 for (j
= 0, p
= schp
->buffer
; j
< onum
; ++j
) {
2199 res
= sg_u_iovec(hp
, iovec_count
, j
, 0, &usglen
, &up
);
2202 usglen
= (num_xfer
> usglen
) ? usglen
: num_xfer
;
2203 if (__copy_to_user(up
, p
, usglen
))
2210 } else { /* kernel using scatter gather list */
2211 struct scatterlist
*sclp
= (struct scatterlist
*) schp
->buffer
;
2213 ksglen
= (int) sclp
->length
;
2214 p
= sg_scatg2virt(sclp
);
2215 for (j
= 0, k
= 0; j
< onum
; ++j
) {
2216 res
= sg_u_iovec(hp
, iovec_count
, j
, 0, &usglen
, &up
);
2220 for (; p
; ++sclp
, ksglen
= (int) sclp
->length
,
2221 p
= sg_scatg2virt(sclp
)) {
2224 if (ksglen
> usglen
) {
2225 if (usglen
>= num_xfer
) {
2231 if (__copy_to_user(up
, p
, usglen
))
2237 if (ksglen
>= num_xfer
) {
2243 if (__copy_to_user(up
, p
, ksglen
))
2249 if (k
>= schp
->k_use_sg
)
2258 sg_read_oxfer(Sg_request
* srp
, char __user
*outp
, int num_read_xfer
)
2260 Sg_scatter_hold
*schp
= &srp
->data
;
2262 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2264 if ((!outp
) || (num_read_xfer
<= 0))
2266 if (schp
->k_use_sg
> 0) {
2268 struct scatterlist
*sclp
= (struct scatterlist
*) schp
->buffer
;
2270 for (k
= 0; (k
< schp
->k_use_sg
) && sg_scatg2virt(sclp
);
2272 num
= (int) sclp
->length
;
2273 if (num
> num_read_xfer
) {
2275 (outp
, sg_scatg2virt(sclp
), num_read_xfer
))
2280 (outp
, sg_scatg2virt(sclp
), num
))
2282 num_read_xfer
-= num
;
2283 if (num_read_xfer
<= 0)
2289 if (__copy_to_user(outp
, schp
->buffer
, num_read_xfer
))
2296 sg_build_reserve(Sg_fd
* sfp
, int req_size
)
2298 Sg_scatter_hold
*schp
= &sfp
->reserve
;
2300 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size
));
2302 if (req_size
< PAGE_SIZE
)
2303 req_size
= PAGE_SIZE
;
2304 if (0 == sg_build_indirect(schp
, sfp
, req_size
))
2307 sg_remove_scat(schp
);
2308 req_size
>>= 1; /* divide by 2 */
2309 } while (req_size
> (PAGE_SIZE
/ 2));
2313 sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
)
2315 Sg_scatter_hold
*req_schp
= &srp
->data
;
2316 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
2319 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size
));
2320 size
= (size
+ 1) & (~1); /* round to even for aha1542 */
2321 if (rsv_schp
->k_use_sg
> 0) {
2324 struct scatterlist
*sclp
=
2325 (struct scatterlist
*) rsv_schp
->buffer
;
2327 for (k
= 0; k
< rsv_schp
->k_use_sg
; ++k
, ++sclp
) {
2328 num
= (int) sclp
->length
;
2331 req_schp
->k_use_sg
= 0;
2332 req_schp
->buffer
= sg_scatg2virt(sclp
);
2334 sfp
->save_scat_len
= num
;
2335 sclp
->length
= (unsigned) rem
;
2336 req_schp
->k_use_sg
= k
+ 1;
2337 req_schp
->sglist_len
=
2338 rsv_schp
->sglist_len
;
2339 req_schp
->buffer
= rsv_schp
->buffer
;
2341 req_schp
->bufflen
= size
;
2342 req_schp
->b_malloc_len
= rsv_schp
->b_malloc_len
;
2347 if (k
>= rsv_schp
->k_use_sg
)
2348 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2350 req_schp
->k_use_sg
= 0;
2351 req_schp
->bufflen
= size
;
2352 req_schp
->buffer
= rsv_schp
->buffer
;
2353 req_schp
->b_malloc_len
= rsv_schp
->b_malloc_len
;
2358 sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
)
2360 Sg_scatter_hold
*req_schp
= &srp
->data
;
2361 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
2363 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2364 (int) req_schp
->k_use_sg
));
2365 if ((rsv_schp
->k_use_sg
> 0) && (req_schp
->k_use_sg
> 0)) {
2366 struct scatterlist
*sclp
=
2367 (struct scatterlist
*) rsv_schp
->buffer
;
2369 if (sfp
->save_scat_len
> 0)
2370 (sclp
+ (req_schp
->k_use_sg
- 1))->length
=
2371 (unsigned) sfp
->save_scat_len
;
2373 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2375 req_schp
->k_use_sg
= 0;
2376 req_schp
->bufflen
= 0;
2377 req_schp
->buffer
= NULL
;
2378 req_schp
->sglist_len
= 0;
2379 sfp
->save_scat_len
= 0;
2384 sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
)
2387 unsigned long iflags
;
2389 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2390 for (resp
= sfp
->headrp
; resp
; resp
= resp
->nextrp
) {
2391 /* look for requests that are ready + not SG_IO owned */
2392 if ((1 == resp
->done
) && (!resp
->sg_io_owned
) &&
2393 ((-1 == pack_id
) || (resp
->header
.pack_id
== pack_id
))) {
2394 resp
->done
= 2; /* guard against other readers */
2398 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2402 #ifdef CONFIG_SCSI_PROC_FS
2404 sg_get_nth_request(Sg_fd
* sfp
, int nth
)
2407 unsigned long iflags
;
2410 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2411 for (k
= 0, resp
= sfp
->headrp
; resp
&& (k
< nth
);
2412 ++k
, resp
= resp
->nextrp
) ;
2413 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2418 /* always adds to end of list */
2420 sg_add_request(Sg_fd
* sfp
)
2423 unsigned long iflags
;
2425 Sg_request
*rp
= sfp
->req_arr
;
2427 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2430 memset(rp
, 0, sizeof (Sg_request
));
2435 if (0 == sfp
->cmd_q
)
2436 resp
= NULL
; /* command queuing disallowed */
2438 for (k
= 0; k
< SG_MAX_QUEUE
; ++k
, ++rp
) {
2442 if (k
< SG_MAX_QUEUE
) {
2443 memset(rp
, 0, sizeof (Sg_request
));
2445 while (resp
->nextrp
)
2446 resp
= resp
->nextrp
;
2454 resp
->nextrp
= NULL
;
2455 resp
->header
.duration
= jiffies_to_msecs(jiffies
);
2456 resp
->my_cmdp
= NULL
;
2458 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2462 /* Return of 1 for found; 0 for not found */
2464 sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
)
2466 Sg_request
*prev_rp
;
2468 unsigned long iflags
;
2471 if ((!sfp
) || (!srp
) || (!sfp
->headrp
))
2473 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2474 prev_rp
= sfp
->headrp
;
2475 if (srp
== prev_rp
) {
2476 sfp
->headrp
= prev_rp
->nextrp
;
2477 prev_rp
->parentfp
= NULL
;
2480 while ((rp
= prev_rp
->nextrp
)) {
2482 prev_rp
->nextrp
= rp
->nextrp
;
2483 rp
->parentfp
= NULL
;
2490 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2494 #ifdef CONFIG_SCSI_PROC_FS
2496 sg_get_nth_sfp(Sg_device
* sdp
, int nth
)
2499 unsigned long iflags
;
2502 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2503 for (k
= 0, resp
= sdp
->headfp
; resp
&& (k
< nth
);
2504 ++k
, resp
= resp
->nextfp
) ;
2505 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2511 sg_add_sfp(Sg_device
* sdp
, int dev
)
2514 unsigned long iflags
;
2516 sfp
= (Sg_fd
*) sg_page_malloc(sizeof (Sg_fd
), 0, NULL
);
2519 memset(sfp
, 0, sizeof (Sg_fd
));
2520 init_waitqueue_head(&sfp
->read_wait
);
2521 rwlock_init(&sfp
->rq_list_lock
);
2523 sfp
->timeout
= SG_DEFAULT_TIMEOUT
;
2524 sfp
->timeout_user
= SG_DEFAULT_TIMEOUT_USER
;
2525 sfp
->force_packid
= SG_DEF_FORCE_PACK_ID
;
2526 sfp
->low_dma
= (SG_DEF_FORCE_LOW_DMA
== 0) ?
2527 sdp
->device
->host
->unchecked_isa_dma
: 1;
2528 sfp
->cmd_q
= SG_DEF_COMMAND_Q
;
2529 sfp
->keep_orphan
= SG_DEF_KEEP_ORPHAN
;
2530 sfp
->parentdp
= sdp
;
2531 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2534 else { /* add to tail of existing list */
2535 Sg_fd
*pfp
= sdp
->headfp
;
2540 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2541 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp
));
2542 sg_build_reserve(sfp
, sg_big_buff
);
2543 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2544 sfp
->reserve
.bufflen
, sfp
->reserve
.k_use_sg
));
2549 __sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
)
2554 prev_fp
= sdp
->headfp
;
2556 sdp
->headfp
= prev_fp
->nextfp
;
2558 while ((fp
= prev_fp
->nextfp
)) {
2560 prev_fp
->nextfp
= fp
->nextfp
;
2566 if (sfp
->reserve
.bufflen
> 0) {
2568 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2569 (int) sfp
->reserve
.bufflen
, (int) sfp
->reserve
.k_use_sg
));
2570 if (sfp
->mmap_called
)
2571 sg_rb_correct4mmap(&sfp
->reserve
, 0); /* undo correction */
2572 sg_remove_scat(&sfp
->reserve
);
2574 sfp
->parentdp
= NULL
;
2575 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp
));
2576 sg_page_free((char *) sfp
, sizeof (Sg_fd
));
2579 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2581 sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
)
2588 for (srp
= sfp
->headrp
; srp
; srp
= tsrp
) {
2590 if (sg_srp_done(srp
, sfp
))
2591 sg_finish_rem_req(srp
);
2596 unsigned long iflags
;
2598 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2599 __sg_remove_sfp(sdp
, sfp
);
2600 if (sdp
->detached
&& (NULL
== sdp
->headfp
)) {
2604 for (k
= 0; k
< maxd
; ++k
) {
2605 if (sdp
== sg_dev_arr
[k
])
2609 sg_dev_arr
[k
] = NULL
;
2610 kfree((char *) sdp
);
2613 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2615 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2616 /* only bump the access_count if we actually succeeded in
2617 * throwing another counter on the host module */
2618 scsi_device_get(sdp
->device
); /* XXX: retval ignored? */
2619 sfp
->closed
= 1; /* flag dirty state on this fd */
2620 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2627 sg_res_in_use(Sg_fd
* sfp
)
2629 const Sg_request
*srp
;
2630 unsigned long iflags
;
2632 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2633 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
)
2636 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2640 /* If retSzp==NULL want exact size or fail */
2642 sg_page_malloc(int rqSz
, int lowDma
, int *retSzp
)
2653 page_mask
= GFP_ATOMIC
| GFP_DMA
| __GFP_NOWARN
;
2655 page_mask
= GFP_ATOMIC
| __GFP_NOWARN
;
2657 for (order
= 0, a_size
= PAGE_SIZE
; a_size
< rqSz
;
2658 order
++, a_size
<<= 1) ;
2659 resp
= (char *) __get_free_pages(page_mask
, order
);
2660 while ((!resp
) && order
&& retSzp
) {
2662 a_size
>>= 1; /* divide by 2, until PAGE_SIZE */
2663 resp
= (char *) __get_free_pages(page_mask
, order
); /* try half */
2667 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2668 memset(resp
, 0, resSz
);
2676 sg_page_free(char *buff
, int size
)
2682 for (order
= 0, a_size
= PAGE_SIZE
; a_size
< size
;
2683 order
++, a_size
<<= 1) ;
2684 free_pages((unsigned long) buff
, order
);
2687 #ifndef MAINTENANCE_IN_CMD
2688 #define MAINTENANCE_IN_CMD 0xa3
2691 static unsigned char allow_ops
[] = { TEST_UNIT_READY
, REQUEST_SENSE
,
2692 INQUIRY
, READ_CAPACITY
, READ_BUFFER
, READ_6
, READ_10
, READ_12
,
2693 READ_16
, MODE_SENSE
, MODE_SENSE_10
, LOG_SENSE
, REPORT_LUNS
,
2694 SERVICE_ACTION_IN
, RECEIVE_DIAGNOSTIC
, READ_LONG
, MAINTENANCE_IN_CMD
2698 sg_allow_access(unsigned char opcode
, char dev_type
)
2702 if (TYPE_SCANNER
== dev_type
) /* TYPE_ROM maybe burner */
2704 for (k
= 0; k
< sizeof (allow_ops
); ++k
) {
2705 if (opcode
== allow_ops
[k
])
2711 #ifdef CONFIG_SCSI_PROC_FS
2716 unsigned long iflags
;
2718 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2719 for (k
= sg_dev_max
- 1; k
>= 0; --k
)
2720 if (sg_dev_arr
[k
] && sg_dev_arr
[k
]->device
)
2722 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2723 return k
+ 1; /* origin 1 */
2730 Sg_device
*sdp
= NULL
;
2731 unsigned long iflags
;
2733 if (sg_dev_arr
&& (dev
>= 0)) {
2734 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2735 if (dev
< sg_dev_max
)
2736 sdp
= sg_dev_arr
[dev
];
2737 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2742 #ifdef CONFIG_SCSI_PROC_FS
2744 static struct proc_dir_entry
*sg_proc_sgp
= NULL
;
2746 static char sg_proc_sg_dirname
[] = "scsi/sg";
2748 static int sg_proc_seq_show_int(struct seq_file
*s
, void *v
);
2750 static int sg_proc_single_open_adio(struct inode
*inode
, struct file
*file
);
2751 static ssize_t
sg_proc_write_adio(struct file
*filp
, const char __user
*buffer
,
2752 size_t count
, loff_t
*off
);
2753 static struct file_operations adio_fops
= {
2754 /* .owner, .read and .llseek added in sg_proc_init() */
2755 .open
= sg_proc_single_open_adio
,
2756 .write
= sg_proc_write_adio
,
2757 .release
= single_release
,
2760 static int sg_proc_single_open_dressz(struct inode
*inode
, struct file
*file
);
2761 static ssize_t
sg_proc_write_dressz(struct file
*filp
,
2762 const char __user
*buffer
, size_t count
, loff_t
*off
);
2763 static struct file_operations dressz_fops
= {
2764 .open
= sg_proc_single_open_dressz
,
2765 .write
= sg_proc_write_dressz
,
2766 .release
= single_release
,
2769 static int sg_proc_seq_show_version(struct seq_file
*s
, void *v
);
2770 static int sg_proc_single_open_version(struct inode
*inode
, struct file
*file
);
2771 static struct file_operations version_fops
= {
2772 .open
= sg_proc_single_open_version
,
2773 .release
= single_release
,
2776 static int sg_proc_seq_show_devhdr(struct seq_file
*s
, void *v
);
2777 static int sg_proc_single_open_devhdr(struct inode
*inode
, struct file
*file
);
2778 static struct file_operations devhdr_fops
= {
2779 .open
= sg_proc_single_open_devhdr
,
2780 .release
= single_release
,
2783 static int sg_proc_seq_show_dev(struct seq_file
*s
, void *v
);
2784 static int sg_proc_open_dev(struct inode
*inode
, struct file
*file
);
2785 static void * dev_seq_start(struct seq_file
*s
, loff_t
*pos
);
2786 static void * dev_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
);
2787 static void dev_seq_stop(struct seq_file
*s
, void *v
);
2788 static struct file_operations dev_fops
= {
2789 .open
= sg_proc_open_dev
,
2790 .release
= seq_release
,
2792 static struct seq_operations dev_seq_ops
= {
2793 .start
= dev_seq_start
,
2794 .next
= dev_seq_next
,
2795 .stop
= dev_seq_stop
,
2796 .show
= sg_proc_seq_show_dev
,
2799 static int sg_proc_seq_show_devstrs(struct seq_file
*s
, void *v
);
2800 static int sg_proc_open_devstrs(struct inode
*inode
, struct file
*file
);
2801 static struct file_operations devstrs_fops
= {
2802 .open
= sg_proc_open_devstrs
,
2803 .release
= seq_release
,
2805 static struct seq_operations devstrs_seq_ops
= {
2806 .start
= dev_seq_start
,
2807 .next
= dev_seq_next
,
2808 .stop
= dev_seq_stop
,
2809 .show
= sg_proc_seq_show_devstrs
,
2812 static int sg_proc_seq_show_debug(struct seq_file
*s
, void *v
);
2813 static int sg_proc_open_debug(struct inode
*inode
, struct file
*file
);
2814 static struct file_operations debug_fops
= {
2815 .open
= sg_proc_open_debug
,
2816 .release
= seq_release
,
2818 static struct seq_operations debug_seq_ops
= {
2819 .start
= dev_seq_start
,
2820 .next
= dev_seq_next
,
2821 .stop
= dev_seq_stop
,
2822 .show
= sg_proc_seq_show_debug
,
2826 struct sg_proc_leaf
{
2828 struct file_operations
* fops
;
2831 static struct sg_proc_leaf sg_proc_leaf_arr
[] = {
2832 {"allow_dio", &adio_fops
},
2833 {"debug", &debug_fops
},
2834 {"def_reserved_size", &dressz_fops
},
2835 {"device_hdr", &devhdr_fops
},
2836 {"devices", &dev_fops
},
2837 {"device_strs", &devstrs_fops
},
2838 {"version", &version_fops
}
2846 sizeof (sg_proc_leaf_arr
) / sizeof (sg_proc_leaf_arr
[0]);
2847 struct proc_dir_entry
*pdep
;
2848 struct sg_proc_leaf
* leaf
;
2850 sg_proc_sgp
= create_proc_entry(sg_proc_sg_dirname
,
2851 S_IFDIR
| S_IRUGO
| S_IXUGO
, NULL
);
2854 for (k
= 0; k
< num_leaves
; ++k
) {
2855 leaf
= &sg_proc_leaf_arr
[k
];
2856 mask
= leaf
->fops
->write
? S_IRUGO
| S_IWUSR
: S_IRUGO
;
2857 pdep
= create_proc_entry(leaf
->name
, mask
, sg_proc_sgp
);
2859 leaf
->fops
->owner
= THIS_MODULE
,
2860 leaf
->fops
->read
= seq_read
,
2861 leaf
->fops
->llseek
= seq_lseek
,
2862 pdep
->proc_fops
= leaf
->fops
;
2869 sg_proc_cleanup(void)
2873 sizeof (sg_proc_leaf_arr
) / sizeof (sg_proc_leaf_arr
[0]);
2877 for (k
= 0; k
< num_leaves
; ++k
)
2878 remove_proc_entry(sg_proc_leaf_arr
[k
].name
, sg_proc_sgp
);
2879 remove_proc_entry(sg_proc_sg_dirname
, NULL
);
2883 static int sg_proc_seq_show_int(struct seq_file
*s
, void *v
)
2885 seq_printf(s
, "%d\n", *((int *)s
->private));
2889 static int sg_proc_single_open_adio(struct inode
*inode
, struct file
*file
)
2891 return single_open(file
, sg_proc_seq_show_int
, &sg_allow_dio
);
2895 sg_proc_write_adio(struct file
*filp
, const char __user
*buffer
,
2896 size_t count
, loff_t
*off
)
2901 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2903 num
= (count
< 10) ? count
: 10;
2904 if (copy_from_user(buff
, buffer
, num
))
2907 sg_allow_dio
= simple_strtoul(buff
, NULL
, 10) ? 1 : 0;
2911 static int sg_proc_single_open_dressz(struct inode
*inode
, struct file
*file
)
2913 return single_open(file
, sg_proc_seq_show_int
, &sg_big_buff
);
2917 sg_proc_write_dressz(struct file
*filp
, const char __user
*buffer
,
2918 size_t count
, loff_t
*off
)
2921 unsigned long k
= ULONG_MAX
;
2924 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2926 num
= (count
< 10) ? count
: 10;
2927 if (copy_from_user(buff
, buffer
, num
))
2930 k
= simple_strtoul(buff
, NULL
, 10);
2931 if (k
<= 1048576) { /* limit "big buff" to 1 MB */
2938 static int sg_proc_seq_show_version(struct seq_file
*s
, void *v
)
2940 seq_printf(s
, "%d\t%s [%s]\n", sg_version_num
, SG_VERSION_STR
,
2945 static int sg_proc_single_open_version(struct inode
*inode
, struct file
*file
)
2947 return single_open(file
, sg_proc_seq_show_version
, NULL
);
2950 static int sg_proc_seq_show_devhdr(struct seq_file
*s
, void *v
)
2952 seq_printf(s
, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2957 static int sg_proc_single_open_devhdr(struct inode
*inode
, struct file
*file
)
2959 return single_open(file
, sg_proc_seq_show_devhdr
, NULL
);
2962 struct sg_proc_deviter
{
2967 static void * dev_seq_start(struct seq_file
*s
, loff_t
*pos
)
2969 struct sg_proc_deviter
* it
= kmalloc(sizeof(*it
), GFP_KERNEL
);
2973 if (NULL
== sg_dev_arr
)
2976 it
->max
= sg_last_dev();
2977 if (it
->index
>= it
->max
)
2985 static void * dev_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2987 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2990 return (it
->index
< it
->max
) ? it
: NULL
;
2993 static void dev_seq_stop(struct seq_file
*s
, void *v
)
2998 static int sg_proc_open_dev(struct inode
*inode
, struct file
*file
)
3000 return seq_open(file
, &dev_seq_ops
);
3003 static int sg_proc_seq_show_dev(struct seq_file
*s
, void *v
)
3005 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
3007 struct scsi_device
*scsidp
;
3009 sdp
= it
? sg_get_dev(it
->index
) : NULL
;
3010 if (sdp
&& (scsidp
= sdp
->device
) && (!sdp
->detached
))
3011 seq_printf(s
, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
3012 scsidp
->host
->host_no
, scsidp
->channel
,
3013 scsidp
->id
, scsidp
->lun
, (int) scsidp
->type
,
3015 (int) scsidp
->queue_depth
,
3016 (int) scsidp
->device_busy
,
3017 (int) scsi_device_online(scsidp
));
3019 seq_printf(s
, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
3023 static int sg_proc_open_devstrs(struct inode
*inode
, struct file
*file
)
3025 return seq_open(file
, &devstrs_seq_ops
);
3028 static int sg_proc_seq_show_devstrs(struct seq_file
*s
, void *v
)
3030 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
3032 struct scsi_device
*scsidp
;
3034 sdp
= it
? sg_get_dev(it
->index
) : NULL
;
3035 if (sdp
&& (scsidp
= sdp
->device
) && (!sdp
->detached
))
3036 seq_printf(s
, "%8.8s\t%16.16s\t%4.4s\n",
3037 scsidp
->vendor
, scsidp
->model
, scsidp
->rev
);
3039 seq_printf(s
, "<no active device>\n");
3043 static void sg_proc_debug_helper(struct seq_file
*s
, Sg_device
* sdp
)
3045 int k
, m
, new_interface
, blen
, usg
;
3048 const sg_io_hdr_t
*hp
;
3052 for (k
= 0; (fp
= sg_get_nth_sfp(sdp
, k
)); ++k
) {
3053 seq_printf(s
, " FD(%d): timeout=%dms bufflen=%d "
3054 "(res)sgat=%d low_dma=%d\n", k
+ 1,
3055 jiffies_to_msecs(fp
->timeout
),
3056 fp
->reserve
.bufflen
,
3057 (int) fp
->reserve
.k_use_sg
,
3059 seq_printf(s
, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
3060 (int) fp
->cmd_q
, (int) fp
->force_packid
,
3061 (int) fp
->keep_orphan
, (int) fp
->closed
);
3062 for (m
= 0; (srp
= sg_get_nth_request(fp
, m
)); ++m
) {
3064 new_interface
= (hp
->interface_id
== '\0') ? 0 : 1;
3065 if (srp
->res_used
) {
3066 if (new_interface
&&
3067 (SG_FLAG_MMAP_IO
& hp
->flags
))
3072 if (SG_INFO_DIRECT_IO_MASK
& hp
->info
)
3078 blen
= srp
->my_cmdp
?
3079 srp
->my_cmdp
->sr_bufflen
: srp
->data
.bufflen
;
3080 usg
= srp
->my_cmdp
?
3081 srp
->my_cmdp
->sr_use_sg
: srp
->data
.k_use_sg
;
3082 seq_printf(s
, srp
->done
?
3083 ((1 == srp
->done
) ? "rcv:" : "fin:")
3084 : (srp
->my_cmdp
? "act:" : "prior:"));
3085 seq_printf(s
, " id=%d blen=%d",
3086 srp
->header
.pack_id
, blen
);
3088 seq_printf(s
, " dur=%d", hp
->duration
);
3090 ms
= jiffies_to_msecs(jiffies
);
3091 seq_printf(s
, " t_o/elap=%d/%d",
3092 (new_interface
? hp
->timeout
:
3093 jiffies_to_msecs(fp
->timeout
)),
3094 (ms
> hp
->duration
? ms
- hp
->duration
: 0));
3096 seq_printf(s
, "ms sgat=%d op=0x%02x\n", usg
,
3097 (int) srp
->data
.cmd_opcode
);
3100 seq_printf(s
, " No requests active\n");
3104 static int sg_proc_open_debug(struct inode
*inode
, struct file
*file
)
3106 return seq_open(file
, &debug_seq_ops
);
3109 static int sg_proc_seq_show_debug(struct seq_file
*s
, void *v
)
3111 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
3114 if (it
&& (0 == it
->index
)) {
3115 seq_printf(s
, "dev_max(currently)=%d max_active_device=%d "
3116 "(origin 1)\n", sg_dev_max
, (int)it
->max
);
3117 seq_printf(s
, " def_reserved_size=%d\n", sg_big_buff
);
3119 sdp
= it
? sg_get_dev(it
->index
) : NULL
;
3121 struct scsi_device
*scsidp
= sdp
->device
;
3123 if (NULL
== scsidp
) {
3124 seq_printf(s
, "device %d detached ??\n",
3129 if (sg_get_nth_sfp(sdp
, 0)) {
3130 seq_printf(s
, " >>> device=%s ",
3131 sdp
->disk
->disk_name
);
3133 seq_printf(s
, "detached pending close ");
3136 (s
, "scsi%d chan=%d id=%d lun=%d em=%d",
3137 scsidp
->host
->host_no
,
3138 scsidp
->channel
, scsidp
->id
,
3140 scsidp
->host
->hostt
->emulated
);
3141 seq_printf(s
, " sg_tablesize=%d excl=%d\n",
3142 sdp
->sg_tablesize
, sdp
->exclude
);
3144 sg_proc_debug_helper(s
, sdp
);
3149 #endif /* CONFIG_SCSI_PROC_FS */
3151 module_init(init_sg
);
3152 module_exit(exit_sg
);
3153 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR
);