1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * History:
4*4882a593Smuzhiyun * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
5*4882a593Smuzhiyun * to allow user process control of SCSI devices.
6*4882a593Smuzhiyun * Development Sponsored by Killy Corp. NY NY
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Original driver (sg.c):
9*4882a593Smuzhiyun * Copyright (C) 1992 Lawrence Foard
10*4882a593Smuzhiyun * Version 2 and 3 extensions to driver:
11*4882a593Smuzhiyun * Copyright (C) 1998 - 2014 Douglas Gilbert
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun static int sg_version_num = 30536; /* 2 digits for each component */
15*4882a593Smuzhiyun #define SG_VERSION_STR "3.5.36"
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun * D. P. Gilbert (dgilbert@interlog.com), notes:
19*4882a593Smuzhiyun * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
20*4882a593Smuzhiyun * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
21*4882a593Smuzhiyun * (otherwise the macros compile to empty statements).
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun #include <linux/module.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <linux/fs.h>
27*4882a593Smuzhiyun #include <linux/kernel.h>
28*4882a593Smuzhiyun #include <linux/sched.h>
29*4882a593Smuzhiyun #include <linux/string.h>
30*4882a593Smuzhiyun #include <linux/mm.h>
31*4882a593Smuzhiyun #include <linux/errno.h>
32*4882a593Smuzhiyun #include <linux/mtio.h>
33*4882a593Smuzhiyun #include <linux/ioctl.h>
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <linux/fcntl.h>
36*4882a593Smuzhiyun #include <linux/init.h>
37*4882a593Smuzhiyun #include <linux/poll.h>
38*4882a593Smuzhiyun #include <linux/moduleparam.h>
39*4882a593Smuzhiyun #include <linux/cdev.h>
40*4882a593Smuzhiyun #include <linux/idr.h>
41*4882a593Smuzhiyun #include <linux/seq_file.h>
42*4882a593Smuzhiyun #include <linux/blkdev.h>
43*4882a593Smuzhiyun #include <linux/delay.h>
44*4882a593Smuzhiyun #include <linux/blktrace_api.h>
45*4882a593Smuzhiyun #include <linux/mutex.h>
46*4882a593Smuzhiyun #include <linux/atomic.h>
47*4882a593Smuzhiyun #include <linux/ratelimit.h>
48*4882a593Smuzhiyun #include <linux/uio.h>
49*4882a593Smuzhiyun #include <linux/cred.h> /* for sg_check_file_access() */
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #include "scsi.h"
52*4882a593Smuzhiyun #include <scsi/scsi_dbg.h>
53*4882a593Smuzhiyun #include <scsi/scsi_host.h>
54*4882a593Smuzhiyun #include <scsi/scsi_driver.h>
55*4882a593Smuzhiyun #include <scsi/scsi_ioctl.h>
56*4882a593Smuzhiyun #include <scsi/sg.h>
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #include "scsi_logging.h"
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #ifdef CONFIG_SCSI_PROC_FS
61*4882a593Smuzhiyun #include <linux/proc_fs.h>
62*4882a593Smuzhiyun static char *sg_version_date = "20140603";
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun static int sg_proc_init(void);
65*4882a593Smuzhiyun #endif
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define SG_ALLOW_DIO_DEF 0
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define SG_MAX_DEVS 32768
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
72*4882a593Smuzhiyun * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
73*4882a593Smuzhiyun * than 16 bytes are "variable length" whose length is a multiple of 4
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun #define SG_MAX_CDB_SIZE 252
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #define SG_DEFAULT_TIMEOUT mult_frac(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun int sg_big_buff = SG_DEF_RESERVED_SIZE;
80*4882a593Smuzhiyun /* N.B. This variable is readable and writeable via
81*4882a593Smuzhiyun /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
82*4882a593Smuzhiyun of this size (or less if there is not enough memory) will be reserved
83*4882a593Smuzhiyun for use by this file descriptor. [Deprecated usage: this variable is also
84*4882a593Smuzhiyun readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
85*4882a593Smuzhiyun the kernel (i.e. it is not a module).] */
86*4882a593Smuzhiyun static int def_reserved_size = -1; /* picks up init parameter */
87*4882a593Smuzhiyun static int sg_allow_dio = SG_ALLOW_DIO_DEF;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun static int scatter_elem_sz = SG_SCATTER_SZ;
90*4882a593Smuzhiyun static int scatter_elem_sz_prev = SG_SCATTER_SZ;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #define SG_SECTOR_SZ 512
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun static int sg_add_device(struct device *, struct class_interface *);
95*4882a593Smuzhiyun static void sg_remove_device(struct device *, struct class_interface *);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static DEFINE_IDR(sg_index_idr);
98*4882a593Smuzhiyun static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
99*4882a593Smuzhiyun file descriptor list for device */
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static struct class_interface sg_interface = {
102*4882a593Smuzhiyun .add_dev = sg_add_device,
103*4882a593Smuzhiyun .remove_dev = sg_remove_device,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
107*4882a593Smuzhiyun unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
108*4882a593Smuzhiyun unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
109*4882a593Smuzhiyun unsigned bufflen; /* Size of (aggregate) data buffer */
110*4882a593Smuzhiyun struct page **pages;
111*4882a593Smuzhiyun int page_order;
112*4882a593Smuzhiyun char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
113*4882a593Smuzhiyun unsigned char cmd_opcode; /* first byte of command */
114*4882a593Smuzhiyun } Sg_scatter_hold;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun struct sg_device; /* forward declarations */
117*4882a593Smuzhiyun struct sg_fd;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
120*4882a593Smuzhiyun struct list_head entry; /* list entry */
121*4882a593Smuzhiyun struct sg_fd *parentfp; /* NULL -> not in use */
122*4882a593Smuzhiyun Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
123*4882a593Smuzhiyun sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
124*4882a593Smuzhiyun unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
125*4882a593Smuzhiyun char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
126*4882a593Smuzhiyun char orphan; /* 1 -> drop on sight, 0 -> normal */
127*4882a593Smuzhiyun char sg_io_owned; /* 1 -> packet belongs to SG_IO */
128*4882a593Smuzhiyun /* done protected by rq_list_lock */
129*4882a593Smuzhiyun char done; /* 0->before bh, 1->before read, 2->read */
130*4882a593Smuzhiyun struct request *rq;
131*4882a593Smuzhiyun struct bio *bio;
132*4882a593Smuzhiyun struct execute_work ew;
133*4882a593Smuzhiyun } Sg_request;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun typedef struct sg_fd { /* holds the state of a file descriptor */
136*4882a593Smuzhiyun struct list_head sfd_siblings; /* protected by device's sfd_lock */
137*4882a593Smuzhiyun struct sg_device *parentdp; /* owning device */
138*4882a593Smuzhiyun wait_queue_head_t read_wait; /* queue read until command done */
139*4882a593Smuzhiyun rwlock_t rq_list_lock; /* protect access to list in req_arr */
140*4882a593Smuzhiyun struct mutex f_mutex; /* protect against changes in this fd */
141*4882a593Smuzhiyun int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
142*4882a593Smuzhiyun int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
143*4882a593Smuzhiyun Sg_scatter_hold reserve; /* buffer held for this file descriptor */
144*4882a593Smuzhiyun struct list_head rq_list; /* head of request list */
145*4882a593Smuzhiyun struct fasync_struct *async_qp; /* used by asynchronous notification */
146*4882a593Smuzhiyun Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
147*4882a593Smuzhiyun char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
148*4882a593Smuzhiyun char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
149*4882a593Smuzhiyun unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
150*4882a593Smuzhiyun char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
151*4882a593Smuzhiyun char mmap_called; /* 0 -> mmap() never called on this fd */
152*4882a593Smuzhiyun char res_in_use; /* 1 -> 'reserve' array in use */
153*4882a593Smuzhiyun struct kref f_ref;
154*4882a593Smuzhiyun struct execute_work ew;
155*4882a593Smuzhiyun } Sg_fd;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun typedef struct sg_device { /* holds the state of each scsi generic device */
158*4882a593Smuzhiyun struct scsi_device *device;
159*4882a593Smuzhiyun wait_queue_head_t open_wait; /* queue open() when O_EXCL present */
160*4882a593Smuzhiyun struct mutex open_rel_lock; /* held when in open() or release() */
161*4882a593Smuzhiyun int sg_tablesize; /* adapter's max scatter-gather table size */
162*4882a593Smuzhiyun u32 index; /* device index number */
163*4882a593Smuzhiyun struct list_head sfds;
164*4882a593Smuzhiyun rwlock_t sfd_lock; /* protect access to sfd list */
165*4882a593Smuzhiyun atomic_t detaching; /* 0->device usable, 1->device detaching */
166*4882a593Smuzhiyun bool exclude; /* 1->open(O_EXCL) succeeded and is active */
167*4882a593Smuzhiyun int open_cnt; /* count of opens (perhaps < num(sfds) ) */
168*4882a593Smuzhiyun char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
169*4882a593Smuzhiyun struct gendisk *disk;
170*4882a593Smuzhiyun struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
171*4882a593Smuzhiyun struct kref d_ref;
172*4882a593Smuzhiyun } Sg_device;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* tasklet or soft irq callback */
175*4882a593Smuzhiyun static void sg_rq_end_io(struct request *rq, blk_status_t status);
176*4882a593Smuzhiyun static int sg_start_req(Sg_request *srp, unsigned char *cmd);
177*4882a593Smuzhiyun static int sg_finish_rem_req(Sg_request * srp);
178*4882a593Smuzhiyun static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
179*4882a593Smuzhiyun static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
180*4882a593Smuzhiyun Sg_request * srp);
181*4882a593Smuzhiyun static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
182*4882a593Smuzhiyun const char __user *buf, size_t count, int blocking,
183*4882a593Smuzhiyun int read_only, int sg_io_owned, Sg_request **o_srp);
184*4882a593Smuzhiyun static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
185*4882a593Smuzhiyun unsigned char *cmnd, int timeout, int blocking);
186*4882a593Smuzhiyun static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
187*4882a593Smuzhiyun static void sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp);
188*4882a593Smuzhiyun static void sg_build_reserve(Sg_fd * sfp, int req_size);
189*4882a593Smuzhiyun static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
190*4882a593Smuzhiyun static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
191*4882a593Smuzhiyun static Sg_fd *sg_add_sfp(Sg_device * sdp);
192*4882a593Smuzhiyun static void sg_remove_sfp(struct kref *);
193*4882a593Smuzhiyun static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id, bool *busy);
194*4882a593Smuzhiyun static Sg_request *sg_add_request(Sg_fd * sfp);
195*4882a593Smuzhiyun static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
196*4882a593Smuzhiyun static Sg_device *sg_get_dev(int dev);
197*4882a593Smuzhiyun static void sg_device_destroy(struct kref *kref);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun #define SZ_SG_HEADER sizeof(struct sg_header)
200*4882a593Smuzhiyun #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
201*4882a593Smuzhiyun #define SZ_SG_IOVEC sizeof(sg_iovec_t)
202*4882a593Smuzhiyun #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun #define sg_printk(prefix, sdp, fmt, a...) \
205*4882a593Smuzhiyun sdev_prefix_printk(prefix, (sdp)->device, \
206*4882a593Smuzhiyun (sdp)->disk->disk_name, fmt, ##a)
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /*
209*4882a593Smuzhiyun * The SCSI interfaces that use read() and write() as an asynchronous variant of
210*4882a593Smuzhiyun * ioctl(..., SG_IO, ...) are fundamentally unsafe, since there are lots of ways
211*4882a593Smuzhiyun * to trigger read() and write() calls from various contexts with elevated
212*4882a593Smuzhiyun * privileges. This can lead to kernel memory corruption (e.g. if these
213*4882a593Smuzhiyun * interfaces are called through splice()) and privilege escalation inside
214*4882a593Smuzhiyun * userspace (e.g. if a process with access to such a device passes a file
215*4882a593Smuzhiyun * descriptor to a SUID binary as stdin/stdout/stderr).
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * This function provides protection for the legacy API by restricting the
218*4882a593Smuzhiyun * calling context.
219*4882a593Smuzhiyun */
sg_check_file_access(struct file * filp,const char * caller)220*4882a593Smuzhiyun static int sg_check_file_access(struct file *filp, const char *caller)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun if (filp->f_cred != current_real_cred()) {
223*4882a593Smuzhiyun pr_err_once("%s: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
224*4882a593Smuzhiyun caller, task_tgid_vnr(current), current->comm);
225*4882a593Smuzhiyun return -EPERM;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun if (uaccess_kernel()) {
228*4882a593Smuzhiyun pr_err_once("%s: process %d (%s) called from kernel context, this is not allowed.\n",
229*4882a593Smuzhiyun caller, task_tgid_vnr(current), current->comm);
230*4882a593Smuzhiyun return -EACCES;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun return 0;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
sg_allow_access(struct file * filp,unsigned char * cmd)235*4882a593Smuzhiyun static int sg_allow_access(struct file *filp, unsigned char *cmd)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun struct sg_fd *sfp = filp->private_data;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (sfp->parentdp->device->type == TYPE_SCANNER)
240*4882a593Smuzhiyun return 0;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun return blk_verify_command(cmd, filp->f_mode);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun static int
open_wait(Sg_device * sdp,int flags)246*4882a593Smuzhiyun open_wait(Sg_device *sdp, int flags)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun int retval = 0;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun if (flags & O_EXCL) {
251*4882a593Smuzhiyun while (sdp->open_cnt > 0) {
252*4882a593Smuzhiyun mutex_unlock(&sdp->open_rel_lock);
253*4882a593Smuzhiyun retval = wait_event_interruptible(sdp->open_wait,
254*4882a593Smuzhiyun (atomic_read(&sdp->detaching) ||
255*4882a593Smuzhiyun !sdp->open_cnt));
256*4882a593Smuzhiyun mutex_lock(&sdp->open_rel_lock);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun if (retval) /* -ERESTARTSYS */
259*4882a593Smuzhiyun return retval;
260*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
261*4882a593Smuzhiyun return -ENODEV;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun } else {
264*4882a593Smuzhiyun while (sdp->exclude) {
265*4882a593Smuzhiyun mutex_unlock(&sdp->open_rel_lock);
266*4882a593Smuzhiyun retval = wait_event_interruptible(sdp->open_wait,
267*4882a593Smuzhiyun (atomic_read(&sdp->detaching) ||
268*4882a593Smuzhiyun !sdp->exclude));
269*4882a593Smuzhiyun mutex_lock(&sdp->open_rel_lock);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun if (retval) /* -ERESTARTSYS */
272*4882a593Smuzhiyun return retval;
273*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
274*4882a593Smuzhiyun return -ENODEV;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return retval;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* Returns 0 on success, else a negated errno value */
282*4882a593Smuzhiyun static int
sg_open(struct inode * inode,struct file * filp)283*4882a593Smuzhiyun sg_open(struct inode *inode, struct file *filp)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun int dev = iminor(inode);
286*4882a593Smuzhiyun int flags = filp->f_flags;
287*4882a593Smuzhiyun struct request_queue *q;
288*4882a593Smuzhiyun Sg_device *sdp;
289*4882a593Smuzhiyun Sg_fd *sfp;
290*4882a593Smuzhiyun int retval;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun nonseekable_open(inode, filp);
293*4882a593Smuzhiyun if ((flags & O_EXCL) && (O_RDONLY == (flags & O_ACCMODE)))
294*4882a593Smuzhiyun return -EPERM; /* Can't lock it with read only access */
295*4882a593Smuzhiyun sdp = sg_get_dev(dev);
296*4882a593Smuzhiyun if (IS_ERR(sdp))
297*4882a593Smuzhiyun return PTR_ERR(sdp);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
300*4882a593Smuzhiyun "sg_open: flags=0x%x\n", flags));
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /* This driver's module count bumped by fops_get in <linux/fs.h> */
303*4882a593Smuzhiyun /* Prevent the device driver from vanishing while we sleep */
304*4882a593Smuzhiyun retval = scsi_device_get(sdp->device);
305*4882a593Smuzhiyun if (retval)
306*4882a593Smuzhiyun goto sg_put;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun retval = scsi_autopm_get_device(sdp->device);
309*4882a593Smuzhiyun if (retval)
310*4882a593Smuzhiyun goto sdp_put;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* scsi_block_when_processing_errors() may block so bypass
313*4882a593Smuzhiyun * check if O_NONBLOCK. Permits SCSI commands to be issued
314*4882a593Smuzhiyun * during error recovery. Tread carefully. */
315*4882a593Smuzhiyun if (!((flags & O_NONBLOCK) ||
316*4882a593Smuzhiyun scsi_block_when_processing_errors(sdp->device))) {
317*4882a593Smuzhiyun retval = -ENXIO;
318*4882a593Smuzhiyun /* we are in error recovery for this device */
319*4882a593Smuzhiyun goto error_out;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun mutex_lock(&sdp->open_rel_lock);
323*4882a593Smuzhiyun if (flags & O_NONBLOCK) {
324*4882a593Smuzhiyun if (flags & O_EXCL) {
325*4882a593Smuzhiyun if (sdp->open_cnt > 0) {
326*4882a593Smuzhiyun retval = -EBUSY;
327*4882a593Smuzhiyun goto error_mutex_locked;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun } else {
330*4882a593Smuzhiyun if (sdp->exclude) {
331*4882a593Smuzhiyun retval = -EBUSY;
332*4882a593Smuzhiyun goto error_mutex_locked;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun } else {
336*4882a593Smuzhiyun retval = open_wait(sdp, flags);
337*4882a593Smuzhiyun if (retval) /* -ERESTARTSYS or -ENODEV */
338*4882a593Smuzhiyun goto error_mutex_locked;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /* N.B. at this point we are holding the open_rel_lock */
342*4882a593Smuzhiyun if (flags & O_EXCL)
343*4882a593Smuzhiyun sdp->exclude = true;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if (sdp->open_cnt < 1) { /* no existing opens */
346*4882a593Smuzhiyun sdp->sgdebug = 0;
347*4882a593Smuzhiyun q = sdp->device->request_queue;
348*4882a593Smuzhiyun sdp->sg_tablesize = queue_max_segments(q);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun sfp = sg_add_sfp(sdp);
351*4882a593Smuzhiyun if (IS_ERR(sfp)) {
352*4882a593Smuzhiyun retval = PTR_ERR(sfp);
353*4882a593Smuzhiyun goto out_undo;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun filp->private_data = sfp;
357*4882a593Smuzhiyun sdp->open_cnt++;
358*4882a593Smuzhiyun mutex_unlock(&sdp->open_rel_lock);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun retval = 0;
361*4882a593Smuzhiyun sg_put:
362*4882a593Smuzhiyun kref_put(&sdp->d_ref, sg_device_destroy);
363*4882a593Smuzhiyun return retval;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun out_undo:
366*4882a593Smuzhiyun if (flags & O_EXCL) {
367*4882a593Smuzhiyun sdp->exclude = false; /* undo if error */
368*4882a593Smuzhiyun wake_up_interruptible(&sdp->open_wait);
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun error_mutex_locked:
371*4882a593Smuzhiyun mutex_unlock(&sdp->open_rel_lock);
372*4882a593Smuzhiyun error_out:
373*4882a593Smuzhiyun scsi_autopm_put_device(sdp->device);
374*4882a593Smuzhiyun sdp_put:
375*4882a593Smuzhiyun scsi_device_put(sdp->device);
376*4882a593Smuzhiyun goto sg_put;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* Release resources associated with a successful sg_open()
380*4882a593Smuzhiyun * Returns 0 on success, else a negated errno value */
381*4882a593Smuzhiyun static int
sg_release(struct inode * inode,struct file * filp)382*4882a593Smuzhiyun sg_release(struct inode *inode, struct file *filp)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun Sg_device *sdp;
385*4882a593Smuzhiyun Sg_fd *sfp;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
388*4882a593Smuzhiyun return -ENXIO;
389*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, "sg_release\n"));
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun mutex_lock(&sdp->open_rel_lock);
392*4882a593Smuzhiyun scsi_autopm_put_device(sdp->device);
393*4882a593Smuzhiyun kref_put(&sfp->f_ref, sg_remove_sfp);
394*4882a593Smuzhiyun sdp->open_cnt--;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun /* possibly many open()s waiting on exlude clearing, start many;
397*4882a593Smuzhiyun * only open(O_EXCL)s wait on 0==open_cnt so only start one */
398*4882a593Smuzhiyun if (sdp->exclude) {
399*4882a593Smuzhiyun sdp->exclude = false;
400*4882a593Smuzhiyun wake_up_interruptible_all(&sdp->open_wait);
401*4882a593Smuzhiyun } else if (0 == sdp->open_cnt) {
402*4882a593Smuzhiyun wake_up_interruptible(&sdp->open_wait);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun mutex_unlock(&sdp->open_rel_lock);
405*4882a593Smuzhiyun return 0;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
get_sg_io_pack_id(int * pack_id,void __user * buf,size_t count)408*4882a593Smuzhiyun static int get_sg_io_pack_id(int *pack_id, void __user *buf, size_t count)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun struct sg_header __user *old_hdr = buf;
411*4882a593Smuzhiyun int reply_len;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun if (count >= SZ_SG_HEADER) {
414*4882a593Smuzhiyun /* negative reply_len means v3 format, otherwise v1/v2 */
415*4882a593Smuzhiyun if (get_user(reply_len, &old_hdr->reply_len))
416*4882a593Smuzhiyun return -EFAULT;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (reply_len >= 0)
419*4882a593Smuzhiyun return get_user(*pack_id, &old_hdr->pack_id);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun if (in_compat_syscall() &&
422*4882a593Smuzhiyun count >= sizeof(struct compat_sg_io_hdr)) {
423*4882a593Smuzhiyun struct compat_sg_io_hdr __user *hp = buf;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun return get_user(*pack_id, &hp->pack_id);
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (count >= sizeof(struct sg_io_hdr)) {
429*4882a593Smuzhiyun struct sg_io_hdr __user *hp = buf;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun return get_user(*pack_id, &hp->pack_id);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun /* no valid header was passed, so ignore the pack_id */
436*4882a593Smuzhiyun *pack_id = -1;
437*4882a593Smuzhiyun return 0;
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun static ssize_t
sg_read(struct file * filp,char __user * buf,size_t count,loff_t * ppos)441*4882a593Smuzhiyun sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun Sg_device *sdp;
444*4882a593Smuzhiyun Sg_fd *sfp;
445*4882a593Smuzhiyun Sg_request *srp;
446*4882a593Smuzhiyun int req_pack_id = -1;
447*4882a593Smuzhiyun bool busy;
448*4882a593Smuzhiyun sg_io_hdr_t *hp;
449*4882a593Smuzhiyun struct sg_header *old_hdr;
450*4882a593Smuzhiyun int retval;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /*
453*4882a593Smuzhiyun * This could cause a response to be stranded. Close the associated
454*4882a593Smuzhiyun * file descriptor to free up any resources being held.
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun retval = sg_check_file_access(filp, __func__);
457*4882a593Smuzhiyun if (retval)
458*4882a593Smuzhiyun return retval;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
461*4882a593Smuzhiyun return -ENXIO;
462*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
463*4882a593Smuzhiyun "sg_read: count=%d\n", (int) count));
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun if (sfp->force_packid)
466*4882a593Smuzhiyun retval = get_sg_io_pack_id(&req_pack_id, buf, count);
467*4882a593Smuzhiyun if (retval)
468*4882a593Smuzhiyun return retval;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun srp = sg_get_rq_mark(sfp, req_pack_id, &busy);
471*4882a593Smuzhiyun if (!srp) { /* now wait on packet to arrive */
472*4882a593Smuzhiyun if (filp->f_flags & O_NONBLOCK)
473*4882a593Smuzhiyun return -EAGAIN;
474*4882a593Smuzhiyun retval = wait_event_interruptible(sfp->read_wait,
475*4882a593Smuzhiyun ((srp = sg_get_rq_mark(sfp, req_pack_id, &busy)) ||
476*4882a593Smuzhiyun (!busy && atomic_read(&sdp->detaching))));
477*4882a593Smuzhiyun if (!srp)
478*4882a593Smuzhiyun /* signal or detaching */
479*4882a593Smuzhiyun return retval ? retval : -ENODEV;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun if (srp->header.interface_id != '\0')
482*4882a593Smuzhiyun return sg_new_read(sfp, buf, count, srp);
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun hp = &srp->header;
485*4882a593Smuzhiyun old_hdr = kzalloc(SZ_SG_HEADER, GFP_KERNEL);
486*4882a593Smuzhiyun if (!old_hdr)
487*4882a593Smuzhiyun return -ENOMEM;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun old_hdr->reply_len = (int) hp->timeout;
490*4882a593Smuzhiyun old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
491*4882a593Smuzhiyun old_hdr->pack_id = hp->pack_id;
492*4882a593Smuzhiyun old_hdr->twelve_byte =
493*4882a593Smuzhiyun ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
494*4882a593Smuzhiyun old_hdr->target_status = hp->masked_status;
495*4882a593Smuzhiyun old_hdr->host_status = hp->host_status;
496*4882a593Smuzhiyun old_hdr->driver_status = hp->driver_status;
497*4882a593Smuzhiyun if ((CHECK_CONDITION & hp->masked_status) ||
498*4882a593Smuzhiyun (DRIVER_SENSE & hp->driver_status))
499*4882a593Smuzhiyun memcpy(old_hdr->sense_buffer, srp->sense_b,
500*4882a593Smuzhiyun sizeof (old_hdr->sense_buffer));
501*4882a593Smuzhiyun switch (hp->host_status) {
502*4882a593Smuzhiyun /* This setup of 'result' is for backward compatibility and is best
503*4882a593Smuzhiyun ignored by the user who should use target, host + driver status */
504*4882a593Smuzhiyun case DID_OK:
505*4882a593Smuzhiyun case DID_PASSTHROUGH:
506*4882a593Smuzhiyun case DID_SOFT_ERROR:
507*4882a593Smuzhiyun old_hdr->result = 0;
508*4882a593Smuzhiyun break;
509*4882a593Smuzhiyun case DID_NO_CONNECT:
510*4882a593Smuzhiyun case DID_BUS_BUSY:
511*4882a593Smuzhiyun case DID_TIME_OUT:
512*4882a593Smuzhiyun old_hdr->result = EBUSY;
513*4882a593Smuzhiyun break;
514*4882a593Smuzhiyun case DID_BAD_TARGET:
515*4882a593Smuzhiyun case DID_ABORT:
516*4882a593Smuzhiyun case DID_PARITY:
517*4882a593Smuzhiyun case DID_RESET:
518*4882a593Smuzhiyun case DID_BAD_INTR:
519*4882a593Smuzhiyun old_hdr->result = EIO;
520*4882a593Smuzhiyun break;
521*4882a593Smuzhiyun case DID_ERROR:
522*4882a593Smuzhiyun old_hdr->result = (srp->sense_b[0] == 0 &&
523*4882a593Smuzhiyun hp->masked_status == GOOD) ? 0 : EIO;
524*4882a593Smuzhiyun break;
525*4882a593Smuzhiyun default:
526*4882a593Smuzhiyun old_hdr->result = EIO;
527*4882a593Smuzhiyun break;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /* Now copy the result back to the user buffer. */
531*4882a593Smuzhiyun if (count >= SZ_SG_HEADER) {
532*4882a593Smuzhiyun if (copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
533*4882a593Smuzhiyun retval = -EFAULT;
534*4882a593Smuzhiyun goto free_old_hdr;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun buf += SZ_SG_HEADER;
537*4882a593Smuzhiyun if (count > old_hdr->reply_len)
538*4882a593Smuzhiyun count = old_hdr->reply_len;
539*4882a593Smuzhiyun if (count > SZ_SG_HEADER) {
540*4882a593Smuzhiyun if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
541*4882a593Smuzhiyun retval = -EFAULT;
542*4882a593Smuzhiyun goto free_old_hdr;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun } else
546*4882a593Smuzhiyun count = (old_hdr->result == 0) ? 0 : -EIO;
547*4882a593Smuzhiyun sg_finish_rem_req(srp);
548*4882a593Smuzhiyun sg_remove_request(sfp, srp);
549*4882a593Smuzhiyun retval = count;
550*4882a593Smuzhiyun free_old_hdr:
551*4882a593Smuzhiyun kfree(old_hdr);
552*4882a593Smuzhiyun return retval;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun static ssize_t
sg_new_read(Sg_fd * sfp,char __user * buf,size_t count,Sg_request * srp)556*4882a593Smuzhiyun sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun sg_io_hdr_t *hp = &srp->header;
559*4882a593Smuzhiyun int err = 0, err2;
560*4882a593Smuzhiyun int len;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun if (in_compat_syscall()) {
563*4882a593Smuzhiyun if (count < sizeof(struct compat_sg_io_hdr)) {
564*4882a593Smuzhiyun err = -EINVAL;
565*4882a593Smuzhiyun goto err_out;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun } else if (count < SZ_SG_IO_HDR) {
568*4882a593Smuzhiyun err = -EINVAL;
569*4882a593Smuzhiyun goto err_out;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun hp->sb_len_wr = 0;
572*4882a593Smuzhiyun if ((hp->mx_sb_len > 0) && hp->sbp) {
573*4882a593Smuzhiyun if ((CHECK_CONDITION & hp->masked_status) ||
574*4882a593Smuzhiyun (DRIVER_SENSE & hp->driver_status)) {
575*4882a593Smuzhiyun int sb_len = SCSI_SENSE_BUFFERSIZE;
576*4882a593Smuzhiyun sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
577*4882a593Smuzhiyun len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
578*4882a593Smuzhiyun len = (len > sb_len) ? sb_len : len;
579*4882a593Smuzhiyun if (copy_to_user(hp->sbp, srp->sense_b, len)) {
580*4882a593Smuzhiyun err = -EFAULT;
581*4882a593Smuzhiyun goto err_out;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun hp->sb_len_wr = len;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun if (hp->masked_status || hp->host_status || hp->driver_status)
587*4882a593Smuzhiyun hp->info |= SG_INFO_CHECK;
588*4882a593Smuzhiyun err = put_sg_io_hdr(hp, buf);
589*4882a593Smuzhiyun err_out:
590*4882a593Smuzhiyun err2 = sg_finish_rem_req(srp);
591*4882a593Smuzhiyun sg_remove_request(sfp, srp);
592*4882a593Smuzhiyun return err ? : err2 ? : count;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun static ssize_t
sg_write(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)596*4882a593Smuzhiyun sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun int mxsize, cmd_size, k;
599*4882a593Smuzhiyun int input_size, blocking;
600*4882a593Smuzhiyun unsigned char opcode;
601*4882a593Smuzhiyun Sg_device *sdp;
602*4882a593Smuzhiyun Sg_fd *sfp;
603*4882a593Smuzhiyun Sg_request *srp;
604*4882a593Smuzhiyun struct sg_header old_hdr;
605*4882a593Smuzhiyun sg_io_hdr_t *hp;
606*4882a593Smuzhiyun unsigned char cmnd[SG_MAX_CDB_SIZE];
607*4882a593Smuzhiyun int retval;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun retval = sg_check_file_access(filp, __func__);
610*4882a593Smuzhiyun if (retval)
611*4882a593Smuzhiyun return retval;
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
614*4882a593Smuzhiyun return -ENXIO;
615*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
616*4882a593Smuzhiyun "sg_write: count=%d\n", (int) count));
617*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
618*4882a593Smuzhiyun return -ENODEV;
619*4882a593Smuzhiyun if (!((filp->f_flags & O_NONBLOCK) ||
620*4882a593Smuzhiyun scsi_block_when_processing_errors(sdp->device)))
621*4882a593Smuzhiyun return -ENXIO;
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun if (count < SZ_SG_HEADER)
624*4882a593Smuzhiyun return -EIO;
625*4882a593Smuzhiyun if (copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
626*4882a593Smuzhiyun return -EFAULT;
627*4882a593Smuzhiyun blocking = !(filp->f_flags & O_NONBLOCK);
628*4882a593Smuzhiyun if (old_hdr.reply_len < 0)
629*4882a593Smuzhiyun return sg_new_write(sfp, filp, buf, count,
630*4882a593Smuzhiyun blocking, 0, 0, NULL);
631*4882a593Smuzhiyun if (count < (SZ_SG_HEADER + 6))
632*4882a593Smuzhiyun return -EIO; /* The minimum scsi command length is 6 bytes. */
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun buf += SZ_SG_HEADER;
635*4882a593Smuzhiyun if (get_user(opcode, buf))
636*4882a593Smuzhiyun return -EFAULT;
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun if (!(srp = sg_add_request(sfp))) {
639*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sdp,
640*4882a593Smuzhiyun "sg_write: queue full\n"));
641*4882a593Smuzhiyun return -EDOM;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun mutex_lock(&sfp->f_mutex);
644*4882a593Smuzhiyun if (sfp->next_cmd_len > 0) {
645*4882a593Smuzhiyun cmd_size = sfp->next_cmd_len;
646*4882a593Smuzhiyun sfp->next_cmd_len = 0; /* reset so only this write() effected */
647*4882a593Smuzhiyun } else {
648*4882a593Smuzhiyun cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
649*4882a593Smuzhiyun if ((opcode >= 0xc0) && old_hdr.twelve_byte)
650*4882a593Smuzhiyun cmd_size = 12;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
653*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
654*4882a593Smuzhiyun "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
655*4882a593Smuzhiyun /* Determine buffer size. */
656*4882a593Smuzhiyun input_size = count - cmd_size;
657*4882a593Smuzhiyun mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
658*4882a593Smuzhiyun mxsize -= SZ_SG_HEADER;
659*4882a593Smuzhiyun input_size -= SZ_SG_HEADER;
660*4882a593Smuzhiyun if (input_size < 0) {
661*4882a593Smuzhiyun sg_remove_request(sfp, srp);
662*4882a593Smuzhiyun return -EIO; /* User did not pass enough bytes for this command. */
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun hp = &srp->header;
665*4882a593Smuzhiyun hp->interface_id = '\0'; /* indicator of old interface tunnelled */
666*4882a593Smuzhiyun hp->cmd_len = (unsigned char) cmd_size;
667*4882a593Smuzhiyun hp->iovec_count = 0;
668*4882a593Smuzhiyun hp->mx_sb_len = 0;
669*4882a593Smuzhiyun if (input_size > 0)
670*4882a593Smuzhiyun hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
671*4882a593Smuzhiyun SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
672*4882a593Smuzhiyun else
673*4882a593Smuzhiyun hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
674*4882a593Smuzhiyun hp->dxfer_len = mxsize;
675*4882a593Smuzhiyun if ((hp->dxfer_direction == SG_DXFER_TO_DEV) ||
676*4882a593Smuzhiyun (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV))
677*4882a593Smuzhiyun hp->dxferp = (char __user *)buf + cmd_size;
678*4882a593Smuzhiyun else
679*4882a593Smuzhiyun hp->dxferp = NULL;
680*4882a593Smuzhiyun hp->sbp = NULL;
681*4882a593Smuzhiyun hp->timeout = old_hdr.reply_len; /* structure abuse ... */
682*4882a593Smuzhiyun hp->flags = input_size; /* structure abuse ... */
683*4882a593Smuzhiyun hp->pack_id = old_hdr.pack_id;
684*4882a593Smuzhiyun hp->usr_ptr = NULL;
685*4882a593Smuzhiyun if (copy_from_user(cmnd, buf, cmd_size)) {
686*4882a593Smuzhiyun sg_remove_request(sfp, srp);
687*4882a593Smuzhiyun return -EFAULT;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun /*
690*4882a593Smuzhiyun * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
691*4882a593Smuzhiyun * but is is possible that the app intended SG_DXFER_TO_DEV, because there
692*4882a593Smuzhiyun * is a non-zero input_size, so emit a warning.
693*4882a593Smuzhiyun */
694*4882a593Smuzhiyun if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
695*4882a593Smuzhiyun printk_ratelimited(KERN_WARNING
696*4882a593Smuzhiyun "sg_write: data in/out %d/%d bytes "
697*4882a593Smuzhiyun "for SCSI command 0x%x-- guessing "
698*4882a593Smuzhiyun "data in;\n program %s not setting "
699*4882a593Smuzhiyun "count and/or reply_len properly\n",
700*4882a593Smuzhiyun old_hdr.reply_len - (int)SZ_SG_HEADER,
701*4882a593Smuzhiyun input_size, (unsigned int) cmnd[0],
702*4882a593Smuzhiyun current->comm);
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
705*4882a593Smuzhiyun return (k < 0) ? k : count;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun static ssize_t
sg_new_write(Sg_fd * sfp,struct file * file,const char __user * buf,size_t count,int blocking,int read_only,int sg_io_owned,Sg_request ** o_srp)709*4882a593Smuzhiyun sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
710*4882a593Smuzhiyun size_t count, int blocking, int read_only, int sg_io_owned,
711*4882a593Smuzhiyun Sg_request **o_srp)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun int k;
714*4882a593Smuzhiyun Sg_request *srp;
715*4882a593Smuzhiyun sg_io_hdr_t *hp;
716*4882a593Smuzhiyun unsigned char cmnd[SG_MAX_CDB_SIZE];
717*4882a593Smuzhiyun int timeout;
718*4882a593Smuzhiyun unsigned long ul_timeout;
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun if (count < SZ_SG_IO_HDR)
721*4882a593Smuzhiyun return -EINVAL;
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
724*4882a593Smuzhiyun if (!(srp = sg_add_request(sfp))) {
725*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
726*4882a593Smuzhiyun "sg_new_write: queue full\n"));
727*4882a593Smuzhiyun return -EDOM;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun srp->sg_io_owned = sg_io_owned;
730*4882a593Smuzhiyun hp = &srp->header;
731*4882a593Smuzhiyun if (get_sg_io_hdr(hp, buf)) {
732*4882a593Smuzhiyun sg_remove_request(sfp, srp);
733*4882a593Smuzhiyun return -EFAULT;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun if (hp->interface_id != 'S') {
736*4882a593Smuzhiyun sg_remove_request(sfp, srp);
737*4882a593Smuzhiyun return -ENOSYS;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun if (hp->flags & SG_FLAG_MMAP_IO) {
740*4882a593Smuzhiyun if (hp->dxfer_len > sfp->reserve.bufflen) {
741*4882a593Smuzhiyun sg_remove_request(sfp, srp);
742*4882a593Smuzhiyun return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun if (hp->flags & SG_FLAG_DIRECT_IO) {
745*4882a593Smuzhiyun sg_remove_request(sfp, srp);
746*4882a593Smuzhiyun return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun if (sfp->res_in_use) {
749*4882a593Smuzhiyun sg_remove_request(sfp, srp);
750*4882a593Smuzhiyun return -EBUSY; /* reserve buffer already being used */
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun ul_timeout = msecs_to_jiffies(srp->header.timeout);
754*4882a593Smuzhiyun timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
755*4882a593Smuzhiyun if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
756*4882a593Smuzhiyun sg_remove_request(sfp, srp);
757*4882a593Smuzhiyun return -EMSGSIZE;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun if (copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
760*4882a593Smuzhiyun sg_remove_request(sfp, srp);
761*4882a593Smuzhiyun return -EFAULT;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun if (read_only && sg_allow_access(file, cmnd)) {
764*4882a593Smuzhiyun sg_remove_request(sfp, srp);
765*4882a593Smuzhiyun return -EPERM;
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
768*4882a593Smuzhiyun if (k < 0)
769*4882a593Smuzhiyun return k;
770*4882a593Smuzhiyun if (o_srp)
771*4882a593Smuzhiyun *o_srp = srp;
772*4882a593Smuzhiyun return count;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun static int
sg_common_write(Sg_fd * sfp,Sg_request * srp,unsigned char * cmnd,int timeout,int blocking)776*4882a593Smuzhiyun sg_common_write(Sg_fd * sfp, Sg_request * srp,
777*4882a593Smuzhiyun unsigned char *cmnd, int timeout, int blocking)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun int k, at_head;
780*4882a593Smuzhiyun Sg_device *sdp = sfp->parentdp;
781*4882a593Smuzhiyun sg_io_hdr_t *hp = &srp->header;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
784*4882a593Smuzhiyun hp->status = 0;
785*4882a593Smuzhiyun hp->masked_status = 0;
786*4882a593Smuzhiyun hp->msg_status = 0;
787*4882a593Smuzhiyun hp->info = 0;
788*4882a593Smuzhiyun hp->host_status = 0;
789*4882a593Smuzhiyun hp->driver_status = 0;
790*4882a593Smuzhiyun hp->resid = 0;
791*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
792*4882a593Smuzhiyun "sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
793*4882a593Smuzhiyun (int) cmnd[0], (int) hp->cmd_len));
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun if (hp->dxfer_len >= SZ_256M) {
796*4882a593Smuzhiyun sg_remove_request(sfp, srp);
797*4882a593Smuzhiyun return -EINVAL;
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun k = sg_start_req(srp, cmnd);
801*4882a593Smuzhiyun if (k) {
802*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
803*4882a593Smuzhiyun "sg_common_write: start_req err=%d\n", k));
804*4882a593Smuzhiyun sg_finish_rem_req(srp);
805*4882a593Smuzhiyun sg_remove_request(sfp, srp);
806*4882a593Smuzhiyun return k; /* probably out of space --> ENOMEM */
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun if (atomic_read(&sdp->detaching)) {
809*4882a593Smuzhiyun if (srp->bio) {
810*4882a593Smuzhiyun scsi_req_free_cmd(scsi_req(srp->rq));
811*4882a593Smuzhiyun blk_put_request(srp->rq);
812*4882a593Smuzhiyun srp->rq = NULL;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun sg_finish_rem_req(srp);
816*4882a593Smuzhiyun sg_remove_request(sfp, srp);
817*4882a593Smuzhiyun return -ENODEV;
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun hp->duration = jiffies_to_msecs(jiffies);
821*4882a593Smuzhiyun if (hp->interface_id != '\0' && /* v3 (or later) interface */
822*4882a593Smuzhiyun (SG_FLAG_Q_AT_TAIL & hp->flags))
823*4882a593Smuzhiyun at_head = 0;
824*4882a593Smuzhiyun else
825*4882a593Smuzhiyun at_head = 1;
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun srp->rq->timeout = timeout;
828*4882a593Smuzhiyun kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
829*4882a593Smuzhiyun blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
830*4882a593Smuzhiyun srp->rq, at_head, sg_rq_end_io);
831*4882a593Smuzhiyun return 0;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
srp_done(Sg_fd * sfp,Sg_request * srp)834*4882a593Smuzhiyun static int srp_done(Sg_fd *sfp, Sg_request *srp)
835*4882a593Smuzhiyun {
836*4882a593Smuzhiyun unsigned long flags;
837*4882a593Smuzhiyun int ret;
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun read_lock_irqsave(&sfp->rq_list_lock, flags);
840*4882a593Smuzhiyun ret = srp->done;
841*4882a593Smuzhiyun read_unlock_irqrestore(&sfp->rq_list_lock, flags);
842*4882a593Smuzhiyun return ret;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun
max_sectors_bytes(struct request_queue * q)845*4882a593Smuzhiyun static int max_sectors_bytes(struct request_queue *q)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun unsigned int max_sectors = queue_max_sectors(q);
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun return max_sectors << 9;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun static void
sg_fill_request_table(Sg_fd * sfp,sg_req_info_t * rinfo)855*4882a593Smuzhiyun sg_fill_request_table(Sg_fd *sfp, sg_req_info_t *rinfo)
856*4882a593Smuzhiyun {
857*4882a593Smuzhiyun Sg_request *srp;
858*4882a593Smuzhiyun int val;
859*4882a593Smuzhiyun unsigned int ms;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun val = 0;
862*4882a593Smuzhiyun list_for_each_entry(srp, &sfp->rq_list, entry) {
863*4882a593Smuzhiyun if (val >= SG_MAX_QUEUE)
864*4882a593Smuzhiyun break;
865*4882a593Smuzhiyun rinfo[val].req_state = srp->done + 1;
866*4882a593Smuzhiyun rinfo[val].problem =
867*4882a593Smuzhiyun srp->header.masked_status &
868*4882a593Smuzhiyun srp->header.host_status &
869*4882a593Smuzhiyun srp->header.driver_status;
870*4882a593Smuzhiyun if (srp->done)
871*4882a593Smuzhiyun rinfo[val].duration =
872*4882a593Smuzhiyun srp->header.duration;
873*4882a593Smuzhiyun else {
874*4882a593Smuzhiyun ms = jiffies_to_msecs(jiffies);
875*4882a593Smuzhiyun rinfo[val].duration =
876*4882a593Smuzhiyun (ms > srp->header.duration) ?
877*4882a593Smuzhiyun (ms - srp->header.duration) : 0;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun rinfo[val].orphan = srp->orphan;
880*4882a593Smuzhiyun rinfo[val].sg_io_owned = srp->sg_io_owned;
881*4882a593Smuzhiyun rinfo[val].pack_id = srp->header.pack_id;
882*4882a593Smuzhiyun rinfo[val].usr_ptr = srp->header.usr_ptr;
883*4882a593Smuzhiyun val++;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
888*4882a593Smuzhiyun struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
889*4882a593Smuzhiyun char req_state;
890*4882a593Smuzhiyun char orphan;
891*4882a593Smuzhiyun char sg_io_owned;
892*4882a593Smuzhiyun char problem;
893*4882a593Smuzhiyun int pack_id;
894*4882a593Smuzhiyun compat_uptr_t usr_ptr;
895*4882a593Smuzhiyun unsigned int duration;
896*4882a593Smuzhiyun int unused;
897*4882a593Smuzhiyun };
898*4882a593Smuzhiyun
put_compat_request_table(struct compat_sg_req_info __user * o,struct sg_req_info * rinfo)899*4882a593Smuzhiyun static int put_compat_request_table(struct compat_sg_req_info __user *o,
900*4882a593Smuzhiyun struct sg_req_info *rinfo)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun int i;
903*4882a593Smuzhiyun for (i = 0; i < SG_MAX_QUEUE; i++) {
904*4882a593Smuzhiyun if (copy_to_user(o + i, rinfo + i, offsetof(sg_req_info_t, usr_ptr)) ||
905*4882a593Smuzhiyun put_user((uintptr_t)rinfo[i].usr_ptr, &o[i].usr_ptr) ||
906*4882a593Smuzhiyun put_user(rinfo[i].duration, &o[i].duration) ||
907*4882a593Smuzhiyun put_user(rinfo[i].unused, &o[i].unused))
908*4882a593Smuzhiyun return -EFAULT;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun return 0;
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun #endif
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun static long
sg_ioctl_common(struct file * filp,Sg_device * sdp,Sg_fd * sfp,unsigned int cmd_in,void __user * p)915*4882a593Smuzhiyun sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,
916*4882a593Smuzhiyun unsigned int cmd_in, void __user *p)
917*4882a593Smuzhiyun {
918*4882a593Smuzhiyun int __user *ip = p;
919*4882a593Smuzhiyun int result, val, read_only;
920*4882a593Smuzhiyun Sg_request *srp;
921*4882a593Smuzhiyun unsigned long iflags;
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
924*4882a593Smuzhiyun "sg_ioctl: cmd=0x%x\n", (int) cmd_in));
925*4882a593Smuzhiyun read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun switch (cmd_in) {
928*4882a593Smuzhiyun case SG_IO:
929*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
930*4882a593Smuzhiyun return -ENODEV;
931*4882a593Smuzhiyun if (!scsi_block_when_processing_errors(sdp->device))
932*4882a593Smuzhiyun return -ENXIO;
933*4882a593Smuzhiyun result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
934*4882a593Smuzhiyun 1, read_only, 1, &srp);
935*4882a593Smuzhiyun if (result < 0)
936*4882a593Smuzhiyun return result;
937*4882a593Smuzhiyun result = wait_event_interruptible(sfp->read_wait,
938*4882a593Smuzhiyun srp_done(sfp, srp));
939*4882a593Smuzhiyun write_lock_irq(&sfp->rq_list_lock);
940*4882a593Smuzhiyun if (srp->done) {
941*4882a593Smuzhiyun srp->done = 2;
942*4882a593Smuzhiyun write_unlock_irq(&sfp->rq_list_lock);
943*4882a593Smuzhiyun result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
944*4882a593Smuzhiyun return (result < 0) ? result : 0;
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun srp->orphan = 1;
947*4882a593Smuzhiyun write_unlock_irq(&sfp->rq_list_lock);
948*4882a593Smuzhiyun return result; /* -ERESTARTSYS because signal hit process */
949*4882a593Smuzhiyun case SG_SET_TIMEOUT:
950*4882a593Smuzhiyun result = get_user(val, ip);
951*4882a593Smuzhiyun if (result)
952*4882a593Smuzhiyun return result;
953*4882a593Smuzhiyun if (val < 0)
954*4882a593Smuzhiyun return -EIO;
955*4882a593Smuzhiyun if (val >= mult_frac((s64)INT_MAX, USER_HZ, HZ))
956*4882a593Smuzhiyun val = min_t(s64, mult_frac((s64)INT_MAX, USER_HZ, HZ),
957*4882a593Smuzhiyun INT_MAX);
958*4882a593Smuzhiyun sfp->timeout_user = val;
959*4882a593Smuzhiyun sfp->timeout = mult_frac(val, HZ, USER_HZ);
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun return 0;
962*4882a593Smuzhiyun case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
963*4882a593Smuzhiyun /* strange ..., for backward compatibility */
964*4882a593Smuzhiyun return sfp->timeout_user;
965*4882a593Smuzhiyun case SG_SET_FORCE_LOW_DMA:
966*4882a593Smuzhiyun /*
967*4882a593Smuzhiyun * N.B. This ioctl never worked properly, but failed to
968*4882a593Smuzhiyun * return an error value. So returning '0' to keep compability
969*4882a593Smuzhiyun * with legacy applications.
970*4882a593Smuzhiyun */
971*4882a593Smuzhiyun return 0;
972*4882a593Smuzhiyun case SG_GET_LOW_DMA:
973*4882a593Smuzhiyun return put_user((int) sdp->device->host->unchecked_isa_dma, ip);
974*4882a593Smuzhiyun case SG_GET_SCSI_ID:
975*4882a593Smuzhiyun {
976*4882a593Smuzhiyun sg_scsi_id_t v;
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
979*4882a593Smuzhiyun return -ENODEV;
980*4882a593Smuzhiyun memset(&v, 0, sizeof(v));
981*4882a593Smuzhiyun v.host_no = sdp->device->host->host_no;
982*4882a593Smuzhiyun v.channel = sdp->device->channel;
983*4882a593Smuzhiyun v.scsi_id = sdp->device->id;
984*4882a593Smuzhiyun v.lun = sdp->device->lun;
985*4882a593Smuzhiyun v.scsi_type = sdp->device->type;
986*4882a593Smuzhiyun v.h_cmd_per_lun = sdp->device->host->cmd_per_lun;
987*4882a593Smuzhiyun v.d_queue_depth = sdp->device->queue_depth;
988*4882a593Smuzhiyun if (copy_to_user(p, &v, sizeof(sg_scsi_id_t)))
989*4882a593Smuzhiyun return -EFAULT;
990*4882a593Smuzhiyun return 0;
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun case SG_SET_FORCE_PACK_ID:
993*4882a593Smuzhiyun result = get_user(val, ip);
994*4882a593Smuzhiyun if (result)
995*4882a593Smuzhiyun return result;
996*4882a593Smuzhiyun sfp->force_packid = val ? 1 : 0;
997*4882a593Smuzhiyun return 0;
998*4882a593Smuzhiyun case SG_GET_PACK_ID:
999*4882a593Smuzhiyun read_lock_irqsave(&sfp->rq_list_lock, iflags);
1000*4882a593Smuzhiyun list_for_each_entry(srp, &sfp->rq_list, entry) {
1001*4882a593Smuzhiyun if ((1 == srp->done) && (!srp->sg_io_owned)) {
1002*4882a593Smuzhiyun read_unlock_irqrestore(&sfp->rq_list_lock,
1003*4882a593Smuzhiyun iflags);
1004*4882a593Smuzhiyun return put_user(srp->header.pack_id, ip);
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1008*4882a593Smuzhiyun return put_user(-1, ip);
1009*4882a593Smuzhiyun case SG_GET_NUM_WAITING:
1010*4882a593Smuzhiyun read_lock_irqsave(&sfp->rq_list_lock, iflags);
1011*4882a593Smuzhiyun val = 0;
1012*4882a593Smuzhiyun list_for_each_entry(srp, &sfp->rq_list, entry) {
1013*4882a593Smuzhiyun if ((1 == srp->done) && (!srp->sg_io_owned))
1014*4882a593Smuzhiyun ++val;
1015*4882a593Smuzhiyun }
1016*4882a593Smuzhiyun read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1017*4882a593Smuzhiyun return put_user(val, ip);
1018*4882a593Smuzhiyun case SG_GET_SG_TABLESIZE:
1019*4882a593Smuzhiyun return put_user(sdp->sg_tablesize, ip);
1020*4882a593Smuzhiyun case SG_SET_RESERVED_SIZE:
1021*4882a593Smuzhiyun result = get_user(val, ip);
1022*4882a593Smuzhiyun if (result)
1023*4882a593Smuzhiyun return result;
1024*4882a593Smuzhiyun if (val < 0)
1025*4882a593Smuzhiyun return -EINVAL;
1026*4882a593Smuzhiyun val = min_t(int, val,
1027*4882a593Smuzhiyun max_sectors_bytes(sdp->device->request_queue));
1028*4882a593Smuzhiyun mutex_lock(&sfp->f_mutex);
1029*4882a593Smuzhiyun if (val != sfp->reserve.bufflen) {
1030*4882a593Smuzhiyun if (sfp->mmap_called ||
1031*4882a593Smuzhiyun sfp->res_in_use) {
1032*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
1033*4882a593Smuzhiyun return -EBUSY;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun sg_remove_scat(sfp, &sfp->reserve);
1037*4882a593Smuzhiyun sg_build_reserve(sfp, val);
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
1040*4882a593Smuzhiyun return 0;
1041*4882a593Smuzhiyun case SG_GET_RESERVED_SIZE:
1042*4882a593Smuzhiyun val = min_t(int, sfp->reserve.bufflen,
1043*4882a593Smuzhiyun max_sectors_bytes(sdp->device->request_queue));
1044*4882a593Smuzhiyun return put_user(val, ip);
1045*4882a593Smuzhiyun case SG_SET_COMMAND_Q:
1046*4882a593Smuzhiyun result = get_user(val, ip);
1047*4882a593Smuzhiyun if (result)
1048*4882a593Smuzhiyun return result;
1049*4882a593Smuzhiyun sfp->cmd_q = val ? 1 : 0;
1050*4882a593Smuzhiyun return 0;
1051*4882a593Smuzhiyun case SG_GET_COMMAND_Q:
1052*4882a593Smuzhiyun return put_user((int) sfp->cmd_q, ip);
1053*4882a593Smuzhiyun case SG_SET_KEEP_ORPHAN:
1054*4882a593Smuzhiyun result = get_user(val, ip);
1055*4882a593Smuzhiyun if (result)
1056*4882a593Smuzhiyun return result;
1057*4882a593Smuzhiyun sfp->keep_orphan = val;
1058*4882a593Smuzhiyun return 0;
1059*4882a593Smuzhiyun case SG_GET_KEEP_ORPHAN:
1060*4882a593Smuzhiyun return put_user((int) sfp->keep_orphan, ip);
1061*4882a593Smuzhiyun case SG_NEXT_CMD_LEN:
1062*4882a593Smuzhiyun result = get_user(val, ip);
1063*4882a593Smuzhiyun if (result)
1064*4882a593Smuzhiyun return result;
1065*4882a593Smuzhiyun if (val > SG_MAX_CDB_SIZE)
1066*4882a593Smuzhiyun return -ENOMEM;
1067*4882a593Smuzhiyun sfp->next_cmd_len = (val > 0) ? val : 0;
1068*4882a593Smuzhiyun return 0;
1069*4882a593Smuzhiyun case SG_GET_VERSION_NUM:
1070*4882a593Smuzhiyun return put_user(sg_version_num, ip);
1071*4882a593Smuzhiyun case SG_GET_ACCESS_COUNT:
1072*4882a593Smuzhiyun /* faked - we don't have a real access count anymore */
1073*4882a593Smuzhiyun val = (sdp->device ? 1 : 0);
1074*4882a593Smuzhiyun return put_user(val, ip);
1075*4882a593Smuzhiyun case SG_GET_REQUEST_TABLE:
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun sg_req_info_t *rinfo;
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun rinfo = kcalloc(SG_MAX_QUEUE, SZ_SG_REQ_INFO,
1080*4882a593Smuzhiyun GFP_KERNEL);
1081*4882a593Smuzhiyun if (!rinfo)
1082*4882a593Smuzhiyun return -ENOMEM;
1083*4882a593Smuzhiyun read_lock_irqsave(&sfp->rq_list_lock, iflags);
1084*4882a593Smuzhiyun sg_fill_request_table(sfp, rinfo);
1085*4882a593Smuzhiyun read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1086*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
1087*4882a593Smuzhiyun if (in_compat_syscall())
1088*4882a593Smuzhiyun result = put_compat_request_table(p, rinfo);
1089*4882a593Smuzhiyun else
1090*4882a593Smuzhiyun #endif
1091*4882a593Smuzhiyun result = copy_to_user(p, rinfo,
1092*4882a593Smuzhiyun SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1093*4882a593Smuzhiyun result = result ? -EFAULT : 0;
1094*4882a593Smuzhiyun kfree(rinfo);
1095*4882a593Smuzhiyun return result;
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun case SG_EMULATED_HOST:
1098*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
1099*4882a593Smuzhiyun return -ENODEV;
1100*4882a593Smuzhiyun return put_user(sdp->device->host->hostt->emulated, ip);
1101*4882a593Smuzhiyun case SCSI_IOCTL_SEND_COMMAND:
1102*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
1103*4882a593Smuzhiyun return -ENODEV;
1104*4882a593Smuzhiyun return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
1105*4882a593Smuzhiyun case SG_SET_DEBUG:
1106*4882a593Smuzhiyun result = get_user(val, ip);
1107*4882a593Smuzhiyun if (result)
1108*4882a593Smuzhiyun return result;
1109*4882a593Smuzhiyun sdp->sgdebug = (char) val;
1110*4882a593Smuzhiyun return 0;
1111*4882a593Smuzhiyun case BLKSECTGET:
1112*4882a593Smuzhiyun return put_user(max_sectors_bytes(sdp->device->request_queue),
1113*4882a593Smuzhiyun ip);
1114*4882a593Smuzhiyun case BLKTRACESETUP:
1115*4882a593Smuzhiyun return blk_trace_setup(sdp->device->request_queue,
1116*4882a593Smuzhiyun sdp->disk->disk_name,
1117*4882a593Smuzhiyun MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
1118*4882a593Smuzhiyun NULL, p);
1119*4882a593Smuzhiyun case BLKTRACESTART:
1120*4882a593Smuzhiyun return blk_trace_startstop(sdp->device->request_queue, 1);
1121*4882a593Smuzhiyun case BLKTRACESTOP:
1122*4882a593Smuzhiyun return blk_trace_startstop(sdp->device->request_queue, 0);
1123*4882a593Smuzhiyun case BLKTRACETEARDOWN:
1124*4882a593Smuzhiyun return blk_trace_remove(sdp->device->request_queue);
1125*4882a593Smuzhiyun case SCSI_IOCTL_GET_IDLUN:
1126*4882a593Smuzhiyun case SCSI_IOCTL_GET_BUS_NUMBER:
1127*4882a593Smuzhiyun case SCSI_IOCTL_PROBE_HOST:
1128*4882a593Smuzhiyun case SG_GET_TRANSFORM:
1129*4882a593Smuzhiyun case SG_SCSI_RESET:
1130*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
1131*4882a593Smuzhiyun return -ENODEV;
1132*4882a593Smuzhiyun break;
1133*4882a593Smuzhiyun default:
1134*4882a593Smuzhiyun if (read_only)
1135*4882a593Smuzhiyun return -EPERM; /* don't know so take safe approach */
1136*4882a593Smuzhiyun break;
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun result = scsi_ioctl_block_when_processing_errors(sdp->device,
1140*4882a593Smuzhiyun cmd_in, filp->f_flags & O_NDELAY);
1141*4882a593Smuzhiyun if (result)
1142*4882a593Smuzhiyun return result;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun return -ENOIOCTLCMD;
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun static long
sg_ioctl(struct file * filp,unsigned int cmd_in,unsigned long arg)1148*4882a593Smuzhiyun sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun void __user *p = (void __user *)arg;
1151*4882a593Smuzhiyun Sg_device *sdp;
1152*4882a593Smuzhiyun Sg_fd *sfp;
1153*4882a593Smuzhiyun int ret;
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1156*4882a593Smuzhiyun return -ENXIO;
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun ret = sg_ioctl_common(filp, sdp, sfp, cmd_in, p);
1159*4882a593Smuzhiyun if (ret != -ENOIOCTLCMD)
1160*4882a593Smuzhiyun return ret;
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun return scsi_ioctl(sdp->device, cmd_in, p);
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
sg_compat_ioctl(struct file * filp,unsigned int cmd_in,unsigned long arg)1166*4882a593Smuzhiyun static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1167*4882a593Smuzhiyun {
1168*4882a593Smuzhiyun void __user *p = compat_ptr(arg);
1169*4882a593Smuzhiyun Sg_device *sdp;
1170*4882a593Smuzhiyun Sg_fd *sfp;
1171*4882a593Smuzhiyun int ret;
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1174*4882a593Smuzhiyun return -ENXIO;
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun ret = sg_ioctl_common(filp, sdp, sfp, cmd_in, p);
1177*4882a593Smuzhiyun if (ret != -ENOIOCTLCMD)
1178*4882a593Smuzhiyun return ret;
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun return scsi_compat_ioctl(sdp->device, cmd_in, p);
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun #endif
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun static __poll_t
sg_poll(struct file * filp,poll_table * wait)1185*4882a593Smuzhiyun sg_poll(struct file *filp, poll_table * wait)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun __poll_t res = 0;
1188*4882a593Smuzhiyun Sg_device *sdp;
1189*4882a593Smuzhiyun Sg_fd *sfp;
1190*4882a593Smuzhiyun Sg_request *srp;
1191*4882a593Smuzhiyun int count = 0;
1192*4882a593Smuzhiyun unsigned long iflags;
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun sfp = filp->private_data;
1195*4882a593Smuzhiyun if (!sfp)
1196*4882a593Smuzhiyun return EPOLLERR;
1197*4882a593Smuzhiyun sdp = sfp->parentdp;
1198*4882a593Smuzhiyun if (!sdp)
1199*4882a593Smuzhiyun return EPOLLERR;
1200*4882a593Smuzhiyun poll_wait(filp, &sfp->read_wait, wait);
1201*4882a593Smuzhiyun read_lock_irqsave(&sfp->rq_list_lock, iflags);
1202*4882a593Smuzhiyun list_for_each_entry(srp, &sfp->rq_list, entry) {
1203*4882a593Smuzhiyun /* if any read waiting, flag it */
1204*4882a593Smuzhiyun if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1205*4882a593Smuzhiyun res = EPOLLIN | EPOLLRDNORM;
1206*4882a593Smuzhiyun ++count;
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
1211*4882a593Smuzhiyun res |= EPOLLHUP;
1212*4882a593Smuzhiyun else if (!sfp->cmd_q) {
1213*4882a593Smuzhiyun if (0 == count)
1214*4882a593Smuzhiyun res |= EPOLLOUT | EPOLLWRNORM;
1215*4882a593Smuzhiyun } else if (count < SG_MAX_QUEUE)
1216*4882a593Smuzhiyun res |= EPOLLOUT | EPOLLWRNORM;
1217*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
1218*4882a593Smuzhiyun "sg_poll: res=0x%x\n", (__force u32) res));
1219*4882a593Smuzhiyun return res;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun static int
sg_fasync(int fd,struct file * filp,int mode)1223*4882a593Smuzhiyun sg_fasync(int fd, struct file *filp, int mode)
1224*4882a593Smuzhiyun {
1225*4882a593Smuzhiyun Sg_device *sdp;
1226*4882a593Smuzhiyun Sg_fd *sfp;
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1229*4882a593Smuzhiyun return -ENXIO;
1230*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
1231*4882a593Smuzhiyun "sg_fasync: mode=%d\n", mode));
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun return fasync_helper(fd, filp, mode, &sfp->async_qp);
1234*4882a593Smuzhiyun }
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun static vm_fault_t
sg_vma_fault(struct vm_fault * vmf)1237*4882a593Smuzhiyun sg_vma_fault(struct vm_fault *vmf)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun struct vm_area_struct *vma = vmf->vma;
1240*4882a593Smuzhiyun Sg_fd *sfp;
1241*4882a593Smuzhiyun unsigned long offset, len, sa;
1242*4882a593Smuzhiyun Sg_scatter_hold *rsv_schp;
1243*4882a593Smuzhiyun int k, length;
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1246*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
1247*4882a593Smuzhiyun rsv_schp = &sfp->reserve;
1248*4882a593Smuzhiyun offset = vmf->pgoff << PAGE_SHIFT;
1249*4882a593Smuzhiyun if (offset >= rsv_schp->bufflen)
1250*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
1251*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
1252*4882a593Smuzhiyun "sg_vma_fault: offset=%lu, scatg=%d\n",
1253*4882a593Smuzhiyun offset, rsv_schp->k_use_sg));
1254*4882a593Smuzhiyun sa = vma->vm_start;
1255*4882a593Smuzhiyun length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1256*4882a593Smuzhiyun for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
1257*4882a593Smuzhiyun len = vma->vm_end - sa;
1258*4882a593Smuzhiyun len = (len < length) ? len : length;
1259*4882a593Smuzhiyun if (offset < len) {
1260*4882a593Smuzhiyun struct page *page = nth_page(rsv_schp->pages[k],
1261*4882a593Smuzhiyun offset >> PAGE_SHIFT);
1262*4882a593Smuzhiyun get_page(page); /* increment page count */
1263*4882a593Smuzhiyun vmf->page = page;
1264*4882a593Smuzhiyun return 0; /* success */
1265*4882a593Smuzhiyun }
1266*4882a593Smuzhiyun sa += len;
1267*4882a593Smuzhiyun offset -= len;
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun static const struct vm_operations_struct sg_mmap_vm_ops = {
1274*4882a593Smuzhiyun .fault = sg_vma_fault,
1275*4882a593Smuzhiyun };
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun static int
sg_mmap(struct file * filp,struct vm_area_struct * vma)1278*4882a593Smuzhiyun sg_mmap(struct file *filp, struct vm_area_struct *vma)
1279*4882a593Smuzhiyun {
1280*4882a593Smuzhiyun Sg_fd *sfp;
1281*4882a593Smuzhiyun unsigned long req_sz, len, sa;
1282*4882a593Smuzhiyun Sg_scatter_hold *rsv_schp;
1283*4882a593Smuzhiyun int k, length;
1284*4882a593Smuzhiyun int ret = 0;
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1287*4882a593Smuzhiyun return -ENXIO;
1288*4882a593Smuzhiyun req_sz = vma->vm_end - vma->vm_start;
1289*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
1290*4882a593Smuzhiyun "sg_mmap starting, vm_start=%p, len=%d\n",
1291*4882a593Smuzhiyun (void *) vma->vm_start, (int) req_sz));
1292*4882a593Smuzhiyun if (vma->vm_pgoff)
1293*4882a593Smuzhiyun return -EINVAL; /* want no offset */
1294*4882a593Smuzhiyun rsv_schp = &sfp->reserve;
1295*4882a593Smuzhiyun mutex_lock(&sfp->f_mutex);
1296*4882a593Smuzhiyun if (req_sz > rsv_schp->bufflen) {
1297*4882a593Smuzhiyun ret = -ENOMEM; /* cannot map more than reserved buffer */
1298*4882a593Smuzhiyun goto out;
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun sa = vma->vm_start;
1302*4882a593Smuzhiyun length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1303*4882a593Smuzhiyun for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
1304*4882a593Smuzhiyun len = vma->vm_end - sa;
1305*4882a593Smuzhiyun len = (len < length) ? len : length;
1306*4882a593Smuzhiyun sa += len;
1307*4882a593Smuzhiyun }
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun sfp->mmap_called = 1;
1310*4882a593Smuzhiyun vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1311*4882a593Smuzhiyun vma->vm_private_data = sfp;
1312*4882a593Smuzhiyun vma->vm_ops = &sg_mmap_vm_ops;
1313*4882a593Smuzhiyun out:
1314*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
1315*4882a593Smuzhiyun return ret;
1316*4882a593Smuzhiyun }
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun static void
sg_rq_end_io_usercontext(struct work_struct * work)1319*4882a593Smuzhiyun sg_rq_end_io_usercontext(struct work_struct *work)
1320*4882a593Smuzhiyun {
1321*4882a593Smuzhiyun struct sg_request *srp = container_of(work, struct sg_request, ew.work);
1322*4882a593Smuzhiyun struct sg_fd *sfp = srp->parentfp;
1323*4882a593Smuzhiyun
1324*4882a593Smuzhiyun sg_finish_rem_req(srp);
1325*4882a593Smuzhiyun sg_remove_request(sfp, srp);
1326*4882a593Smuzhiyun kref_put(&sfp->f_ref, sg_remove_sfp);
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun /*
1330*4882a593Smuzhiyun * This function is a "bottom half" handler that is called by the mid
1331*4882a593Smuzhiyun * level when a command is completed (or has failed).
1332*4882a593Smuzhiyun */
1333*4882a593Smuzhiyun static void
sg_rq_end_io(struct request * rq,blk_status_t status)1334*4882a593Smuzhiyun sg_rq_end_io(struct request *rq, blk_status_t status)
1335*4882a593Smuzhiyun {
1336*4882a593Smuzhiyun struct sg_request *srp = rq->end_io_data;
1337*4882a593Smuzhiyun struct scsi_request *req = scsi_req(rq);
1338*4882a593Smuzhiyun Sg_device *sdp;
1339*4882a593Smuzhiyun Sg_fd *sfp;
1340*4882a593Smuzhiyun unsigned long iflags;
1341*4882a593Smuzhiyun unsigned int ms;
1342*4882a593Smuzhiyun char *sense;
1343*4882a593Smuzhiyun int result, resid, done = 1;
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun if (WARN_ON(srp->done != 0))
1346*4882a593Smuzhiyun return;
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun sfp = srp->parentfp;
1349*4882a593Smuzhiyun if (WARN_ON(sfp == NULL))
1350*4882a593Smuzhiyun return;
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun sdp = sfp->parentdp;
1353*4882a593Smuzhiyun if (unlikely(atomic_read(&sdp->detaching)))
1354*4882a593Smuzhiyun pr_info("%s: device detaching\n", __func__);
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun sense = req->sense;
1357*4882a593Smuzhiyun result = req->result;
1358*4882a593Smuzhiyun resid = req->resid_len;
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
1361*4882a593Smuzhiyun "sg_cmd_done: pack_id=%d, res=0x%x\n",
1362*4882a593Smuzhiyun srp->header.pack_id, result));
1363*4882a593Smuzhiyun srp->header.resid = resid;
1364*4882a593Smuzhiyun ms = jiffies_to_msecs(jiffies);
1365*4882a593Smuzhiyun srp->header.duration = (ms > srp->header.duration) ?
1366*4882a593Smuzhiyun (ms - srp->header.duration) : 0;
1367*4882a593Smuzhiyun if (0 != result) {
1368*4882a593Smuzhiyun struct scsi_sense_hdr sshdr;
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun srp->header.status = 0xff & result;
1371*4882a593Smuzhiyun srp->header.masked_status = status_byte(result);
1372*4882a593Smuzhiyun srp->header.msg_status = msg_byte(result);
1373*4882a593Smuzhiyun srp->header.host_status = host_byte(result);
1374*4882a593Smuzhiyun srp->header.driver_status = driver_byte(result);
1375*4882a593Smuzhiyun if ((sdp->sgdebug > 0) &&
1376*4882a593Smuzhiyun ((CHECK_CONDITION == srp->header.masked_status) ||
1377*4882a593Smuzhiyun (COMMAND_TERMINATED == srp->header.masked_status)))
1378*4882a593Smuzhiyun __scsi_print_sense(sdp->device, __func__, sense,
1379*4882a593Smuzhiyun SCSI_SENSE_BUFFERSIZE);
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun /* Following if statement is a patch supplied by Eric Youngdale */
1382*4882a593Smuzhiyun if (driver_byte(result) != 0
1383*4882a593Smuzhiyun && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1384*4882a593Smuzhiyun && !scsi_sense_is_deferred(&sshdr)
1385*4882a593Smuzhiyun && sshdr.sense_key == UNIT_ATTENTION
1386*4882a593Smuzhiyun && sdp->device->removable) {
1387*4882a593Smuzhiyun /* Detected possible disc change. Set the bit - this */
1388*4882a593Smuzhiyun /* may be used if there are filesystems using this device */
1389*4882a593Smuzhiyun sdp->device->changed = 1;
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun }
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun if (req->sense_len)
1394*4882a593Smuzhiyun memcpy(srp->sense_b, req->sense, SCSI_SENSE_BUFFERSIZE);
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun /* Rely on write phase to clean out srp status values, so no "else" */
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun /*
1399*4882a593Smuzhiyun * Free the request as soon as it is complete so that its resources
1400*4882a593Smuzhiyun * can be reused without waiting for userspace to read() the
1401*4882a593Smuzhiyun * result. But keep the associated bio (if any) around until
1402*4882a593Smuzhiyun * blk_rq_unmap_user() can be called from user context.
1403*4882a593Smuzhiyun */
1404*4882a593Smuzhiyun srp->rq = NULL;
1405*4882a593Smuzhiyun scsi_req_free_cmd(scsi_req(rq));
1406*4882a593Smuzhiyun blk_put_request(rq);
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun write_lock_irqsave(&sfp->rq_list_lock, iflags);
1409*4882a593Smuzhiyun if (unlikely(srp->orphan)) {
1410*4882a593Smuzhiyun if (sfp->keep_orphan)
1411*4882a593Smuzhiyun srp->sg_io_owned = 0;
1412*4882a593Smuzhiyun else
1413*4882a593Smuzhiyun done = 0;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun srp->done = done;
1416*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun if (likely(done)) {
1419*4882a593Smuzhiyun /* Now wake up any sg_read() that is waiting for this
1420*4882a593Smuzhiyun * packet.
1421*4882a593Smuzhiyun */
1422*4882a593Smuzhiyun wake_up_interruptible(&sfp->read_wait);
1423*4882a593Smuzhiyun kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1424*4882a593Smuzhiyun kref_put(&sfp->f_ref, sg_remove_sfp);
1425*4882a593Smuzhiyun } else {
1426*4882a593Smuzhiyun INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
1427*4882a593Smuzhiyun schedule_work(&srp->ew.work);
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun static const struct file_operations sg_fops = {
1432*4882a593Smuzhiyun .owner = THIS_MODULE,
1433*4882a593Smuzhiyun .read = sg_read,
1434*4882a593Smuzhiyun .write = sg_write,
1435*4882a593Smuzhiyun .poll = sg_poll,
1436*4882a593Smuzhiyun .unlocked_ioctl = sg_ioctl,
1437*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
1438*4882a593Smuzhiyun .compat_ioctl = sg_compat_ioctl,
1439*4882a593Smuzhiyun #endif
1440*4882a593Smuzhiyun .open = sg_open,
1441*4882a593Smuzhiyun .mmap = sg_mmap,
1442*4882a593Smuzhiyun .release = sg_release,
1443*4882a593Smuzhiyun .fasync = sg_fasync,
1444*4882a593Smuzhiyun .llseek = no_llseek,
1445*4882a593Smuzhiyun };
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun static struct class *sg_sysfs_class;
1448*4882a593Smuzhiyun
1449*4882a593Smuzhiyun static int sg_sysfs_valid = 0;
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun static Sg_device *
sg_alloc(struct gendisk * disk,struct scsi_device * scsidp)1452*4882a593Smuzhiyun sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun struct request_queue *q = scsidp->request_queue;
1455*4882a593Smuzhiyun Sg_device *sdp;
1456*4882a593Smuzhiyun unsigned long iflags;
1457*4882a593Smuzhiyun int error;
1458*4882a593Smuzhiyun u32 k;
1459*4882a593Smuzhiyun
1460*4882a593Smuzhiyun sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1461*4882a593Smuzhiyun if (!sdp) {
1462*4882a593Smuzhiyun sdev_printk(KERN_WARNING, scsidp, "%s: kmalloc Sg_device "
1463*4882a593Smuzhiyun "failure\n", __func__);
1464*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun idr_preload(GFP_KERNEL);
1468*4882a593Smuzhiyun write_lock_irqsave(&sg_index_lock, iflags);
1469*4882a593Smuzhiyun
1470*4882a593Smuzhiyun error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
1471*4882a593Smuzhiyun if (error < 0) {
1472*4882a593Smuzhiyun if (error == -ENOSPC) {
1473*4882a593Smuzhiyun sdev_printk(KERN_WARNING, scsidp,
1474*4882a593Smuzhiyun "Unable to attach sg device type=%d, minor number exceeds %d\n",
1475*4882a593Smuzhiyun scsidp->type, SG_MAX_DEVS - 1);
1476*4882a593Smuzhiyun error = -ENODEV;
1477*4882a593Smuzhiyun } else {
1478*4882a593Smuzhiyun sdev_printk(KERN_WARNING, scsidp, "%s: idr "
1479*4882a593Smuzhiyun "allocation Sg_device failure: %d\n",
1480*4882a593Smuzhiyun __func__, error);
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun goto out_unlock;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun k = error;
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO, scsidp,
1487*4882a593Smuzhiyun "sg_alloc: dev=%d \n", k));
1488*4882a593Smuzhiyun sprintf(disk->disk_name, "sg%d", k);
1489*4882a593Smuzhiyun disk->first_minor = k;
1490*4882a593Smuzhiyun sdp->disk = disk;
1491*4882a593Smuzhiyun sdp->device = scsidp;
1492*4882a593Smuzhiyun mutex_init(&sdp->open_rel_lock);
1493*4882a593Smuzhiyun INIT_LIST_HEAD(&sdp->sfds);
1494*4882a593Smuzhiyun init_waitqueue_head(&sdp->open_wait);
1495*4882a593Smuzhiyun atomic_set(&sdp->detaching, 0);
1496*4882a593Smuzhiyun rwlock_init(&sdp->sfd_lock);
1497*4882a593Smuzhiyun sdp->sg_tablesize = queue_max_segments(q);
1498*4882a593Smuzhiyun sdp->index = k;
1499*4882a593Smuzhiyun kref_init(&sdp->d_ref);
1500*4882a593Smuzhiyun error = 0;
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun out_unlock:
1503*4882a593Smuzhiyun write_unlock_irqrestore(&sg_index_lock, iflags);
1504*4882a593Smuzhiyun idr_preload_end();
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun if (error) {
1507*4882a593Smuzhiyun kfree(sdp);
1508*4882a593Smuzhiyun return ERR_PTR(error);
1509*4882a593Smuzhiyun }
1510*4882a593Smuzhiyun return sdp;
1511*4882a593Smuzhiyun }
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun static int
sg_add_device(struct device * cl_dev,struct class_interface * cl_intf)1514*4882a593Smuzhiyun sg_add_device(struct device *cl_dev, struct class_interface *cl_intf)
1515*4882a593Smuzhiyun {
1516*4882a593Smuzhiyun struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1517*4882a593Smuzhiyun struct gendisk *disk;
1518*4882a593Smuzhiyun Sg_device *sdp = NULL;
1519*4882a593Smuzhiyun struct cdev * cdev = NULL;
1520*4882a593Smuzhiyun int error;
1521*4882a593Smuzhiyun unsigned long iflags;
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun disk = alloc_disk(1);
1524*4882a593Smuzhiyun if (!disk) {
1525*4882a593Smuzhiyun pr_warn("%s: alloc_disk failed\n", __func__);
1526*4882a593Smuzhiyun return -ENOMEM;
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun disk->major = SCSI_GENERIC_MAJOR;
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun error = -ENOMEM;
1531*4882a593Smuzhiyun cdev = cdev_alloc();
1532*4882a593Smuzhiyun if (!cdev) {
1533*4882a593Smuzhiyun pr_warn("%s: cdev_alloc failed\n", __func__);
1534*4882a593Smuzhiyun goto out;
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun cdev->owner = THIS_MODULE;
1537*4882a593Smuzhiyun cdev->ops = &sg_fops;
1538*4882a593Smuzhiyun
1539*4882a593Smuzhiyun sdp = sg_alloc(disk, scsidp);
1540*4882a593Smuzhiyun if (IS_ERR(sdp)) {
1541*4882a593Smuzhiyun pr_warn("%s: sg_alloc failed\n", __func__);
1542*4882a593Smuzhiyun error = PTR_ERR(sdp);
1543*4882a593Smuzhiyun goto out;
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
1547*4882a593Smuzhiyun if (error)
1548*4882a593Smuzhiyun goto cdev_add_err;
1549*4882a593Smuzhiyun
1550*4882a593Smuzhiyun sdp->cdev = cdev;
1551*4882a593Smuzhiyun if (sg_sysfs_valid) {
1552*4882a593Smuzhiyun struct device *sg_class_member;
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1555*4882a593Smuzhiyun MKDEV(SCSI_GENERIC_MAJOR,
1556*4882a593Smuzhiyun sdp->index),
1557*4882a593Smuzhiyun sdp, "%s", disk->disk_name);
1558*4882a593Smuzhiyun if (IS_ERR(sg_class_member)) {
1559*4882a593Smuzhiyun pr_err("%s: device_create failed\n", __func__);
1560*4882a593Smuzhiyun error = PTR_ERR(sg_class_member);
1561*4882a593Smuzhiyun goto cdev_add_err;
1562*4882a593Smuzhiyun }
1563*4882a593Smuzhiyun error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1564*4882a593Smuzhiyun &sg_class_member->kobj, "generic");
1565*4882a593Smuzhiyun if (error)
1566*4882a593Smuzhiyun pr_err("%s: unable to make symlink 'generic' back "
1567*4882a593Smuzhiyun "to sg%d\n", __func__, sdp->index);
1568*4882a593Smuzhiyun } else
1569*4882a593Smuzhiyun pr_warn("%s: sg_sys Invalid\n", __func__);
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun sdev_printk(KERN_NOTICE, scsidp, "Attached scsi generic sg%d "
1572*4882a593Smuzhiyun "type %d\n", sdp->index, scsidp->type);
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun dev_set_drvdata(cl_dev, sdp);
1575*4882a593Smuzhiyun
1576*4882a593Smuzhiyun return 0;
1577*4882a593Smuzhiyun
1578*4882a593Smuzhiyun cdev_add_err:
1579*4882a593Smuzhiyun write_lock_irqsave(&sg_index_lock, iflags);
1580*4882a593Smuzhiyun idr_remove(&sg_index_idr, sdp->index);
1581*4882a593Smuzhiyun write_unlock_irqrestore(&sg_index_lock, iflags);
1582*4882a593Smuzhiyun kfree(sdp);
1583*4882a593Smuzhiyun
1584*4882a593Smuzhiyun out:
1585*4882a593Smuzhiyun put_disk(disk);
1586*4882a593Smuzhiyun if (cdev)
1587*4882a593Smuzhiyun cdev_del(cdev);
1588*4882a593Smuzhiyun return error;
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun static void
sg_device_destroy(struct kref * kref)1592*4882a593Smuzhiyun sg_device_destroy(struct kref *kref)
1593*4882a593Smuzhiyun {
1594*4882a593Smuzhiyun struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1595*4882a593Smuzhiyun unsigned long flags;
1596*4882a593Smuzhiyun
1597*4882a593Smuzhiyun /* CAUTION! Note that the device can still be found via idr_find()
1598*4882a593Smuzhiyun * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1599*4882a593Smuzhiyun * any other cleanup.
1600*4882a593Smuzhiyun */
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun write_lock_irqsave(&sg_index_lock, flags);
1603*4882a593Smuzhiyun idr_remove(&sg_index_idr, sdp->index);
1604*4882a593Smuzhiyun write_unlock_irqrestore(&sg_index_lock, flags);
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3,
1607*4882a593Smuzhiyun sg_printk(KERN_INFO, sdp, "sg_device_destroy\n"));
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun put_disk(sdp->disk);
1610*4882a593Smuzhiyun kfree(sdp);
1611*4882a593Smuzhiyun }
1612*4882a593Smuzhiyun
1613*4882a593Smuzhiyun static void
sg_remove_device(struct device * cl_dev,struct class_interface * cl_intf)1614*4882a593Smuzhiyun sg_remove_device(struct device *cl_dev, struct class_interface *cl_intf)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1617*4882a593Smuzhiyun Sg_device *sdp = dev_get_drvdata(cl_dev);
1618*4882a593Smuzhiyun unsigned long iflags;
1619*4882a593Smuzhiyun Sg_fd *sfp;
1620*4882a593Smuzhiyun int val;
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun if (!sdp)
1623*4882a593Smuzhiyun return;
1624*4882a593Smuzhiyun /* want sdp->detaching non-zero as soon as possible */
1625*4882a593Smuzhiyun val = atomic_inc_return(&sdp->detaching);
1626*4882a593Smuzhiyun if (val > 1)
1627*4882a593Smuzhiyun return; /* only want to do following once per device */
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
1630*4882a593Smuzhiyun "%s\n", __func__));
1631*4882a593Smuzhiyun
1632*4882a593Smuzhiyun read_lock_irqsave(&sdp->sfd_lock, iflags);
1633*4882a593Smuzhiyun list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
1634*4882a593Smuzhiyun wake_up_interruptible_all(&sfp->read_wait);
1635*4882a593Smuzhiyun kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun wake_up_interruptible_all(&sdp->open_wait);
1638*4882a593Smuzhiyun read_unlock_irqrestore(&sdp->sfd_lock, iflags);
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1641*4882a593Smuzhiyun device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1642*4882a593Smuzhiyun cdev_del(sdp->cdev);
1643*4882a593Smuzhiyun sdp->cdev = NULL;
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun kref_put(&sdp->d_ref, sg_device_destroy);
1646*4882a593Smuzhiyun }
1647*4882a593Smuzhiyun
1648*4882a593Smuzhiyun module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1649*4882a593Smuzhiyun module_param_named(def_reserved_size, def_reserved_size, int,
1650*4882a593Smuzhiyun S_IRUGO | S_IWUSR);
1651*4882a593Smuzhiyun module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun MODULE_AUTHOR("Douglas Gilbert");
1654*4882a593Smuzhiyun MODULE_DESCRIPTION("SCSI generic (sg) driver");
1655*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1656*4882a593Smuzhiyun MODULE_VERSION(SG_VERSION_STR);
1657*4882a593Smuzhiyun MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1660*4882a593Smuzhiyun "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1661*4882a593Smuzhiyun MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1662*4882a593Smuzhiyun MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1663*4882a593Smuzhiyun
1664*4882a593Smuzhiyun static int __init
init_sg(void)1665*4882a593Smuzhiyun init_sg(void)
1666*4882a593Smuzhiyun {
1667*4882a593Smuzhiyun int rc;
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun if (scatter_elem_sz < PAGE_SIZE) {
1670*4882a593Smuzhiyun scatter_elem_sz = PAGE_SIZE;
1671*4882a593Smuzhiyun scatter_elem_sz_prev = scatter_elem_sz;
1672*4882a593Smuzhiyun }
1673*4882a593Smuzhiyun if (def_reserved_size >= 0)
1674*4882a593Smuzhiyun sg_big_buff = def_reserved_size;
1675*4882a593Smuzhiyun else
1676*4882a593Smuzhiyun def_reserved_size = sg_big_buff;
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1679*4882a593Smuzhiyun SG_MAX_DEVS, "sg");
1680*4882a593Smuzhiyun if (rc)
1681*4882a593Smuzhiyun return rc;
1682*4882a593Smuzhiyun sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1683*4882a593Smuzhiyun if ( IS_ERR(sg_sysfs_class) ) {
1684*4882a593Smuzhiyun rc = PTR_ERR(sg_sysfs_class);
1685*4882a593Smuzhiyun goto err_out;
1686*4882a593Smuzhiyun }
1687*4882a593Smuzhiyun sg_sysfs_valid = 1;
1688*4882a593Smuzhiyun rc = scsi_register_interface(&sg_interface);
1689*4882a593Smuzhiyun if (0 == rc) {
1690*4882a593Smuzhiyun #ifdef CONFIG_SCSI_PROC_FS
1691*4882a593Smuzhiyun sg_proc_init();
1692*4882a593Smuzhiyun #endif /* CONFIG_SCSI_PROC_FS */
1693*4882a593Smuzhiyun return 0;
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun class_destroy(sg_sysfs_class);
1696*4882a593Smuzhiyun err_out:
1697*4882a593Smuzhiyun unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1698*4882a593Smuzhiyun return rc;
1699*4882a593Smuzhiyun }
1700*4882a593Smuzhiyun
1701*4882a593Smuzhiyun static void __exit
exit_sg(void)1702*4882a593Smuzhiyun exit_sg(void)
1703*4882a593Smuzhiyun {
1704*4882a593Smuzhiyun #ifdef CONFIG_SCSI_PROC_FS
1705*4882a593Smuzhiyun remove_proc_subtree("scsi/sg", NULL);
1706*4882a593Smuzhiyun #endif /* CONFIG_SCSI_PROC_FS */
1707*4882a593Smuzhiyun scsi_unregister_interface(&sg_interface);
1708*4882a593Smuzhiyun class_destroy(sg_sysfs_class);
1709*4882a593Smuzhiyun sg_sysfs_valid = 0;
1710*4882a593Smuzhiyun unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1711*4882a593Smuzhiyun SG_MAX_DEVS);
1712*4882a593Smuzhiyun idr_destroy(&sg_index_idr);
1713*4882a593Smuzhiyun }
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun static int
sg_start_req(Sg_request * srp,unsigned char * cmd)1716*4882a593Smuzhiyun sg_start_req(Sg_request *srp, unsigned char *cmd)
1717*4882a593Smuzhiyun {
1718*4882a593Smuzhiyun int res;
1719*4882a593Smuzhiyun struct request *rq;
1720*4882a593Smuzhiyun struct scsi_request *req;
1721*4882a593Smuzhiyun Sg_fd *sfp = srp->parentfp;
1722*4882a593Smuzhiyun sg_io_hdr_t *hp = &srp->header;
1723*4882a593Smuzhiyun int dxfer_len = (int) hp->dxfer_len;
1724*4882a593Smuzhiyun int dxfer_dir = hp->dxfer_direction;
1725*4882a593Smuzhiyun unsigned int iov_count = hp->iovec_count;
1726*4882a593Smuzhiyun Sg_scatter_hold *req_schp = &srp->data;
1727*4882a593Smuzhiyun Sg_scatter_hold *rsv_schp = &sfp->reserve;
1728*4882a593Smuzhiyun struct request_queue *q = sfp->parentdp->device->request_queue;
1729*4882a593Smuzhiyun struct rq_map_data *md, map_data;
1730*4882a593Smuzhiyun int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1731*4882a593Smuzhiyun unsigned char *long_cmdp = NULL;
1732*4882a593Smuzhiyun
1733*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
1734*4882a593Smuzhiyun "sg_start_req: dxfer_len=%d\n",
1735*4882a593Smuzhiyun dxfer_len));
1736*4882a593Smuzhiyun
1737*4882a593Smuzhiyun if (hp->cmd_len > BLK_MAX_CDB) {
1738*4882a593Smuzhiyun long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
1739*4882a593Smuzhiyun if (!long_cmdp)
1740*4882a593Smuzhiyun return -ENOMEM;
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun /*
1744*4882a593Smuzhiyun * NOTE
1745*4882a593Smuzhiyun *
1746*4882a593Smuzhiyun * With scsi-mq enabled, there are a fixed number of preallocated
1747*4882a593Smuzhiyun * requests equal in number to shost->can_queue. If all of the
1748*4882a593Smuzhiyun * preallocated requests are already in use, then blk_get_request()
1749*4882a593Smuzhiyun * will sleep until an active command completes, freeing up a request.
1750*4882a593Smuzhiyun * Although waiting in an asynchronous interface is less than ideal, we
1751*4882a593Smuzhiyun * do not want to use BLK_MQ_REQ_NOWAIT here because userspace might
1752*4882a593Smuzhiyun * not expect an EWOULDBLOCK from this condition.
1753*4882a593Smuzhiyun */
1754*4882a593Smuzhiyun rq = blk_get_request(q, hp->dxfer_direction == SG_DXFER_TO_DEV ?
1755*4882a593Smuzhiyun REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
1756*4882a593Smuzhiyun if (IS_ERR(rq)) {
1757*4882a593Smuzhiyun kfree(long_cmdp);
1758*4882a593Smuzhiyun return PTR_ERR(rq);
1759*4882a593Smuzhiyun }
1760*4882a593Smuzhiyun req = scsi_req(rq);
1761*4882a593Smuzhiyun
1762*4882a593Smuzhiyun if (hp->cmd_len > BLK_MAX_CDB)
1763*4882a593Smuzhiyun req->cmd = long_cmdp;
1764*4882a593Smuzhiyun memcpy(req->cmd, cmd, hp->cmd_len);
1765*4882a593Smuzhiyun req->cmd_len = hp->cmd_len;
1766*4882a593Smuzhiyun
1767*4882a593Smuzhiyun srp->rq = rq;
1768*4882a593Smuzhiyun rq->end_io_data = srp;
1769*4882a593Smuzhiyun req->retries = SG_DEFAULT_RETRIES;
1770*4882a593Smuzhiyun
1771*4882a593Smuzhiyun if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1772*4882a593Smuzhiyun return 0;
1773*4882a593Smuzhiyun
1774*4882a593Smuzhiyun if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1775*4882a593Smuzhiyun dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1776*4882a593Smuzhiyun !sfp->parentdp->device->host->unchecked_isa_dma &&
1777*4882a593Smuzhiyun blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
1778*4882a593Smuzhiyun md = NULL;
1779*4882a593Smuzhiyun else
1780*4882a593Smuzhiyun md = &map_data;
1781*4882a593Smuzhiyun
1782*4882a593Smuzhiyun if (md) {
1783*4882a593Smuzhiyun mutex_lock(&sfp->f_mutex);
1784*4882a593Smuzhiyun if (dxfer_len <= rsv_schp->bufflen &&
1785*4882a593Smuzhiyun !sfp->res_in_use) {
1786*4882a593Smuzhiyun sfp->res_in_use = 1;
1787*4882a593Smuzhiyun sg_link_reserve(sfp, srp, dxfer_len);
1788*4882a593Smuzhiyun } else if (hp->flags & SG_FLAG_MMAP_IO) {
1789*4882a593Smuzhiyun res = -EBUSY; /* sfp->res_in_use == 1 */
1790*4882a593Smuzhiyun if (dxfer_len > rsv_schp->bufflen)
1791*4882a593Smuzhiyun res = -ENOMEM;
1792*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
1793*4882a593Smuzhiyun return res;
1794*4882a593Smuzhiyun } else {
1795*4882a593Smuzhiyun res = sg_build_indirect(req_schp, sfp, dxfer_len);
1796*4882a593Smuzhiyun if (res) {
1797*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
1798*4882a593Smuzhiyun return res;
1799*4882a593Smuzhiyun }
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun mutex_unlock(&sfp->f_mutex);
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun md->pages = req_schp->pages;
1804*4882a593Smuzhiyun md->page_order = req_schp->page_order;
1805*4882a593Smuzhiyun md->nr_entries = req_schp->k_use_sg;
1806*4882a593Smuzhiyun md->offset = 0;
1807*4882a593Smuzhiyun md->null_mapped = hp->dxferp ? 0 : 1;
1808*4882a593Smuzhiyun if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
1809*4882a593Smuzhiyun md->from_user = 1;
1810*4882a593Smuzhiyun else
1811*4882a593Smuzhiyun md->from_user = 0;
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun
1814*4882a593Smuzhiyun if (iov_count) {
1815*4882a593Smuzhiyun struct iovec *iov = NULL;
1816*4882a593Smuzhiyun struct iov_iter i;
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun res = import_iovec(rw, hp->dxferp, iov_count, 0, &iov, &i);
1819*4882a593Smuzhiyun if (res < 0)
1820*4882a593Smuzhiyun return res;
1821*4882a593Smuzhiyun
1822*4882a593Smuzhiyun iov_iter_truncate(&i, hp->dxfer_len);
1823*4882a593Smuzhiyun if (!iov_iter_count(&i)) {
1824*4882a593Smuzhiyun kfree(iov);
1825*4882a593Smuzhiyun return -EINVAL;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun res = blk_rq_map_user_iov(q, rq, md, &i, GFP_ATOMIC);
1829*4882a593Smuzhiyun kfree(iov);
1830*4882a593Smuzhiyun } else
1831*4882a593Smuzhiyun res = blk_rq_map_user(q, rq, md, hp->dxferp,
1832*4882a593Smuzhiyun hp->dxfer_len, GFP_ATOMIC);
1833*4882a593Smuzhiyun
1834*4882a593Smuzhiyun if (!res) {
1835*4882a593Smuzhiyun srp->bio = rq->bio;
1836*4882a593Smuzhiyun
1837*4882a593Smuzhiyun if (!md) {
1838*4882a593Smuzhiyun req_schp->dio_in_use = 1;
1839*4882a593Smuzhiyun hp->info |= SG_INFO_DIRECT_IO;
1840*4882a593Smuzhiyun }
1841*4882a593Smuzhiyun }
1842*4882a593Smuzhiyun return res;
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun
1845*4882a593Smuzhiyun static int
sg_finish_rem_req(Sg_request * srp)1846*4882a593Smuzhiyun sg_finish_rem_req(Sg_request *srp)
1847*4882a593Smuzhiyun {
1848*4882a593Smuzhiyun int ret = 0;
1849*4882a593Smuzhiyun
1850*4882a593Smuzhiyun Sg_fd *sfp = srp->parentfp;
1851*4882a593Smuzhiyun Sg_scatter_hold *req_schp = &srp->data;
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
1854*4882a593Smuzhiyun "sg_finish_rem_req: res_used=%d\n",
1855*4882a593Smuzhiyun (int) srp->res_used));
1856*4882a593Smuzhiyun if (srp->bio)
1857*4882a593Smuzhiyun ret = blk_rq_unmap_user(srp->bio);
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun if (srp->rq) {
1860*4882a593Smuzhiyun scsi_req_free_cmd(scsi_req(srp->rq));
1861*4882a593Smuzhiyun blk_put_request(srp->rq);
1862*4882a593Smuzhiyun }
1863*4882a593Smuzhiyun
1864*4882a593Smuzhiyun if (srp->res_used)
1865*4882a593Smuzhiyun sg_unlink_reserve(sfp, srp);
1866*4882a593Smuzhiyun else
1867*4882a593Smuzhiyun sg_remove_scat(sfp, req_schp);
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun return ret;
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun
1872*4882a593Smuzhiyun static int
sg_build_sgat(Sg_scatter_hold * schp,const Sg_fd * sfp,int tablesize)1873*4882a593Smuzhiyun sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1874*4882a593Smuzhiyun {
1875*4882a593Smuzhiyun int sg_bufflen = tablesize * sizeof(struct page *);
1876*4882a593Smuzhiyun gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun schp->pages = kzalloc(sg_bufflen, gfp_flags);
1879*4882a593Smuzhiyun if (!schp->pages)
1880*4882a593Smuzhiyun return -ENOMEM;
1881*4882a593Smuzhiyun schp->sglist_len = sg_bufflen;
1882*4882a593Smuzhiyun return tablesize; /* number of scat_gath elements allocated */
1883*4882a593Smuzhiyun }
1884*4882a593Smuzhiyun
1885*4882a593Smuzhiyun static int
sg_build_indirect(Sg_scatter_hold * schp,Sg_fd * sfp,int buff_size)1886*4882a593Smuzhiyun sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1887*4882a593Smuzhiyun {
1888*4882a593Smuzhiyun int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
1889*4882a593Smuzhiyun int sg_tablesize = sfp->parentdp->sg_tablesize;
1890*4882a593Smuzhiyun int blk_size = buff_size, order;
1891*4882a593Smuzhiyun gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO;
1892*4882a593Smuzhiyun struct sg_device *sdp = sfp->parentdp;
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun if (blk_size < 0)
1895*4882a593Smuzhiyun return -EFAULT;
1896*4882a593Smuzhiyun if (0 == blk_size)
1897*4882a593Smuzhiyun ++blk_size; /* don't know why */
1898*4882a593Smuzhiyun /* round request up to next highest SG_SECTOR_SZ byte boundary */
1899*4882a593Smuzhiyun blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
1900*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
1901*4882a593Smuzhiyun "sg_build_indirect: buff_size=%d, blk_size=%d\n",
1902*4882a593Smuzhiyun buff_size, blk_size));
1903*4882a593Smuzhiyun
1904*4882a593Smuzhiyun /* N.B. ret_sz carried into this block ... */
1905*4882a593Smuzhiyun mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1906*4882a593Smuzhiyun if (mx_sc_elems < 0)
1907*4882a593Smuzhiyun return mx_sc_elems; /* most likely -ENOMEM */
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun num = scatter_elem_sz;
1910*4882a593Smuzhiyun if (unlikely(num != scatter_elem_sz_prev)) {
1911*4882a593Smuzhiyun if (num < PAGE_SIZE) {
1912*4882a593Smuzhiyun scatter_elem_sz = PAGE_SIZE;
1913*4882a593Smuzhiyun scatter_elem_sz_prev = PAGE_SIZE;
1914*4882a593Smuzhiyun } else
1915*4882a593Smuzhiyun scatter_elem_sz_prev = num;
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun if (sdp->device->host->unchecked_isa_dma)
1919*4882a593Smuzhiyun gfp_mask |= GFP_DMA;
1920*4882a593Smuzhiyun
1921*4882a593Smuzhiyun order = get_order(num);
1922*4882a593Smuzhiyun retry:
1923*4882a593Smuzhiyun ret_sz = 1 << (PAGE_SHIFT + order);
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1926*4882a593Smuzhiyun k++, rem_sz -= ret_sz) {
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun num = (rem_sz > scatter_elem_sz_prev) ?
1929*4882a593Smuzhiyun scatter_elem_sz_prev : rem_sz;
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun schp->pages[k] = alloc_pages(gfp_mask, order);
1932*4882a593Smuzhiyun if (!schp->pages[k])
1933*4882a593Smuzhiyun goto out;
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun if (num == scatter_elem_sz_prev) {
1936*4882a593Smuzhiyun if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1937*4882a593Smuzhiyun scatter_elem_sz = ret_sz;
1938*4882a593Smuzhiyun scatter_elem_sz_prev = ret_sz;
1939*4882a593Smuzhiyun }
1940*4882a593Smuzhiyun }
1941*4882a593Smuzhiyun
1942*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
1943*4882a593Smuzhiyun "sg_build_indirect: k=%d, num=%d, ret_sz=%d\n",
1944*4882a593Smuzhiyun k, num, ret_sz));
1945*4882a593Smuzhiyun } /* end of for loop */
1946*4882a593Smuzhiyun
1947*4882a593Smuzhiyun schp->page_order = order;
1948*4882a593Smuzhiyun schp->k_use_sg = k;
1949*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
1950*4882a593Smuzhiyun "sg_build_indirect: k_use_sg=%d, rem_sz=%d\n",
1951*4882a593Smuzhiyun k, rem_sz));
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun schp->bufflen = blk_size;
1954*4882a593Smuzhiyun if (rem_sz > 0) /* must have failed */
1955*4882a593Smuzhiyun return -ENOMEM;
1956*4882a593Smuzhiyun return 0;
1957*4882a593Smuzhiyun out:
1958*4882a593Smuzhiyun for (i = 0; i < k; i++)
1959*4882a593Smuzhiyun __free_pages(schp->pages[i], order);
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun if (--order >= 0)
1962*4882a593Smuzhiyun goto retry;
1963*4882a593Smuzhiyun
1964*4882a593Smuzhiyun return -ENOMEM;
1965*4882a593Smuzhiyun }
1966*4882a593Smuzhiyun
1967*4882a593Smuzhiyun static void
sg_remove_scat(Sg_fd * sfp,Sg_scatter_hold * schp)1968*4882a593Smuzhiyun sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp)
1969*4882a593Smuzhiyun {
1970*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
1971*4882a593Smuzhiyun "sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1972*4882a593Smuzhiyun if (schp->pages && schp->sglist_len > 0) {
1973*4882a593Smuzhiyun if (!schp->dio_in_use) {
1974*4882a593Smuzhiyun int k;
1975*4882a593Smuzhiyun
1976*4882a593Smuzhiyun for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
1977*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(5,
1978*4882a593Smuzhiyun sg_printk(KERN_INFO, sfp->parentdp,
1979*4882a593Smuzhiyun "sg_remove_scat: k=%d, pg=0x%p\n",
1980*4882a593Smuzhiyun k, schp->pages[k]));
1981*4882a593Smuzhiyun __free_pages(schp->pages[k], schp->page_order);
1982*4882a593Smuzhiyun }
1983*4882a593Smuzhiyun
1984*4882a593Smuzhiyun kfree(schp->pages);
1985*4882a593Smuzhiyun }
1986*4882a593Smuzhiyun }
1987*4882a593Smuzhiyun memset(schp, 0, sizeof (*schp));
1988*4882a593Smuzhiyun }
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun static int
sg_read_oxfer(Sg_request * srp,char __user * outp,int num_read_xfer)1991*4882a593Smuzhiyun sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1992*4882a593Smuzhiyun {
1993*4882a593Smuzhiyun Sg_scatter_hold *schp = &srp->data;
1994*4882a593Smuzhiyun int k, num;
1995*4882a593Smuzhiyun
1996*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
1997*4882a593Smuzhiyun "sg_read_oxfer: num_read_xfer=%d\n",
1998*4882a593Smuzhiyun num_read_xfer));
1999*4882a593Smuzhiyun if ((!outp) || (num_read_xfer <= 0))
2000*4882a593Smuzhiyun return 0;
2001*4882a593Smuzhiyun
2002*4882a593Smuzhiyun num = 1 << (PAGE_SHIFT + schp->page_order);
2003*4882a593Smuzhiyun for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
2004*4882a593Smuzhiyun if (num > num_read_xfer) {
2005*4882a593Smuzhiyun if (copy_to_user(outp, page_address(schp->pages[k]),
2006*4882a593Smuzhiyun num_read_xfer))
2007*4882a593Smuzhiyun return -EFAULT;
2008*4882a593Smuzhiyun break;
2009*4882a593Smuzhiyun } else {
2010*4882a593Smuzhiyun if (copy_to_user(outp, page_address(schp->pages[k]),
2011*4882a593Smuzhiyun num))
2012*4882a593Smuzhiyun return -EFAULT;
2013*4882a593Smuzhiyun num_read_xfer -= num;
2014*4882a593Smuzhiyun if (num_read_xfer <= 0)
2015*4882a593Smuzhiyun break;
2016*4882a593Smuzhiyun outp += num;
2017*4882a593Smuzhiyun }
2018*4882a593Smuzhiyun }
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun return 0;
2021*4882a593Smuzhiyun }
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun static void
sg_build_reserve(Sg_fd * sfp,int req_size)2024*4882a593Smuzhiyun sg_build_reserve(Sg_fd * sfp, int req_size)
2025*4882a593Smuzhiyun {
2026*4882a593Smuzhiyun Sg_scatter_hold *schp = &sfp->reserve;
2027*4882a593Smuzhiyun
2028*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
2029*4882a593Smuzhiyun "sg_build_reserve: req_size=%d\n", req_size));
2030*4882a593Smuzhiyun do {
2031*4882a593Smuzhiyun if (req_size < PAGE_SIZE)
2032*4882a593Smuzhiyun req_size = PAGE_SIZE;
2033*4882a593Smuzhiyun if (0 == sg_build_indirect(schp, sfp, req_size))
2034*4882a593Smuzhiyun return;
2035*4882a593Smuzhiyun else
2036*4882a593Smuzhiyun sg_remove_scat(sfp, schp);
2037*4882a593Smuzhiyun req_size >>= 1; /* divide by 2 */
2038*4882a593Smuzhiyun } while (req_size > (PAGE_SIZE / 2));
2039*4882a593Smuzhiyun }
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun static void
sg_link_reserve(Sg_fd * sfp,Sg_request * srp,int size)2042*4882a593Smuzhiyun sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2043*4882a593Smuzhiyun {
2044*4882a593Smuzhiyun Sg_scatter_hold *req_schp = &srp->data;
2045*4882a593Smuzhiyun Sg_scatter_hold *rsv_schp = &sfp->reserve;
2046*4882a593Smuzhiyun int k, num, rem;
2047*4882a593Smuzhiyun
2048*4882a593Smuzhiyun srp->res_used = 1;
2049*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
2050*4882a593Smuzhiyun "sg_link_reserve: size=%d\n", size));
2051*4882a593Smuzhiyun rem = size;
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
2054*4882a593Smuzhiyun for (k = 0; k < rsv_schp->k_use_sg; k++) {
2055*4882a593Smuzhiyun if (rem <= num) {
2056*4882a593Smuzhiyun req_schp->k_use_sg = k + 1;
2057*4882a593Smuzhiyun req_schp->sglist_len = rsv_schp->sglist_len;
2058*4882a593Smuzhiyun req_schp->pages = rsv_schp->pages;
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun req_schp->bufflen = size;
2061*4882a593Smuzhiyun req_schp->page_order = rsv_schp->page_order;
2062*4882a593Smuzhiyun break;
2063*4882a593Smuzhiyun } else
2064*4882a593Smuzhiyun rem -= num;
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun if (k >= rsv_schp->k_use_sg)
2068*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
2069*4882a593Smuzhiyun "sg_link_reserve: BAD size\n"));
2070*4882a593Smuzhiyun }
2071*4882a593Smuzhiyun
2072*4882a593Smuzhiyun static void
sg_unlink_reserve(Sg_fd * sfp,Sg_request * srp)2073*4882a593Smuzhiyun sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2074*4882a593Smuzhiyun {
2075*4882a593Smuzhiyun Sg_scatter_hold *req_schp = &srp->data;
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
2078*4882a593Smuzhiyun "sg_unlink_reserve: req->k_use_sg=%d\n",
2079*4882a593Smuzhiyun (int) req_schp->k_use_sg));
2080*4882a593Smuzhiyun req_schp->k_use_sg = 0;
2081*4882a593Smuzhiyun req_schp->bufflen = 0;
2082*4882a593Smuzhiyun req_schp->pages = NULL;
2083*4882a593Smuzhiyun req_schp->page_order = 0;
2084*4882a593Smuzhiyun req_schp->sglist_len = 0;
2085*4882a593Smuzhiyun srp->res_used = 0;
2086*4882a593Smuzhiyun /* Called without mutex lock to avoid deadlock */
2087*4882a593Smuzhiyun sfp->res_in_use = 0;
2088*4882a593Smuzhiyun }
2089*4882a593Smuzhiyun
2090*4882a593Smuzhiyun static Sg_request *
sg_get_rq_mark(Sg_fd * sfp,int pack_id,bool * busy)2091*4882a593Smuzhiyun sg_get_rq_mark(Sg_fd * sfp, int pack_id, bool *busy)
2092*4882a593Smuzhiyun {
2093*4882a593Smuzhiyun Sg_request *resp;
2094*4882a593Smuzhiyun unsigned long iflags;
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun *busy = false;
2097*4882a593Smuzhiyun write_lock_irqsave(&sfp->rq_list_lock, iflags);
2098*4882a593Smuzhiyun list_for_each_entry(resp, &sfp->rq_list, entry) {
2099*4882a593Smuzhiyun /* look for requests that are not SG_IO owned */
2100*4882a593Smuzhiyun if ((!resp->sg_io_owned) &&
2101*4882a593Smuzhiyun ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2102*4882a593Smuzhiyun switch (resp->done) {
2103*4882a593Smuzhiyun case 0: /* request active */
2104*4882a593Smuzhiyun *busy = true;
2105*4882a593Smuzhiyun break;
2106*4882a593Smuzhiyun case 1: /* request done; response ready to return */
2107*4882a593Smuzhiyun resp->done = 2; /* guard against other readers */
2108*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2109*4882a593Smuzhiyun return resp;
2110*4882a593Smuzhiyun case 2: /* response already being returned */
2111*4882a593Smuzhiyun break;
2112*4882a593Smuzhiyun }
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun }
2115*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2116*4882a593Smuzhiyun return NULL;
2117*4882a593Smuzhiyun }
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun /* always adds to end of list */
2120*4882a593Smuzhiyun static Sg_request *
sg_add_request(Sg_fd * sfp)2121*4882a593Smuzhiyun sg_add_request(Sg_fd * sfp)
2122*4882a593Smuzhiyun {
2123*4882a593Smuzhiyun int k;
2124*4882a593Smuzhiyun unsigned long iflags;
2125*4882a593Smuzhiyun Sg_request *rp = sfp->req_arr;
2126*4882a593Smuzhiyun
2127*4882a593Smuzhiyun write_lock_irqsave(&sfp->rq_list_lock, iflags);
2128*4882a593Smuzhiyun if (!list_empty(&sfp->rq_list)) {
2129*4882a593Smuzhiyun if (!sfp->cmd_q)
2130*4882a593Smuzhiyun goto out_unlock;
2131*4882a593Smuzhiyun
2132*4882a593Smuzhiyun for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2133*4882a593Smuzhiyun if (!rp->parentfp)
2134*4882a593Smuzhiyun break;
2135*4882a593Smuzhiyun }
2136*4882a593Smuzhiyun if (k >= SG_MAX_QUEUE)
2137*4882a593Smuzhiyun goto out_unlock;
2138*4882a593Smuzhiyun }
2139*4882a593Smuzhiyun memset(rp, 0, sizeof (Sg_request));
2140*4882a593Smuzhiyun rp->parentfp = sfp;
2141*4882a593Smuzhiyun rp->header.duration = jiffies_to_msecs(jiffies);
2142*4882a593Smuzhiyun list_add_tail(&rp->entry, &sfp->rq_list);
2143*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2144*4882a593Smuzhiyun return rp;
2145*4882a593Smuzhiyun out_unlock:
2146*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2147*4882a593Smuzhiyun return NULL;
2148*4882a593Smuzhiyun }
2149*4882a593Smuzhiyun
2150*4882a593Smuzhiyun /* Return of 1 for found; 0 for not found */
2151*4882a593Smuzhiyun static int
sg_remove_request(Sg_fd * sfp,Sg_request * srp)2152*4882a593Smuzhiyun sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2153*4882a593Smuzhiyun {
2154*4882a593Smuzhiyun unsigned long iflags;
2155*4882a593Smuzhiyun int res = 0;
2156*4882a593Smuzhiyun
2157*4882a593Smuzhiyun if (!sfp || !srp || list_empty(&sfp->rq_list))
2158*4882a593Smuzhiyun return res;
2159*4882a593Smuzhiyun write_lock_irqsave(&sfp->rq_list_lock, iflags);
2160*4882a593Smuzhiyun if (!list_empty(&srp->entry)) {
2161*4882a593Smuzhiyun list_del(&srp->entry);
2162*4882a593Smuzhiyun srp->parentfp = NULL;
2163*4882a593Smuzhiyun res = 1;
2164*4882a593Smuzhiyun }
2165*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2166*4882a593Smuzhiyun
2167*4882a593Smuzhiyun /*
2168*4882a593Smuzhiyun * If the device is detaching, wakeup any readers in case we just
2169*4882a593Smuzhiyun * removed the last response, which would leave nothing for them to
2170*4882a593Smuzhiyun * return other than -ENODEV.
2171*4882a593Smuzhiyun */
2172*4882a593Smuzhiyun if (unlikely(atomic_read(&sfp->parentdp->detaching)))
2173*4882a593Smuzhiyun wake_up_interruptible_all(&sfp->read_wait);
2174*4882a593Smuzhiyun
2175*4882a593Smuzhiyun return res;
2176*4882a593Smuzhiyun }
2177*4882a593Smuzhiyun
2178*4882a593Smuzhiyun static Sg_fd *
sg_add_sfp(Sg_device * sdp)2179*4882a593Smuzhiyun sg_add_sfp(Sg_device * sdp)
2180*4882a593Smuzhiyun {
2181*4882a593Smuzhiyun Sg_fd *sfp;
2182*4882a593Smuzhiyun unsigned long iflags;
2183*4882a593Smuzhiyun int bufflen;
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2186*4882a593Smuzhiyun if (!sfp)
2187*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
2188*4882a593Smuzhiyun
2189*4882a593Smuzhiyun init_waitqueue_head(&sfp->read_wait);
2190*4882a593Smuzhiyun rwlock_init(&sfp->rq_list_lock);
2191*4882a593Smuzhiyun INIT_LIST_HEAD(&sfp->rq_list);
2192*4882a593Smuzhiyun kref_init(&sfp->f_ref);
2193*4882a593Smuzhiyun mutex_init(&sfp->f_mutex);
2194*4882a593Smuzhiyun sfp->timeout = SG_DEFAULT_TIMEOUT;
2195*4882a593Smuzhiyun sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2196*4882a593Smuzhiyun sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2197*4882a593Smuzhiyun sfp->cmd_q = SG_DEF_COMMAND_Q;
2198*4882a593Smuzhiyun sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2199*4882a593Smuzhiyun sfp->parentdp = sdp;
2200*4882a593Smuzhiyun write_lock_irqsave(&sdp->sfd_lock, iflags);
2201*4882a593Smuzhiyun if (atomic_read(&sdp->detaching)) {
2202*4882a593Smuzhiyun write_unlock_irqrestore(&sdp->sfd_lock, iflags);
2203*4882a593Smuzhiyun kfree(sfp);
2204*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
2205*4882a593Smuzhiyun }
2206*4882a593Smuzhiyun list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
2207*4882a593Smuzhiyun write_unlock_irqrestore(&sdp->sfd_lock, iflags);
2208*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
2209*4882a593Smuzhiyun "sg_add_sfp: sfp=0x%p\n", sfp));
2210*4882a593Smuzhiyun if (unlikely(sg_big_buff != def_reserved_size))
2211*4882a593Smuzhiyun sg_big_buff = def_reserved_size;
2212*4882a593Smuzhiyun
2213*4882a593Smuzhiyun bufflen = min_t(int, sg_big_buff,
2214*4882a593Smuzhiyun max_sectors_bytes(sdp->device->request_queue));
2215*4882a593Smuzhiyun sg_build_reserve(sfp, bufflen);
2216*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
2217*4882a593Smuzhiyun "sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2218*4882a593Smuzhiyun sfp->reserve.bufflen,
2219*4882a593Smuzhiyun sfp->reserve.k_use_sg));
2220*4882a593Smuzhiyun
2221*4882a593Smuzhiyun kref_get(&sdp->d_ref);
2222*4882a593Smuzhiyun __module_get(THIS_MODULE);
2223*4882a593Smuzhiyun return sfp;
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun
2226*4882a593Smuzhiyun static void
sg_remove_sfp_usercontext(struct work_struct * work)2227*4882a593Smuzhiyun sg_remove_sfp_usercontext(struct work_struct *work)
2228*4882a593Smuzhiyun {
2229*4882a593Smuzhiyun struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2230*4882a593Smuzhiyun struct sg_device *sdp = sfp->parentdp;
2231*4882a593Smuzhiyun Sg_request *srp;
2232*4882a593Smuzhiyun unsigned long iflags;
2233*4882a593Smuzhiyun
2234*4882a593Smuzhiyun /* Cleanup any responses which were never read(). */
2235*4882a593Smuzhiyun write_lock_irqsave(&sfp->rq_list_lock, iflags);
2236*4882a593Smuzhiyun while (!list_empty(&sfp->rq_list)) {
2237*4882a593Smuzhiyun srp = list_first_entry(&sfp->rq_list, Sg_request, entry);
2238*4882a593Smuzhiyun sg_finish_rem_req(srp);
2239*4882a593Smuzhiyun list_del(&srp->entry);
2240*4882a593Smuzhiyun srp->parentfp = NULL;
2241*4882a593Smuzhiyun }
2242*4882a593Smuzhiyun write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2243*4882a593Smuzhiyun
2244*4882a593Smuzhiyun if (sfp->reserve.bufflen > 0) {
2245*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
2246*4882a593Smuzhiyun "sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2247*4882a593Smuzhiyun (int) sfp->reserve.bufflen,
2248*4882a593Smuzhiyun (int) sfp->reserve.k_use_sg));
2249*4882a593Smuzhiyun sg_remove_scat(sfp, &sfp->reserve);
2250*4882a593Smuzhiyun }
2251*4882a593Smuzhiyun
2252*4882a593Smuzhiyun SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
2253*4882a593Smuzhiyun "sg_remove_sfp: sfp=0x%p\n", sfp));
2254*4882a593Smuzhiyun kfree(sfp);
2255*4882a593Smuzhiyun
2256*4882a593Smuzhiyun scsi_device_put(sdp->device);
2257*4882a593Smuzhiyun kref_put(&sdp->d_ref, sg_device_destroy);
2258*4882a593Smuzhiyun module_put(THIS_MODULE);
2259*4882a593Smuzhiyun }
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun static void
sg_remove_sfp(struct kref * kref)2262*4882a593Smuzhiyun sg_remove_sfp(struct kref *kref)
2263*4882a593Smuzhiyun {
2264*4882a593Smuzhiyun struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
2265*4882a593Smuzhiyun struct sg_device *sdp = sfp->parentdp;
2266*4882a593Smuzhiyun unsigned long iflags;
2267*4882a593Smuzhiyun
2268*4882a593Smuzhiyun write_lock_irqsave(&sdp->sfd_lock, iflags);
2269*4882a593Smuzhiyun list_del(&sfp->sfd_siblings);
2270*4882a593Smuzhiyun write_unlock_irqrestore(&sdp->sfd_lock, iflags);
2271*4882a593Smuzhiyun
2272*4882a593Smuzhiyun INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
2273*4882a593Smuzhiyun schedule_work(&sfp->ew.work);
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun #ifdef CONFIG_SCSI_PROC_FS
2277*4882a593Smuzhiyun static int
sg_idr_max_id(int id,void * p,void * data)2278*4882a593Smuzhiyun sg_idr_max_id(int id, void *p, void *data)
2279*4882a593Smuzhiyun {
2280*4882a593Smuzhiyun int *k = data;
2281*4882a593Smuzhiyun
2282*4882a593Smuzhiyun if (*k < id)
2283*4882a593Smuzhiyun *k = id;
2284*4882a593Smuzhiyun
2285*4882a593Smuzhiyun return 0;
2286*4882a593Smuzhiyun }
2287*4882a593Smuzhiyun
2288*4882a593Smuzhiyun static int
sg_last_dev(void)2289*4882a593Smuzhiyun sg_last_dev(void)
2290*4882a593Smuzhiyun {
2291*4882a593Smuzhiyun int k = -1;
2292*4882a593Smuzhiyun unsigned long iflags;
2293*4882a593Smuzhiyun
2294*4882a593Smuzhiyun read_lock_irqsave(&sg_index_lock, iflags);
2295*4882a593Smuzhiyun idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2296*4882a593Smuzhiyun read_unlock_irqrestore(&sg_index_lock, iflags);
2297*4882a593Smuzhiyun return k + 1; /* origin 1 */
2298*4882a593Smuzhiyun }
2299*4882a593Smuzhiyun #endif
2300*4882a593Smuzhiyun
2301*4882a593Smuzhiyun /* must be called with sg_index_lock held */
sg_lookup_dev(int dev)2302*4882a593Smuzhiyun static Sg_device *sg_lookup_dev(int dev)
2303*4882a593Smuzhiyun {
2304*4882a593Smuzhiyun return idr_find(&sg_index_idr, dev);
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun
2307*4882a593Smuzhiyun static Sg_device *
sg_get_dev(int dev)2308*4882a593Smuzhiyun sg_get_dev(int dev)
2309*4882a593Smuzhiyun {
2310*4882a593Smuzhiyun struct sg_device *sdp;
2311*4882a593Smuzhiyun unsigned long flags;
2312*4882a593Smuzhiyun
2313*4882a593Smuzhiyun read_lock_irqsave(&sg_index_lock, flags);
2314*4882a593Smuzhiyun sdp = sg_lookup_dev(dev);
2315*4882a593Smuzhiyun if (!sdp)
2316*4882a593Smuzhiyun sdp = ERR_PTR(-ENXIO);
2317*4882a593Smuzhiyun else if (atomic_read(&sdp->detaching)) {
2318*4882a593Smuzhiyun /* If sdp->detaching, then the refcount may already be 0, in
2319*4882a593Smuzhiyun * which case it would be a bug to do kref_get().
2320*4882a593Smuzhiyun */
2321*4882a593Smuzhiyun sdp = ERR_PTR(-ENODEV);
2322*4882a593Smuzhiyun } else
2323*4882a593Smuzhiyun kref_get(&sdp->d_ref);
2324*4882a593Smuzhiyun read_unlock_irqrestore(&sg_index_lock, flags);
2325*4882a593Smuzhiyun
2326*4882a593Smuzhiyun return sdp;
2327*4882a593Smuzhiyun }
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun #ifdef CONFIG_SCSI_PROC_FS
2330*4882a593Smuzhiyun static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2331*4882a593Smuzhiyun
2332*4882a593Smuzhiyun static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2333*4882a593Smuzhiyun static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2334*4882a593Smuzhiyun size_t count, loff_t *off);
2335*4882a593Smuzhiyun static const struct proc_ops adio_proc_ops = {
2336*4882a593Smuzhiyun .proc_open = sg_proc_single_open_adio,
2337*4882a593Smuzhiyun .proc_read = seq_read,
2338*4882a593Smuzhiyun .proc_lseek = seq_lseek,
2339*4882a593Smuzhiyun .proc_write = sg_proc_write_adio,
2340*4882a593Smuzhiyun .proc_release = single_release,
2341*4882a593Smuzhiyun };
2342*4882a593Smuzhiyun
2343*4882a593Smuzhiyun static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2344*4882a593Smuzhiyun static ssize_t sg_proc_write_dressz(struct file *filp,
2345*4882a593Smuzhiyun const char __user *buffer, size_t count, loff_t *off);
2346*4882a593Smuzhiyun static const struct proc_ops dressz_proc_ops = {
2347*4882a593Smuzhiyun .proc_open = sg_proc_single_open_dressz,
2348*4882a593Smuzhiyun .proc_read = seq_read,
2349*4882a593Smuzhiyun .proc_lseek = seq_lseek,
2350*4882a593Smuzhiyun .proc_write = sg_proc_write_dressz,
2351*4882a593Smuzhiyun .proc_release = single_release,
2352*4882a593Smuzhiyun };
2353*4882a593Smuzhiyun
2354*4882a593Smuzhiyun static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2355*4882a593Smuzhiyun static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2356*4882a593Smuzhiyun static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2357*4882a593Smuzhiyun static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2358*4882a593Smuzhiyun static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2359*4882a593Smuzhiyun static void dev_seq_stop(struct seq_file *s, void *v);
2360*4882a593Smuzhiyun static const struct seq_operations dev_seq_ops = {
2361*4882a593Smuzhiyun .start = dev_seq_start,
2362*4882a593Smuzhiyun .next = dev_seq_next,
2363*4882a593Smuzhiyun .stop = dev_seq_stop,
2364*4882a593Smuzhiyun .show = sg_proc_seq_show_dev,
2365*4882a593Smuzhiyun };
2366*4882a593Smuzhiyun
2367*4882a593Smuzhiyun static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2368*4882a593Smuzhiyun static const struct seq_operations devstrs_seq_ops = {
2369*4882a593Smuzhiyun .start = dev_seq_start,
2370*4882a593Smuzhiyun .next = dev_seq_next,
2371*4882a593Smuzhiyun .stop = dev_seq_stop,
2372*4882a593Smuzhiyun .show = sg_proc_seq_show_devstrs,
2373*4882a593Smuzhiyun };
2374*4882a593Smuzhiyun
2375*4882a593Smuzhiyun static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2376*4882a593Smuzhiyun static const struct seq_operations debug_seq_ops = {
2377*4882a593Smuzhiyun .start = dev_seq_start,
2378*4882a593Smuzhiyun .next = dev_seq_next,
2379*4882a593Smuzhiyun .stop = dev_seq_stop,
2380*4882a593Smuzhiyun .show = sg_proc_seq_show_debug,
2381*4882a593Smuzhiyun };
2382*4882a593Smuzhiyun
2383*4882a593Smuzhiyun static int
sg_proc_init(void)2384*4882a593Smuzhiyun sg_proc_init(void)
2385*4882a593Smuzhiyun {
2386*4882a593Smuzhiyun struct proc_dir_entry *p;
2387*4882a593Smuzhiyun
2388*4882a593Smuzhiyun p = proc_mkdir("scsi/sg", NULL);
2389*4882a593Smuzhiyun if (!p)
2390*4882a593Smuzhiyun return 1;
2391*4882a593Smuzhiyun
2392*4882a593Smuzhiyun proc_create("allow_dio", S_IRUGO | S_IWUSR, p, &adio_proc_ops);
2393*4882a593Smuzhiyun proc_create_seq("debug", S_IRUGO, p, &debug_seq_ops);
2394*4882a593Smuzhiyun proc_create("def_reserved_size", S_IRUGO | S_IWUSR, p, &dressz_proc_ops);
2395*4882a593Smuzhiyun proc_create_single("device_hdr", S_IRUGO, p, sg_proc_seq_show_devhdr);
2396*4882a593Smuzhiyun proc_create_seq("devices", S_IRUGO, p, &dev_seq_ops);
2397*4882a593Smuzhiyun proc_create_seq("device_strs", S_IRUGO, p, &devstrs_seq_ops);
2398*4882a593Smuzhiyun proc_create_single("version", S_IRUGO, p, sg_proc_seq_show_version);
2399*4882a593Smuzhiyun return 0;
2400*4882a593Smuzhiyun }
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun
sg_proc_seq_show_int(struct seq_file * s,void * v)2403*4882a593Smuzhiyun static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2404*4882a593Smuzhiyun {
2405*4882a593Smuzhiyun seq_printf(s, "%d\n", *((int *)s->private));
2406*4882a593Smuzhiyun return 0;
2407*4882a593Smuzhiyun }
2408*4882a593Smuzhiyun
sg_proc_single_open_adio(struct inode * inode,struct file * file)2409*4882a593Smuzhiyun static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2410*4882a593Smuzhiyun {
2411*4882a593Smuzhiyun return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun
2414*4882a593Smuzhiyun static ssize_t
sg_proc_write_adio(struct file * filp,const char __user * buffer,size_t count,loff_t * off)2415*4882a593Smuzhiyun sg_proc_write_adio(struct file *filp, const char __user *buffer,
2416*4882a593Smuzhiyun size_t count, loff_t *off)
2417*4882a593Smuzhiyun {
2418*4882a593Smuzhiyun int err;
2419*4882a593Smuzhiyun unsigned long num;
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2422*4882a593Smuzhiyun return -EACCES;
2423*4882a593Smuzhiyun err = kstrtoul_from_user(buffer, count, 0, &num);
2424*4882a593Smuzhiyun if (err)
2425*4882a593Smuzhiyun return err;
2426*4882a593Smuzhiyun sg_allow_dio = num ? 1 : 0;
2427*4882a593Smuzhiyun return count;
2428*4882a593Smuzhiyun }
2429*4882a593Smuzhiyun
sg_proc_single_open_dressz(struct inode * inode,struct file * file)2430*4882a593Smuzhiyun static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2431*4882a593Smuzhiyun {
2432*4882a593Smuzhiyun return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2433*4882a593Smuzhiyun }
2434*4882a593Smuzhiyun
2435*4882a593Smuzhiyun static ssize_t
sg_proc_write_dressz(struct file * filp,const char __user * buffer,size_t count,loff_t * off)2436*4882a593Smuzhiyun sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2437*4882a593Smuzhiyun size_t count, loff_t *off)
2438*4882a593Smuzhiyun {
2439*4882a593Smuzhiyun int err;
2440*4882a593Smuzhiyun unsigned long k = ULONG_MAX;
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2443*4882a593Smuzhiyun return -EACCES;
2444*4882a593Smuzhiyun
2445*4882a593Smuzhiyun err = kstrtoul_from_user(buffer, count, 0, &k);
2446*4882a593Smuzhiyun if (err)
2447*4882a593Smuzhiyun return err;
2448*4882a593Smuzhiyun if (k <= 1048576) { /* limit "big buff" to 1 MB */
2449*4882a593Smuzhiyun sg_big_buff = k;
2450*4882a593Smuzhiyun return count;
2451*4882a593Smuzhiyun }
2452*4882a593Smuzhiyun return -ERANGE;
2453*4882a593Smuzhiyun }
2454*4882a593Smuzhiyun
sg_proc_seq_show_version(struct seq_file * s,void * v)2455*4882a593Smuzhiyun static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2456*4882a593Smuzhiyun {
2457*4882a593Smuzhiyun seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2458*4882a593Smuzhiyun sg_version_date);
2459*4882a593Smuzhiyun return 0;
2460*4882a593Smuzhiyun }
2461*4882a593Smuzhiyun
sg_proc_seq_show_devhdr(struct seq_file * s,void * v)2462*4882a593Smuzhiyun static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2463*4882a593Smuzhiyun {
2464*4882a593Smuzhiyun seq_puts(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n");
2465*4882a593Smuzhiyun return 0;
2466*4882a593Smuzhiyun }
2467*4882a593Smuzhiyun
2468*4882a593Smuzhiyun struct sg_proc_deviter {
2469*4882a593Smuzhiyun loff_t index;
2470*4882a593Smuzhiyun size_t max;
2471*4882a593Smuzhiyun };
2472*4882a593Smuzhiyun
dev_seq_start(struct seq_file * s,loff_t * pos)2473*4882a593Smuzhiyun static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2474*4882a593Smuzhiyun {
2475*4882a593Smuzhiyun struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2476*4882a593Smuzhiyun
2477*4882a593Smuzhiyun s->private = it;
2478*4882a593Smuzhiyun if (! it)
2479*4882a593Smuzhiyun return NULL;
2480*4882a593Smuzhiyun
2481*4882a593Smuzhiyun it->index = *pos;
2482*4882a593Smuzhiyun it->max = sg_last_dev();
2483*4882a593Smuzhiyun if (it->index >= it->max)
2484*4882a593Smuzhiyun return NULL;
2485*4882a593Smuzhiyun return it;
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun
dev_seq_next(struct seq_file * s,void * v,loff_t * pos)2488*4882a593Smuzhiyun static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2489*4882a593Smuzhiyun {
2490*4882a593Smuzhiyun struct sg_proc_deviter * it = s->private;
2491*4882a593Smuzhiyun
2492*4882a593Smuzhiyun *pos = ++it->index;
2493*4882a593Smuzhiyun return (it->index < it->max) ? it : NULL;
2494*4882a593Smuzhiyun }
2495*4882a593Smuzhiyun
dev_seq_stop(struct seq_file * s,void * v)2496*4882a593Smuzhiyun static void dev_seq_stop(struct seq_file *s, void *v)
2497*4882a593Smuzhiyun {
2498*4882a593Smuzhiyun kfree(s->private);
2499*4882a593Smuzhiyun }
2500*4882a593Smuzhiyun
sg_proc_seq_show_dev(struct seq_file * s,void * v)2501*4882a593Smuzhiyun static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2502*4882a593Smuzhiyun {
2503*4882a593Smuzhiyun struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2504*4882a593Smuzhiyun Sg_device *sdp;
2505*4882a593Smuzhiyun struct scsi_device *scsidp;
2506*4882a593Smuzhiyun unsigned long iflags;
2507*4882a593Smuzhiyun
2508*4882a593Smuzhiyun read_lock_irqsave(&sg_index_lock, iflags);
2509*4882a593Smuzhiyun sdp = it ? sg_lookup_dev(it->index) : NULL;
2510*4882a593Smuzhiyun if ((NULL == sdp) || (NULL == sdp->device) ||
2511*4882a593Smuzhiyun (atomic_read(&sdp->detaching)))
2512*4882a593Smuzhiyun seq_puts(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2513*4882a593Smuzhiyun else {
2514*4882a593Smuzhiyun scsidp = sdp->device;
2515*4882a593Smuzhiyun seq_printf(s, "%d\t%d\t%d\t%llu\t%d\t%d\t%d\t%d\t%d\n",
2516*4882a593Smuzhiyun scsidp->host->host_no, scsidp->channel,
2517*4882a593Smuzhiyun scsidp->id, scsidp->lun, (int) scsidp->type,
2518*4882a593Smuzhiyun 1,
2519*4882a593Smuzhiyun (int) scsidp->queue_depth,
2520*4882a593Smuzhiyun (int) atomic_read(&scsidp->device_busy),
2521*4882a593Smuzhiyun (int) scsi_device_online(scsidp));
2522*4882a593Smuzhiyun }
2523*4882a593Smuzhiyun read_unlock_irqrestore(&sg_index_lock, iflags);
2524*4882a593Smuzhiyun return 0;
2525*4882a593Smuzhiyun }
2526*4882a593Smuzhiyun
sg_proc_seq_show_devstrs(struct seq_file * s,void * v)2527*4882a593Smuzhiyun static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2528*4882a593Smuzhiyun {
2529*4882a593Smuzhiyun struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2530*4882a593Smuzhiyun Sg_device *sdp;
2531*4882a593Smuzhiyun struct scsi_device *scsidp;
2532*4882a593Smuzhiyun unsigned long iflags;
2533*4882a593Smuzhiyun
2534*4882a593Smuzhiyun read_lock_irqsave(&sg_index_lock, iflags);
2535*4882a593Smuzhiyun sdp = it ? sg_lookup_dev(it->index) : NULL;
2536*4882a593Smuzhiyun scsidp = sdp ? sdp->device : NULL;
2537*4882a593Smuzhiyun if (sdp && scsidp && (!atomic_read(&sdp->detaching)))
2538*4882a593Smuzhiyun seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2539*4882a593Smuzhiyun scsidp->vendor, scsidp->model, scsidp->rev);
2540*4882a593Smuzhiyun else
2541*4882a593Smuzhiyun seq_puts(s, "<no active device>\n");
2542*4882a593Smuzhiyun read_unlock_irqrestore(&sg_index_lock, iflags);
2543*4882a593Smuzhiyun return 0;
2544*4882a593Smuzhiyun }
2545*4882a593Smuzhiyun
2546*4882a593Smuzhiyun /* must be called while holding sg_index_lock */
sg_proc_debug_helper(struct seq_file * s,Sg_device * sdp)2547*4882a593Smuzhiyun static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2548*4882a593Smuzhiyun {
2549*4882a593Smuzhiyun int k, new_interface, blen, usg;
2550*4882a593Smuzhiyun Sg_request *srp;
2551*4882a593Smuzhiyun Sg_fd *fp;
2552*4882a593Smuzhiyun const sg_io_hdr_t *hp;
2553*4882a593Smuzhiyun const char * cp;
2554*4882a593Smuzhiyun unsigned int ms;
2555*4882a593Smuzhiyun
2556*4882a593Smuzhiyun k = 0;
2557*4882a593Smuzhiyun list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
2558*4882a593Smuzhiyun k++;
2559*4882a593Smuzhiyun read_lock(&fp->rq_list_lock); /* irqs already disabled */
2560*4882a593Smuzhiyun seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2561*4882a593Smuzhiyun "(res)sgat=%d low_dma=%d\n", k,
2562*4882a593Smuzhiyun jiffies_to_msecs(fp->timeout),
2563*4882a593Smuzhiyun fp->reserve.bufflen,
2564*4882a593Smuzhiyun (int) fp->reserve.k_use_sg,
2565*4882a593Smuzhiyun (int) sdp->device->host->unchecked_isa_dma);
2566*4882a593Smuzhiyun seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
2567*4882a593Smuzhiyun (int) fp->cmd_q, (int) fp->force_packid,
2568*4882a593Smuzhiyun (int) fp->keep_orphan);
2569*4882a593Smuzhiyun list_for_each_entry(srp, &fp->rq_list, entry) {
2570*4882a593Smuzhiyun hp = &srp->header;
2571*4882a593Smuzhiyun new_interface = (hp->interface_id == '\0') ? 0 : 1;
2572*4882a593Smuzhiyun if (srp->res_used) {
2573*4882a593Smuzhiyun if (new_interface &&
2574*4882a593Smuzhiyun (SG_FLAG_MMAP_IO & hp->flags))
2575*4882a593Smuzhiyun cp = " mmap>> ";
2576*4882a593Smuzhiyun else
2577*4882a593Smuzhiyun cp = " rb>> ";
2578*4882a593Smuzhiyun } else {
2579*4882a593Smuzhiyun if (SG_INFO_DIRECT_IO_MASK & hp->info)
2580*4882a593Smuzhiyun cp = " dio>> ";
2581*4882a593Smuzhiyun else
2582*4882a593Smuzhiyun cp = " ";
2583*4882a593Smuzhiyun }
2584*4882a593Smuzhiyun seq_puts(s, cp);
2585*4882a593Smuzhiyun blen = srp->data.bufflen;
2586*4882a593Smuzhiyun usg = srp->data.k_use_sg;
2587*4882a593Smuzhiyun seq_puts(s, srp->done ?
2588*4882a593Smuzhiyun ((1 == srp->done) ? "rcv:" : "fin:")
2589*4882a593Smuzhiyun : "act:");
2590*4882a593Smuzhiyun seq_printf(s, " id=%d blen=%d",
2591*4882a593Smuzhiyun srp->header.pack_id, blen);
2592*4882a593Smuzhiyun if (srp->done)
2593*4882a593Smuzhiyun seq_printf(s, " dur=%d", hp->duration);
2594*4882a593Smuzhiyun else {
2595*4882a593Smuzhiyun ms = jiffies_to_msecs(jiffies);
2596*4882a593Smuzhiyun seq_printf(s, " t_o/elap=%d/%d",
2597*4882a593Smuzhiyun (new_interface ? hp->timeout :
2598*4882a593Smuzhiyun jiffies_to_msecs(fp->timeout)),
2599*4882a593Smuzhiyun (ms > hp->duration ? ms - hp->duration : 0));
2600*4882a593Smuzhiyun }
2601*4882a593Smuzhiyun seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2602*4882a593Smuzhiyun (int) srp->data.cmd_opcode);
2603*4882a593Smuzhiyun }
2604*4882a593Smuzhiyun if (list_empty(&fp->rq_list))
2605*4882a593Smuzhiyun seq_puts(s, " No requests active\n");
2606*4882a593Smuzhiyun read_unlock(&fp->rq_list_lock);
2607*4882a593Smuzhiyun }
2608*4882a593Smuzhiyun }
2609*4882a593Smuzhiyun
sg_proc_seq_show_debug(struct seq_file * s,void * v)2610*4882a593Smuzhiyun static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2611*4882a593Smuzhiyun {
2612*4882a593Smuzhiyun struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2613*4882a593Smuzhiyun Sg_device *sdp;
2614*4882a593Smuzhiyun unsigned long iflags;
2615*4882a593Smuzhiyun
2616*4882a593Smuzhiyun if (it && (0 == it->index))
2617*4882a593Smuzhiyun seq_printf(s, "max_active_device=%d def_reserved_size=%d\n",
2618*4882a593Smuzhiyun (int)it->max, sg_big_buff);
2619*4882a593Smuzhiyun
2620*4882a593Smuzhiyun read_lock_irqsave(&sg_index_lock, iflags);
2621*4882a593Smuzhiyun sdp = it ? sg_lookup_dev(it->index) : NULL;
2622*4882a593Smuzhiyun if (NULL == sdp)
2623*4882a593Smuzhiyun goto skip;
2624*4882a593Smuzhiyun read_lock(&sdp->sfd_lock);
2625*4882a593Smuzhiyun if (!list_empty(&sdp->sfds)) {
2626*4882a593Smuzhiyun seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2627*4882a593Smuzhiyun if (atomic_read(&sdp->detaching))
2628*4882a593Smuzhiyun seq_puts(s, "detaching pending close ");
2629*4882a593Smuzhiyun else if (sdp->device) {
2630*4882a593Smuzhiyun struct scsi_device *scsidp = sdp->device;
2631*4882a593Smuzhiyun
2632*4882a593Smuzhiyun seq_printf(s, "%d:%d:%d:%llu em=%d",
2633*4882a593Smuzhiyun scsidp->host->host_no,
2634*4882a593Smuzhiyun scsidp->channel, scsidp->id,
2635*4882a593Smuzhiyun scsidp->lun,
2636*4882a593Smuzhiyun scsidp->host->hostt->emulated);
2637*4882a593Smuzhiyun }
2638*4882a593Smuzhiyun seq_printf(s, " sg_tablesize=%d excl=%d open_cnt=%d\n",
2639*4882a593Smuzhiyun sdp->sg_tablesize, sdp->exclude, sdp->open_cnt);
2640*4882a593Smuzhiyun sg_proc_debug_helper(s, sdp);
2641*4882a593Smuzhiyun }
2642*4882a593Smuzhiyun read_unlock(&sdp->sfd_lock);
2643*4882a593Smuzhiyun skip:
2644*4882a593Smuzhiyun read_unlock_irqrestore(&sg_index_lock, iflags);
2645*4882a593Smuzhiyun return 0;
2646*4882a593Smuzhiyun }
2647*4882a593Smuzhiyun
2648*4882a593Smuzhiyun #endif /* CONFIG_SCSI_PROC_FS */
2649*4882a593Smuzhiyun
2650*4882a593Smuzhiyun module_init(init_sg);
2651*4882a593Smuzhiyun module_exit(exit_sg);
2652