blob: 37fb44b0074b6222c1fc777e7e139be3b3c98584 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * History:
3 * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
4 * to allow user process control of SCSI devices.
5 * Development Sponsored by Killy Corp. NY NY
6 *
7 * Original driver (sg.c):
8 * Copyright (C) 1992 Lawrence Foard
9 * Version 2 and 3 extensions to driver:
Douglas Gilbert65c26a02014-06-03 13:18:18 -040010 * Copyright (C) 1998 - 2014 Douglas Gilbert
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 */
18
Douglas Gilbert65c26a02014-06-03 13:18:18 -040019static int sg_version_num = 30536; /* 2 digits for each component */
20#define SG_VERSION_STR "3.5.36"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
Douglas Gilbert65c26a02014-06-03 13:18:18 -040023 * D. P. Gilbert (dgilbert@interlog.com), notes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
25 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
26 * (otherwise the macros compile to empty statements).
27 *
28 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/string.h>
35#include <linux/mm.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070036#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/errno.h>
38#include <linux/mtio.h>
39#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/fcntl.h>
42#include <linux/init.h>
43#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/cdev.h>
James Bottomley7c07d612007-08-05 13:36:11 -050046#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/seq_file.h>
48#include <linux/blkdev.h>
49#include <linux/delay.h>
Christof Schmitt6da127a2008-01-11 10:09:43 +010050#include <linux/blktrace_api.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020051#include <linux/mutex.h>
Christian Dietrich2fe038e2011-06-04 17:36:10 +020052#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "scsi.h"
db9dff32005-04-03 14:53:59 -050055#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <scsi/scsi_driver.h>
58#include <scsi/scsi_ioctl.h>
59#include <scsi/sg.h>
60
61#include "scsi_logging.h"
62
63#ifdef CONFIG_SCSI_PROC_FS
64#include <linux/proc_fs.h>
Douglas Gilbert65c26a02014-06-03 13:18:18 -040065static char *sg_version_date = "20140603";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int sg_proc_init(void);
68static void sg_proc_cleanup(void);
69#endif
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SG_ALLOW_DIO_DEF 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define SG_MAX_DEVS 32768
74
Douglas Gilbert65c26a02014-06-03 13:18:18 -040075/* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
76 * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
77 * than 16 bytes are "variable length" whose length is a multiple of 4
78 */
79#define SG_MAX_CDB_SIZE 252
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
83 * Then when using 32 bit integers x * m may overflow during the calculation.
84 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
85 * calculates the same, but prevents the overflow when both m and d
86 * are "small" numbers (like HZ and USER_HZ).
87 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
88 * in 32 bits.
89 */
90#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
91
92#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
93
94int sg_big_buff = SG_DEF_RESERVED_SIZE;
95/* N.B. This variable is readable and writeable via
96 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
97 of this size (or less if there is not enough memory) will be reserved
98 for use by this file descriptor. [Deprecated usage: this variable is also
99 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
100 the kernel (i.e. it is not a module).] */
101static int def_reserved_size = -1; /* picks up init parameter */
102static int sg_allow_dio = SG_ALLOW_DIO_DEF;
103
Douglas Gilbert6460e752006-09-20 18:20:49 -0400104static int scatter_elem_sz = SG_SCATTER_SZ;
105static int scatter_elem_sz_prev = SG_SCATTER_SZ;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define SG_SECTOR_SZ 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Tony Jonesee959b02008-02-22 00:13:36 +0100109static int sg_add(struct device *, struct class_interface *);
110static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
James Bottomley98481ff2013-10-25 10:26:38 +0100112static DEFINE_SPINLOCK(sg_open_exclusive_lock);
113
James Bottomley7c07d612007-08-05 13:36:11 -0500114static DEFINE_IDR(sg_index_idr);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100115static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
116 file descriptor list for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100119 .add_dev = sg_add,
120 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
124 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900125 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200127 struct page **pages;
128 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
130 unsigned char cmd_opcode; /* first byte of command */
131} Sg_scatter_hold;
132
133struct sg_device; /* forward declarations */
134struct sg_fd;
135
136typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct sg_request *nextrp; /* NULL -> tail request (slist) */
138 struct sg_fd *parentfp; /* NULL -> not in use */
139 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
140 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600141 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
143 char orphan; /* 1 -> drop on sight, 0 -> normal */
144 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
Jörn Engel6acddc52012-04-12 17:33:58 -0400145 /* done protected by rq_list_lock */
146 char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900147 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900148 struct bio *bio;
FUJITA Tomonoric96952e2009-02-04 11:36:27 +0900149 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150} Sg_request;
151
152typedef struct sg_fd { /* holds the state of a file descriptor */
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100153 /* sfd_siblings is protected by sg_index_lock */
154 struct list_head sfd_siblings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct sg_device *parentdp; /* owning device */
156 wait_queue_head_t read_wait; /* queue read until command done */
157 rwlock_t rq_list_lock; /* protect access to list in req_arr */
158 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
159 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
160 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
161 unsigned save_scat_len; /* original length of trunc. scat. element */
162 Sg_request *headrp; /* head of request slist, NULL->empty */
163 struct fasync_struct *async_qp; /* used by asynchronous notification */
164 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
165 char low_dma; /* as in parent but possibly overridden to 1 */
166 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
Douglas Gilbert65c26a02014-06-03 13:18:18 -0400168 unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
170 char mmap_called; /* 0 -> mmap() never called on this fd */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500171 struct kref f_ref;
172 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173} Sg_fd;
174
175typedef struct sg_device { /* holds the state of each scsi generic device */
176 struct scsi_device *device;
James Bottomley065b4a22013-10-25 10:27:02 +0100177 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500179 u32 index; /* device index number */
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100180 /* sfds is protected by sg_index_lock */
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900181 struct list_head sfds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 volatile char detached; /* 0->attached, 1->detached pending removal */
James Bottomley98481ff2013-10-25 10:26:38 +0100183 /* exclude protected by sg_open_exclusive_lock */
Jörn Engelb499e522012-04-24 16:13:11 -0400184 char exclude; /* opened for exclusive access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
186 struct gendisk *disk;
187 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500188 struct kref d_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189} Sg_device;
190
Mike Christied6b10342005-11-08 04:06:41 -0600191/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900192static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900193static int sg_start_req(Sg_request *srp, unsigned char *cmd);
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +0900194static int sg_finish_rem_req(Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
197 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200198static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
199 const char __user *buf, size_t count, int blocking,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500200 int read_only, int sg_io_owned, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
202 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
204static void sg_remove_scat(Sg_scatter_hold * schp);
205static void sg_build_reserve(Sg_fd * sfp, int req_size);
206static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
207static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500209static void sg_remove_sfp(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
211static Sg_request *sg_add_request(Sg_fd * sfp);
212static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
213static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static Sg_device *sg_get_dev(int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500215static void sg_put_dev(Sg_device *sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define SZ_SG_HEADER sizeof(struct sg_header)
218#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
219#define SZ_SG_IOVEC sizeof(sg_iovec_t)
220#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
221
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900222static int sg_allow_access(struct file *filp, unsigned char *cmd)
223{
Joe Perches35df8392010-09-04 18:52:46 -0700224 struct sg_fd *sfp = filp->private_data;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900225
226 if (sfp->parentdp->device->type == TYPE_SCANNER)
227 return 0;
228
Jens Axboe018e0442009-06-26 16:27:10 +0200229 return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900230}
231
James Bottomley98481ff2013-10-25 10:26:38 +0100232static int get_exclude(Sg_device *sdp)
233{
234 unsigned long flags;
235 int ret;
236
237 spin_lock_irqsave(&sg_open_exclusive_lock, flags);
238 ret = sdp->exclude;
239 spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
240 return ret;
241}
242
243static int set_exclude(Sg_device *sdp, char val)
244{
245 unsigned long flags;
246
247 spin_lock_irqsave(&sg_open_exclusive_lock, flags);
248 sdp->exclude = val;
249 spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
250 return val;
251}
252
Jörn Engel035d12e2012-04-25 11:17:29 -0400253static int sfds_list_empty(Sg_device *sdp)
254{
255 unsigned long flags;
256 int ret;
257
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100258 read_lock_irqsave(&sg_index_lock, flags);
Jörn Engel035d12e2012-04-25 11:17:29 -0400259 ret = list_empty(&sdp->sfds);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100260 read_unlock_irqrestore(&sg_index_lock, flags);
Jörn Engel035d12e2012-04-25 11:17:29 -0400261 return ret;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int
265sg_open(struct inode *inode, struct file *filp)
266{
267 int dev = iminor(inode);
268 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600269 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 Sg_device *sdp;
271 Sg_fd *sfp;
James Bottomley065b4a22013-10-25 10:27:02 +0100272 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int retval;
274
275 nonseekable_open(inode, filp);
276 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
277 sdp = sg_get_dev(dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500278 if (IS_ERR(sdp)) {
279 retval = PTR_ERR(sdp);
280 sdp = NULL;
281 goto sg_put;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* This driver's module count bumped by fops_get in <linux/fs.h> */
285 /* Prevent the device driver from vanishing while we sleep */
286 retval = scsi_device_get(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500287 if (retval)
288 goto sg_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Alan Sternbc4f2402010-06-17 10:41:42 -0400290 retval = scsi_autopm_get_device(sdp->device);
291 if (retval)
292 goto sdp_put;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!((flags & O_NONBLOCK) ||
295 scsi_block_when_processing_errors(sdp->device))) {
296 retval = -ENXIO;
297 /* we are in error recovery for this device */
298 goto error_out;
299 }
300
James Bottomley065b4a22013-10-25 10:27:02 +0100301 if (flags & O_EXCL) {
302 if (O_RDONLY == (flags & O_ACCMODE)) {
303 retval = -EPERM; /* Can't lock it with read only access */
304 goto error_out;
Vaughan Cao15b06f92013-08-29 10:00:36 +0800305 }
James Bottomley065b4a22013-10-25 10:27:02 +0100306 if (!sfds_list_empty(sdp) && (flags & O_NONBLOCK)) {
307 retval = -EBUSY;
308 goto error_out;
309 }
310 res = wait_event_interruptible(sdp->o_excl_wait,
311 ((!sfds_list_empty(sdp) || get_exclude(sdp)) ? 0 : set_exclude(sdp, 1)));
312 if (res) {
313 retval = res; /* -ERESTARTSYS because signal hit process */
314 goto error_out;
315 }
316 } else if (get_exclude(sdp)) { /* some other fd has an exclusive lock on dev */
317 if (flags & O_NONBLOCK) {
318 retval = -EBUSY;
319 goto error_out;
320 }
321 res = wait_event_interruptible(sdp->o_excl_wait, !get_exclude(sdp));
322 if (res) {
323 retval = res; /* -ERESTARTSYS because signal hit process */
324 goto error_out;
325 }
Vaughan Cao15b06f92013-08-29 10:00:36 +0800326 }
James Bottomleybafc8ad2013-10-25 10:25:14 +0100327 if (sdp->detached) {
328 retval = -ENODEV;
James Bottomley065b4a22013-10-25 10:27:02 +0100329 goto error_out;
James Bottomleybafc8ad2013-10-25 10:25:14 +0100330 }
Jörn Engel035d12e2012-04-25 11:17:29 -0400331 if (sfds_list_empty(sdp)) { /* no existing opens on this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600333 q = sdp->device->request_queue;
Martin K. Petersen8a783622010-02-26 00:20:39 -0500334 sdp->sg_tablesize = queue_max_segments(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
James Bottomleybafc8ad2013-10-25 10:25:14 +0100336 if ((sfp = sg_add_sfp(sdp, dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 filp->private_data = sfp;
338 else {
Tony Battersbyc6517b792009-01-21 14:45:50 -0500339 if (flags & O_EXCL) {
James Bottomley98481ff2013-10-25 10:26:38 +0100340 set_exclude(sdp, 0); /* undo if error */
James Bottomley065b4a22013-10-25 10:27:02 +0100341 wake_up_interruptible(&sdp->o_excl_wait);
342 }
343 retval = -ENOMEM;
344 goto error_out;
345 }
346 retval = 0;
Tony Battersbyc6517b792009-01-21 14:45:50 -0500347error_out:
James Bottomley065b4a22013-10-25 10:27:02 +0100348 if (retval) {
Alan Sternbc4f2402010-06-17 10:41:42 -0400349 scsi_autopm_put_device(sdp->device);
350sdp_put:
Tony Battersbyc6517b792009-01-21 14:45:50 -0500351 scsi_device_put(sdp->device);
Alan Sternbc4f2402010-06-17 10:41:42 -0400352 }
Tony Battersbyc6517b792009-01-21 14:45:50 -0500353sg_put:
354 if (sdp)
355 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return retval;
357}
358
359/* Following function was formerly called 'sg_close' */
360static int
361sg_release(struct inode *inode, struct file *filp)
362{
363 Sg_device *sdp;
364 Sg_fd *sfp;
365
366 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
367 return -ENXIO;
368 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
Tony Battersbyc6517b792009-01-21 14:45:50 -0500369
James Bottomley98481ff2013-10-25 10:26:38 +0100370 set_exclude(sdp, 0);
James Bottomley065b4a22013-10-25 10:27:02 +0100371 wake_up_interruptible(&sdp->o_excl_wait);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500372
Alan Sternbc4f2402010-06-17 10:41:42 -0400373 scsi_autopm_put_device(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500374 kref_put(&sfp->f_ref, sg_remove_sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376}
377
378static ssize_t
379sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 Sg_device *sdp;
382 Sg_fd *sfp;
383 Sg_request *srp;
384 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600386 struct sg_header *old_hdr = NULL;
387 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
390 return -ENXIO;
391 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
392 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!access_ok(VERIFY_WRITE, buf, count))
395 return -EFAULT;
396 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600397 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
398 if (!old_hdr)
399 return -ENOMEM;
400 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
401 retval = -EFAULT;
402 goto free_old_hdr;
403 }
404 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600406 sg_io_hdr_t *new_hdr;
407 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
408 if (!new_hdr) {
409 retval = -ENOMEM;
410 goto free_old_hdr;
411 }
412 retval =__copy_from_user
413 (new_hdr, buf, SZ_SG_IO_HDR);
414 req_pack_id = new_hdr->pack_id;
415 kfree(new_hdr);
416 if (retval) {
417 retval = -EFAULT;
418 goto free_old_hdr;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 } else
cb59e842005-04-02 13:51:23 -0600422 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 srp = sg_get_rq_mark(sfp, req_pack_id);
425 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600426 if (sdp->detached) {
427 retval = -ENODEV;
428 goto free_old_hdr;
429 }
430 if (filp->f_flags & O_NONBLOCK) {
431 retval = -EAGAIN;
432 goto free_old_hdr;
433 }
Jörn Engel3f0c6ab2012-04-12 17:33:25 -0400434 retval = wait_event_interruptible(sfp->read_wait,
Jörn Engel794c10f2012-04-12 17:32:48 -0400435 (sdp->detached ||
Jörn Engel3f0c6ab2012-04-12 17:33:25 -0400436 (srp = sg_get_rq_mark(sfp, req_pack_id))));
Jörn Engel794c10f2012-04-12 17:32:48 -0400437 if (sdp->detached) {
438 retval = -ENODEV;
439 goto free_old_hdr;
440 }
441 if (retval) {
cb59e842005-04-02 13:51:23 -0600442 /* -ERESTARTSYS as signal hit process */
443 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 }
cb59e842005-04-02 13:51:23 -0600446 if (srp->header.interface_id != '\0') {
447 retval = sg_new_read(sfp, buf, count, srp);
448 goto free_old_hdr;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600452 if (old_hdr == NULL) {
453 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
454 if (! old_hdr) {
455 retval = -ENOMEM;
456 goto free_old_hdr;
457 }
458 }
459 memset(old_hdr, 0, SZ_SG_HEADER);
460 old_hdr->reply_len = (int) hp->timeout;
461 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
462 old_hdr->pack_id = hp->pack_id;
463 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600465 old_hdr->target_status = hp->masked_status;
466 old_hdr->host_status = hp->host_status;
467 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if ((CHECK_CONDITION & hp->masked_status) ||
469 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600470 memcpy(old_hdr->sense_buffer, srp->sense_b,
471 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 switch (hp->host_status) {
473 /* This setup of 'result' is for backward compatibility and is best
474 ignored by the user who should use target, host + driver status */
475 case DID_OK:
476 case DID_PASSTHROUGH:
477 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600478 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case DID_NO_CONNECT:
481 case DID_BUS_BUSY:
482 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600483 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case DID_BAD_TARGET:
486 case DID_ABORT:
487 case DID_PARITY:
488 case DID_RESET:
489 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600490 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600493 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 hp->masked_status == GOOD) ? 0 : EIO;
495 break;
496 default:
cb59e842005-04-02 13:51:23 -0600497 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 }
500
501 /* Now copy the result back to the user buffer. */
502 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600503 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
504 retval = -EFAULT;
505 goto free_old_hdr;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600508 if (count > old_hdr->reply_len)
509 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600511 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
512 retval = -EFAULT;
513 goto free_old_hdr;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 } else
cb59e842005-04-02 13:51:23 -0600517 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600519 retval = count;
520free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800521 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600522 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525static ssize_t
526sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
527{
528 sg_io_hdr_t *hp = &srp->header;
529 int err = 0;
530 int len;
531
532 if (count < SZ_SG_IO_HDR) {
533 err = -EINVAL;
534 goto err_out;
535 }
536 hp->sb_len_wr = 0;
537 if ((hp->mx_sb_len > 0) && hp->sbp) {
538 if ((CHECK_CONDITION & hp->masked_status) ||
539 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600540 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
542 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
543 len = (len > sb_len) ? sb_len : len;
544 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
545 err = -EFAULT;
546 goto err_out;
547 }
548 hp->sb_len_wr = len;
549 }
550 }
551 if (hp->masked_status || hp->host_status || hp->driver_status)
552 hp->info |= SG_INFO_CHECK;
553 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
554 err = -EFAULT;
555 goto err_out;
556 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900557err_out:
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +0900558 err = sg_finish_rem_req(srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return (0 == err) ? count : err;
560}
561
562static ssize_t
563sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
564{
565 int mxsize, cmd_size, k;
566 int input_size, blocking;
567 unsigned char opcode;
568 Sg_device *sdp;
569 Sg_fd *sfp;
570 Sg_request *srp;
571 struct sg_header old_hdr;
572 sg_io_hdr_t *hp;
Douglas Gilbert65c26a02014-06-03 13:18:18 -0400573 unsigned char cmnd[SG_MAX_CDB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
576 return -ENXIO;
577 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
578 sdp->disk->disk_name, (int) count));
579 if (sdp->detached)
580 return -ENODEV;
581 if (!((filp->f_flags & O_NONBLOCK) ||
582 scsi_block_when_processing_errors(sdp->device)))
583 return -ENXIO;
584
585 if (!access_ok(VERIFY_READ, buf, count))
586 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
587 if (count < SZ_SG_HEADER)
588 return -EIO;
589 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
590 return -EFAULT;
591 blocking = !(filp->f_flags & O_NONBLOCK);
592 if (old_hdr.reply_len < 0)
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500593 return sg_new_write(sfp, filp, buf, count,
594 blocking, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (count < (SZ_SG_HEADER + 6))
596 return -EIO; /* The minimum scsi command length is 6 bytes. */
597
598 if (!(srp = sg_add_request(sfp))) {
599 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
600 return -EDOM;
601 }
602 buf += SZ_SG_HEADER;
603 __get_user(opcode, buf);
604 if (sfp->next_cmd_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 cmd_size = sfp->next_cmd_len;
606 sfp->next_cmd_len = 0; /* reset so only this write() effected */
607 } else {
608 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
609 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
610 cmd_size = 12;
611 }
612 SCSI_LOG_TIMEOUT(4, printk(
613 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
614/* Determine buffer size. */
615 input_size = count - cmd_size;
616 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
617 mxsize -= SZ_SG_HEADER;
618 input_size -= SZ_SG_HEADER;
619 if (input_size < 0) {
620 sg_remove_request(sfp, srp);
621 return -EIO; /* User did not pass enough bytes for this command. */
622 }
623 hp = &srp->header;
624 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
625 hp->cmd_len = (unsigned char) cmd_size;
626 hp->iovec_count = 0;
627 hp->mx_sb_len = 0;
628 if (input_size > 0)
629 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
630 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
631 else
632 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
633 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900634 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
635 hp->dxferp = (char __user *)buf + cmd_size;
636 else
637 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 hp->sbp = NULL;
639 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
640 hp->flags = input_size; /* structure abuse ... */
641 hp->pack_id = old_hdr.pack_id;
642 hp->usr_ptr = NULL;
643 if (__copy_from_user(cmnd, buf, cmd_size))
644 return -EFAULT;
645 /*
646 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
647 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
648 * is a non-zero input_size, so emit a warning.
649 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100650 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
651 static char cmd[TASK_COMM_LEN];
Christian Dietrich2fe038e2011-06-04 17:36:10 +0200652 if (strcmp(current->comm, cmd)) {
653 printk_ratelimited(KERN_WARNING
654 "sg_write: data in/out %d/%d bytes "
655 "for SCSI command 0x%x-- guessing "
656 "data in;\n program %s not setting "
657 "count and/or reply_len properly\n",
658 old_hdr.reply_len - (int)SZ_SG_HEADER,
659 input_size, (unsigned int) cmnd[0],
660 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100661 strcpy(cmd, current->comm);
662 }
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
665 return (k < 0) ? k : count;
666}
667
668static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200669sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500670 size_t count, int blocking, int read_only, int sg_io_owned,
Adel Gadllah0b07de82008-06-26 13:48:27 +0200671 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int k;
674 Sg_request *srp;
675 sg_io_hdr_t *hp;
Douglas Gilbert65c26a02014-06-03 13:18:18 -0400676 unsigned char cmnd[SG_MAX_CDB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int timeout;
678 unsigned long ul_timeout;
679
680 if (count < SZ_SG_IO_HDR)
681 return -EINVAL;
682 if (!access_ok(VERIFY_READ, buf, count))
683 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
684
685 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
686 if (!(srp = sg_add_request(sfp))) {
687 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
688 return -EDOM;
689 }
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500690 srp->sg_io_owned = sg_io_owned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 hp = &srp->header;
692 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
693 sg_remove_request(sfp, srp);
694 return -EFAULT;
695 }
696 if (hp->interface_id != 'S') {
697 sg_remove_request(sfp, srp);
698 return -ENOSYS;
699 }
700 if (hp->flags & SG_FLAG_MMAP_IO) {
701 if (hp->dxfer_len > sfp->reserve.bufflen) {
702 sg_remove_request(sfp, srp);
703 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
704 }
705 if (hp->flags & SG_FLAG_DIRECT_IO) {
706 sg_remove_request(sfp, srp);
707 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
708 }
709 if (sg_res_in_use(sfp)) {
710 sg_remove_request(sfp, srp);
711 return -EBUSY; /* reserve buffer already being used */
712 }
713 }
714 ul_timeout = msecs_to_jiffies(srp->header.timeout);
715 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
716 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
717 sg_remove_request(sfp, srp);
718 return -EMSGSIZE;
719 }
720 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
721 sg_remove_request(sfp, srp);
722 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
723 }
724 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
725 sg_remove_request(sfp, srp);
726 return -EFAULT;
727 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900728 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 sg_remove_request(sfp, srp);
730 return -EPERM;
731 }
732 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
733 if (k < 0)
734 return k;
735 if (o_srp)
736 *o_srp = srp;
737 return count;
738}
739
740static int
741sg_common_write(Sg_fd * sfp, Sg_request * srp,
742 unsigned char *cmnd, int timeout, int blocking)
743{
Mike Christied6b10342005-11-08 04:06:41 -0600744 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 Sg_device *sdp = sfp->parentdp;
746 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
749 hp->status = 0;
750 hp->masked_status = 0;
751 hp->msg_status = 0;
752 hp->info = 0;
753 hp->host_status = 0;
754 hp->driver_status = 0;
755 hp->resid = 0;
756 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
757 (int) cmnd[0], (int) hp->cmd_len));
758
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900759 k = sg_start_req(srp, cmnd);
760 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400761 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sg_finish_rem_req(srp);
763 return k; /* probably out of space --> ENOMEM */
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (sdp->detached) {
FUJITA Tomonoricaf19d32010-07-22 09:36:51 +0900766 if (srp->bio)
767 blk_end_request_all(srp->rq, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sg_finish_rem_req(srp);
769 return -ENODEV;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 switch (hp->dxfer_direction) {
773 case SG_DXFER_TO_FROM_DEV:
774 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600775 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600778 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600781 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783 default:
Mike Christied6b10342005-11-08 04:06:41 -0600784 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786 }
cb59e842005-04-02 13:51:23 -0600787 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200788
789 srp->rq->timeout = timeout;
Tony Battersbyc6517b792009-01-21 14:45:50 -0500790 kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200791 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
792 srp->rq, 1, sg_rq_end_io);
793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Jörn Engel6acddc52012-04-12 17:33:58 -0400796static int srp_done(Sg_fd *sfp, Sg_request *srp)
797{
798 unsigned long flags;
799 int ret;
800
801 read_lock_irqsave(&sfp->rq_list_lock, flags);
802 ret = srp->done;
803 read_unlock_irqrestore(&sfp->rq_list_lock, flags);
804 return ret;
805}
806
Akinobu Mita46f69e62014-06-02 22:56:46 +0900807static int max_sectors_bytes(struct request_queue *q)
808{
809 unsigned int max_sectors = queue_max_sectors(q);
810
811 max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
812
813 return max_sectors << 9;
814}
815
Jörn Engel37b9d1e2012-04-12 17:35:05 -0400816static long
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200817sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 void __user *p = (void __user *)arg;
820 int __user *ip = p;
821 int result, val, read_only;
822 Sg_device *sdp;
823 Sg_fd *sfp;
824 Sg_request *srp;
825 unsigned long iflags;
826
827 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
828 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
831 sdp->disk->disk_name, (int) cmd_in));
832 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
833
834 switch (cmd_in) {
835 case SG_IO:
Jörn Engeldddbf8d2012-04-12 17:32:17 -0400836 if (sdp->detached)
837 return -ENODEV;
838 if (!scsi_block_when_processing_errors(sdp->device))
839 return -ENXIO;
840 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
841 return -EFAULT;
842 result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
843 1, read_only, 1, &srp);
844 if (result < 0)
845 return result;
Jörn Engel3f0c6ab2012-04-12 17:33:25 -0400846 result = wait_event_interruptible(sfp->read_wait,
Jörn Engel6acddc52012-04-12 17:33:58 -0400847 (srp_done(sfp, srp) || sdp->detached));
Jörn Engel794c10f2012-04-12 17:32:48 -0400848 if (sdp->detached)
849 return -ENODEV;
850 write_lock_irq(&sfp->rq_list_lock);
851 if (srp->done) {
852 srp->done = 2;
Jörn Engeldddbf8d2012-04-12 17:32:17 -0400853 write_unlock_irq(&sfp->rq_list_lock);
Jörn Engel794c10f2012-04-12 17:32:48 -0400854 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
855 return (result < 0) ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Jörn Engel794c10f2012-04-12 17:32:48 -0400857 srp->orphan = 1;
858 write_unlock_irq(&sfp->rq_list_lock);
859 return result; /* -ERESTARTSYS because signal hit process */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 case SG_SET_TIMEOUT:
861 result = get_user(val, ip);
862 if (result)
863 return result;
864 if (val < 0)
865 return -EIO;
866 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
867 val = MULDIV (INT_MAX, USER_HZ, HZ);
868 sfp->timeout_user = val;
869 sfp->timeout = MULDIV (val, HZ, USER_HZ);
870
871 return 0;
872 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
873 /* strange ..., for backward compatibility */
874 return sfp->timeout_user;
875 case SG_SET_FORCE_LOW_DMA:
876 result = get_user(val, ip);
877 if (result)
878 return result;
879 if (val) {
880 sfp->low_dma = 1;
881 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
882 val = (int) sfp->reserve.bufflen;
883 sg_remove_scat(&sfp->reserve);
884 sg_build_reserve(sfp, val);
885 }
886 } else {
887 if (sdp->detached)
888 return -ENODEV;
889 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
890 }
891 return 0;
892 case SG_GET_LOW_DMA:
893 return put_user((int) sfp->low_dma, ip);
894 case SG_GET_SCSI_ID:
895 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
896 return -EFAULT;
897 else {
898 sg_scsi_id_t __user *sg_idp = p;
899
900 if (sdp->detached)
901 return -ENODEV;
902 __put_user((int) sdp->device->host->host_no,
903 &sg_idp->host_no);
904 __put_user((int) sdp->device->channel,
905 &sg_idp->channel);
906 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
907 __put_user((int) sdp->device->lun, &sg_idp->lun);
908 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
909 __put_user((short) sdp->device->host->cmd_per_lun,
910 &sg_idp->h_cmd_per_lun);
911 __put_user((short) sdp->device->queue_depth,
912 &sg_idp->d_queue_depth);
913 __put_user(0, &sg_idp->unused[0]);
914 __put_user(0, &sg_idp->unused[1]);
915 return 0;
916 }
917 case SG_SET_FORCE_PACK_ID:
918 result = get_user(val, ip);
919 if (result)
920 return result;
921 sfp->force_packid = val ? 1 : 0;
922 return 0;
923 case SG_GET_PACK_ID:
924 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
925 return -EFAULT;
926 read_lock_irqsave(&sfp->rq_list_lock, iflags);
927 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
928 if ((1 == srp->done) && (!srp->sg_io_owned)) {
929 read_unlock_irqrestore(&sfp->rq_list_lock,
930 iflags);
931 __put_user(srp->header.pack_id, ip);
932 return 0;
933 }
934 }
935 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
936 __put_user(-1, ip);
937 return 0;
938 case SG_GET_NUM_WAITING:
939 read_lock_irqsave(&sfp->rq_list_lock, iflags);
940 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
941 if ((1 == srp->done) && (!srp->sg_io_owned))
942 ++val;
943 }
944 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
945 return put_user(val, ip);
946 case SG_GET_SG_TABLESIZE:
947 return put_user(sdp->sg_tablesize, ip);
948 case SG_SET_RESERVED_SIZE:
949 result = get_user(val, ip);
950 if (result)
951 return result;
952 if (val < 0)
953 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500954 val = min_t(int, val,
Akinobu Mita46f69e62014-06-02 22:56:46 +0900955 max_sectors_bytes(sdp->device->request_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (val != sfp->reserve.bufflen) {
957 if (sg_res_in_use(sfp) || sfp->mmap_called)
958 return -EBUSY;
959 sg_remove_scat(&sfp->reserve);
960 sg_build_reserve(sfp, val);
961 }
962 return 0;
963 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500964 val = min_t(int, sfp->reserve.bufflen,
Akinobu Mita46f69e62014-06-02 22:56:46 +0900965 max_sectors_bytes(sdp->device->request_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return put_user(val, ip);
967 case SG_SET_COMMAND_Q:
968 result = get_user(val, ip);
969 if (result)
970 return result;
971 sfp->cmd_q = val ? 1 : 0;
972 return 0;
973 case SG_GET_COMMAND_Q:
974 return put_user((int) sfp->cmd_q, ip);
975 case SG_SET_KEEP_ORPHAN:
976 result = get_user(val, ip);
977 if (result)
978 return result;
979 sfp->keep_orphan = val;
980 return 0;
981 case SG_GET_KEEP_ORPHAN:
982 return put_user((int) sfp->keep_orphan, ip);
983 case SG_NEXT_CMD_LEN:
984 result = get_user(val, ip);
985 if (result)
986 return result;
987 sfp->next_cmd_len = (val > 0) ? val : 0;
988 return 0;
989 case SG_GET_VERSION_NUM:
990 return put_user(sg_version_num, ip);
991 case SG_GET_ACCESS_COUNT:
992 /* faked - we don't have a real access count anymore */
993 val = (sdp->device ? 1 : 0);
994 return put_user(val, ip);
995 case SG_GET_REQUEST_TABLE:
996 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
997 return -EFAULT;
998 else {
cb59e842005-04-02 13:51:23 -0600999 sg_req_info_t *rinfo;
1000 unsigned int ms;
1001
1002 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
1003 GFP_KERNEL);
1004 if (!rinfo)
1005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1007 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
1008 ++val, srp = srp ? srp->nextrp : srp) {
1009 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
1010 if (srp) {
1011 rinfo[val].req_state = srp->done + 1;
1012 rinfo[val].problem =
1013 srp->header.masked_status &
1014 srp->header.host_status &
1015 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -06001016 if (srp->done)
1017 rinfo[val].duration =
1018 srp->header.duration;
1019 else {
1020 ms = jiffies_to_msecs(jiffies);
1021 rinfo[val].duration =
1022 (ms > srp->header.duration) ?
1023 (ms - srp->header.duration) : 0;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001026 rinfo[val].sg_io_owned =
1027 srp->sg_io_owned;
1028 rinfo[val].pack_id =
1029 srp->header.pack_id;
1030 rinfo[val].usr_ptr =
1031 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 }
1034 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001035 result = __copy_to_user(p, rinfo,
1036 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1037 result = result ? -EFAULT : 0;
1038 kfree(rinfo);
1039 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 case SG_EMULATED_HOST:
1042 if (sdp->detached)
1043 return -ENODEV;
1044 return put_user(sdp->device->host->hostt->emulated, ip);
1045 case SG_SCSI_RESET:
1046 if (sdp->detached)
1047 return -ENODEV;
1048 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001049 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -EBUSY;
1051 } else if (!scsi_block_when_processing_errors(sdp->device))
1052 return -EBUSY;
1053 result = get_user(val, ip);
1054 if (result)
1055 return result;
1056 if (SG_SCSI_RESET_NOTHING == val)
1057 return 0;
1058 switch (val) {
1059 case SG_SCSI_RESET_DEVICE:
1060 val = SCSI_TRY_RESET_DEVICE;
1061 break;
Brian King39120e12008-07-01 13:03:19 -05001062 case SG_SCSI_RESET_TARGET:
1063 val = SCSI_TRY_RESET_TARGET;
1064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 case SG_SCSI_RESET_BUS:
1066 val = SCSI_TRY_RESET_BUS;
1067 break;
1068 case SG_SCSI_RESET_HOST:
1069 val = SCSI_TRY_RESET_HOST;
1070 break;
1071 default:
1072 return -EINVAL;
1073 }
1074 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1075 return -EACCES;
1076 return (scsi_reset_provider(sdp->device, val) ==
1077 SUCCESS) ? 0 : -EIO;
1078 case SCSI_IOCTL_SEND_COMMAND:
1079 if (sdp->detached)
1080 return -ENODEV;
1081 if (read_only) {
1082 unsigned char opcode = WRITE_6;
1083 Scsi_Ioctl_Command __user *siocp = p;
1084
1085 if (copy_from_user(&opcode, siocp->data, 1))
1086 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001087 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EPERM;
1089 }
Al Viroe915e872008-09-02 17:16:41 -04001090 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 case SG_SET_DEBUG:
1092 result = get_user(val, ip);
1093 if (result)
1094 return result;
1095 sdp->sgdebug = (char) val;
1096 return 0;
1097 case SCSI_IOCTL_GET_IDLUN:
1098 case SCSI_IOCTL_GET_BUS_NUMBER:
1099 case SCSI_IOCTL_PROBE_HOST:
1100 case SG_GET_TRANSFORM:
1101 if (sdp->detached)
1102 return -ENODEV;
1103 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001104 case BLKSECTGET:
Akinobu Mita46f69e62014-06-02 22:56:46 +09001105 return put_user(max_sectors_bytes(sdp->device->request_queue),
Alan Stern44ec9542007-02-20 11:01:57 -05001106 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001107 case BLKTRACESETUP:
1108 return blk_trace_setup(sdp->device->request_queue,
1109 sdp->disk->disk_name,
Martin Peschke76e3a192009-01-30 15:46:23 +01001110 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
Shawn Dud0deef52009-04-14 13:58:56 +08001111 NULL,
Christof Schmitt6da127a2008-01-11 10:09:43 +01001112 (char *)arg);
1113 case BLKTRACESTART:
1114 return blk_trace_startstop(sdp->device->request_queue, 1);
1115 case BLKTRACESTOP:
1116 return blk_trace_startstop(sdp->device->request_queue, 0);
1117 case BLKTRACETEARDOWN:
1118 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 default:
1120 if (read_only)
1121 return -EPERM; /* don't know so take safe approach */
1122 return scsi_ioctl(sdp->device, cmd_in, p);
1123 }
1124}
1125
1126#ifdef CONFIG_COMPAT
1127static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1128{
1129 Sg_device *sdp;
1130 Sg_fd *sfp;
1131 struct scsi_device *sdev;
1132
1133 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1134 return -ENXIO;
1135
1136 sdev = sdp->device;
1137 if (sdev->host->hostt->compat_ioctl) {
1138 int ret;
1139
1140 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1141
1142 return ret;
1143 }
1144
1145 return -ENOIOCTLCMD;
1146}
1147#endif
1148
1149static unsigned int
1150sg_poll(struct file *filp, poll_table * wait)
1151{
1152 unsigned int res = 0;
1153 Sg_device *sdp;
1154 Sg_fd *sfp;
1155 Sg_request *srp;
1156 int count = 0;
1157 unsigned long iflags;
1158
Jörn Engelebaf4662012-04-12 17:33:39 -04001159 sfp = filp->private_data;
1160 if (!sfp)
1161 return POLLERR;
1162 sdp = sfp->parentdp;
1163 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return POLLERR;
1165 poll_wait(filp, &sfp->read_wait, wait);
1166 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1167 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1168 /* if any read waiting, flag it */
1169 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1170 res = POLLIN | POLLRDNORM;
1171 ++count;
1172 }
1173 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1174
1175 if (sdp->detached)
1176 res |= POLLHUP;
1177 else if (!sfp->cmd_q) {
1178 if (0 == count)
1179 res |= POLLOUT | POLLWRNORM;
1180 } else if (count < SG_MAX_QUEUE)
1181 res |= POLLOUT | POLLWRNORM;
1182 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1183 sdp->disk->disk_name, (int) res));
1184 return res;
1185}
1186
1187static int
1188sg_fasync(int fd, struct file *filp, int mode)
1189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 Sg_device *sdp;
1191 Sg_fd *sfp;
1192
1193 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1194 return -ENXIO;
1195 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1196 sdp->disk->disk_name, mode));
1197
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001198 return fasync_helper(fd, filp, mode, &sfp->async_qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Nick Piggina13ff0b2008-02-07 18:46:06 -08001201static int
1202sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001205 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001207 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001210 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001212 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001214 return VM_FAULT_SIGBUS;
1215 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001217 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001218 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1219 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001220 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001221 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001222 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001223 struct page *page = nth_page(rsv_schp->pages[k],
1224 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001225 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001226 vmf->page = page;
1227 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Mike Christied6b10342005-11-08 04:06:41 -06001229 sa += len;
1230 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Mike Christied6b10342005-11-08 04:06:41 -06001232
Nick Piggina13ff0b2008-02-07 18:46:06 -08001233 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001236static const struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001237 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238};
1239
1240static int
1241sg_mmap(struct file *filp, struct vm_area_struct *vma)
1242{
1243 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001244 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001246 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1249 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001250 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1252 (void *) vma->vm_start, (int) req_sz));
1253 if (vma->vm_pgoff)
1254 return -EINVAL; /* want no offset */
1255 rsv_schp = &sfp->reserve;
1256 if (req_sz > rsv_schp->bufflen)
1257 return -ENOMEM; /* cannot map more than reserved buffer */
1258
Mike Christied6b10342005-11-08 04:06:41 -06001259 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001260 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1261 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001262 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001263 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001264 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Mike Christied6b10342005-11-08 04:06:41 -06001266
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001267 sfp->mmap_called = 1;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001268 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 vma->vm_private_data = sfp;
1270 vma->vm_ops = &sg_mmap_vm_ops;
1271 return 0;
1272}
1273
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001274static void sg_rq_end_io_usercontext(struct work_struct *work)
1275{
1276 struct sg_request *srp = container_of(work, struct sg_request, ew.work);
1277 struct sg_fd *sfp = srp->parentfp;
1278
1279 sg_finish_rem_req(srp);
1280 kref_put(&sfp->f_ref, sg_remove_sfp);
1281}
1282
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001283/*
1284 * This function is a "bottom half" handler that is called by the mid
1285 * level when a command is completed (or has failed).
1286 */
1287static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001289 struct sg_request *srp = rq->end_io_data;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001290 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001293 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001294 char *sense;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001295 int result, resid, done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Tony Battersbyc6517b792009-01-21 14:45:50 -05001297 if (WARN_ON(srp->done != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 sfp = srp->parentfp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001301 if (WARN_ON(sfp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001303
1304 sdp = sfp->parentdp;
1305 if (unlikely(sdp->detached))
1306 printk(KERN_INFO "sg_rq_end_io: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001308 sense = rq->sense;
1309 result = rq->errors;
Tejun Heoc3a4d782009-05-07 22:24:37 +09001310 resid = rq->resid_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001313 sdp->disk->disk_name, srp->header.pack_id, result));
1314 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001315 ms = jiffies_to_msecs(jiffies);
1316 srp->header.duration = (ms > srp->header.duration) ?
1317 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001318 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct scsi_sense_hdr sshdr;
1320
Mike Christied6b10342005-11-08 04:06:41 -06001321 srp->header.status = 0xff & result;
1322 srp->header.masked_status = status_byte(result);
1323 srp->header.msg_status = msg_byte(result);
1324 srp->header.host_status = host_byte(result);
1325 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if ((sdp->sgdebug > 0) &&
1327 ((CHECK_CONDITION == srp->header.masked_status) ||
1328 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001329 __scsi_print_sense("sg_cmd_done", sense,
1330 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001333 if (driver_byte(result) != 0
1334 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 && !scsi_sense_is_deferred(&sshdr)
1336 && sshdr.sense_key == UNIT_ATTENTION
1337 && sdp->device->removable) {
1338 /* Detected possible disc change. Set the bit - this */
1339 /* may be used if there are filesystems using this device */
1340 sdp->device->changed = 1;
1341 }
1342 }
1343 /* Rely on write phase to clean out srp status values, so no "else" */
1344
Tony Battersbyc6517b792009-01-21 14:45:50 -05001345 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1346 if (unlikely(srp->orphan)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (sfp->keep_orphan)
1348 srp->sg_io_owned = 0;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001349 else
1350 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001352 srp->done = done;
1353 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1354
1355 if (likely(done)) {
1356 /* Now wake up any sg_read() that is waiting for this
1357 * packet.
1358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 wake_up_interruptible(&sfp->read_wait);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001360 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001361 kref_put(&sfp->f_ref, sg_remove_sfp);
FUJITA Tomonori015640e2009-04-03 19:28:06 +09001362 } else {
1363 INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
1364 schedule_work(&srp->ew.work);
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001368static const struct file_operations sg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 .owner = THIS_MODULE,
1370 .read = sg_read,
1371 .write = sg_write,
1372 .poll = sg_poll,
Jörn Engel37b9d1e2012-04-12 17:35:05 -04001373 .unlocked_ioctl = sg_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374#ifdef CONFIG_COMPAT
1375 .compat_ioctl = sg_compat_ioctl,
1376#endif
1377 .open = sg_open,
1378 .mmap = sg_mmap,
1379 .release = sg_release,
1380 .fasync = sg_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001381 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382};
1383
gregkh@suse.ded2538782005-03-23 09:55:22 -08001384static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386static int sg_sysfs_valid = 0;
1387
James Bottomley7c07d612007-08-05 13:36:11 -05001388static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Mike Christied6b10342005-11-08 04:06:41 -06001390 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 Sg_device *sdp;
1392 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001393 int error;
1394 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Jes Sorensen24669f752006-01-16 10:31:18 -05001396 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (!sdp) {
1398 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001399 return ERR_PTR(-ENOMEM);
1400 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001401
Tejun Heob98c52b2013-02-27 17:04:42 -08001402 idr_preload(GFP_KERNEL);
James Bottomley7c07d612007-08-05 13:36:11 -05001403 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Tejun Heob98c52b2013-02-27 17:04:42 -08001405 error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
1406 if (error < 0) {
1407 if (error == -ENOSPC) {
1408 sdev_printk(KERN_WARNING, scsidp,
1409 "Unable to attach sg device type=%d, minor number exceeds %d\n",
1410 scsidp->type, SG_MAX_DEVS - 1);
1411 error = -ENODEV;
1412 } else {
1413 printk(KERN_WARNING
1414 "idr allocation Sg_device failure: %d\n", error);
1415 }
1416 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Tejun Heob98c52b2013-02-27 17:04:42 -08001418 k = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1421 sprintf(disk->disk_name, "sg%d", k);
1422 disk->first_minor = k;
1423 sdp->disk = disk;
1424 sdp->device = scsidp;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09001425 INIT_LIST_HEAD(&sdp->sfds);
James Bottomley065b4a22013-10-25 10:27:02 +01001426 init_waitqueue_head(&sdp->o_excl_wait);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001427 sdp->sg_tablesize = queue_max_segments(q);
James Bottomley7c07d612007-08-05 13:36:11 -05001428 sdp->index = k;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001429 kref_init(&sdp->d_ref);
James Bottomley7c07d612007-08-05 13:36:11 -05001430 error = 0;
Tejun Heob98c52b2013-02-27 17:04:42 -08001431
1432out_unlock:
1433 write_unlock_irqrestore(&sg_index_lock, iflags);
1434 idr_preload_end();
1435
James Bottomley7c07d612007-08-05 13:36:11 -05001436 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001438 return ERR_PTR(error);
1439 }
1440 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443static int
Tony Jonesee959b02008-02-22 00:13:36 +01001444sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Tony Jonesee959b02008-02-22 00:13:36 +01001446 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 struct gendisk *disk;
1448 Sg_device *sdp = NULL;
1449 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001450 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001451 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 disk = alloc_disk(1);
1454 if (!disk) {
1455 printk(KERN_WARNING "alloc_disk failed\n");
1456 return -ENOMEM;
1457 }
1458 disk->major = SCSI_GENERIC_MAJOR;
1459
1460 error = -ENOMEM;
1461 cdev = cdev_alloc();
1462 if (!cdev) {
1463 printk(KERN_WARNING "cdev_alloc failed\n");
1464 goto out;
1465 }
1466 cdev->owner = THIS_MODULE;
1467 cdev->ops = &sg_fops;
1468
James Bottomley7c07d612007-08-05 13:36:11 -05001469 sdp = sg_alloc(disk, scsidp);
1470 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001472 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto out;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
James Bottomley7c07d612007-08-05 13:36:11 -05001476 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001477 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001478 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 sdp->cdev = cdev;
1481 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001482 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Greg Kroah-Hartmand73a1a672008-07-21 20:03:34 -07001484 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1485 MKDEV(SCSI_GENERIC_MAJOR,
1486 sdp->index),
1487 sdp, "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001488 if (IS_ERR(sg_class_member)) {
1489 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001490 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001491 error = PTR_ERR(sg_class_member);
1492 goto cdev_add_err;
1493 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001494 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 &sg_class_member->kobj, "generic");
1496 if (error)
1497 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001498 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001500 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
James Bottomley9ccfc752005-10-02 11:45:08 -05001502 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001503 "Attached scsi generic sg%d type %d\n", sdp->index,
1504 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Tony Jonesee959b02008-02-22 00:13:36 +01001506 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001510cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001511 write_lock_irqsave(&sg_index_lock, iflags);
1512 idr_remove(&sg_index_idr, sdp->index);
1513 write_unlock_irqrestore(&sg_index_lock, iflags);
1514 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516out:
1517 put_disk(disk);
1518 if (cdev)
1519 cdev_del(cdev);
1520 return error;
1521}
1522
Tony Battersbyc6517b792009-01-21 14:45:50 -05001523static void sg_device_destroy(struct kref *kref)
1524{
1525 struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1526 unsigned long flags;
1527
1528 /* CAUTION! Note that the device can still be found via idr_find()
1529 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1530 * any other cleanup.
1531 */
1532
1533 write_lock_irqsave(&sg_index_lock, flags);
1534 idr_remove(&sg_index_idr, sdp->index);
1535 write_unlock_irqrestore(&sg_index_lock, flags);
1536
1537 SCSI_LOG_TIMEOUT(3,
1538 printk("sg_device_destroy: %s\n",
1539 sdp->disk->disk_name));
1540
1541 put_disk(sdp->disk);
1542 kfree(sdp);
1543}
1544
1545static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Tony Jonesee959b02008-02-22 00:13:36 +01001547 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1548 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 unsigned long iflags;
1550 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Tony Battersbyc6517b792009-01-21 14:45:50 -05001552 if (!sdp || sdp->detached)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Tony Battersbyc6517b792009-01-21 14:45:50 -05001555 SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
1556
1557 /* Need a write lock to set sdp->detached. */
James Bottomley7c07d612007-08-05 13:36:11 -05001558 write_lock_irqsave(&sg_index_lock, iflags);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001559 sdp->detached = 1;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09001560 list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05001561 wake_up_interruptible(&sfp->read_wait);
1562 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
James Bottomley7c07d612007-08-05 13:36:11 -05001564 write_unlock_irqrestore(&sg_index_lock, iflags);
1565
1566 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001567 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001568 cdev_del(sdp->cdev);
1569 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Tony Battersbyc6517b792009-01-21 14:45:50 -05001571 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Douglas Gilbert6460e752006-09-20 18:20:49 -04001574module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1575module_param_named(def_reserved_size, def_reserved_size, int,
1576 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1578
1579MODULE_AUTHOR("Douglas Gilbert");
1580MODULE_DESCRIPTION("SCSI generic (sg) driver");
1581MODULE_LICENSE("GPL");
1582MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001583MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Douglas Gilbert6460e752006-09-20 18:20:49 -04001585MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1586 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1588MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1589
1590static int __init
1591init_sg(void)
1592{
1593 int rc;
1594
Douglas Gilbert6460e752006-09-20 18:20:49 -04001595 if (scatter_elem_sz < PAGE_SIZE) {
1596 scatter_elem_sz = PAGE_SIZE;
1597 scatter_elem_sz_prev = scatter_elem_sz;
1598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (def_reserved_size >= 0)
1600 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001601 else
1602 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1605 SG_MAX_DEVS, "sg");
1606 if (rc)
1607 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001608 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if ( IS_ERR(sg_sysfs_class) ) {
1610 rc = PTR_ERR(sg_sysfs_class);
1611 goto err_out;
1612 }
1613 sg_sysfs_valid = 1;
1614 rc = scsi_register_interface(&sg_interface);
1615 if (0 == rc) {
1616#ifdef CONFIG_SCSI_PROC_FS
1617 sg_proc_init();
1618#endif /* CONFIG_SCSI_PROC_FS */
1619 return 0;
1620 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001621 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622err_out:
1623 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1624 return rc;
1625}
1626
1627static void __exit
1628exit_sg(void)
1629{
1630#ifdef CONFIG_SCSI_PROC_FS
1631 sg_proc_cleanup();
1632#endif /* CONFIG_SCSI_PROC_FS */
1633 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001634 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 sg_sysfs_valid = 0;
1636 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1637 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001638 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001641static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001642{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001643 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001644 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001645 Sg_fd *sfp = srp->parentfp;
1646 sg_io_hdr_t *hp = &srp->header;
1647 int dxfer_len = (int) hp->dxfer_len;
1648 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001649 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001650 Sg_scatter_hold *req_schp = &srp->data;
1651 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1652 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001653 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001654 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001655 unsigned char *long_cmdp = NULL;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001656
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001657 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1658 dxfer_len));
1659
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001660 if (hp->cmd_len > BLK_MAX_CDB) {
1661 long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
1662 if (!long_cmdp)
1663 return -ENOMEM;
1664 }
1665
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001666 rq = blk_get_request(q, rw, GFP_ATOMIC);
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001667 if (!rq) {
1668 kfree(long_cmdp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001669 return -ENOMEM;
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001670 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001671
Jens Axboef27b0872014-06-06 07:57:37 -06001672 blk_rq_set_block_pc(rq);
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001673
1674 if (hp->cmd_len > BLK_MAX_CDB)
1675 rq->cmd = long_cmdp;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001676 memcpy(rq->cmd, cmd, hp->cmd_len);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001677 rq->cmd_len = hp->cmd_len;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001678
1679 srp->rq = rq;
1680 rq->end_io_data = srp;
1681 rq->sense = srp->sense_b;
1682 rq->retries = SG_DEFAULT_RETRIES;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001685 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001686
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001687 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1688 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1689 !sfp->parentdp->device->host->unchecked_isa_dma &&
Namhyung Kim2610a252010-09-16 12:55:57 +09001690 blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001691 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001692 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001693 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001694
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001695 if (md) {
1696 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1697 sg_link_reserve(sfp, srp, dxfer_len);
1698 else {
1699 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1700 if (res)
1701 return res;
1702 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001703
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001704 md->pages = req_schp->pages;
1705 md->page_order = req_schp->page_order;
1706 md->nr_entries = req_schp->k_use_sg;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001707 md->offset = 0;
FUJITA Tomonori97ae77a2008-12-18 14:49:38 +09001708 md->null_mapped = hp->dxferp ? 0 : 1;
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001709 if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
1710 md->from_user = 1;
1711 else
1712 md->from_user = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001714
FUJITA Tomonori0fdf96b2009-04-03 09:12:20 +09001715 if (iov_count) {
1716 int len, size = sizeof(struct sg_iovec) * iov_count;
1717 struct iovec *iov;
1718
Julia Lawall30941412010-08-10 18:01:27 -07001719 iov = memdup_user(hp->dxferp, size);
1720 if (IS_ERR(iov))
1721 return PTR_ERR(iov);
FUJITA Tomonori0fdf96b2009-04-03 09:12:20 +09001722
1723 len = iov_length(iov, iov_count);
1724 if (hp->dxfer_len < len) {
1725 iov_count = iov_shorten(iov, iov_count, hp->dxfer_len);
1726 len = hp->dxfer_len;
1727 }
1728
1729 res = blk_rq_map_user_iov(q, rq, md, (struct sg_iovec *)iov,
1730 iov_count,
1731 len, GFP_ATOMIC);
1732 kfree(iov);
1733 } else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001734 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1735 hp->dxfer_len, GFP_ATOMIC);
1736
1737 if (!res) {
1738 srp->bio = rq->bio;
1739
1740 if (!md) {
1741 req_schp->dio_in_use = 1;
1742 hp->info |= SG_INFO_DIRECT_IO;
1743 }
1744 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001745 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001748static int sg_finish_rem_req(Sg_request * srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001750 int ret = 0;
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 Sg_fd *sfp = srp->parentfp;
1753 Sg_scatter_hold *req_schp = &srp->data;
1754
1755 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001756 if (srp->rq) {
1757 if (srp->bio)
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001758 ret = blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001759
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001760 if (srp->rq->cmd != srp->rq->__cmd)
1761 kfree(srp->rq->cmd);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001762 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001763 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001764
Christof Schmitte27168f2009-09-17 09:10:14 +02001765 if (srp->res_used)
1766 sg_unlink_reserve(sfp, srp);
1767 else
1768 sg_remove_scat(req_schp);
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 sg_remove_request(sfp, srp);
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001771
1772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
1775static int
1776sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1777{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001778 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001779 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001781 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1782 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001785 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static int
1789sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1790{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001791 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001792 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001793 int blk_size = buff_size, order;
1794 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Eric Sesterhennfb119932007-05-23 14:41:36 -07001796 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return -EFAULT;
1798 if (0 == blk_size)
1799 ++blk_size; /* don't know why */
FUJITA Tomonorib2ed6c62009-02-12 02:42:57 +09001800 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1801 blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1803 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001804
1805 /* N.B. ret_sz carried into this block ... */
1806 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1807 if (mx_sc_elems < 0)
1808 return mx_sc_elems; /* most likely -ENOMEM */
1809
Douglas Gilbert6460e752006-09-20 18:20:49 -04001810 num = scatter_elem_sz;
1811 if (unlikely(num != scatter_elem_sz_prev)) {
1812 if (num < PAGE_SIZE) {
1813 scatter_elem_sz = PAGE_SIZE;
1814 scatter_elem_sz_prev = PAGE_SIZE;
1815 } else
1816 scatter_elem_sz_prev = num;
1817 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001818
1819 if (sfp->low_dma)
1820 gfp_mask |= GFP_DMA;
1821
1822 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1823 gfp_mask |= __GFP_ZERO;
1824
1825 order = get_order(num);
1826retry:
1827 ret_sz = 1 << (PAGE_SHIFT + order);
1828
1829 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1830 k++, rem_sz -= ret_sz) {
1831
Douglas Gilbert6460e752006-09-20 18:20:49 -04001832 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001833 scatter_elem_sz_prev : rem_sz;
1834
1835 schp->pages[k] = alloc_pages(gfp_mask, order);
1836 if (!schp->pages[k])
1837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Douglas Gilbert6460e752006-09-20 18:20:49 -04001839 if (num == scatter_elem_sz_prev) {
1840 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1841 scatter_elem_sz = ret_sz;
1842 scatter_elem_sz_prev = ret_sz;
1843 }
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001846 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1847 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001848 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001850 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001851 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001852 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1853 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001854
1855 schp->bufflen = blk_size;
1856 if (rem_sz > 0) /* must have failed */
1857 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001859out:
1860 for (i = 0; i < k; i++)
Michal Schmidte71044e2009-09-03 14:27:08 +02001861 __free_pages(schp->pages[i], order);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001862
1863 if (--order >= 0)
1864 goto retry;
1865
1866 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869static void
1870sg_remove_scat(Sg_scatter_hold * schp)
1871{
1872 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001873 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001874 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int k;
1876
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001877 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001879 "sg_remove_scat: k=%d, pg=0x%p\n",
1880 k, schp->pages[k]));
1881 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001883
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001884 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Mike Christied6b10342005-11-08 04:06:41 -06001886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 memset(schp, 0, sizeof (*schp));
1888}
1889
1890static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1892{
1893 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001894 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1897 num_read_xfer));
1898 if ((!outp) || (num_read_xfer <= 0))
1899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001901 num = 1 << (PAGE_SHIFT + schp->page_order);
1902 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001903 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001904 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001905 num_read_xfer))
1906 return -EFAULT;
1907 break;
1908 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001909 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001910 num))
1911 return -EFAULT;
1912 num_read_xfer -= num;
1913 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
Mike Christied6b10342005-11-08 04:06:41 -06001915 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Mike Christied6b10342005-11-08 04:06:41 -06001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
1921
1922static void
1923sg_build_reserve(Sg_fd * sfp, int req_size)
1924{
1925 Sg_scatter_hold *schp = &sfp->reserve;
1926
1927 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1928 do {
1929 if (req_size < PAGE_SIZE)
1930 req_size = PAGE_SIZE;
1931 if (0 == sg_build_indirect(schp, sfp, req_size))
1932 return;
1933 else
1934 sg_remove_scat(schp);
1935 req_size >>= 1; /* divide by 2 */
1936 } while (req_size > (PAGE_SIZE / 2));
1937}
1938
1939static void
1940sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1941{
1942 Sg_scatter_hold *req_schp = &srp->data;
1943 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001944 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 srp->res_used = 1;
1947 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001948 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001950 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1951 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001952 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001953 req_schp->k_use_sg = k + 1;
1954 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001955 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001956
1957 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001958 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001959 break;
1960 } else
1961 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Mike Christied6b10342005-11-08 04:06:41 -06001963
1964 if (k >= rsv_schp->k_use_sg)
1965 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968static void
1969sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1970{
1971 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1974 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 req_schp->k_use_sg = 0;
1976 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001977 req_schp->pages = NULL;
1978 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 req_schp->sglist_len = 0;
1980 sfp->save_scat_len = 0;
1981 srp->res_used = 0;
1982}
1983
1984static Sg_request *
1985sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1986{
1987 Sg_request *resp;
1988 unsigned long iflags;
1989
1990 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1991 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1992 /* look for requests that are ready + not SG_IO owned */
1993 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1994 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1995 resp->done = 2; /* guard against other readers */
1996 break;
1997 }
1998 }
1999 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2000 return resp;
2001}
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/* always adds to end of list */
2004static Sg_request *
2005sg_add_request(Sg_fd * sfp)
2006{
2007 int k;
2008 unsigned long iflags;
2009 Sg_request *resp;
2010 Sg_request *rp = sfp->req_arr;
2011
2012 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2013 resp = sfp->headrp;
2014 if (!resp) {
2015 memset(rp, 0, sizeof (Sg_request));
2016 rp->parentfp = sfp;
2017 resp = rp;
2018 sfp->headrp = resp;
2019 } else {
2020 if (0 == sfp->cmd_q)
2021 resp = NULL; /* command queuing disallowed */
2022 else {
2023 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2024 if (!rp->parentfp)
2025 break;
2026 }
2027 if (k < SG_MAX_QUEUE) {
2028 memset(rp, 0, sizeof (Sg_request));
2029 rp->parentfp = sfp;
2030 while (resp->nextrp)
2031 resp = resp->nextrp;
2032 resp->nextrp = rp;
2033 resp = rp;
2034 } else
2035 resp = NULL;
2036 }
2037 }
2038 if (resp) {
2039 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002040 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2043 return resp;
2044}
2045
2046/* Return of 1 for found; 0 for not found */
2047static int
2048sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2049{
2050 Sg_request *prev_rp;
2051 Sg_request *rp;
2052 unsigned long iflags;
2053 int res = 0;
2054
2055 if ((!sfp) || (!srp) || (!sfp->headrp))
2056 return res;
2057 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2058 prev_rp = sfp->headrp;
2059 if (srp == prev_rp) {
2060 sfp->headrp = prev_rp->nextrp;
2061 prev_rp->parentfp = NULL;
2062 res = 1;
2063 } else {
2064 while ((rp = prev_rp->nextrp)) {
2065 if (srp == rp) {
2066 prev_rp->nextrp = rp->nextrp;
2067 rp->parentfp = NULL;
2068 res = 1;
2069 break;
2070 }
2071 prev_rp = rp;
2072 }
2073 }
2074 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2075 return res;
2076}
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078static Sg_fd *
2079sg_add_sfp(Sg_device * sdp, int dev)
2080{
2081 Sg_fd *sfp;
2082 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002083 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Mike Christied6b10342005-11-08 04:06:41 -06002085 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!sfp)
James Bottomleybafc8ad2013-10-25 10:25:14 +01002087 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 init_waitqueue_head(&sfp->read_wait);
2090 rwlock_init(&sfp->rq_list_lock);
2091
Tony Battersbyc6517b792009-01-21 14:45:50 -05002092 kref_init(&sfp->f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 sfp->timeout = SG_DEFAULT_TIMEOUT;
2094 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2095 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2096 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2097 sdp->device->host->unchecked_isa_dma : 1;
2098 sfp->cmd_q = SG_DEF_COMMAND_Q;
2099 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2100 sfp->parentdp = sdp;
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002101 write_lock_irqsave(&sg_index_lock, iflags);
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002102 list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002103 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002105 if (unlikely(sg_big_buff != def_reserved_size))
2106 sg_big_buff = def_reserved_size;
2107
Alan Stern44ec9542007-02-20 11:01:57 -05002108 bufflen = min_t(int, sg_big_buff,
Akinobu Mita46f69e62014-06-02 22:56:46 +09002109 max_sectors_bytes(sdp->device->request_queue));
Alan Stern44ec9542007-02-20 11:01:57 -05002110 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2112 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
Tony Battersbyc6517b792009-01-21 14:45:50 -05002113
2114 kref_get(&sdp->d_ref);
2115 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return sfp;
2117}
2118
Tony Battersbyc6517b792009-01-21 14:45:50 -05002119static void sg_remove_sfp_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002121 struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2122 struct sg_device *sdp = sfp->parentdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Tony Battersbyc6517b792009-01-21 14:45:50 -05002124 /* Cleanup any responses which were never read(). */
2125 while (sfp->headrp)
2126 sg_finish_rem_req(sfp->headrp);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (sfp->reserve.bufflen > 0) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05002129 SCSI_LOG_TIMEOUT(6,
2130 printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2131 (int) sfp->reserve.bufflen,
2132 (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 sg_remove_scat(&sfp->reserve);
2134 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002135
2136 SCSI_LOG_TIMEOUT(6,
2137 printk("sg_remove_sfp: %s, sfp=0x%p\n",
2138 sdp->disk->disk_name,
2139 sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002140 kfree(sfp);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002141
2142 scsi_device_put(sdp->device);
2143 sg_put_dev(sdp);
2144 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Tony Battersbyc6517b792009-01-21 14:45:50 -05002147static void sg_remove_sfp(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002149 struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
James Bottomley065b4a22013-10-25 10:27:02 +01002150 struct sg_device *sdp = sfp->parentdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002151 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002153 write_lock_irqsave(&sg_index_lock, iflags);
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002154 list_del(&sfp->sfd_siblings);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002155 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley065b4a22013-10-25 10:27:02 +01002156 wake_up_interruptible(&sdp->o_excl_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
FUJITA Tomonori015640e2009-04-03 19:28:06 +09002158 INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
2159 schedule_work(&sfp->ew.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162static int
2163sg_res_in_use(Sg_fd * sfp)
2164{
2165 const Sg_request *srp;
2166 unsigned long iflags;
2167
2168 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2169 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2170 if (srp->res_used)
2171 break;
2172 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2173 return srp ? 1 : 0;
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#ifdef CONFIG_SCSI_PROC_FS
2177static int
James Bottomley7c07d612007-08-05 13:36:11 -05002178sg_idr_max_id(int id, void *p, void *data)
2179{
2180 int *k = data;
2181
2182 if (*k < id)
2183 *k = id;
2184
2185 return 0;
2186}
2187
2188static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189sg_last_dev(void)
2190{
Tony Battersby53474c02008-01-22 15:25:49 -05002191 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 unsigned long iflags;
2193
James Bottomley7c07d612007-08-05 13:36:11 -05002194 read_lock_irqsave(&sg_index_lock, iflags);
2195 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2196 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return k + 1; /* origin 1 */
2198}
2199#endif
2200
Tony Battersbyc6517b792009-01-21 14:45:50 -05002201/* must be called with sg_index_lock held */
2202static Sg_device *sg_lookup_dev(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002204 return idr_find(&sg_index_idr, dev);
2205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Tony Battersbyc6517b792009-01-21 14:45:50 -05002207static Sg_device *sg_get_dev(int dev)
2208{
2209 struct sg_device *sdp;
2210 unsigned long flags;
2211
2212 read_lock_irqsave(&sg_index_lock, flags);
2213 sdp = sg_lookup_dev(dev);
2214 if (!sdp)
2215 sdp = ERR_PTR(-ENXIO);
2216 else if (sdp->detached) {
2217 /* If sdp->detached, then the refcount may already be 0, in
2218 * which case it would be a bug to do kref_get().
2219 */
2220 sdp = ERR_PTR(-ENODEV);
2221 } else
2222 kref_get(&sdp->d_ref);
2223 read_unlock_irqrestore(&sg_index_lock, flags);
James Bottomley7c07d612007-08-05 13:36:11 -05002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return sdp;
2226}
2227
Tony Battersbyc6517b792009-01-21 14:45:50 -05002228static void sg_put_dev(struct sg_device *sdp)
2229{
2230 kref_put(&sdp->d_ref, sg_device_destroy);
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233#ifdef CONFIG_SCSI_PROC_FS
2234
2235static struct proc_dir_entry *sg_proc_sgp = NULL;
2236
2237static char sg_proc_sg_dirname[] = "scsi/sg";
2238
2239static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2240
2241static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2242static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2243 size_t count, loff_t *off);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002244static const struct file_operations adio_fops = {
2245 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 .open = sg_proc_single_open_adio,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002247 .read = seq_read,
2248 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 .write = sg_proc_write_adio,
2250 .release = single_release,
2251};
2252
2253static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2254static ssize_t sg_proc_write_dressz(struct file *filp,
2255 const char __user *buffer, size_t count, loff_t *off);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002256static const struct file_operations dressz_fops = {
2257 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .open = sg_proc_single_open_dressz,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002259 .read = seq_read,
2260 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 .write = sg_proc_write_dressz,
2262 .release = single_release,
2263};
2264
2265static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2266static int sg_proc_single_open_version(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002267static const struct file_operations version_fops = {
2268 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 .open = sg_proc_single_open_version,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002270 .read = seq_read,
2271 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 .release = single_release,
2273};
2274
2275static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2276static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002277static const struct file_operations devhdr_fops = {
2278 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .open = sg_proc_single_open_devhdr,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002280 .read = seq_read,
2281 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 .release = single_release,
2283};
2284
2285static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2286static int sg_proc_open_dev(struct inode *inode, struct file *file);
2287static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2288static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2289static void dev_seq_stop(struct seq_file *s, void *v);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002290static const struct file_operations dev_fops = {
2291 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 .open = sg_proc_open_dev,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002293 .read = seq_read,
2294 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 .release = seq_release,
2296};
James Morris88e9d342009-09-22 16:43:43 -07002297static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 .start = dev_seq_start,
2299 .next = dev_seq_next,
2300 .stop = dev_seq_stop,
2301 .show = sg_proc_seq_show_dev,
2302};
2303
2304static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2305static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002306static const struct file_operations devstrs_fops = {
2307 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 .open = sg_proc_open_devstrs,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002309 .read = seq_read,
2310 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 .release = seq_release,
2312};
James Morris88e9d342009-09-22 16:43:43 -07002313static const struct seq_operations devstrs_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 .start = dev_seq_start,
2315 .next = dev_seq_next,
2316 .stop = dev_seq_stop,
2317 .show = sg_proc_seq_show_devstrs,
2318};
2319
2320static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2321static int sg_proc_open_debug(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002322static const struct file_operations debug_fops = {
2323 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 .open = sg_proc_open_debug,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002325 .read = seq_read,
2326 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 .release = seq_release,
2328};
James Morris88e9d342009-09-22 16:43:43 -07002329static const struct seq_operations debug_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 .start = dev_seq_start,
2331 .next = dev_seq_next,
2332 .stop = dev_seq_stop,
2333 .show = sg_proc_seq_show_debug,
2334};
2335
2336
2337struct sg_proc_leaf {
2338 const char * name;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002339 const struct file_operations * fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340};
2341
Jörn Engel18b8ba62012-04-12 17:35:25 -04002342static const struct sg_proc_leaf sg_proc_leaf_arr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 {"allow_dio", &adio_fops},
2344 {"debug", &debug_fops},
2345 {"def_reserved_size", &dressz_fops},
2346 {"device_hdr", &devhdr_fops},
2347 {"devices", &dev_fops},
2348 {"device_strs", &devstrs_fops},
2349 {"version", &version_fops}
2350};
2351
2352static int
2353sg_proc_init(void)
2354{
Tobias Klauser6391a112006-06-08 22:23:48 -07002355 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Al Virod161a132011-07-24 03:36:29 -04002356 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Al Viro66600222005-09-28 22:32:57 +01002358 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (!sg_proc_sgp)
2360 return 1;
2361 for (k = 0; k < num_leaves; ++k) {
Jörn Engel18b8ba62012-04-12 17:35:25 -04002362 const struct sg_proc_leaf *leaf = &sg_proc_leaf_arr[k];
Al Virod161a132011-07-24 03:36:29 -04002363 umode_t mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002364 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366 return 0;
2367}
2368
2369static void
2370sg_proc_cleanup(void)
2371{
2372 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002373 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 if (!sg_proc_sgp)
2376 return;
2377 for (k = 0; k < num_leaves; ++k)
2378 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2379 remove_proc_entry(sg_proc_sg_dirname, NULL);
2380}
2381
2382
2383static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2384{
2385 seq_printf(s, "%d\n", *((int *)s->private));
2386 return 0;
2387}
2388
2389static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2390{
2391 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2392}
2393
2394static ssize_t
2395sg_proc_write_adio(struct file *filp, const char __user *buffer,
2396 size_t count, loff_t *off)
2397{
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002398 int err;
2399 unsigned long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2402 return -EACCES;
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002403 err = kstrtoul_from_user(buffer, count, 0, &num);
2404 if (err)
2405 return err;
2406 sg_allow_dio = num ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return count;
2408}
2409
2410static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2411{
2412 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2413}
2414
2415static ssize_t
2416sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2417 size_t count, loff_t *off)
2418{
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002419 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 unsigned long k = ULONG_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2423 return -EACCES;
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002424
2425 err = kstrtoul_from_user(buffer, count, 0, &k);
2426 if (err)
2427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2429 sg_big_buff = k;
2430 return count;
2431 }
2432 return -ERANGE;
2433}
2434
2435static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2436{
2437 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2438 sg_version_date);
2439 return 0;
2440}
2441
2442static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2443{
2444 return single_open(file, sg_proc_seq_show_version, NULL);
2445}
2446
2447static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2448{
2449 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2450 "online\n");
2451 return 0;
2452}
2453
2454static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2455{
2456 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2457}
2458
2459struct sg_proc_deviter {
2460 loff_t index;
2461 size_t max;
2462};
2463
2464static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2465{
2466 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2467
Jan Blunck729d70f2005-08-27 11:07:52 -07002468 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (! it)
2470 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 it->index = *pos;
2473 it->max = sg_last_dev();
2474 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002475 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2480{
Jan Blunck729d70f2005-08-27 11:07:52 -07002481 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 *pos = ++it->index;
2484 return (it->index < it->max) ? it : NULL;
2485}
2486
2487static void dev_seq_stop(struct seq_file *s, void *v)
2488{
Jan Blunck729d70f2005-08-27 11:07:52 -07002489 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492static int sg_proc_open_dev(struct inode *inode, struct file *file)
2493{
2494 return seq_open(file, &dev_seq_ops);
2495}
2496
2497static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2498{
2499 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2500 Sg_device *sdp;
2501 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002502 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Tony Battersbyc6517b792009-01-21 14:45:50 -05002504 read_lock_irqsave(&sg_index_lock, iflags);
2505 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2507 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2508 scsidp->host->host_no, scsidp->channel,
2509 scsidp->id, scsidp->lun, (int) scsidp->type,
2510 1,
2511 (int) scsidp->queue_depth,
2512 (int) scsidp->device_busy,
2513 (int) scsi_device_online(scsidp));
2514 else
2515 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002516 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
2518}
2519
2520static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2521{
2522 return seq_open(file, &devstrs_seq_ops);
2523}
2524
2525static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2526{
2527 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2528 Sg_device *sdp;
2529 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002530 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Tony Battersbyc6517b792009-01-21 14:45:50 -05002532 read_lock_irqsave(&sg_index_lock, iflags);
2533 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2535 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2536 scsidp->vendor, scsidp->model, scsidp->rev);
2537 else
2538 seq_printf(s, "<no active device>\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002539 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return 0;
2541}
2542
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002543/* must be called while holding sg_index_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2545{
2546 int k, m, new_interface, blen, usg;
2547 Sg_request *srp;
2548 Sg_fd *fp;
2549 const sg_io_hdr_t *hp;
2550 const char * cp;
cb59e842005-04-02 13:51:23 -06002551 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002553 k = 0;
2554 list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
2555 k++;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002556 read_lock(&fp->rq_list_lock); /* irqs already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002558 "(res)sgat=%d low_dma=%d\n", k,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 jiffies_to_msecs(fp->timeout),
2560 fp->reserve.bufflen,
2561 (int) fp->reserve.k_use_sg,
2562 (int) fp->low_dma);
Jörn Engelebaf4662012-04-12 17:33:39 -04002563 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 (int) fp->cmd_q, (int) fp->force_packid,
Jörn Engelebaf4662012-04-12 17:33:39 -04002565 (int) fp->keep_orphan);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002566 for (m = 0, srp = fp->headrp;
2567 srp != NULL;
2568 ++m, srp = srp->nextrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 hp = &srp->header;
2570 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2571 if (srp->res_used) {
2572 if (new_interface &&
2573 (SG_FLAG_MMAP_IO & hp->flags))
2574 cp = " mmap>> ";
2575 else
2576 cp = " rb>> ";
2577 } else {
2578 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2579 cp = " dio>> ";
2580 else
2581 cp = " ";
2582 }
2583 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002584 blen = srp->data.bufflen;
2585 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 seq_printf(s, srp->done ?
2587 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002588 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 seq_printf(s, " id=%d blen=%d",
2590 srp->header.pack_id, blen);
2591 if (srp->done)
2592 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002593 else {
2594 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002596 (new_interface ? hp->timeout :
2597 jiffies_to_msecs(fp->timeout)),
2598 (ms > hp->duration ? ms - hp->duration : 0));
2599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2601 (int) srp->data.cmd_opcode);
2602 }
2603 if (0 == m)
2604 seq_printf(s, " No requests active\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002605 read_unlock(&fp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607}
2608
2609static int sg_proc_open_debug(struct inode *inode, struct file *file)
2610{
2611 return seq_open(file, &debug_seq_ops);
2612}
2613
2614static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2615{
2616 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2617 Sg_device *sdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002618 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002621 seq_printf(s, "max_active_device=%d(origin 1)\n",
2622 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2624 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002625
2626 read_lock_irqsave(&sg_index_lock, iflags);
2627 sdp = it ? sg_lookup_dev(it->index) : NULL;
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002628 if (sdp && !list_empty(&sdp->sfds)) {
2629 struct scsi_device *scsidp = sdp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002631 seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2632 if (sdp->detached)
2633 seq_printf(s, "detached pending close ");
2634 else
2635 seq_printf
2636 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2637 scsidp->host->host_no,
2638 scsidp->channel, scsidp->id,
2639 scsidp->lun,
2640 scsidp->host->hostt->emulated);
2641 seq_printf(s, " sg_tablesize=%d excl=%d\n",
James Bottomley98481ff2013-10-25 10:26:38 +01002642 sdp->sg_tablesize, get_exclude(sdp));
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002643 sg_proc_debug_helper(s, sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002645 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return 0;
2647}
2648
2649#endif /* CONFIG_SCSI_PROC_FS */
2650
2651module_init(init_sg);
2652module_exit(exit_sg);