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 - 2014 Douglas Gilbert
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
19 static int sg_version_num
= 30536; /* 2 digits for each component */
20 #define SG_VERSION_STR "3.5.36"
23 * D. P. Gilbert (dgilbert@interlog.com), notes:
24 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
25 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
26 * (otherwise the macros compile to empty statements).
29 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/string.h>
36 #include <linux/errno.h>
37 #include <linux/mtio.h>
38 #include <linux/ioctl.h>
39 #include <linux/slab.h>
40 #include <linux/fcntl.h>
41 #include <linux/init.h>
42 #include <linux/poll.h>
43 #include <linux/moduleparam.h>
44 #include <linux/cdev.h>
45 #include <linux/idr.h>
46 #include <linux/seq_file.h>
47 #include <linux/blkdev.h>
48 #include <linux/delay.h>
49 #include <linux/blktrace_api.h>
50 #include <linux/mutex.h>
51 #include <linux/atomic.h>
52 #include <linux/ratelimit.h>
53 #include <linux/uio.h>
56 #include <scsi/scsi_dbg.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_driver.h>
59 #include <scsi/scsi_ioctl.h>
62 #include "scsi_logging.h"
64 #ifdef CONFIG_SCSI_PROC_FS
65 #include <linux/proc_fs.h>
66 static char *sg_version_date
= "20140603";
68 static int sg_proc_init(void);
69 static void sg_proc_cleanup(void);
72 #define SG_ALLOW_DIO_DEF 0
74 #define SG_MAX_DEVS 32768
76 /* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
77 * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
78 * than 16 bytes are "variable length" whose length is a multiple of 4
80 #define SG_MAX_CDB_SIZE 252
82 #define SG_DEFAULT_TIMEOUT mult_frac(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
84 int sg_big_buff
= SG_DEF_RESERVED_SIZE
;
85 /* N.B. This variable is readable and writeable via
86 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
87 of this size (or less if there is not enough memory) will be reserved
88 for use by this file descriptor. [Deprecated usage: this variable is also
89 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
90 the kernel (i.e. it is not a module).] */
91 static int def_reserved_size
= -1; /* picks up init parameter */
92 static int sg_allow_dio
= SG_ALLOW_DIO_DEF
;
94 static int scatter_elem_sz
= SG_SCATTER_SZ
;
95 static int scatter_elem_sz_prev
= SG_SCATTER_SZ
;
97 #define SG_SECTOR_SZ 512
99 static int sg_add_device(struct device
*, struct class_interface
*);
100 static void sg_remove_device(struct device
*, struct class_interface
*);
102 static DEFINE_IDR(sg_index_idr
);
103 static DEFINE_RWLOCK(sg_index_lock
); /* Also used to lock
104 file descriptor list for device */
106 static struct class_interface sg_interface
= {
107 .add_dev
= sg_add_device
,
108 .remove_dev
= sg_remove_device
,
111 typedef struct sg_scatter_hold
{ /* holding area for scsi scatter gather info */
112 unsigned short k_use_sg
; /* Count of kernel scatter-gather pieces */
113 unsigned sglist_len
; /* size of malloc'd scatter-gather list ++ */
114 unsigned bufflen
; /* Size of (aggregate) data buffer */
117 char dio_in_use
; /* 0->indirect IO (or mmap), 1->dio */
118 unsigned char cmd_opcode
; /* first byte of command */
121 struct sg_device
; /* forward declarations */
124 typedef struct sg_request
{ /* SG_MAX_QUEUE requests outstanding per file */
125 struct sg_request
*nextrp
; /* NULL -> tail request (slist) */
126 struct sg_fd
*parentfp
; /* NULL -> not in use */
127 Sg_scatter_hold data
; /* hold buffer, perhaps scatter list */
128 sg_io_hdr_t header
; /* scsi command+info, see <scsi/sg.h> */
129 unsigned char sense_b
[SCSI_SENSE_BUFFERSIZE
];
130 char res_used
; /* 1 -> using reserve buffer, 0 -> not ... */
131 char orphan
; /* 1 -> drop on sight, 0 -> normal */
132 char sg_io_owned
; /* 1 -> packet belongs to SG_IO */
133 /* done protected by rq_list_lock */
134 char done
; /* 0->before bh, 1->before read, 2->read */
137 struct execute_work ew
;
140 typedef struct sg_fd
{ /* holds the state of a file descriptor */
141 struct list_head sfd_siblings
; /* protected by device's sfd_lock */
142 struct sg_device
*parentdp
; /* owning device */
143 wait_queue_head_t read_wait
; /* queue read until command done */
144 rwlock_t rq_list_lock
; /* protect access to list in req_arr */
145 int timeout
; /* defaults to SG_DEFAULT_TIMEOUT */
146 int timeout_user
; /* defaults to SG_DEFAULT_TIMEOUT_USER */
147 Sg_scatter_hold reserve
; /* buffer held for this file descriptor */
148 unsigned save_scat_len
; /* original length of trunc. scat. element */
149 Sg_request
*headrp
; /* head of request slist, NULL->empty */
150 struct fasync_struct
*async_qp
; /* used by asynchronous notification */
151 Sg_request req_arr
[SG_MAX_QUEUE
]; /* used as singly-linked list */
152 char low_dma
; /* as in parent but possibly overridden to 1 */
153 char force_packid
; /* 1 -> pack_id input to read(), 0 -> ignored */
154 char cmd_q
; /* 1 -> allow command queuing, 0 -> don't */
155 unsigned char next_cmd_len
; /* 0: automatic, >0: use on next write() */
156 char keep_orphan
; /* 0 -> drop orphan (def), 1 -> keep for read() */
157 char mmap_called
; /* 0 -> mmap() never called on this fd */
159 struct execute_work ew
;
162 typedef struct sg_device
{ /* holds the state of each scsi generic device */
163 struct scsi_device
*device
;
164 wait_queue_head_t open_wait
; /* queue open() when O_EXCL present */
165 struct mutex open_rel_lock
; /* held when in open() or release() */
166 int sg_tablesize
; /* adapter's max scatter-gather table size */
167 u32 index
; /* device index number */
168 struct list_head sfds
;
169 rwlock_t sfd_lock
; /* protect access to sfd list */
170 atomic_t detaching
; /* 0->device usable, 1->device detaching */
171 bool exclude
; /* 1->open(O_EXCL) succeeded and is active */
172 int open_cnt
; /* count of opens (perhaps < num(sfds) ) */
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>] */
179 /* tasklet or soft irq callback */
180 static void sg_rq_end_io(struct request
*rq
, int uptodate
);
181 static int sg_start_req(Sg_request
*srp
, unsigned char *cmd
);
182 static int sg_finish_rem_req(Sg_request
* srp
);
183 static int sg_build_indirect(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
);
184 static ssize_t
sg_new_read(Sg_fd
* sfp
, char __user
*buf
, size_t count
,
186 static ssize_t
sg_new_write(Sg_fd
*sfp
, struct file
*file
,
187 const char __user
*buf
, size_t count
, int blocking
,
188 int read_only
, int sg_io_owned
, 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_read_oxfer(Sg_request
* srp
, char __user
*outp
, int num_read_xfer
);
192 static void sg_remove_scat(Sg_fd
* sfp
, Sg_scatter_hold
* schp
);
193 static void sg_build_reserve(Sg_fd
* sfp
, int req_size
);
194 static void sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
);
195 static void sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
);
196 static Sg_fd
*sg_add_sfp(Sg_device
* sdp
);
197 static void sg_remove_sfp(struct kref
*);
198 static Sg_request
*sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
);
199 static Sg_request
*sg_add_request(Sg_fd
* sfp
);
200 static int sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
);
201 static int sg_res_in_use(Sg_fd
* sfp
);
202 static Sg_device
*sg_get_dev(int dev
);
203 static void sg_device_destroy(struct kref
*kref
);
205 #define SZ_SG_HEADER sizeof(struct sg_header)
206 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
207 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
208 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
210 #define sg_printk(prefix, sdp, fmt, a...) \
211 sdev_prefix_printk(prefix, (sdp)->device, \
212 (sdp)->disk->disk_name, fmt, ##a)
214 static int sg_allow_access(struct file
*filp
, unsigned char *cmd
)
216 struct sg_fd
*sfp
= filp
->private_data
;
218 if (sfp
->parentdp
->device
->type
== TYPE_SCANNER
)
221 return blk_verify_command(cmd
, filp
->f_mode
& FMODE_WRITE
);
225 open_wait(Sg_device
*sdp
, int flags
)
229 if (flags
& O_EXCL
) {
230 while (sdp
->open_cnt
> 0) {
231 mutex_unlock(&sdp
->open_rel_lock
);
232 retval
= wait_event_interruptible(sdp
->open_wait
,
233 (atomic_read(&sdp
->detaching
) ||
235 mutex_lock(&sdp
->open_rel_lock
);
237 if (retval
) /* -ERESTARTSYS */
239 if (atomic_read(&sdp
->detaching
))
243 while (sdp
->exclude
) {
244 mutex_unlock(&sdp
->open_rel_lock
);
245 retval
= wait_event_interruptible(sdp
->open_wait
,
246 (atomic_read(&sdp
->detaching
) ||
248 mutex_lock(&sdp
->open_rel_lock
);
250 if (retval
) /* -ERESTARTSYS */
252 if (atomic_read(&sdp
->detaching
))
260 /* Returns 0 on success, else a negated errno value */
262 sg_open(struct inode
*inode
, struct file
*filp
)
264 int dev
= iminor(inode
);
265 int flags
= filp
->f_flags
;
266 struct request_queue
*q
;
271 nonseekable_open(inode
, filp
);
272 if ((flags
& O_EXCL
) && (O_RDONLY
== (flags
& O_ACCMODE
)))
273 return -EPERM
; /* Can't lock it with read only access */
274 sdp
= sg_get_dev(dev
);
278 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
279 "sg_open: flags=0x%x\n", flags
));
281 /* This driver's module count bumped by fops_get in <linux/fs.h> */
282 /* Prevent the device driver from vanishing while we sleep */
283 retval
= scsi_device_get(sdp
->device
);
287 retval
= scsi_autopm_get_device(sdp
->device
);
291 /* scsi_block_when_processing_errors() may block so bypass
292 * check if O_NONBLOCK. Permits SCSI commands to be issued
293 * during error recovery. Tread carefully. */
294 if (!((flags
& O_NONBLOCK
) ||
295 scsi_block_when_processing_errors(sdp
->device
))) {
297 /* we are in error recovery for this device */
301 mutex_lock(&sdp
->open_rel_lock
);
302 if (flags
& O_NONBLOCK
) {
303 if (flags
& O_EXCL
) {
304 if (sdp
->open_cnt
> 0) {
306 goto error_mutex_locked
;
311 goto error_mutex_locked
;
315 retval
= open_wait(sdp
, flags
);
316 if (retval
) /* -ERESTARTSYS or -ENODEV */
317 goto error_mutex_locked
;
320 /* N.B. at this point we are holding the open_rel_lock */
324 if (sdp
->open_cnt
< 1) { /* no existing opens */
326 q
= sdp
->device
->request_queue
;
327 sdp
->sg_tablesize
= queue_max_segments(q
);
329 sfp
= sg_add_sfp(sdp
);
331 retval
= PTR_ERR(sfp
);
335 filp
->private_data
= sfp
;
337 mutex_unlock(&sdp
->open_rel_lock
);
341 kref_put(&sdp
->d_ref
, sg_device_destroy
);
345 if (flags
& O_EXCL
) {
346 sdp
->exclude
= false; /* undo if error */
347 wake_up_interruptible(&sdp
->open_wait
);
350 mutex_unlock(&sdp
->open_rel_lock
);
352 scsi_autopm_put_device(sdp
->device
);
354 scsi_device_put(sdp
->device
);
358 /* Release resources associated with a successful sg_open()
359 * Returns 0 on success, else a negated errno value */
361 sg_release(struct inode
*inode
, struct file
*filp
)
366 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
368 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
, "sg_release\n"));
370 mutex_lock(&sdp
->open_rel_lock
);
371 scsi_autopm_put_device(sdp
->device
);
372 kref_put(&sfp
->f_ref
, sg_remove_sfp
);
375 /* possibly many open()s waiting on exlude clearing, start many;
376 * only open(O_EXCL)s wait on 0==open_cnt so only start one */
378 sdp
->exclude
= false;
379 wake_up_interruptible_all(&sdp
->open_wait
);
380 } else if (0 == sdp
->open_cnt
) {
381 wake_up_interruptible(&sdp
->open_wait
);
383 mutex_unlock(&sdp
->open_rel_lock
);
388 sg_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
* ppos
)
393 int req_pack_id
= -1;
395 struct sg_header
*old_hdr
= NULL
;
398 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
400 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
401 "sg_read: count=%d\n", (int) count
));
403 if (!access_ok(VERIFY_WRITE
, buf
, count
))
405 if (sfp
->force_packid
&& (count
>= SZ_SG_HEADER
)) {
406 old_hdr
= kmalloc(SZ_SG_HEADER
, GFP_KERNEL
);
409 if (__copy_from_user(old_hdr
, buf
, SZ_SG_HEADER
)) {
413 if (old_hdr
->reply_len
< 0) {
414 if (count
>= SZ_SG_IO_HDR
) {
415 sg_io_hdr_t
*new_hdr
;
416 new_hdr
= kmalloc(SZ_SG_IO_HDR
, GFP_KERNEL
);
421 retval
=__copy_from_user
422 (new_hdr
, buf
, SZ_SG_IO_HDR
);
423 req_pack_id
= new_hdr
->pack_id
;
431 req_pack_id
= old_hdr
->pack_id
;
433 srp
= sg_get_rq_mark(sfp
, req_pack_id
);
434 if (!srp
) { /* now wait on packet to arrive */
435 if (atomic_read(&sdp
->detaching
)) {
439 if (filp
->f_flags
& O_NONBLOCK
) {
443 retval
= wait_event_interruptible(sfp
->read_wait
,
444 (atomic_read(&sdp
->detaching
) ||
445 (srp
= sg_get_rq_mark(sfp
, req_pack_id
))));
446 if (atomic_read(&sdp
->detaching
)) {
451 /* -ERESTARTSYS as signal hit process */
455 if (srp
->header
.interface_id
!= '\0') {
456 retval
= sg_new_read(sfp
, buf
, count
, srp
);
461 if (old_hdr
== NULL
) {
462 old_hdr
= kmalloc(SZ_SG_HEADER
, GFP_KERNEL
);
468 memset(old_hdr
, 0, SZ_SG_HEADER
);
469 old_hdr
->reply_len
= (int) hp
->timeout
;
470 old_hdr
->pack_len
= old_hdr
->reply_len
; /* old, strange behaviour */
471 old_hdr
->pack_id
= hp
->pack_id
;
472 old_hdr
->twelve_byte
=
473 ((srp
->data
.cmd_opcode
>= 0xc0) && (12 == hp
->cmd_len
)) ? 1 : 0;
474 old_hdr
->target_status
= hp
->masked_status
;
475 old_hdr
->host_status
= hp
->host_status
;
476 old_hdr
->driver_status
= hp
->driver_status
;
477 if ((CHECK_CONDITION
& hp
->masked_status
) ||
478 (DRIVER_SENSE
& hp
->driver_status
))
479 memcpy(old_hdr
->sense_buffer
, srp
->sense_b
,
480 sizeof (old_hdr
->sense_buffer
));
481 switch (hp
->host_status
) {
482 /* This setup of 'result' is for backward compatibility and is best
483 ignored by the user who should use target, host + driver status */
485 case DID_PASSTHROUGH
:
492 old_hdr
->result
= EBUSY
;
499 old_hdr
->result
= EIO
;
502 old_hdr
->result
= (srp
->sense_b
[0] == 0 &&
503 hp
->masked_status
== GOOD
) ? 0 : EIO
;
506 old_hdr
->result
= EIO
;
510 /* Now copy the result back to the user buffer. */
511 if (count
>= SZ_SG_HEADER
) {
512 if (__copy_to_user(buf
, old_hdr
, SZ_SG_HEADER
)) {
517 if (count
> old_hdr
->reply_len
)
518 count
= old_hdr
->reply_len
;
519 if (count
> SZ_SG_HEADER
) {
520 if (sg_read_oxfer(srp
, buf
, count
- SZ_SG_HEADER
)) {
526 count
= (old_hdr
->result
== 0) ? 0 : -EIO
;
527 sg_finish_rem_req(srp
);
535 sg_new_read(Sg_fd
* sfp
, char __user
*buf
, size_t count
, Sg_request
* srp
)
537 sg_io_hdr_t
*hp
= &srp
->header
;
541 if (count
< SZ_SG_IO_HDR
) {
546 if ((hp
->mx_sb_len
> 0) && hp
->sbp
) {
547 if ((CHECK_CONDITION
& hp
->masked_status
) ||
548 (DRIVER_SENSE
& hp
->driver_status
)) {
549 int sb_len
= SCSI_SENSE_BUFFERSIZE
;
550 sb_len
= (hp
->mx_sb_len
> sb_len
) ? sb_len
: hp
->mx_sb_len
;
551 len
= 8 + (int) srp
->sense_b
[7]; /* Additional sense length field */
552 len
= (len
> sb_len
) ? sb_len
: len
;
553 if (copy_to_user(hp
->sbp
, srp
->sense_b
, len
)) {
560 if (hp
->masked_status
|| hp
->host_status
|| hp
->driver_status
)
561 hp
->info
|= SG_INFO_CHECK
;
562 if (copy_to_user(buf
, hp
, SZ_SG_IO_HDR
)) {
567 err2
= sg_finish_rem_req(srp
);
568 return err
? : err2
? : count
;
572 sg_write(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
* ppos
)
574 int mxsize
, cmd_size
, k
;
575 int input_size
, blocking
;
576 unsigned char opcode
;
580 struct sg_header old_hdr
;
582 unsigned char cmnd
[SG_MAX_CDB_SIZE
];
584 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
586 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
587 "sg_write: count=%d\n", (int) count
));
588 if (atomic_read(&sdp
->detaching
))
590 if (!((filp
->f_flags
& O_NONBLOCK
) ||
591 scsi_block_when_processing_errors(sdp
->device
)))
594 if (!access_ok(VERIFY_READ
, buf
, count
))
595 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
596 if (count
< SZ_SG_HEADER
)
598 if (__copy_from_user(&old_hdr
, buf
, SZ_SG_HEADER
))
600 blocking
= !(filp
->f_flags
& O_NONBLOCK
);
601 if (old_hdr
.reply_len
< 0)
602 return sg_new_write(sfp
, filp
, buf
, count
,
603 blocking
, 0, 0, NULL
);
604 if (count
< (SZ_SG_HEADER
+ 6))
605 return -EIO
; /* The minimum scsi command length is 6 bytes. */
607 if (!(srp
= sg_add_request(sfp
))) {
608 SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO
, sdp
,
609 "sg_write: queue full\n"));
613 __get_user(opcode
, buf
);
614 if (sfp
->next_cmd_len
> 0) {
615 cmd_size
= sfp
->next_cmd_len
;
616 sfp
->next_cmd_len
= 0; /* reset so only this write() effected */
618 cmd_size
= COMMAND_SIZE(opcode
); /* based on SCSI command group */
619 if ((opcode
>= 0xc0) && old_hdr
.twelve_byte
)
622 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sdp
,
623 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode
, cmd_size
));
624 /* Determine buffer size. */
625 input_size
= count
- cmd_size
;
626 mxsize
= (input_size
> old_hdr
.reply_len
) ? input_size
: old_hdr
.reply_len
;
627 mxsize
-= SZ_SG_HEADER
;
628 input_size
-= SZ_SG_HEADER
;
629 if (input_size
< 0) {
630 sg_remove_request(sfp
, srp
);
631 return -EIO
; /* User did not pass enough bytes for this command. */
634 hp
->interface_id
= '\0'; /* indicator of old interface tunnelled */
635 hp
->cmd_len
= (unsigned char) cmd_size
;
639 hp
->dxfer_direction
= (old_hdr
.reply_len
> SZ_SG_HEADER
) ?
640 SG_DXFER_TO_FROM_DEV
: SG_DXFER_TO_DEV
;
642 hp
->dxfer_direction
= (mxsize
> 0) ? SG_DXFER_FROM_DEV
: SG_DXFER_NONE
;
643 hp
->dxfer_len
= mxsize
;
644 if ((hp
->dxfer_direction
== SG_DXFER_TO_DEV
) ||
645 (hp
->dxfer_direction
== SG_DXFER_TO_FROM_DEV
))
646 hp
->dxferp
= (char __user
*)buf
+ cmd_size
;
650 hp
->timeout
= old_hdr
.reply_len
; /* structure abuse ... */
651 hp
->flags
= input_size
; /* structure abuse ... */
652 hp
->pack_id
= old_hdr
.pack_id
;
654 if (__copy_from_user(cmnd
, buf
, cmd_size
))
657 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
658 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
659 * is a non-zero input_size, so emit a warning.
661 if (hp
->dxfer_direction
== SG_DXFER_TO_FROM_DEV
) {
662 static char cmd
[TASK_COMM_LEN
];
663 if (strcmp(current
->comm
, cmd
)) {
664 printk_ratelimited(KERN_WARNING
665 "sg_write: data in/out %d/%d bytes "
666 "for SCSI command 0x%x-- guessing "
667 "data in;\n program %s not setting "
668 "count and/or reply_len properly\n",
669 old_hdr
.reply_len
- (int)SZ_SG_HEADER
,
670 input_size
, (unsigned int) cmnd
[0],
672 strcpy(cmd
, current
->comm
);
675 k
= sg_common_write(sfp
, srp
, cmnd
, sfp
->timeout
, blocking
);
676 return (k
< 0) ? k
: count
;
680 sg_new_write(Sg_fd
*sfp
, struct file
*file
, const char __user
*buf
,
681 size_t count
, int blocking
, int read_only
, int sg_io_owned
,
687 unsigned char cmnd
[SG_MAX_CDB_SIZE
];
689 unsigned long ul_timeout
;
691 if (count
< SZ_SG_IO_HDR
)
693 if (!access_ok(VERIFY_READ
, buf
, count
))
694 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
696 sfp
->cmd_q
= 1; /* when sg_io_hdr seen, set command queuing on */
697 if (!(srp
= sg_add_request(sfp
))) {
698 SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO
, sfp
->parentdp
,
699 "sg_new_write: queue full\n"));
702 srp
->sg_io_owned
= sg_io_owned
;
704 if (__copy_from_user(hp
, buf
, SZ_SG_IO_HDR
)) {
705 sg_remove_request(sfp
, srp
);
708 if (hp
->interface_id
!= 'S') {
709 sg_remove_request(sfp
, srp
);
712 if (hp
->flags
& SG_FLAG_MMAP_IO
) {
713 if (hp
->dxfer_len
> sfp
->reserve
.bufflen
) {
714 sg_remove_request(sfp
, srp
);
715 return -ENOMEM
; /* MMAP_IO size must fit in reserve buffer */
717 if (hp
->flags
& SG_FLAG_DIRECT_IO
) {
718 sg_remove_request(sfp
, srp
);
719 return -EINVAL
; /* either MMAP_IO or DIRECT_IO (not both) */
721 if (sg_res_in_use(sfp
)) {
722 sg_remove_request(sfp
, srp
);
723 return -EBUSY
; /* reserve buffer already being used */
726 ul_timeout
= msecs_to_jiffies(srp
->header
.timeout
);
727 timeout
= (ul_timeout
< INT_MAX
) ? ul_timeout
: INT_MAX
;
728 if ((!hp
->cmdp
) || (hp
->cmd_len
< 6) || (hp
->cmd_len
> sizeof (cmnd
))) {
729 sg_remove_request(sfp
, srp
);
732 if (!access_ok(VERIFY_READ
, hp
->cmdp
, hp
->cmd_len
)) {
733 sg_remove_request(sfp
, srp
);
734 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
736 if (__copy_from_user(cmnd
, hp
->cmdp
, hp
->cmd_len
)) {
737 sg_remove_request(sfp
, srp
);
740 if (read_only
&& sg_allow_access(file
, cmnd
)) {
741 sg_remove_request(sfp
, srp
);
744 k
= sg_common_write(sfp
, srp
, cmnd
, timeout
, blocking
);
753 sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
754 unsigned char *cmnd
, int timeout
, int blocking
)
757 Sg_device
*sdp
= sfp
->parentdp
;
758 sg_io_hdr_t
*hp
= &srp
->header
;
760 srp
->data
.cmd_opcode
= cmnd
[0]; /* hold opcode of command */
762 hp
->masked_status
= 0;
766 hp
->driver_status
= 0;
768 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
769 "sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
770 (int) cmnd
[0], (int) hp
->cmd_len
));
772 k
= sg_start_req(srp
, cmnd
);
774 SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO
, sfp
->parentdp
,
775 "sg_common_write: start_req err=%d\n", k
));
776 sg_finish_rem_req(srp
);
777 return k
; /* probably out of space --> ENOMEM */
779 if (atomic_read(&sdp
->detaching
)) {
781 if (srp
->rq
->cmd
!= srp
->rq
->__cmd
)
784 blk_end_request_all(srp
->rq
, -EIO
);
788 sg_finish_rem_req(srp
);
792 hp
->duration
= jiffies_to_msecs(jiffies
);
793 if (hp
->interface_id
!= '\0' && /* v3 (or later) interface */
794 (SG_FLAG_Q_AT_TAIL
& hp
->flags
))
799 srp
->rq
->timeout
= timeout
;
800 kref_get(&sfp
->f_ref
); /* sg_rq_end_io() does kref_put(). */
801 blk_execute_rq_nowait(sdp
->device
->request_queue
, sdp
->disk
,
802 srp
->rq
, at_head
, sg_rq_end_io
);
806 static int srp_done(Sg_fd
*sfp
, Sg_request
*srp
)
811 read_lock_irqsave(&sfp
->rq_list_lock
, flags
);
813 read_unlock_irqrestore(&sfp
->rq_list_lock
, flags
);
817 static int max_sectors_bytes(struct request_queue
*q
)
819 unsigned int max_sectors
= queue_max_sectors(q
);
821 max_sectors
= min_t(unsigned int, max_sectors
, INT_MAX
>> 9);
823 return max_sectors
<< 9;
827 sg_ioctl(struct file
*filp
, unsigned int cmd_in
, unsigned long arg
)
829 void __user
*p
= (void __user
*)arg
;
831 int result
, val
, read_only
;
835 unsigned long iflags
;
837 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
840 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
841 "sg_ioctl: cmd=0x%x\n", (int) cmd_in
));
842 read_only
= (O_RDWR
!= (filp
->f_flags
& O_ACCMODE
));
846 if (atomic_read(&sdp
->detaching
))
848 if (!scsi_block_when_processing_errors(sdp
->device
))
850 if (!access_ok(VERIFY_WRITE
, p
, SZ_SG_IO_HDR
))
852 result
= sg_new_write(sfp
, filp
, p
, SZ_SG_IO_HDR
,
853 1, read_only
, 1, &srp
);
856 result
= wait_event_interruptible(sfp
->read_wait
,
857 (srp_done(sfp
, srp
) || atomic_read(&sdp
->detaching
)));
858 if (atomic_read(&sdp
->detaching
))
860 write_lock_irq(&sfp
->rq_list_lock
);
863 write_unlock_irq(&sfp
->rq_list_lock
);
864 result
= sg_new_read(sfp
, p
, SZ_SG_IO_HDR
, srp
);
865 return (result
< 0) ? result
: 0;
868 write_unlock_irq(&sfp
->rq_list_lock
);
869 return result
; /* -ERESTARTSYS because signal hit process */
871 result
= get_user(val
, ip
);
876 if (val
>= mult_frac((s64
)INT_MAX
, USER_HZ
, HZ
))
877 val
= min_t(s64
, mult_frac((s64
)INT_MAX
, USER_HZ
, HZ
),
879 sfp
->timeout_user
= val
;
880 sfp
->timeout
= mult_frac(val
, HZ
, USER_HZ
);
883 case SG_GET_TIMEOUT
: /* N.B. User receives timeout as return value */
884 /* strange ..., for backward compatibility */
885 return sfp
->timeout_user
;
886 case SG_SET_FORCE_LOW_DMA
:
887 result
= get_user(val
, ip
);
892 if ((0 == sfp
->low_dma
) && (0 == sg_res_in_use(sfp
))) {
893 val
= (int) sfp
->reserve
.bufflen
;
894 sg_remove_scat(sfp
, &sfp
->reserve
);
895 sg_build_reserve(sfp
, val
);
898 if (atomic_read(&sdp
->detaching
))
900 sfp
->low_dma
= sdp
->device
->host
->unchecked_isa_dma
;
904 return put_user((int) sfp
->low_dma
, ip
);
906 if (!access_ok(VERIFY_WRITE
, p
, sizeof (sg_scsi_id_t
)))
909 sg_scsi_id_t __user
*sg_idp
= p
;
911 if (atomic_read(&sdp
->detaching
))
913 __put_user((int) sdp
->device
->host
->host_no
,
915 __put_user((int) sdp
->device
->channel
,
917 __put_user((int) sdp
->device
->id
, &sg_idp
->scsi_id
);
918 __put_user((int) sdp
->device
->lun
, &sg_idp
->lun
);
919 __put_user((int) sdp
->device
->type
, &sg_idp
->scsi_type
);
920 __put_user((short) sdp
->device
->host
->cmd_per_lun
,
921 &sg_idp
->h_cmd_per_lun
);
922 __put_user((short) sdp
->device
->queue_depth
,
923 &sg_idp
->d_queue_depth
);
924 __put_user(0, &sg_idp
->unused
[0]);
925 __put_user(0, &sg_idp
->unused
[1]);
928 case SG_SET_FORCE_PACK_ID
:
929 result
= get_user(val
, ip
);
932 sfp
->force_packid
= val
? 1 : 0;
935 if (!access_ok(VERIFY_WRITE
, ip
, sizeof (int)))
937 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
938 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
939 if ((1 == srp
->done
) && (!srp
->sg_io_owned
)) {
940 read_unlock_irqrestore(&sfp
->rq_list_lock
,
942 __put_user(srp
->header
.pack_id
, ip
);
946 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
949 case SG_GET_NUM_WAITING
:
950 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
951 for (val
= 0, srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
952 if ((1 == srp
->done
) && (!srp
->sg_io_owned
))
955 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
956 return put_user(val
, ip
);
957 case SG_GET_SG_TABLESIZE
:
958 return put_user(sdp
->sg_tablesize
, ip
);
959 case SG_SET_RESERVED_SIZE
:
960 result
= get_user(val
, ip
);
965 val
= min_t(int, val
,
966 max_sectors_bytes(sdp
->device
->request_queue
));
967 if (val
!= sfp
->reserve
.bufflen
) {
968 if (sg_res_in_use(sfp
) || sfp
->mmap_called
)
970 sg_remove_scat(sfp
, &sfp
->reserve
);
971 sg_build_reserve(sfp
, val
);
974 case SG_GET_RESERVED_SIZE
:
975 val
= min_t(int, sfp
->reserve
.bufflen
,
976 max_sectors_bytes(sdp
->device
->request_queue
));
977 return put_user(val
, ip
);
978 case SG_SET_COMMAND_Q
:
979 result
= get_user(val
, ip
);
982 sfp
->cmd_q
= val
? 1 : 0;
984 case SG_GET_COMMAND_Q
:
985 return put_user((int) sfp
->cmd_q
, ip
);
986 case SG_SET_KEEP_ORPHAN
:
987 result
= get_user(val
, ip
);
990 sfp
->keep_orphan
= val
;
992 case SG_GET_KEEP_ORPHAN
:
993 return put_user((int) sfp
->keep_orphan
, ip
);
994 case SG_NEXT_CMD_LEN
:
995 result
= get_user(val
, ip
);
998 sfp
->next_cmd_len
= (val
> 0) ? val
: 0;
1000 case SG_GET_VERSION_NUM
:
1001 return put_user(sg_version_num
, ip
);
1002 case SG_GET_ACCESS_COUNT
:
1003 /* faked - we don't have a real access count anymore */
1004 val
= (sdp
->device
? 1 : 0);
1005 return put_user(val
, ip
);
1006 case SG_GET_REQUEST_TABLE
:
1007 if (!access_ok(VERIFY_WRITE
, p
, SZ_SG_REQ_INFO
* SG_MAX_QUEUE
))
1010 sg_req_info_t
*rinfo
;
1013 rinfo
= kmalloc(SZ_SG_REQ_INFO
* SG_MAX_QUEUE
,
1017 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1018 for (srp
= sfp
->headrp
, val
= 0; val
< SG_MAX_QUEUE
;
1019 ++val
, srp
= srp
? srp
->nextrp
: srp
) {
1020 memset(&rinfo
[val
], 0, SZ_SG_REQ_INFO
);
1022 rinfo
[val
].req_state
= srp
->done
+ 1;
1023 rinfo
[val
].problem
=
1024 srp
->header
.masked_status
&
1025 srp
->header
.host_status
&
1026 srp
->header
.driver_status
;
1028 rinfo
[val
].duration
=
1029 srp
->header
.duration
;
1031 ms
= jiffies_to_msecs(jiffies
);
1032 rinfo
[val
].duration
=
1033 (ms
> srp
->header
.duration
) ?
1034 (ms
- srp
->header
.duration
) : 0;
1036 rinfo
[val
].orphan
= srp
->orphan
;
1037 rinfo
[val
].sg_io_owned
=
1039 rinfo
[val
].pack_id
=
1040 srp
->header
.pack_id
;
1041 rinfo
[val
].usr_ptr
=
1042 srp
->header
.usr_ptr
;
1045 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1046 result
= __copy_to_user(p
, rinfo
,
1047 SZ_SG_REQ_INFO
* SG_MAX_QUEUE
);
1048 result
= result
? -EFAULT
: 0;
1052 case SG_EMULATED_HOST
:
1053 if (atomic_read(&sdp
->detaching
))
1055 return put_user(sdp
->device
->host
->hostt
->emulated
, ip
);
1056 case SCSI_IOCTL_SEND_COMMAND
:
1057 if (atomic_read(&sdp
->detaching
))
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(filp
, &opcode
))
1068 return sg_scsi_ioctl(sdp
->device
->request_queue
, NULL
, filp
->f_mode
, p
);
1070 result
= get_user(val
, ip
);
1073 sdp
->sgdebug
= (char) val
;
1076 return put_user(max_sectors_bytes(sdp
->device
->request_queue
),
1079 return blk_trace_setup(sdp
->device
->request_queue
,
1080 sdp
->disk
->disk_name
,
1081 MKDEV(SCSI_GENERIC_MAJOR
, sdp
->index
),
1085 return blk_trace_startstop(sdp
->device
->request_queue
, 1);
1087 return blk_trace_startstop(sdp
->device
->request_queue
, 0);
1088 case BLKTRACETEARDOWN
:
1089 return blk_trace_remove(sdp
->device
->request_queue
);
1090 case SCSI_IOCTL_GET_IDLUN
:
1091 case SCSI_IOCTL_GET_BUS_NUMBER
:
1092 case SCSI_IOCTL_PROBE_HOST
:
1093 case SG_GET_TRANSFORM
:
1095 if (atomic_read(&sdp
->detaching
))
1100 return -EPERM
; /* don't know so take safe approach */
1104 result
= scsi_ioctl_block_when_processing_errors(sdp
->device
,
1105 cmd_in
, filp
->f_flags
& O_NDELAY
);
1108 return scsi_ioctl(sdp
->device
, cmd_in
, p
);
1111 #ifdef CONFIG_COMPAT
1112 static long sg_compat_ioctl(struct file
*filp
, unsigned int cmd_in
, unsigned long arg
)
1116 struct scsi_device
*sdev
;
1118 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
1122 if (sdev
->host
->hostt
->compat_ioctl
) {
1125 ret
= sdev
->host
->hostt
->compat_ioctl(sdev
, cmd_in
, (void __user
*)arg
);
1130 return -ENOIOCTLCMD
;
1135 sg_poll(struct file
*filp
, poll_table
* wait
)
1137 unsigned int res
= 0;
1142 unsigned long iflags
;
1144 sfp
= filp
->private_data
;
1147 sdp
= sfp
->parentdp
;
1150 poll_wait(filp
, &sfp
->read_wait
, wait
);
1151 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1152 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
1153 /* if any read waiting, flag it */
1154 if ((0 == res
) && (1 == srp
->done
) && (!srp
->sg_io_owned
))
1155 res
= POLLIN
| POLLRDNORM
;
1158 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1160 if (atomic_read(&sdp
->detaching
))
1162 else if (!sfp
->cmd_q
) {
1164 res
|= POLLOUT
| POLLWRNORM
;
1165 } else if (count
< SG_MAX_QUEUE
)
1166 res
|= POLLOUT
| POLLWRNORM
;
1167 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
1168 "sg_poll: res=0x%x\n", (int) res
));
1173 sg_fasync(int fd
, struct file
*filp
, int mode
)
1178 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
1180 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
1181 "sg_fasync: mode=%d\n", mode
));
1183 return fasync_helper(fd
, filp
, mode
, &sfp
->async_qp
);
1187 sg_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1190 unsigned long offset
, len
, sa
;
1191 Sg_scatter_hold
*rsv_schp
;
1194 if ((NULL
== vma
) || (!(sfp
= (Sg_fd
*) vma
->vm_private_data
)))
1195 return VM_FAULT_SIGBUS
;
1196 rsv_schp
= &sfp
->reserve
;
1197 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1198 if (offset
>= rsv_schp
->bufflen
)
1199 return VM_FAULT_SIGBUS
;
1200 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sfp
->parentdp
,
1201 "sg_vma_fault: offset=%lu, scatg=%d\n",
1202 offset
, rsv_schp
->k_use_sg
));
1204 length
= 1 << (PAGE_SHIFT
+ rsv_schp
->page_order
);
1205 for (k
= 0; k
< rsv_schp
->k_use_sg
&& sa
< vma
->vm_end
; k
++) {
1206 len
= vma
->vm_end
- sa
;
1207 len
= (len
< length
) ? len
: length
;
1209 struct page
*page
= nth_page(rsv_schp
->pages
[k
],
1210 offset
>> PAGE_SHIFT
);
1211 get_page(page
); /* increment page count */
1213 return 0; /* success */
1219 return VM_FAULT_SIGBUS
;
1222 static const struct vm_operations_struct sg_mmap_vm_ops
= {
1223 .fault
= sg_vma_fault
,
1227 sg_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1230 unsigned long req_sz
, len
, sa
;
1231 Sg_scatter_hold
*rsv_schp
;
1234 if ((!filp
) || (!vma
) || (!(sfp
= (Sg_fd
*) filp
->private_data
)))
1236 req_sz
= vma
->vm_end
- vma
->vm_start
;
1237 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sfp
->parentdp
,
1238 "sg_mmap starting, vm_start=%p, len=%d\n",
1239 (void *) vma
->vm_start
, (int) req_sz
));
1241 return -EINVAL
; /* want no offset */
1242 rsv_schp
= &sfp
->reserve
;
1243 if (req_sz
> rsv_schp
->bufflen
)
1244 return -ENOMEM
; /* cannot map more than reserved buffer */
1247 length
= 1 << (PAGE_SHIFT
+ rsv_schp
->page_order
);
1248 for (k
= 0; k
< rsv_schp
->k_use_sg
&& sa
< vma
->vm_end
; k
++) {
1249 len
= vma
->vm_end
- sa
;
1250 len
= (len
< length
) ? len
: length
;
1254 sfp
->mmap_called
= 1;
1255 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
1256 vma
->vm_private_data
= sfp
;
1257 vma
->vm_ops
= &sg_mmap_vm_ops
;
1262 sg_rq_end_io_usercontext(struct work_struct
*work
)
1264 struct sg_request
*srp
= container_of(work
, struct sg_request
, ew
.work
);
1265 struct sg_fd
*sfp
= srp
->parentfp
;
1267 sg_finish_rem_req(srp
);
1268 kref_put(&sfp
->f_ref
, sg_remove_sfp
);
1272 * This function is a "bottom half" handler that is called by the mid
1273 * level when a command is completed (or has failed).
1276 sg_rq_end_io(struct request
*rq
, int uptodate
)
1278 struct sg_request
*srp
= rq
->end_io_data
;
1281 unsigned long iflags
;
1284 int result
, resid
, done
= 1;
1286 if (WARN_ON(srp
->done
!= 0))
1289 sfp
= srp
->parentfp
;
1290 if (WARN_ON(sfp
== NULL
))
1293 sdp
= sfp
->parentdp
;
1294 if (unlikely(atomic_read(&sdp
->detaching
)))
1295 pr_info("%s: device detaching\n", __func__
);
1298 result
= rq
->errors
;
1299 resid
= rq
->resid_len
;
1301 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sdp
,
1302 "sg_cmd_done: pack_id=%d, res=0x%x\n",
1303 srp
->header
.pack_id
, result
));
1304 srp
->header
.resid
= resid
;
1305 ms
= jiffies_to_msecs(jiffies
);
1306 srp
->header
.duration
= (ms
> srp
->header
.duration
) ?
1307 (ms
- srp
->header
.duration
) : 0;
1309 struct scsi_sense_hdr sshdr
;
1311 srp
->header
.status
= 0xff & result
;
1312 srp
->header
.masked_status
= status_byte(result
);
1313 srp
->header
.msg_status
= msg_byte(result
);
1314 srp
->header
.host_status
= host_byte(result
);
1315 srp
->header
.driver_status
= driver_byte(result
);
1316 if ((sdp
->sgdebug
> 0) &&
1317 ((CHECK_CONDITION
== srp
->header
.masked_status
) ||
1318 (COMMAND_TERMINATED
== srp
->header
.masked_status
)))
1319 __scsi_print_sense(sdp
->device
, __func__
, sense
,
1320 SCSI_SENSE_BUFFERSIZE
);
1322 /* Following if statement is a patch supplied by Eric Youngdale */
1323 if (driver_byte(result
) != 0
1324 && scsi_normalize_sense(sense
, SCSI_SENSE_BUFFERSIZE
, &sshdr
)
1325 && !scsi_sense_is_deferred(&sshdr
)
1326 && sshdr
.sense_key
== UNIT_ATTENTION
1327 && sdp
->device
->removable
) {
1328 /* Detected possible disc change. Set the bit - this */
1329 /* may be used if there are filesystems using this device */
1330 sdp
->device
->changed
= 1;
1333 /* Rely on write phase to clean out srp status values, so no "else" */
1336 * Free the request as soon as it is complete so that its resources
1337 * can be reused without waiting for userspace to read() the
1338 * result. But keep the associated bio (if any) around until
1339 * blk_rq_unmap_user() can be called from user context.
1342 if (rq
->cmd
!= rq
->__cmd
)
1344 __blk_put_request(rq
->q
, rq
);
1346 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1347 if (unlikely(srp
->orphan
)) {
1348 if (sfp
->keep_orphan
)
1349 srp
->sg_io_owned
= 0;
1354 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1357 /* Now wake up any sg_read() that is waiting for this
1360 wake_up_interruptible(&sfp
->read_wait
);
1361 kill_fasync(&sfp
->async_qp
, SIGPOLL
, POLL_IN
);
1362 kref_put(&sfp
->f_ref
, sg_remove_sfp
);
1364 INIT_WORK(&srp
->ew
.work
, sg_rq_end_io_usercontext
);
1365 schedule_work(&srp
->ew
.work
);
1369 static const struct file_operations sg_fops
= {
1370 .owner
= THIS_MODULE
,
1374 .unlocked_ioctl
= sg_ioctl
,
1375 #ifdef CONFIG_COMPAT
1376 .compat_ioctl
= sg_compat_ioctl
,
1380 .release
= sg_release
,
1381 .fasync
= sg_fasync
,
1382 .llseek
= no_llseek
,
1385 static struct class *sg_sysfs_class
;
1387 static int sg_sysfs_valid
= 0;
1390 sg_alloc(struct gendisk
*disk
, struct scsi_device
*scsidp
)
1392 struct request_queue
*q
= scsidp
->request_queue
;
1394 unsigned long iflags
;
1398 sdp
= kzalloc(sizeof(Sg_device
), GFP_KERNEL
);
1400 sdev_printk(KERN_WARNING
, scsidp
, "%s: kmalloc Sg_device "
1401 "failure\n", __func__
);
1402 return ERR_PTR(-ENOMEM
);
1405 idr_preload(GFP_KERNEL
);
1406 write_lock_irqsave(&sg_index_lock
, iflags
);
1408 error
= idr_alloc(&sg_index_idr
, sdp
, 0, SG_MAX_DEVS
, GFP_NOWAIT
);
1410 if (error
== -ENOSPC
) {
1411 sdev_printk(KERN_WARNING
, scsidp
,
1412 "Unable to attach sg device type=%d, minor number exceeds %d\n",
1413 scsidp
->type
, SG_MAX_DEVS
- 1);
1416 sdev_printk(KERN_WARNING
, scsidp
, "%s: idr "
1417 "allocation Sg_device failure: %d\n",
1424 SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO
, scsidp
,
1425 "sg_alloc: dev=%d \n", k
));
1426 sprintf(disk
->disk_name
, "sg%d", k
);
1427 disk
->first_minor
= k
;
1429 sdp
->device
= scsidp
;
1430 mutex_init(&sdp
->open_rel_lock
);
1431 INIT_LIST_HEAD(&sdp
->sfds
);
1432 init_waitqueue_head(&sdp
->open_wait
);
1433 atomic_set(&sdp
->detaching
, 0);
1434 rwlock_init(&sdp
->sfd_lock
);
1435 sdp
->sg_tablesize
= queue_max_segments(q
);
1437 kref_init(&sdp
->d_ref
);
1441 write_unlock_irqrestore(&sg_index_lock
, iflags
);
1446 return ERR_PTR(error
);
1452 sg_add_device(struct device
*cl_dev
, struct class_interface
*cl_intf
)
1454 struct scsi_device
*scsidp
= to_scsi_device(cl_dev
->parent
);
1455 struct gendisk
*disk
;
1456 Sg_device
*sdp
= NULL
;
1457 struct cdev
* cdev
= NULL
;
1459 unsigned long iflags
;
1461 disk
= alloc_disk(1);
1463 pr_warn("%s: alloc_disk failed\n", __func__
);
1466 disk
->major
= SCSI_GENERIC_MAJOR
;
1469 cdev
= cdev_alloc();
1471 pr_warn("%s: cdev_alloc failed\n", __func__
);
1474 cdev
->owner
= THIS_MODULE
;
1475 cdev
->ops
= &sg_fops
;
1477 sdp
= sg_alloc(disk
, scsidp
);
1479 pr_warn("%s: sg_alloc failed\n", __func__
);
1480 error
= PTR_ERR(sdp
);
1484 error
= cdev_add(cdev
, MKDEV(SCSI_GENERIC_MAJOR
, sdp
->index
), 1);
1489 if (sg_sysfs_valid
) {
1490 struct device
*sg_class_member
;
1492 sg_class_member
= device_create(sg_sysfs_class
, cl_dev
->parent
,
1493 MKDEV(SCSI_GENERIC_MAJOR
,
1495 sdp
, "%s", disk
->disk_name
);
1496 if (IS_ERR(sg_class_member
)) {
1497 pr_err("%s: device_create failed\n", __func__
);
1498 error
= PTR_ERR(sg_class_member
);
1501 error
= sysfs_create_link(&scsidp
->sdev_gendev
.kobj
,
1502 &sg_class_member
->kobj
, "generic");
1504 pr_err("%s: unable to make symlink 'generic' back "
1505 "to sg%d\n", __func__
, sdp
->index
);
1507 pr_warn("%s: sg_sys Invalid\n", __func__
);
1509 sdev_printk(KERN_NOTICE
, scsidp
, "Attached scsi generic sg%d "
1510 "type %d\n", sdp
->index
, scsidp
->type
);
1512 dev_set_drvdata(cl_dev
, sdp
);
1517 write_lock_irqsave(&sg_index_lock
, iflags
);
1518 idr_remove(&sg_index_idr
, sdp
->index
);
1519 write_unlock_irqrestore(&sg_index_lock
, iflags
);
1530 sg_device_destroy(struct kref
*kref
)
1532 struct sg_device
*sdp
= container_of(kref
, struct sg_device
, d_ref
);
1533 unsigned long flags
;
1535 /* CAUTION! Note that the device can still be found via idr_find()
1536 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1537 * any other cleanup.
1540 write_lock_irqsave(&sg_index_lock
, flags
);
1541 idr_remove(&sg_index_idr
, sdp
->index
);
1542 write_unlock_irqrestore(&sg_index_lock
, flags
);
1545 sg_printk(KERN_INFO
, sdp
, "sg_device_destroy\n"));
1547 put_disk(sdp
->disk
);
1552 sg_remove_device(struct device
*cl_dev
, struct class_interface
*cl_intf
)
1554 struct scsi_device
*scsidp
= to_scsi_device(cl_dev
->parent
);
1555 Sg_device
*sdp
= dev_get_drvdata(cl_dev
);
1556 unsigned long iflags
;
1562 /* want sdp->detaching non-zero as soon as possible */
1563 val
= atomic_inc_return(&sdp
->detaching
);
1565 return; /* only want to do following once per device */
1567 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
1570 read_lock_irqsave(&sdp
->sfd_lock
, iflags
);
1571 list_for_each_entry(sfp
, &sdp
->sfds
, sfd_siblings
) {
1572 wake_up_interruptible_all(&sfp
->read_wait
);
1573 kill_fasync(&sfp
->async_qp
, SIGPOLL
, POLL_HUP
);
1575 wake_up_interruptible_all(&sdp
->open_wait
);
1576 read_unlock_irqrestore(&sdp
->sfd_lock
, iflags
);
1578 sysfs_remove_link(&scsidp
->sdev_gendev
.kobj
, "generic");
1579 device_destroy(sg_sysfs_class
, MKDEV(SCSI_GENERIC_MAJOR
, sdp
->index
));
1580 cdev_del(sdp
->cdev
);
1583 kref_put(&sdp
->d_ref
, sg_device_destroy
);
1586 module_param_named(scatter_elem_sz
, scatter_elem_sz
, int, S_IRUGO
| S_IWUSR
);
1587 module_param_named(def_reserved_size
, def_reserved_size
, int,
1589 module_param_named(allow_dio
, sg_allow_dio
, int, S_IRUGO
| S_IWUSR
);
1591 MODULE_AUTHOR("Douglas Gilbert");
1592 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1593 MODULE_LICENSE("GPL");
1594 MODULE_VERSION(SG_VERSION_STR
);
1595 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR
);
1597 MODULE_PARM_DESC(scatter_elem_sz
, "scatter gather element "
1598 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1599 MODULE_PARM_DESC(def_reserved_size
, "size of buffer reserved for each fd");
1600 MODULE_PARM_DESC(allow_dio
, "allow direct I/O (default: 0 (disallow))");
1607 if (scatter_elem_sz
< PAGE_SIZE
) {
1608 scatter_elem_sz
= PAGE_SIZE
;
1609 scatter_elem_sz_prev
= scatter_elem_sz
;
1611 if (def_reserved_size
>= 0)
1612 sg_big_buff
= def_reserved_size
;
1614 def_reserved_size
= sg_big_buff
;
1616 rc
= register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0),
1620 sg_sysfs_class
= class_create(THIS_MODULE
, "scsi_generic");
1621 if ( IS_ERR(sg_sysfs_class
) ) {
1622 rc
= PTR_ERR(sg_sysfs_class
);
1626 rc
= scsi_register_interface(&sg_interface
);
1628 #ifdef CONFIG_SCSI_PROC_FS
1630 #endif /* CONFIG_SCSI_PROC_FS */
1633 class_destroy(sg_sysfs_class
);
1635 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0), SG_MAX_DEVS
);
1642 #ifdef CONFIG_SCSI_PROC_FS
1644 #endif /* CONFIG_SCSI_PROC_FS */
1645 scsi_unregister_interface(&sg_interface
);
1646 class_destroy(sg_sysfs_class
);
1648 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0),
1650 idr_destroy(&sg_index_idr
);
1654 sg_start_req(Sg_request
*srp
, unsigned char *cmd
)
1658 Sg_fd
*sfp
= srp
->parentfp
;
1659 sg_io_hdr_t
*hp
= &srp
->header
;
1660 int dxfer_len
= (int) hp
->dxfer_len
;
1661 int dxfer_dir
= hp
->dxfer_direction
;
1662 unsigned int iov_count
= hp
->iovec_count
;
1663 Sg_scatter_hold
*req_schp
= &srp
->data
;
1664 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
1665 struct request_queue
*q
= sfp
->parentdp
->device
->request_queue
;
1666 struct rq_map_data
*md
, map_data
;
1667 int rw
= hp
->dxfer_direction
== SG_DXFER_TO_DEV
? WRITE
: READ
;
1668 unsigned char *long_cmdp
= NULL
;
1670 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
1671 "sg_start_req: dxfer_len=%d\n",
1674 if (hp
->cmd_len
> BLK_MAX_CDB
) {
1675 long_cmdp
= kzalloc(hp
->cmd_len
, GFP_KERNEL
);
1683 * With scsi-mq enabled, there are a fixed number of preallocated
1684 * requests equal in number to shost->can_queue. If all of the
1685 * preallocated requests are already in use, then using GFP_ATOMIC with
1686 * blk_get_request() will return -EWOULDBLOCK, whereas using GFP_KERNEL
1687 * will cause blk_get_request() to sleep until an active command
1688 * completes, freeing up a request. Neither option is ideal, but
1689 * GFP_KERNEL is the better choice to prevent userspace from getting an
1690 * unexpected EWOULDBLOCK.
1692 * With scsi-mq disabled, blk_get_request() with GFP_KERNEL usually
1693 * does not sleep except under memory pressure.
1695 rq
= blk_get_request(q
, rw
, GFP_KERNEL
);
1701 blk_rq_set_block_pc(rq
);
1703 if (hp
->cmd_len
> BLK_MAX_CDB
)
1704 rq
->cmd
= long_cmdp
;
1705 memcpy(rq
->cmd
, cmd
, hp
->cmd_len
);
1706 rq
->cmd_len
= hp
->cmd_len
;
1709 rq
->end_io_data
= srp
;
1710 rq
->sense
= srp
->sense_b
;
1711 rq
->retries
= SG_DEFAULT_RETRIES
;
1713 if ((dxfer_len
<= 0) || (dxfer_dir
== SG_DXFER_NONE
))
1716 if (sg_allow_dio
&& hp
->flags
& SG_FLAG_DIRECT_IO
&&
1717 dxfer_dir
!= SG_DXFER_UNKNOWN
&& !iov_count
&&
1718 !sfp
->parentdp
->device
->host
->unchecked_isa_dma
&&
1719 blk_rq_aligned(q
, (unsigned long)hp
->dxferp
, dxfer_len
))
1725 if (!sg_res_in_use(sfp
) && dxfer_len
<= rsv_schp
->bufflen
)
1726 sg_link_reserve(sfp
, srp
, dxfer_len
);
1728 res
= sg_build_indirect(req_schp
, sfp
, dxfer_len
);
1733 md
->pages
= req_schp
->pages
;
1734 md
->page_order
= req_schp
->page_order
;
1735 md
->nr_entries
= req_schp
->k_use_sg
;
1737 md
->null_mapped
= hp
->dxferp
? 0 : 1;
1738 if (dxfer_dir
== SG_DXFER_TO_FROM_DEV
)
1745 struct iovec
*iov
= NULL
;
1748 res
= import_iovec(rw
, hp
->dxferp
, iov_count
, 0, &iov
, &i
);
1752 iov_iter_truncate(&i
, hp
->dxfer_len
);
1754 res
= blk_rq_map_user_iov(q
, rq
, md
, &i
, GFP_ATOMIC
);
1757 res
= blk_rq_map_user(q
, rq
, md
, hp
->dxferp
,
1758 hp
->dxfer_len
, GFP_ATOMIC
);
1764 req_schp
->dio_in_use
= 1;
1765 hp
->info
|= SG_INFO_DIRECT_IO
;
1772 sg_finish_rem_req(Sg_request
*srp
)
1776 Sg_fd
*sfp
= srp
->parentfp
;
1777 Sg_scatter_hold
*req_schp
= &srp
->data
;
1779 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
1780 "sg_finish_rem_req: res_used=%d\n",
1781 (int) srp
->res_used
));
1783 ret
= blk_rq_unmap_user(srp
->bio
);
1786 if (srp
->rq
->cmd
!= srp
->rq
->__cmd
)
1787 kfree(srp
->rq
->cmd
);
1788 blk_put_request(srp
->rq
);
1792 sg_unlink_reserve(sfp
, srp
);
1794 sg_remove_scat(sfp
, req_schp
);
1796 sg_remove_request(sfp
, srp
);
1802 sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
, int tablesize
)
1804 int sg_bufflen
= tablesize
* sizeof(struct page
*);
1805 gfp_t gfp_flags
= GFP_ATOMIC
| __GFP_NOWARN
;
1807 schp
->pages
= kzalloc(sg_bufflen
, gfp_flags
);
1810 schp
->sglist_len
= sg_bufflen
;
1811 return tablesize
; /* number of scat_gath elements allocated */
1815 sg_build_indirect(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
)
1817 int ret_sz
= 0, i
, k
, rem_sz
, num
, mx_sc_elems
;
1818 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1819 int blk_size
= buff_size
, order
;
1820 gfp_t gfp_mask
= GFP_ATOMIC
| __GFP_COMP
| __GFP_NOWARN
;
1825 ++blk_size
; /* don't know why */
1826 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1827 blk_size
= ALIGN(blk_size
, SG_SECTOR_SZ
);
1828 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
1829 "sg_build_indirect: buff_size=%d, blk_size=%d\n",
1830 buff_size
, blk_size
));
1832 /* N.B. ret_sz carried into this block ... */
1833 mx_sc_elems
= sg_build_sgat(schp
, sfp
, sg_tablesize
);
1834 if (mx_sc_elems
< 0)
1835 return mx_sc_elems
; /* most likely -ENOMEM */
1837 num
= scatter_elem_sz
;
1838 if (unlikely(num
!= scatter_elem_sz_prev
)) {
1839 if (num
< PAGE_SIZE
) {
1840 scatter_elem_sz
= PAGE_SIZE
;
1841 scatter_elem_sz_prev
= PAGE_SIZE
;
1843 scatter_elem_sz_prev
= num
;
1847 gfp_mask
|= GFP_DMA
;
1849 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
1850 gfp_mask
|= __GFP_ZERO
;
1852 order
= get_order(num
);
1854 ret_sz
= 1 << (PAGE_SHIFT
+ order
);
1856 for (k
= 0, rem_sz
= blk_size
; rem_sz
> 0 && k
< mx_sc_elems
;
1857 k
++, rem_sz
-= ret_sz
) {
1859 num
= (rem_sz
> scatter_elem_sz_prev
) ?
1860 scatter_elem_sz_prev
: rem_sz
;
1862 schp
->pages
[k
] = alloc_pages(gfp_mask
, order
);
1863 if (!schp
->pages
[k
])
1866 if (num
== scatter_elem_sz_prev
) {
1867 if (unlikely(ret_sz
> scatter_elem_sz_prev
)) {
1868 scatter_elem_sz
= ret_sz
;
1869 scatter_elem_sz_prev
= ret_sz
;
1873 SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO
, sfp
->parentdp
,
1874 "sg_build_indirect: k=%d, num=%d, ret_sz=%d\n",
1876 } /* end of for loop */
1878 schp
->page_order
= order
;
1880 SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO
, sfp
->parentdp
,
1881 "sg_build_indirect: k_use_sg=%d, rem_sz=%d\n",
1884 schp
->bufflen
= blk_size
;
1885 if (rem_sz
> 0) /* must have failed */
1889 for (i
= 0; i
< k
; i
++)
1890 __free_pages(schp
->pages
[i
], order
);
1899 sg_remove_scat(Sg_fd
* sfp
, Sg_scatter_hold
* schp
)
1901 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
1902 "sg_remove_scat: k_use_sg=%d\n", schp
->k_use_sg
));
1903 if (schp
->pages
&& schp
->sglist_len
> 0) {
1904 if (!schp
->dio_in_use
) {
1907 for (k
= 0; k
< schp
->k_use_sg
&& schp
->pages
[k
]; k
++) {
1909 sg_printk(KERN_INFO
, sfp
->parentdp
,
1910 "sg_remove_scat: k=%d, pg=0x%p\n",
1911 k
, schp
->pages
[k
]));
1912 __free_pages(schp
->pages
[k
], schp
->page_order
);
1918 memset(schp
, 0, sizeof (*schp
));
1922 sg_read_oxfer(Sg_request
* srp
, char __user
*outp
, int num_read_xfer
)
1924 Sg_scatter_hold
*schp
= &srp
->data
;
1927 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, srp
->parentfp
->parentdp
,
1928 "sg_read_oxfer: num_read_xfer=%d\n",
1930 if ((!outp
) || (num_read_xfer
<= 0))
1933 num
= 1 << (PAGE_SHIFT
+ schp
->page_order
);
1934 for (k
= 0; k
< schp
->k_use_sg
&& schp
->pages
[k
]; k
++) {
1935 if (num
> num_read_xfer
) {
1936 if (__copy_to_user(outp
, page_address(schp
->pages
[k
]),
1941 if (__copy_to_user(outp
, page_address(schp
->pages
[k
]),
1944 num_read_xfer
-= num
;
1945 if (num_read_xfer
<= 0)
1955 sg_build_reserve(Sg_fd
* sfp
, int req_size
)
1957 Sg_scatter_hold
*schp
= &sfp
->reserve
;
1959 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
1960 "sg_build_reserve: req_size=%d\n", req_size
));
1962 if (req_size
< PAGE_SIZE
)
1963 req_size
= PAGE_SIZE
;
1964 if (0 == sg_build_indirect(schp
, sfp
, req_size
))
1967 sg_remove_scat(sfp
, schp
);
1968 req_size
>>= 1; /* divide by 2 */
1969 } while (req_size
> (PAGE_SIZE
/ 2));
1973 sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
)
1975 Sg_scatter_hold
*req_schp
= &srp
->data
;
1976 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
1980 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, sfp
->parentdp
,
1981 "sg_link_reserve: size=%d\n", size
));
1984 num
= 1 << (PAGE_SHIFT
+ rsv_schp
->page_order
);
1985 for (k
= 0; k
< rsv_schp
->k_use_sg
; k
++) {
1987 req_schp
->k_use_sg
= k
+ 1;
1988 req_schp
->sglist_len
= rsv_schp
->sglist_len
;
1989 req_schp
->pages
= rsv_schp
->pages
;
1991 req_schp
->bufflen
= size
;
1992 req_schp
->page_order
= rsv_schp
->page_order
;
1998 if (k
>= rsv_schp
->k_use_sg
)
1999 SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO
, sfp
->parentdp
,
2000 "sg_link_reserve: BAD size\n"));
2004 sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
)
2006 Sg_scatter_hold
*req_schp
= &srp
->data
;
2008 SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO
, srp
->parentfp
->parentdp
,
2009 "sg_unlink_reserve: req->k_use_sg=%d\n",
2010 (int) req_schp
->k_use_sg
));
2011 req_schp
->k_use_sg
= 0;
2012 req_schp
->bufflen
= 0;
2013 req_schp
->pages
= NULL
;
2014 req_schp
->page_order
= 0;
2015 req_schp
->sglist_len
= 0;
2016 sfp
->save_scat_len
= 0;
2021 sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
)
2024 unsigned long iflags
;
2026 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2027 for (resp
= sfp
->headrp
; resp
; resp
= resp
->nextrp
) {
2028 /* look for requests that are ready + not SG_IO owned */
2029 if ((1 == resp
->done
) && (!resp
->sg_io_owned
) &&
2030 ((-1 == pack_id
) || (resp
->header
.pack_id
== pack_id
))) {
2031 resp
->done
= 2; /* guard against other readers */
2035 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2039 /* always adds to end of list */
2041 sg_add_request(Sg_fd
* sfp
)
2044 unsigned long iflags
;
2046 Sg_request
*rp
= sfp
->req_arr
;
2048 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2051 memset(rp
, 0, sizeof (Sg_request
));
2056 if (0 == sfp
->cmd_q
)
2057 resp
= NULL
; /* command queuing disallowed */
2059 for (k
= 0; k
< SG_MAX_QUEUE
; ++k
, ++rp
) {
2063 if (k
< SG_MAX_QUEUE
) {
2064 memset(rp
, 0, sizeof (Sg_request
));
2066 while (resp
->nextrp
)
2067 resp
= resp
->nextrp
;
2075 resp
->nextrp
= NULL
;
2076 resp
->header
.duration
= jiffies_to_msecs(jiffies
);
2078 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2082 /* Return of 1 for found; 0 for not found */
2084 sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
)
2086 Sg_request
*prev_rp
;
2088 unsigned long iflags
;
2091 if ((!sfp
) || (!srp
) || (!sfp
->headrp
))
2093 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2094 prev_rp
= sfp
->headrp
;
2095 if (srp
== prev_rp
) {
2096 sfp
->headrp
= prev_rp
->nextrp
;
2097 prev_rp
->parentfp
= NULL
;
2100 while ((rp
= prev_rp
->nextrp
)) {
2102 prev_rp
->nextrp
= rp
->nextrp
;
2103 rp
->parentfp
= NULL
;
2110 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2115 sg_add_sfp(Sg_device
* sdp
)
2118 unsigned long iflags
;
2121 sfp
= kzalloc(sizeof(*sfp
), GFP_ATOMIC
| __GFP_NOWARN
);
2123 return ERR_PTR(-ENOMEM
);
2125 init_waitqueue_head(&sfp
->read_wait
);
2126 rwlock_init(&sfp
->rq_list_lock
);
2128 kref_init(&sfp
->f_ref
);
2129 sfp
->timeout
= SG_DEFAULT_TIMEOUT
;
2130 sfp
->timeout_user
= SG_DEFAULT_TIMEOUT_USER
;
2131 sfp
->force_packid
= SG_DEF_FORCE_PACK_ID
;
2132 sfp
->low_dma
= (SG_DEF_FORCE_LOW_DMA
== 0) ?
2133 sdp
->device
->host
->unchecked_isa_dma
: 1;
2134 sfp
->cmd_q
= SG_DEF_COMMAND_Q
;
2135 sfp
->keep_orphan
= SG_DEF_KEEP_ORPHAN
;
2136 sfp
->parentdp
= sdp
;
2137 write_lock_irqsave(&sdp
->sfd_lock
, iflags
);
2138 if (atomic_read(&sdp
->detaching
)) {
2139 write_unlock_irqrestore(&sdp
->sfd_lock
, iflags
);
2140 return ERR_PTR(-ENODEV
);
2142 list_add_tail(&sfp
->sfd_siblings
, &sdp
->sfds
);
2143 write_unlock_irqrestore(&sdp
->sfd_lock
, iflags
);
2144 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
2145 "sg_add_sfp: sfp=0x%p\n", sfp
));
2146 if (unlikely(sg_big_buff
!= def_reserved_size
))
2147 sg_big_buff
= def_reserved_size
;
2149 bufflen
= min_t(int, sg_big_buff
,
2150 max_sectors_bytes(sdp
->device
->request_queue
));
2151 sg_build_reserve(sfp
, bufflen
);
2152 SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO
, sdp
,
2153 "sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2154 sfp
->reserve
.bufflen
,
2155 sfp
->reserve
.k_use_sg
));
2157 kref_get(&sdp
->d_ref
);
2158 __module_get(THIS_MODULE
);
2163 sg_remove_sfp_usercontext(struct work_struct
*work
)
2165 struct sg_fd
*sfp
= container_of(work
, struct sg_fd
, ew
.work
);
2166 struct sg_device
*sdp
= sfp
->parentdp
;
2168 /* Cleanup any responses which were never read(). */
2170 sg_finish_rem_req(sfp
->headrp
);
2172 if (sfp
->reserve
.bufflen
> 0) {
2173 SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO
, sdp
,
2174 "sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2175 (int) sfp
->reserve
.bufflen
,
2176 (int) sfp
->reserve
.k_use_sg
));
2177 sg_remove_scat(sfp
, &sfp
->reserve
);
2180 SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO
, sdp
,
2181 "sg_remove_sfp: sfp=0x%p\n", sfp
));
2184 scsi_device_put(sdp
->device
);
2185 kref_put(&sdp
->d_ref
, sg_device_destroy
);
2186 module_put(THIS_MODULE
);
2190 sg_remove_sfp(struct kref
*kref
)
2192 struct sg_fd
*sfp
= container_of(kref
, struct sg_fd
, f_ref
);
2193 struct sg_device
*sdp
= sfp
->parentdp
;
2194 unsigned long iflags
;
2196 write_lock_irqsave(&sdp
->sfd_lock
, iflags
);
2197 list_del(&sfp
->sfd_siblings
);
2198 write_unlock_irqrestore(&sdp
->sfd_lock
, iflags
);
2200 INIT_WORK(&sfp
->ew
.work
, sg_remove_sfp_usercontext
);
2201 schedule_work(&sfp
->ew
.work
);
2205 sg_res_in_use(Sg_fd
* sfp
)
2207 const Sg_request
*srp
;
2208 unsigned long iflags
;
2210 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2211 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
)
2214 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2218 #ifdef CONFIG_SCSI_PROC_FS
2220 sg_idr_max_id(int id
, void *p
, void *data
)
2234 unsigned long iflags
;
2236 read_lock_irqsave(&sg_index_lock
, iflags
);
2237 idr_for_each(&sg_index_idr
, sg_idr_max_id
, &k
);
2238 read_unlock_irqrestore(&sg_index_lock
, iflags
);
2239 return k
+ 1; /* origin 1 */
2243 /* must be called with sg_index_lock held */
2244 static Sg_device
*sg_lookup_dev(int dev
)
2246 return idr_find(&sg_index_idr
, dev
);
2252 struct sg_device
*sdp
;
2253 unsigned long flags
;
2255 read_lock_irqsave(&sg_index_lock
, flags
);
2256 sdp
= sg_lookup_dev(dev
);
2258 sdp
= ERR_PTR(-ENXIO
);
2259 else if (atomic_read(&sdp
->detaching
)) {
2260 /* If sdp->detaching, then the refcount may already be 0, in
2261 * which case it would be a bug to do kref_get().
2263 sdp
= ERR_PTR(-ENODEV
);
2265 kref_get(&sdp
->d_ref
);
2266 read_unlock_irqrestore(&sg_index_lock
, flags
);
2271 #ifdef CONFIG_SCSI_PROC_FS
2273 static struct proc_dir_entry
*sg_proc_sgp
= NULL
;
2275 static char sg_proc_sg_dirname
[] = "scsi/sg";
2277 static int sg_proc_seq_show_int(struct seq_file
*s
, void *v
);
2279 static int sg_proc_single_open_adio(struct inode
*inode
, struct file
*file
);
2280 static ssize_t
sg_proc_write_adio(struct file
*filp
, const char __user
*buffer
,
2281 size_t count
, loff_t
*off
);
2282 static const struct file_operations adio_fops
= {
2283 .owner
= THIS_MODULE
,
2284 .open
= sg_proc_single_open_adio
,
2286 .llseek
= seq_lseek
,
2287 .write
= sg_proc_write_adio
,
2288 .release
= single_release
,
2291 static int sg_proc_single_open_dressz(struct inode
*inode
, struct file
*file
);
2292 static ssize_t
sg_proc_write_dressz(struct file
*filp
,
2293 const char __user
*buffer
, size_t count
, loff_t
*off
);
2294 static const struct file_operations dressz_fops
= {
2295 .owner
= THIS_MODULE
,
2296 .open
= sg_proc_single_open_dressz
,
2298 .llseek
= seq_lseek
,
2299 .write
= sg_proc_write_dressz
,
2300 .release
= single_release
,
2303 static int sg_proc_seq_show_version(struct seq_file
*s
, void *v
);
2304 static int sg_proc_single_open_version(struct inode
*inode
, struct file
*file
);
2305 static const struct file_operations version_fops
= {
2306 .owner
= THIS_MODULE
,
2307 .open
= sg_proc_single_open_version
,
2309 .llseek
= seq_lseek
,
2310 .release
= single_release
,
2313 static int sg_proc_seq_show_devhdr(struct seq_file
*s
, void *v
);
2314 static int sg_proc_single_open_devhdr(struct inode
*inode
, struct file
*file
);
2315 static const struct file_operations devhdr_fops
= {
2316 .owner
= THIS_MODULE
,
2317 .open
= sg_proc_single_open_devhdr
,
2319 .llseek
= seq_lseek
,
2320 .release
= single_release
,
2323 static int sg_proc_seq_show_dev(struct seq_file
*s
, void *v
);
2324 static int sg_proc_open_dev(struct inode
*inode
, struct file
*file
);
2325 static void * dev_seq_start(struct seq_file
*s
, loff_t
*pos
);
2326 static void * dev_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
);
2327 static void dev_seq_stop(struct seq_file
*s
, void *v
);
2328 static const struct file_operations dev_fops
= {
2329 .owner
= THIS_MODULE
,
2330 .open
= sg_proc_open_dev
,
2332 .llseek
= seq_lseek
,
2333 .release
= seq_release
,
2335 static const struct seq_operations dev_seq_ops
= {
2336 .start
= dev_seq_start
,
2337 .next
= dev_seq_next
,
2338 .stop
= dev_seq_stop
,
2339 .show
= sg_proc_seq_show_dev
,
2342 static int sg_proc_seq_show_devstrs(struct seq_file
*s
, void *v
);
2343 static int sg_proc_open_devstrs(struct inode
*inode
, struct file
*file
);
2344 static const struct file_operations devstrs_fops
= {
2345 .owner
= THIS_MODULE
,
2346 .open
= sg_proc_open_devstrs
,
2348 .llseek
= seq_lseek
,
2349 .release
= seq_release
,
2351 static const struct seq_operations devstrs_seq_ops
= {
2352 .start
= dev_seq_start
,
2353 .next
= dev_seq_next
,
2354 .stop
= dev_seq_stop
,
2355 .show
= sg_proc_seq_show_devstrs
,
2358 static int sg_proc_seq_show_debug(struct seq_file
*s
, void *v
);
2359 static int sg_proc_open_debug(struct inode
*inode
, struct file
*file
);
2360 static const struct file_operations debug_fops
= {
2361 .owner
= THIS_MODULE
,
2362 .open
= sg_proc_open_debug
,
2364 .llseek
= seq_lseek
,
2365 .release
= seq_release
,
2367 static const struct seq_operations debug_seq_ops
= {
2368 .start
= dev_seq_start
,
2369 .next
= dev_seq_next
,
2370 .stop
= dev_seq_stop
,
2371 .show
= sg_proc_seq_show_debug
,
2375 struct sg_proc_leaf
{
2377 const struct file_operations
* fops
;
2380 static const struct sg_proc_leaf sg_proc_leaf_arr
[] = {
2381 {"allow_dio", &adio_fops
},
2382 {"debug", &debug_fops
},
2383 {"def_reserved_size", &dressz_fops
},
2384 {"device_hdr", &devhdr_fops
},
2385 {"devices", &dev_fops
},
2386 {"device_strs", &devstrs_fops
},
2387 {"version", &version_fops
}
2393 int num_leaves
= ARRAY_SIZE(sg_proc_leaf_arr
);
2396 sg_proc_sgp
= proc_mkdir(sg_proc_sg_dirname
, NULL
);
2399 for (k
= 0; k
< num_leaves
; ++k
) {
2400 const struct sg_proc_leaf
*leaf
= &sg_proc_leaf_arr
[k
];
2401 umode_t mask
= leaf
->fops
->write
? S_IRUGO
| S_IWUSR
: S_IRUGO
;
2402 proc_create(leaf
->name
, mask
, sg_proc_sgp
, leaf
->fops
);
2408 sg_proc_cleanup(void)
2411 int num_leaves
= ARRAY_SIZE(sg_proc_leaf_arr
);
2415 for (k
= 0; k
< num_leaves
; ++k
)
2416 remove_proc_entry(sg_proc_leaf_arr
[k
].name
, sg_proc_sgp
);
2417 remove_proc_entry(sg_proc_sg_dirname
, NULL
);
2421 static int sg_proc_seq_show_int(struct seq_file
*s
, void *v
)
2423 seq_printf(s
, "%d\n", *((int *)s
->private));
2427 static int sg_proc_single_open_adio(struct inode
*inode
, struct file
*file
)
2429 return single_open(file
, sg_proc_seq_show_int
, &sg_allow_dio
);
2433 sg_proc_write_adio(struct file
*filp
, const char __user
*buffer
,
2434 size_t count
, loff_t
*off
)
2439 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2441 err
= kstrtoul_from_user(buffer
, count
, 0, &num
);
2444 sg_allow_dio
= num
? 1 : 0;
2448 static int sg_proc_single_open_dressz(struct inode
*inode
, struct file
*file
)
2450 return single_open(file
, sg_proc_seq_show_int
, &sg_big_buff
);
2454 sg_proc_write_dressz(struct file
*filp
, const char __user
*buffer
,
2455 size_t count
, loff_t
*off
)
2458 unsigned long k
= ULONG_MAX
;
2460 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2463 err
= kstrtoul_from_user(buffer
, count
, 0, &k
);
2466 if (k
<= 1048576) { /* limit "big buff" to 1 MB */
2473 static int sg_proc_seq_show_version(struct seq_file
*s
, void *v
)
2475 seq_printf(s
, "%d\t%s [%s]\n", sg_version_num
, SG_VERSION_STR
,
2480 static int sg_proc_single_open_version(struct inode
*inode
, struct file
*file
)
2482 return single_open(file
, sg_proc_seq_show_version
, NULL
);
2485 static int sg_proc_seq_show_devhdr(struct seq_file
*s
, void *v
)
2487 seq_puts(s
, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n");
2491 static int sg_proc_single_open_devhdr(struct inode
*inode
, struct file
*file
)
2493 return single_open(file
, sg_proc_seq_show_devhdr
, NULL
);
2496 struct sg_proc_deviter
{
2501 static void * dev_seq_start(struct seq_file
*s
, loff_t
*pos
)
2503 struct sg_proc_deviter
* it
= kmalloc(sizeof(*it
), GFP_KERNEL
);
2510 it
->max
= sg_last_dev();
2511 if (it
->index
>= it
->max
)
2516 static void * dev_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2518 struct sg_proc_deviter
* it
= s
->private;
2521 return (it
->index
< it
->max
) ? it
: NULL
;
2524 static void dev_seq_stop(struct seq_file
*s
, void *v
)
2529 static int sg_proc_open_dev(struct inode
*inode
, struct file
*file
)
2531 return seq_open(file
, &dev_seq_ops
);
2534 static int sg_proc_seq_show_dev(struct seq_file
*s
, void *v
)
2536 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2538 struct scsi_device
*scsidp
;
2539 unsigned long iflags
;
2541 read_lock_irqsave(&sg_index_lock
, iflags
);
2542 sdp
= it
? sg_lookup_dev(it
->index
) : NULL
;
2543 if ((NULL
== sdp
) || (NULL
== sdp
->device
) ||
2544 (atomic_read(&sdp
->detaching
)))
2545 seq_puts(s
, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2547 scsidp
= sdp
->device
;
2548 seq_printf(s
, "%d\t%d\t%d\t%llu\t%d\t%d\t%d\t%d\t%d\n",
2549 scsidp
->host
->host_no
, scsidp
->channel
,
2550 scsidp
->id
, scsidp
->lun
, (int) scsidp
->type
,
2552 (int) scsidp
->queue_depth
,
2553 (int) atomic_read(&scsidp
->device_busy
),
2554 (int) scsi_device_online(scsidp
));
2556 read_unlock_irqrestore(&sg_index_lock
, iflags
);
2560 static int sg_proc_open_devstrs(struct inode
*inode
, struct file
*file
)
2562 return seq_open(file
, &devstrs_seq_ops
);
2565 static int sg_proc_seq_show_devstrs(struct seq_file
*s
, void *v
)
2567 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2569 struct scsi_device
*scsidp
;
2570 unsigned long iflags
;
2572 read_lock_irqsave(&sg_index_lock
, iflags
);
2573 sdp
= it
? sg_lookup_dev(it
->index
) : NULL
;
2574 scsidp
= sdp
? sdp
->device
: NULL
;
2575 if (sdp
&& scsidp
&& (!atomic_read(&sdp
->detaching
)))
2576 seq_printf(s
, "%8.8s\t%16.16s\t%4.4s\n",
2577 scsidp
->vendor
, scsidp
->model
, scsidp
->rev
);
2579 seq_puts(s
, "<no active device>\n");
2580 read_unlock_irqrestore(&sg_index_lock
, iflags
);
2584 /* must be called while holding sg_index_lock */
2585 static void sg_proc_debug_helper(struct seq_file
*s
, Sg_device
* sdp
)
2587 int k
, m
, new_interface
, blen
, usg
;
2590 const sg_io_hdr_t
*hp
;
2595 list_for_each_entry(fp
, &sdp
->sfds
, sfd_siblings
) {
2597 read_lock(&fp
->rq_list_lock
); /* irqs already disabled */
2598 seq_printf(s
, " FD(%d): timeout=%dms bufflen=%d "
2599 "(res)sgat=%d low_dma=%d\n", k
,
2600 jiffies_to_msecs(fp
->timeout
),
2601 fp
->reserve
.bufflen
,
2602 (int) fp
->reserve
.k_use_sg
,
2604 seq_printf(s
, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
2605 (int) fp
->cmd_q
, (int) fp
->force_packid
,
2606 (int) fp
->keep_orphan
);
2607 for (m
= 0, srp
= fp
->headrp
;
2609 ++m
, srp
= srp
->nextrp
) {
2611 new_interface
= (hp
->interface_id
== '\0') ? 0 : 1;
2612 if (srp
->res_used
) {
2613 if (new_interface
&&
2614 (SG_FLAG_MMAP_IO
& hp
->flags
))
2619 if (SG_INFO_DIRECT_IO_MASK
& hp
->info
)
2625 blen
= srp
->data
.bufflen
;
2626 usg
= srp
->data
.k_use_sg
;
2627 seq_puts(s
, srp
->done
?
2628 ((1 == srp
->done
) ? "rcv:" : "fin:")
2630 seq_printf(s
, " id=%d blen=%d",
2631 srp
->header
.pack_id
, blen
);
2633 seq_printf(s
, " dur=%d", hp
->duration
);
2635 ms
= jiffies_to_msecs(jiffies
);
2636 seq_printf(s
, " t_o/elap=%d/%d",
2637 (new_interface
? hp
->timeout
:
2638 jiffies_to_msecs(fp
->timeout
)),
2639 (ms
> hp
->duration
? ms
- hp
->duration
: 0));
2641 seq_printf(s
, "ms sgat=%d op=0x%02x\n", usg
,
2642 (int) srp
->data
.cmd_opcode
);
2645 seq_puts(s
, " No requests active\n");
2646 read_unlock(&fp
->rq_list_lock
);
2650 static int sg_proc_open_debug(struct inode
*inode
, struct file
*file
)
2652 return seq_open(file
, &debug_seq_ops
);
2655 static int sg_proc_seq_show_debug(struct seq_file
*s
, void *v
)
2657 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2659 unsigned long iflags
;
2661 if (it
&& (0 == it
->index
))
2662 seq_printf(s
, "max_active_device=%d def_reserved_size=%d\n",
2663 (int)it
->max
, sg_big_buff
);
2665 read_lock_irqsave(&sg_index_lock
, iflags
);
2666 sdp
= it
? sg_lookup_dev(it
->index
) : NULL
;
2669 read_lock(&sdp
->sfd_lock
);
2670 if (!list_empty(&sdp
->sfds
)) {
2671 seq_printf(s
, " >>> device=%s ", sdp
->disk
->disk_name
);
2672 if (atomic_read(&sdp
->detaching
))
2673 seq_puts(s
, "detaching pending close ");
2674 else if (sdp
->device
) {
2675 struct scsi_device
*scsidp
= sdp
->device
;
2677 seq_printf(s
, "%d:%d:%d:%llu em=%d",
2678 scsidp
->host
->host_no
,
2679 scsidp
->channel
, scsidp
->id
,
2681 scsidp
->host
->hostt
->emulated
);
2683 seq_printf(s
, " sg_tablesize=%d excl=%d open_cnt=%d\n",
2684 sdp
->sg_tablesize
, sdp
->exclude
, sdp
->open_cnt
);
2685 sg_proc_debug_helper(s
, sdp
);
2687 read_unlock(&sdp
->sfd_lock
);
2689 read_unlock_irqrestore(&sg_index_lock
, iflags
);
2693 #endif /* CONFIG_SCSI_PROC_FS */
2695 module_init(init_sg
);
2696 module_exit(exit_sg
);