blob: cdd83cf97100657a7672198a38b9671b3ac17019 [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:
10 * Copyright (C) 1998 - 2005 Douglas Gilbert
11 *
12 * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 */
20
Douglas Gilbertb2155d02006-08-19 00:11:34 -040021static int sg_version_num = 30534; /* 2 digits for each component */
22#define SG_VERSION_STR "3.5.34"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
26 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
27 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
28 * (otherwise the macros compile to empty statements).
29 *
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32
33#include <linux/fs.h>
34#include <linux/kernel.h>
35#include <linux/sched.h>
36#include <linux/string.h>
37#include <linux/mm.h>
38#include <linux/errno.h>
39#include <linux/mtio.h>
40#include <linux/ioctl.h>
41#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>
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -060051#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "scsi.h"
db9dff32005-04-03 14:53:59 -050054#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <scsi/scsi_host.h>
56#include <scsi/scsi_driver.h>
57#include <scsi/scsi_ioctl.h>
58#include <scsi/sg.h>
59
60#include "scsi_logging.h"
61
62#ifdef CONFIG_SCSI_PROC_FS
63#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040064static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static int sg_proc_init(void);
67static void sg_proc_cleanup(void);
68#endif
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define SG_ALLOW_DIO_DEF 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define SG_MAX_DEVS 32768
73
74/*
75 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
76 * Then when using 32 bit integers x * m may overflow during the calculation.
77 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
78 * calculates the same, but prevents the overflow when both m and d
79 * are "small" numbers (like HZ and USER_HZ).
80 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
81 * in 32 bits.
82 */
83#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
84
85#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
86
87int sg_big_buff = SG_DEF_RESERVED_SIZE;
88/* N.B. This variable is readable and writeable via
89 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
90 of this size (or less if there is not enough memory) will be reserved
91 for use by this file descriptor. [Deprecated usage: this variable is also
92 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
93 the kernel (i.e. it is not a module).] */
94static int def_reserved_size = -1; /* picks up init parameter */
95static int sg_allow_dio = SG_ALLOW_DIO_DEF;
96
Douglas Gilbert6460e752006-09-20 18:20:49 -040097static int scatter_elem_sz = SG_SCATTER_SZ;
98static int scatter_elem_sz_prev = SG_SCATTER_SZ;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define SG_SECTOR_SZ 512
101#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
102
Tony Jonesee959b02008-02-22 00:13:36 +0100103static int sg_add(struct device *, struct class_interface *);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500104static void sg_device_destroy(struct kref *kref);
Tony Jonesee959b02008-02-22 00:13:36 +0100105static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
James Bottomley7c07d612007-08-05 13:36:11 -0500107static DEFINE_IDR(sg_index_idr);
108static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 file descriptor list for device */
110
111static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100112 .add_dev = sg_add,
113 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
117 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900118 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200120 struct page **pages;
121 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
123 unsigned char cmd_opcode; /* first byte of command */
124} Sg_scatter_hold;
125
126struct sg_device; /* forward declarations */
127struct sg_fd;
128
129typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct sg_request *nextrp; /* NULL -> tail request (slist) */
131 struct sg_fd *parentfp; /* NULL -> not in use */
132 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
133 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600134 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
136 char orphan; /* 1 -> drop on sight, 0 -> normal */
137 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
138 volatile char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900139 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900140 struct bio *bio;
FUJITA Tomonoric96952e2009-02-04 11:36:27 +0900141 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142} Sg_request;
143
144typedef struct sg_fd { /* holds the state of a file descriptor */
145 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
146 struct sg_device *parentdp; /* owning device */
147 wait_queue_head_t read_wait; /* queue read until command done */
148 rwlock_t rq_list_lock; /* protect access to list in req_arr */
149 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
150 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
151 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
152 unsigned save_scat_len; /* original length of trunc. scat. element */
153 Sg_request *headrp; /* head of request slist, NULL->empty */
154 struct fasync_struct *async_qp; /* used by asynchronous notification */
155 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
156 char low_dma; /* as in parent but possibly overridden to 1 */
157 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
158 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
159 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
160 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
161 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
162 char mmap_called; /* 0 -> mmap() never called on this fd */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500163 struct kref f_ref;
164 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165} Sg_fd;
166
167typedef struct sg_device { /* holds the state of each scsi generic device */
168 struct scsi_device *device;
169 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
170 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500171 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 Sg_fd *headfp; /* first open fd belonging to this device */
173 volatile char detached; /* 0->attached, 1->detached pending removal */
174 volatile char exclude; /* opened for exclusive access */
175 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
176 struct gendisk *disk;
177 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500178 struct kref d_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179} Sg_device;
180
181static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600182/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900183static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900184static int sg_start_req(Sg_request *srp, unsigned char *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static void sg_finish_rem_req(Sg_request * srp);
186static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
187static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
188 int tablesize);
189static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
190 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200191static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
192 const char __user *buf, size_t count, int blocking,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500193 int read_only, int sg_io_owned, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
195 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
197static void sg_remove_scat(Sg_scatter_hold * schp);
198static void sg_build_reserve(Sg_fd * sfp, int req_size);
199static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
200static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500202static void sg_remove_sfp(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
204static Sg_request *sg_add_request(Sg_fd * sfp);
205static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
206static int sg_res_in_use(Sg_fd * sfp);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500207static Sg_device *sg_lookup_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static Sg_device *sg_get_dev(int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500209static void sg_put_dev(Sg_device *sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#ifdef CONFIG_SCSI_PROC_FS
211static int sg_last_dev(void);
212#endif
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define SZ_SG_HEADER sizeof(struct sg_header)
215#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
216#define SZ_SG_IOVEC sizeof(sg_iovec_t)
217#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
218
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900219static int sg_allow_access(struct file *filp, unsigned char *cmd)
220{
221 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
222 struct request_queue *q = sfp->parentdp->device->request_queue;
223
224 if (sfp->parentdp->device->type == TYPE_SCANNER)
225 return 0;
226
227 return blk_verify_command(&q->cmd_filter,
228 cmd, filp->f_mode & FMODE_WRITE);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static int
232sg_open(struct inode *inode, struct file *filp)
233{
234 int dev = iminor(inode);
235 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600236 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 Sg_device *sdp;
238 Sg_fd *sfp;
239 int res;
240 int retval;
241
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600242 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 nonseekable_open(inode, filp);
244 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
245 sdp = sg_get_dev(dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500246 if (IS_ERR(sdp)) {
247 retval = PTR_ERR(sdp);
248 sdp = NULL;
249 goto sg_put;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* This driver's module count bumped by fops_get in <linux/fs.h> */
253 /* Prevent the device driver from vanishing while we sleep */
254 retval = scsi_device_get(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500255 if (retval)
256 goto sg_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (!((flags & O_NONBLOCK) ||
259 scsi_block_when_processing_errors(sdp->device))) {
260 retval = -ENXIO;
261 /* we are in error recovery for this device */
262 goto error_out;
263 }
264
265 if (flags & O_EXCL) {
266 if (O_RDONLY == (flags & O_ACCMODE)) {
267 retval = -EPERM; /* Can't lock it with read only access */
268 goto error_out;
269 }
270 if (sdp->headfp && (flags & O_NONBLOCK)) {
271 retval = -EBUSY;
272 goto error_out;
273 }
274 res = 0;
275 __wait_event_interruptible(sdp->o_excl_wait,
276 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
277 if (res) {
278 retval = res; /* -ERESTARTSYS because signal hit process */
279 goto error_out;
280 }
281 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
282 if (flags & O_NONBLOCK) {
283 retval = -EBUSY;
284 goto error_out;
285 }
286 res = 0;
287 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
288 res);
289 if (res) {
290 retval = res; /* -ERESTARTSYS because signal hit process */
291 goto error_out;
292 }
293 }
294 if (sdp->detached) {
295 retval = -ENODEV;
296 goto error_out;
297 }
298 if (!sdp->headfp) { /* no existing opens on this device */
299 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600300 q = sdp->device->request_queue;
301 sdp->sg_tablesize = min(q->max_hw_segments,
302 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 if ((sfp = sg_add_sfp(sdp, dev)))
305 filp->private_data = sfp;
306 else {
Tony Battersbyc6517b792009-01-21 14:45:50 -0500307 if (flags & O_EXCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 sdp->exclude = 0; /* undo if error */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500309 wake_up_interruptible(&sdp->o_excl_wait);
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 retval = -ENOMEM;
312 goto error_out;
313 }
Tony Battersbyc6517b792009-01-21 14:45:50 -0500314 retval = 0;
315error_out:
316 if (retval)
317 scsi_device_put(sdp->device);
318sg_put:
319 if (sdp)
320 sg_put_dev(sdp);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600321 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return retval;
323}
324
325/* Following function was formerly called 'sg_close' */
326static int
327sg_release(struct inode *inode, struct file *filp)
328{
329 Sg_device *sdp;
330 Sg_fd *sfp;
331
332 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
333 return -ENXIO;
334 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
Tony Battersbyc6517b792009-01-21 14:45:50 -0500335
336 sfp->closed = 1;
337
338 sdp->exclude = 0;
339 wake_up_interruptible(&sdp->o_excl_wait);
340
341 kref_put(&sfp->f_ref, sg_remove_sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343}
344
345static ssize_t
346sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 Sg_device *sdp;
349 Sg_fd *sfp;
350 Sg_request *srp;
351 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600353 struct sg_header *old_hdr = NULL;
354 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
357 return -ENXIO;
358 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
359 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!access_ok(VERIFY_WRITE, buf, count))
362 return -EFAULT;
363 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600364 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
365 if (!old_hdr)
366 return -ENOMEM;
367 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
368 retval = -EFAULT;
369 goto free_old_hdr;
370 }
371 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600373 sg_io_hdr_t *new_hdr;
374 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
375 if (!new_hdr) {
376 retval = -ENOMEM;
377 goto free_old_hdr;
378 }
379 retval =__copy_from_user
380 (new_hdr, buf, SZ_SG_IO_HDR);
381 req_pack_id = new_hdr->pack_id;
382 kfree(new_hdr);
383 if (retval) {
384 retval = -EFAULT;
385 goto free_old_hdr;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 } else
cb59e842005-04-02 13:51:23 -0600389 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 srp = sg_get_rq_mark(sfp, req_pack_id);
392 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600393 if (sdp->detached) {
394 retval = -ENODEV;
395 goto free_old_hdr;
396 }
397 if (filp->f_flags & O_NONBLOCK) {
398 retval = -EAGAIN;
399 goto free_old_hdr;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 while (1) {
cb59e842005-04-02 13:51:23 -0600402 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600404 (sdp->detached ||
405 (srp = sg_get_rq_mark(sfp, req_pack_id))),
406 retval);
407 if (sdp->detached) {
408 retval = -ENODEV;
409 goto free_old_hdr;
410 }
411 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
cb59e842005-04-02 13:51:23 -0600413
414 /* -ERESTARTSYS as signal hit process */
415 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 }
cb59e842005-04-02 13:51:23 -0600418 if (srp->header.interface_id != '\0') {
419 retval = sg_new_read(sfp, buf, count, srp);
420 goto free_old_hdr;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600424 if (old_hdr == NULL) {
425 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
426 if (! old_hdr) {
427 retval = -ENOMEM;
428 goto free_old_hdr;
429 }
430 }
431 memset(old_hdr, 0, SZ_SG_HEADER);
432 old_hdr->reply_len = (int) hp->timeout;
433 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
434 old_hdr->pack_id = hp->pack_id;
435 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600437 old_hdr->target_status = hp->masked_status;
438 old_hdr->host_status = hp->host_status;
439 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if ((CHECK_CONDITION & hp->masked_status) ||
441 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600442 memcpy(old_hdr->sense_buffer, srp->sense_b,
443 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 switch (hp->host_status) {
445 /* This setup of 'result' is for backward compatibility and is best
446 ignored by the user who should use target, host + driver status */
447 case DID_OK:
448 case DID_PASSTHROUGH:
449 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600450 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 case DID_NO_CONNECT:
453 case DID_BUS_BUSY:
454 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600455 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 case DID_BAD_TARGET:
458 case DID_ABORT:
459 case DID_PARITY:
460 case DID_RESET:
461 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600462 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600465 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 hp->masked_status == GOOD) ? 0 : EIO;
467 break;
468 default:
cb59e842005-04-02 13:51:23 -0600469 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 }
472
473 /* Now copy the result back to the user buffer. */
474 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600475 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
476 retval = -EFAULT;
477 goto free_old_hdr;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600480 if (count > old_hdr->reply_len)
481 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600483 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
484 retval = -EFAULT;
485 goto free_old_hdr;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 } else
cb59e842005-04-02 13:51:23 -0600489 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600491 retval = count;
492free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800493 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600494 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497static ssize_t
498sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
499{
500 sg_io_hdr_t *hp = &srp->header;
501 int err = 0;
502 int len;
503
504 if (count < SZ_SG_IO_HDR) {
505 err = -EINVAL;
506 goto err_out;
507 }
508 hp->sb_len_wr = 0;
509 if ((hp->mx_sb_len > 0) && hp->sbp) {
510 if ((CHECK_CONDITION & hp->masked_status) ||
511 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600512 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
514 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
515 len = (len > sb_len) ? sb_len : len;
516 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
517 err = -EFAULT;
518 goto err_out;
519 }
520 hp->sb_len_wr = len;
521 }
522 }
523 if (hp->masked_status || hp->host_status || hp->driver_status)
524 hp->info |= SG_INFO_CHECK;
525 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
526 err = -EFAULT;
527 goto err_out;
528 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900529err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sg_finish_rem_req(srp);
531 return (0 == err) ? count : err;
532}
533
534static ssize_t
535sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
536{
537 int mxsize, cmd_size, k;
538 int input_size, blocking;
539 unsigned char opcode;
540 Sg_device *sdp;
541 Sg_fd *sfp;
542 Sg_request *srp;
543 struct sg_header old_hdr;
544 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600545 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
548 return -ENXIO;
549 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
550 sdp->disk->disk_name, (int) count));
551 if (sdp->detached)
552 return -ENODEV;
553 if (!((filp->f_flags & O_NONBLOCK) ||
554 scsi_block_when_processing_errors(sdp->device)))
555 return -ENXIO;
556
557 if (!access_ok(VERIFY_READ, buf, count))
558 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
559 if (count < SZ_SG_HEADER)
560 return -EIO;
561 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
562 return -EFAULT;
563 blocking = !(filp->f_flags & O_NONBLOCK);
564 if (old_hdr.reply_len < 0)
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500565 return sg_new_write(sfp, filp, buf, count,
566 blocking, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (count < (SZ_SG_HEADER + 6))
568 return -EIO; /* The minimum scsi command length is 6 bytes. */
569
570 if (!(srp = sg_add_request(sfp))) {
571 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
572 return -EDOM;
573 }
574 buf += SZ_SG_HEADER;
575 __get_user(opcode, buf);
576 if (sfp->next_cmd_len > 0) {
577 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
578 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
579 sfp->next_cmd_len = 0;
580 sg_remove_request(sfp, srp);
581 return -EIO;
582 }
583 cmd_size = sfp->next_cmd_len;
584 sfp->next_cmd_len = 0; /* reset so only this write() effected */
585 } else {
586 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
587 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
588 cmd_size = 12;
589 }
590 SCSI_LOG_TIMEOUT(4, printk(
591 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
592/* Determine buffer size. */
593 input_size = count - cmd_size;
594 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
595 mxsize -= SZ_SG_HEADER;
596 input_size -= SZ_SG_HEADER;
597 if (input_size < 0) {
598 sg_remove_request(sfp, srp);
599 return -EIO; /* User did not pass enough bytes for this command. */
600 }
601 hp = &srp->header;
602 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
603 hp->cmd_len = (unsigned char) cmd_size;
604 hp->iovec_count = 0;
605 hp->mx_sb_len = 0;
606 if (input_size > 0)
607 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
608 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
609 else
610 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
611 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900612 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
613 hp->dxferp = (char __user *)buf + cmd_size;
614 else
615 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 hp->sbp = NULL;
617 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
618 hp->flags = input_size; /* structure abuse ... */
619 hp->pack_id = old_hdr.pack_id;
620 hp->usr_ptr = NULL;
621 if (__copy_from_user(cmnd, buf, cmd_size))
622 return -EFAULT;
623 /*
624 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
625 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
626 * is a non-zero input_size, so emit a warning.
627 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100628 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
629 static char cmd[TASK_COMM_LEN];
630 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 printk(KERN_WARNING
632 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
633 "guessing data in;\n" KERN_WARNING " "
634 "program %s not setting count and/or reply_len properly\n",
635 old_hdr.reply_len - (int)SZ_SG_HEADER,
636 input_size, (unsigned int) cmnd[0],
637 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100638 strcpy(cmd, current->comm);
639 }
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
642 return (k < 0) ? k : count;
643}
644
645static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200646sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500647 size_t count, int blocking, int read_only, int sg_io_owned,
Adel Gadllah0b07de82008-06-26 13:48:27 +0200648 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 int k;
651 Sg_request *srp;
652 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600653 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int timeout;
655 unsigned long ul_timeout;
656
657 if (count < SZ_SG_IO_HDR)
658 return -EINVAL;
659 if (!access_ok(VERIFY_READ, buf, count))
660 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
661
662 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
663 if (!(srp = sg_add_request(sfp))) {
664 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
665 return -EDOM;
666 }
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500667 srp->sg_io_owned = sg_io_owned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 hp = &srp->header;
669 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
670 sg_remove_request(sfp, srp);
671 return -EFAULT;
672 }
673 if (hp->interface_id != 'S') {
674 sg_remove_request(sfp, srp);
675 return -ENOSYS;
676 }
677 if (hp->flags & SG_FLAG_MMAP_IO) {
678 if (hp->dxfer_len > sfp->reserve.bufflen) {
679 sg_remove_request(sfp, srp);
680 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
681 }
682 if (hp->flags & SG_FLAG_DIRECT_IO) {
683 sg_remove_request(sfp, srp);
684 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
685 }
686 if (sg_res_in_use(sfp)) {
687 sg_remove_request(sfp, srp);
688 return -EBUSY; /* reserve buffer already being used */
689 }
690 }
691 ul_timeout = msecs_to_jiffies(srp->header.timeout);
692 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
693 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
694 sg_remove_request(sfp, srp);
695 return -EMSGSIZE;
696 }
697 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
698 sg_remove_request(sfp, srp);
699 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
700 }
701 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
702 sg_remove_request(sfp, srp);
703 return -EFAULT;
704 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900705 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sg_remove_request(sfp, srp);
707 return -EPERM;
708 }
709 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
710 if (k < 0)
711 return k;
712 if (o_srp)
713 *o_srp = srp;
714 return count;
715}
716
717static int
718sg_common_write(Sg_fd * sfp, Sg_request * srp,
719 unsigned char *cmnd, int timeout, int blocking)
720{
Mike Christied6b10342005-11-08 04:06:41 -0600721 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 Sg_device *sdp = sfp->parentdp;
723 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
726 hp->status = 0;
727 hp->masked_status = 0;
728 hp->msg_status = 0;
729 hp->info = 0;
730 hp->host_status = 0;
731 hp->driver_status = 0;
732 hp->resid = 0;
733 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
734 (int) cmnd[0], (int) hp->cmd_len));
735
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900736 k = sg_start_req(srp, cmnd);
737 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400738 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sg_finish_rem_req(srp);
740 return k; /* probably out of space --> ENOMEM */
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (sdp->detached) {
743 sg_finish_rem_req(srp);
744 return -ENODEV;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 switch (hp->dxfer_direction) {
748 case SG_DXFER_TO_FROM_DEV:
749 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600750 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600753 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600756 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 default:
Mike Christied6b10342005-11-08 04:06:41 -0600759 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 }
cb59e842005-04-02 13:51:23 -0600762 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200763
764 srp->rq->timeout = timeout;
Tony Battersbyc6517b792009-01-21 14:45:50 -0500765 kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200766 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
767 srp->rq, 1, sg_rq_end_io);
768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772sg_ioctl(struct inode *inode, struct file *filp,
773 unsigned int cmd_in, unsigned long arg)
774{
775 void __user *p = (void __user *)arg;
776 int __user *ip = p;
777 int result, val, read_only;
778 Sg_device *sdp;
779 Sg_fd *sfp;
780 Sg_request *srp;
781 unsigned long iflags;
782
783 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
784 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
787 sdp->disk->disk_name, (int) cmd_in));
788 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
789
790 switch (cmd_in) {
791 case SG_IO:
792 {
793 int blocking = 1; /* ignore O_NONBLOCK flag */
794
795 if (sdp->detached)
796 return -ENODEV;
797 if (!scsi_block_when_processing_errors(sdp->device))
798 return -ENXIO;
799 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
800 return -EFAULT;
801 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200802 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500803 blocking, read_only, 1, &srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (result < 0)
805 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 while (1) {
807 result = 0; /* following macro to beat race condition */
808 __wait_event_interruptible(sfp->read_wait,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500809 (srp->done || sdp->detached),
810 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (sdp->detached)
812 return -ENODEV;
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500813 write_lock_irq(&sfp->rq_list_lock);
814 if (srp->done) {
815 srp->done = 2;
816 write_unlock_irq(&sfp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 break;
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 srp->orphan = 1;
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500820 write_unlock_irq(&sfp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return result; /* -ERESTARTSYS because signal hit process */
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
824 return (result < 0) ? result : 0;
825 }
826 case SG_SET_TIMEOUT:
827 result = get_user(val, ip);
828 if (result)
829 return result;
830 if (val < 0)
831 return -EIO;
832 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
833 val = MULDIV (INT_MAX, USER_HZ, HZ);
834 sfp->timeout_user = val;
835 sfp->timeout = MULDIV (val, HZ, USER_HZ);
836
837 return 0;
838 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
839 /* strange ..., for backward compatibility */
840 return sfp->timeout_user;
841 case SG_SET_FORCE_LOW_DMA:
842 result = get_user(val, ip);
843 if (result)
844 return result;
845 if (val) {
846 sfp->low_dma = 1;
847 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
848 val = (int) sfp->reserve.bufflen;
849 sg_remove_scat(&sfp->reserve);
850 sg_build_reserve(sfp, val);
851 }
852 } else {
853 if (sdp->detached)
854 return -ENODEV;
855 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
856 }
857 return 0;
858 case SG_GET_LOW_DMA:
859 return put_user((int) sfp->low_dma, ip);
860 case SG_GET_SCSI_ID:
861 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
862 return -EFAULT;
863 else {
864 sg_scsi_id_t __user *sg_idp = p;
865
866 if (sdp->detached)
867 return -ENODEV;
868 __put_user((int) sdp->device->host->host_no,
869 &sg_idp->host_no);
870 __put_user((int) sdp->device->channel,
871 &sg_idp->channel);
872 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
873 __put_user((int) sdp->device->lun, &sg_idp->lun);
874 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
875 __put_user((short) sdp->device->host->cmd_per_lun,
876 &sg_idp->h_cmd_per_lun);
877 __put_user((short) sdp->device->queue_depth,
878 &sg_idp->d_queue_depth);
879 __put_user(0, &sg_idp->unused[0]);
880 __put_user(0, &sg_idp->unused[1]);
881 return 0;
882 }
883 case SG_SET_FORCE_PACK_ID:
884 result = get_user(val, ip);
885 if (result)
886 return result;
887 sfp->force_packid = val ? 1 : 0;
888 return 0;
889 case SG_GET_PACK_ID:
890 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
891 return -EFAULT;
892 read_lock_irqsave(&sfp->rq_list_lock, iflags);
893 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
894 if ((1 == srp->done) && (!srp->sg_io_owned)) {
895 read_unlock_irqrestore(&sfp->rq_list_lock,
896 iflags);
897 __put_user(srp->header.pack_id, ip);
898 return 0;
899 }
900 }
901 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
902 __put_user(-1, ip);
903 return 0;
904 case SG_GET_NUM_WAITING:
905 read_lock_irqsave(&sfp->rq_list_lock, iflags);
906 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
907 if ((1 == srp->done) && (!srp->sg_io_owned))
908 ++val;
909 }
910 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
911 return put_user(val, ip);
912 case SG_GET_SG_TABLESIZE:
913 return put_user(sdp->sg_tablesize, ip);
914 case SG_SET_RESERVED_SIZE:
915 result = get_user(val, ip);
916 if (result)
917 return result;
918 if (val < 0)
919 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500920 val = min_t(int, val,
921 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (val != sfp->reserve.bufflen) {
923 if (sg_res_in_use(sfp) || sfp->mmap_called)
924 return -EBUSY;
925 sg_remove_scat(&sfp->reserve);
926 sg_build_reserve(sfp, val);
927 }
928 return 0;
929 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500930 val = min_t(int, sfp->reserve.bufflen,
931 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return put_user(val, ip);
933 case SG_SET_COMMAND_Q:
934 result = get_user(val, ip);
935 if (result)
936 return result;
937 sfp->cmd_q = val ? 1 : 0;
938 return 0;
939 case SG_GET_COMMAND_Q:
940 return put_user((int) sfp->cmd_q, ip);
941 case SG_SET_KEEP_ORPHAN:
942 result = get_user(val, ip);
943 if (result)
944 return result;
945 sfp->keep_orphan = val;
946 return 0;
947 case SG_GET_KEEP_ORPHAN:
948 return put_user((int) sfp->keep_orphan, ip);
949 case SG_NEXT_CMD_LEN:
950 result = get_user(val, ip);
951 if (result)
952 return result;
953 sfp->next_cmd_len = (val > 0) ? val : 0;
954 return 0;
955 case SG_GET_VERSION_NUM:
956 return put_user(sg_version_num, ip);
957 case SG_GET_ACCESS_COUNT:
958 /* faked - we don't have a real access count anymore */
959 val = (sdp->device ? 1 : 0);
960 return put_user(val, ip);
961 case SG_GET_REQUEST_TABLE:
962 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
963 return -EFAULT;
964 else {
cb59e842005-04-02 13:51:23 -0600965 sg_req_info_t *rinfo;
966 unsigned int ms;
967
968 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
969 GFP_KERNEL);
970 if (!rinfo)
971 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 read_lock_irqsave(&sfp->rq_list_lock, iflags);
973 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
974 ++val, srp = srp ? srp->nextrp : srp) {
975 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
976 if (srp) {
977 rinfo[val].req_state = srp->done + 1;
978 rinfo[val].problem =
979 srp->header.masked_status &
980 srp->header.host_status &
981 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600982 if (srp->done)
983 rinfo[val].duration =
984 srp->header.duration;
985 else {
986 ms = jiffies_to_msecs(jiffies);
987 rinfo[val].duration =
988 (ms > srp->header.duration) ?
989 (ms - srp->header.duration) : 0;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600992 rinfo[val].sg_io_owned =
993 srp->sg_io_owned;
994 rinfo[val].pack_id =
995 srp->header.pack_id;
996 rinfo[val].usr_ptr =
997 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 }
1000 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001001 result = __copy_to_user(p, rinfo,
1002 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1003 result = result ? -EFAULT : 0;
1004 kfree(rinfo);
1005 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 case SG_EMULATED_HOST:
1008 if (sdp->detached)
1009 return -ENODEV;
1010 return put_user(sdp->device->host->hostt->emulated, ip);
1011 case SG_SCSI_RESET:
1012 if (sdp->detached)
1013 return -ENODEV;
1014 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001015 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EBUSY;
1017 } else if (!scsi_block_when_processing_errors(sdp->device))
1018 return -EBUSY;
1019 result = get_user(val, ip);
1020 if (result)
1021 return result;
1022 if (SG_SCSI_RESET_NOTHING == val)
1023 return 0;
1024 switch (val) {
1025 case SG_SCSI_RESET_DEVICE:
1026 val = SCSI_TRY_RESET_DEVICE;
1027 break;
Brian King39120e12008-07-01 13:03:19 -05001028 case SG_SCSI_RESET_TARGET:
1029 val = SCSI_TRY_RESET_TARGET;
1030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 case SG_SCSI_RESET_BUS:
1032 val = SCSI_TRY_RESET_BUS;
1033 break;
1034 case SG_SCSI_RESET_HOST:
1035 val = SCSI_TRY_RESET_HOST;
1036 break;
1037 default:
1038 return -EINVAL;
1039 }
1040 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1041 return -EACCES;
1042 return (scsi_reset_provider(sdp->device, val) ==
1043 SUCCESS) ? 0 : -EIO;
1044 case SCSI_IOCTL_SEND_COMMAND:
1045 if (sdp->detached)
1046 return -ENODEV;
1047 if (read_only) {
1048 unsigned char opcode = WRITE_6;
1049 Scsi_Ioctl_Command __user *siocp = p;
1050
1051 if (copy_from_user(&opcode, siocp->data, 1))
1052 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001053 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EPERM;
1055 }
Al Viroe915e872008-09-02 17:16:41 -04001056 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 case SG_SET_DEBUG:
1058 result = get_user(val, ip);
1059 if (result)
1060 return result;
1061 sdp->sgdebug = (char) val;
1062 return 0;
1063 case SCSI_IOCTL_GET_IDLUN:
1064 case SCSI_IOCTL_GET_BUS_NUMBER:
1065 case SCSI_IOCTL_PROBE_HOST:
1066 case SG_GET_TRANSFORM:
1067 if (sdp->detached)
1068 return -ENODEV;
1069 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001070 case BLKSECTGET:
1071 return put_user(sdp->device->request_queue->max_sectors * 512,
1072 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001073 case BLKTRACESETUP:
1074 return blk_trace_setup(sdp->device->request_queue,
1075 sdp->disk->disk_name,
Martin Peschke76e3a192009-01-30 15:46:23 +01001076 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
Christof Schmitt6da127a2008-01-11 10:09:43 +01001077 (char *)arg);
1078 case BLKTRACESTART:
1079 return blk_trace_startstop(sdp->device->request_queue, 1);
1080 case BLKTRACESTOP:
1081 return blk_trace_startstop(sdp->device->request_queue, 0);
1082 case BLKTRACETEARDOWN:
1083 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 default:
1085 if (read_only)
1086 return -EPERM; /* don't know so take safe approach */
1087 return scsi_ioctl(sdp->device, cmd_in, p);
1088 }
1089}
1090
1091#ifdef CONFIG_COMPAT
1092static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1093{
1094 Sg_device *sdp;
1095 Sg_fd *sfp;
1096 struct scsi_device *sdev;
1097
1098 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1099 return -ENXIO;
1100
1101 sdev = sdp->device;
1102 if (sdev->host->hostt->compat_ioctl) {
1103 int ret;
1104
1105 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1106
1107 return ret;
1108 }
1109
1110 return -ENOIOCTLCMD;
1111}
1112#endif
1113
1114static unsigned int
1115sg_poll(struct file *filp, poll_table * wait)
1116{
1117 unsigned int res = 0;
1118 Sg_device *sdp;
1119 Sg_fd *sfp;
1120 Sg_request *srp;
1121 int count = 0;
1122 unsigned long iflags;
1123
1124 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1125 || sfp->closed)
1126 return POLLERR;
1127 poll_wait(filp, &sfp->read_wait, wait);
1128 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1129 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1130 /* if any read waiting, flag it */
1131 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1132 res = POLLIN | POLLRDNORM;
1133 ++count;
1134 }
1135 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1136
1137 if (sdp->detached)
1138 res |= POLLHUP;
1139 else if (!sfp->cmd_q) {
1140 if (0 == count)
1141 res |= POLLOUT | POLLWRNORM;
1142 } else if (count < SG_MAX_QUEUE)
1143 res |= POLLOUT | POLLWRNORM;
1144 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1145 sdp->disk->disk_name, (int) res));
1146 return res;
1147}
1148
1149static int
1150sg_fasync(int fd, struct file *filp, int mode)
1151{
1152 int retval;
1153 Sg_device *sdp;
1154 Sg_fd *sfp;
1155
1156 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1157 return -ENXIO;
1158 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1159 sdp->disk->disk_name, mode));
1160
1161 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1162 return (retval < 0) ? retval : 0;
1163}
1164
Nick Piggina13ff0b2008-02-07 18:46:06 -08001165static int
1166sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001169 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001171 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001174 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001176 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001178 return VM_FAULT_SIGBUS;
1179 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001181 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001182 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1183 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001184 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001185 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001186 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001187 struct page *page = nth_page(rsv_schp->pages[k],
1188 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001189 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001190 vmf->page = page;
1191 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Mike Christied6b10342005-11-08 04:06:41 -06001193 sa += len;
1194 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Mike Christied6b10342005-11-08 04:06:41 -06001196
Nick Piggina13ff0b2008-02-07 18:46:06 -08001197 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001201 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202};
1203
1204static int
1205sg_mmap(struct file *filp, struct vm_area_struct *vma)
1206{
1207 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001208 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001210 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1213 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001214 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1216 (void *) vma->vm_start, (int) req_sz));
1217 if (vma->vm_pgoff)
1218 return -EINVAL; /* want no offset */
1219 rsv_schp = &sfp->reserve;
1220 if (req_sz > rsv_schp->bufflen)
1221 return -ENOMEM; /* cannot map more than reserved buffer */
1222
Mike Christied6b10342005-11-08 04:06:41 -06001223 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001224 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1225 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001226 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001227 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001228 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Mike Christied6b10342005-11-08 04:06:41 -06001230
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001231 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001232 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 vma->vm_private_data = sfp;
1234 vma->vm_ops = &sg_mmap_vm_ops;
1235 return 0;
1236}
1237
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001238static void sg_rq_end_io_usercontext(struct work_struct *work)
1239{
1240 struct sg_request *srp = container_of(work, struct sg_request, ew.work);
1241 struct sg_fd *sfp = srp->parentfp;
1242
1243 sg_finish_rem_req(srp);
1244 kref_put(&sfp->f_ref, sg_remove_sfp);
1245}
1246
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001247/*
1248 * This function is a "bottom half" handler that is called by the mid
1249 * level when a command is completed (or has failed).
1250 */
1251static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001253 struct sg_request *srp = rq->end_io_data;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001254 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001257 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001258 char *sense;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001259 int result, resid, done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Tony Battersbyc6517b792009-01-21 14:45:50 -05001261 if (WARN_ON(srp->done != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sfp = srp->parentfp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001265 if (WARN_ON(sfp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001267
1268 sdp = sfp->parentdp;
1269 if (unlikely(sdp->detached))
1270 printk(KERN_INFO "sg_rq_end_io: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001272 sense = rq->sense;
1273 result = rq->errors;
1274 resid = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001277 sdp->disk->disk_name, srp->header.pack_id, result));
1278 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001279 ms = jiffies_to_msecs(jiffies);
1280 srp->header.duration = (ms > srp->header.duration) ?
1281 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001282 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct scsi_sense_hdr sshdr;
1284
Mike Christied6b10342005-11-08 04:06:41 -06001285 srp->header.status = 0xff & result;
1286 srp->header.masked_status = status_byte(result);
1287 srp->header.msg_status = msg_byte(result);
1288 srp->header.host_status = host_byte(result);
1289 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if ((sdp->sgdebug > 0) &&
1291 ((CHECK_CONDITION == srp->header.masked_status) ||
1292 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001293 __scsi_print_sense("sg_cmd_done", sense,
1294 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001297 if (driver_byte(result) != 0
1298 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 && !scsi_sense_is_deferred(&sshdr)
1300 && sshdr.sense_key == UNIT_ATTENTION
1301 && sdp->device->removable) {
1302 /* Detected possible disc change. Set the bit - this */
1303 /* may be used if there are filesystems using this device */
1304 sdp->device->changed = 1;
1305 }
1306 }
1307 /* Rely on write phase to clean out srp status values, so no "else" */
1308
Tony Battersbyc6517b792009-01-21 14:45:50 -05001309 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1310 if (unlikely(srp->orphan)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (sfp->keep_orphan)
1312 srp->sg_io_owned = 0;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001313 else
1314 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001316 srp->done = done;
1317 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1318
1319 if (likely(done)) {
1320 /* Now wake up any sg_read() that is waiting for this
1321 * packet.
1322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 wake_up_interruptible(&sfp->read_wait);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001324 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001325 kref_put(&sfp->f_ref, sg_remove_sfp);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001326 } else
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001327 execute_in_process_context(sg_rq_end_io_usercontext, &srp->ew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330static struct file_operations sg_fops = {
1331 .owner = THIS_MODULE,
1332 .read = sg_read,
1333 .write = sg_write,
1334 .poll = sg_poll,
1335 .ioctl = sg_ioctl,
1336#ifdef CONFIG_COMPAT
1337 .compat_ioctl = sg_compat_ioctl,
1338#endif
1339 .open = sg_open,
1340 .mmap = sg_mmap,
1341 .release = sg_release,
1342 .fasync = sg_fasync,
1343};
1344
gregkh@suse.ded2538782005-03-23 09:55:22 -08001345static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347static int sg_sysfs_valid = 0;
1348
James Bottomley7c07d612007-08-05 13:36:11 -05001349static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Mike Christied6b10342005-11-08 04:06:41 -06001351 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 Sg_device *sdp;
1353 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001354 int error;
1355 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jes Sorensen24669f752006-01-16 10:31:18 -05001357 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!sdp) {
1359 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001360 return ERR_PTR(-ENOMEM);
1361 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001362
James Bottomley7c07d612007-08-05 13:36:11 -05001363 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1364 printk(KERN_WARNING "idr expansion Sg_device failure\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05001365 error = -ENOMEM;
James Bottomley7c07d612007-08-05 13:36:11 -05001366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
James Bottomley7c07d612007-08-05 13:36:11 -05001369 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Tony Battersbyc6517b792009-01-21 14:45:50 -05001371 error = idr_get_new(&sg_index_idr, sdp, &k);
James Bottomley7c07d612007-08-05 13:36:11 -05001372 if (error) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05001373 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley7c07d612007-08-05 13:36:11 -05001374 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1375 error);
1376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (unlikely(k >= SG_MAX_DEVS))
1380 goto overflow;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1383 sprintf(disk->disk_name, "sg%d", k);
1384 disk->first_minor = k;
1385 sdp->disk = disk;
1386 sdp->device = scsidp;
1387 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001388 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001389 sdp->index = k;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001390 kref_init(&sdp->d_ref);
1391
1392 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
James Bottomley7c07d612007-08-05 13:36:11 -05001394 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001396 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001398 return ERR_PTR(error);
1399 }
1400 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 overflow:
Tony Battersbyc6517b792009-01-21 14:45:50 -05001403 idr_remove(&sg_index_idr, k);
1404 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley9ccfc752005-10-02 11:45:08 -05001405 sdev_printk(KERN_WARNING, scsidp,
1406 "Unable to attach sg device type=%d, minor "
1407 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 error = -ENODEV;
1409 goto out;
1410}
1411
1412static int
Tony Jonesee959b02008-02-22 00:13:36 +01001413sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Tony Jonesee959b02008-02-22 00:13:36 +01001415 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 struct gendisk *disk;
1417 Sg_device *sdp = NULL;
1418 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001419 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001420 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 disk = alloc_disk(1);
1423 if (!disk) {
1424 printk(KERN_WARNING "alloc_disk failed\n");
1425 return -ENOMEM;
1426 }
1427 disk->major = SCSI_GENERIC_MAJOR;
1428
1429 error = -ENOMEM;
1430 cdev = cdev_alloc();
1431 if (!cdev) {
1432 printk(KERN_WARNING "cdev_alloc failed\n");
1433 goto out;
1434 }
1435 cdev->owner = THIS_MODULE;
1436 cdev->ops = &sg_fops;
1437
James Bottomley7c07d612007-08-05 13:36:11 -05001438 sdp = sg_alloc(disk, scsidp);
1439 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001441 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto out;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
James Bottomley7c07d612007-08-05 13:36:11 -05001445 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001446 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001447 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 sdp->cdev = cdev;
1450 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001451 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Greg Kroah-Hartmand73a1a672008-07-21 20:03:34 -07001453 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1454 MKDEV(SCSI_GENERIC_MAJOR,
1455 sdp->index),
1456 sdp, "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001457 if (IS_ERR(sg_class_member)) {
1458 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001459 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001460 error = PTR_ERR(sg_class_member);
1461 goto cdev_add_err;
1462 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001463 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 &sg_class_member->kobj, "generic");
1465 if (error)
1466 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001467 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001469 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
James Bottomley9ccfc752005-10-02 11:45:08 -05001471 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001472 "Attached scsi generic sg%d type %d\n", sdp->index,
1473 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Tony Jonesee959b02008-02-22 00:13:36 +01001475 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001479cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001480 write_lock_irqsave(&sg_index_lock, iflags);
1481 idr_remove(&sg_index_idr, sdp->index);
1482 write_unlock_irqrestore(&sg_index_lock, iflags);
1483 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485out:
1486 put_disk(disk);
1487 if (cdev)
1488 cdev_del(cdev);
1489 return error;
1490}
1491
Tony Battersbyc6517b792009-01-21 14:45:50 -05001492static void sg_device_destroy(struct kref *kref)
1493{
1494 struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1495 unsigned long flags;
1496
1497 /* CAUTION! Note that the device can still be found via idr_find()
1498 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1499 * any other cleanup.
1500 */
1501
1502 write_lock_irqsave(&sg_index_lock, flags);
1503 idr_remove(&sg_index_idr, sdp->index);
1504 write_unlock_irqrestore(&sg_index_lock, flags);
1505
1506 SCSI_LOG_TIMEOUT(3,
1507 printk("sg_device_destroy: %s\n",
1508 sdp->disk->disk_name));
1509
1510 put_disk(sdp->disk);
1511 kfree(sdp);
1512}
1513
1514static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Tony Jonesee959b02008-02-22 00:13:36 +01001516 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1517 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 unsigned long iflags;
1519 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Tony Battersbyc6517b792009-01-21 14:45:50 -05001521 if (!sdp || sdp->detached)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Tony Battersbyc6517b792009-01-21 14:45:50 -05001524 SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
1525
1526 /* Need a write lock to set sdp->detached. */
James Bottomley7c07d612007-08-05 13:36:11 -05001527 write_lock_irqsave(&sg_index_lock, iflags);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001528 sdp->detached = 1;
1529 for (sfp = sdp->headfp; sfp; sfp = sfp->nextfp) {
1530 wake_up_interruptible(&sfp->read_wait);
1531 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
James Bottomley7c07d612007-08-05 13:36:11 -05001533 write_unlock_irqrestore(&sg_index_lock, iflags);
1534
1535 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001536 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001537 cdev_del(sdp->cdev);
1538 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Tony Battersbyc6517b792009-01-21 14:45:50 -05001540 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Douglas Gilbert6460e752006-09-20 18:20:49 -04001543module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1544module_param_named(def_reserved_size, def_reserved_size, int,
1545 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1547
1548MODULE_AUTHOR("Douglas Gilbert");
1549MODULE_DESCRIPTION("SCSI generic (sg) driver");
1550MODULE_LICENSE("GPL");
1551MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001552MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Douglas Gilbert6460e752006-09-20 18:20:49 -04001554MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1555 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1557MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1558
1559static int __init
1560init_sg(void)
1561{
1562 int rc;
1563
Douglas Gilbert6460e752006-09-20 18:20:49 -04001564 if (scatter_elem_sz < PAGE_SIZE) {
1565 scatter_elem_sz = PAGE_SIZE;
1566 scatter_elem_sz_prev = scatter_elem_sz;
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (def_reserved_size >= 0)
1569 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001570 else
1571 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1574 SG_MAX_DEVS, "sg");
1575 if (rc)
1576 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001577 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if ( IS_ERR(sg_sysfs_class) ) {
1579 rc = PTR_ERR(sg_sysfs_class);
1580 goto err_out;
1581 }
1582 sg_sysfs_valid = 1;
1583 rc = scsi_register_interface(&sg_interface);
1584 if (0 == rc) {
1585#ifdef CONFIG_SCSI_PROC_FS
1586 sg_proc_init();
1587#endif /* CONFIG_SCSI_PROC_FS */
1588 return 0;
1589 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001590 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591err_out:
1592 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1593 return rc;
1594}
1595
1596static void __exit
1597exit_sg(void)
1598{
1599#ifdef CONFIG_SCSI_PROC_FS
1600 sg_proc_cleanup();
1601#endif /* CONFIG_SCSI_PROC_FS */
1602 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001603 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 sg_sysfs_valid = 0;
1605 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1606 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001607 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001610static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001611{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001612 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001613 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001614 Sg_fd *sfp = srp->parentfp;
1615 sg_io_hdr_t *hp = &srp->header;
1616 int dxfer_len = (int) hp->dxfer_len;
1617 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001618 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001619 Sg_scatter_hold *req_schp = &srp->data;
1620 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1621 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001622 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001623 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1624
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001625 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1626 dxfer_len));
1627
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001628 rq = blk_get_request(q, rw, GFP_ATOMIC);
1629 if (!rq)
1630 return -ENOMEM;
1631
1632 memcpy(rq->cmd, cmd, hp->cmd_len);
1633
1634 rq->cmd_len = hp->cmd_len;
1635 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1636
1637 srp->rq = rq;
1638 rq->end_io_data = srp;
1639 rq->sense = srp->sense_b;
1640 rq->retries = SG_DEFAULT_RETRIES;
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001643 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001644
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001645 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1646 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1647 !sfp->parentdp->device->host->unchecked_isa_dma &&
FUJITA Tomonori01cfcdd2008-08-28 15:05:59 +09001648 blk_rq_aligned(q, hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001649 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001650 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001651 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001652
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001653 if (md) {
1654 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1655 sg_link_reserve(sfp, srp, dxfer_len);
1656 else {
1657 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1658 if (res)
1659 return res;
1660 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001661
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001662 md->pages = req_schp->pages;
1663 md->page_order = req_schp->page_order;
1664 md->nr_entries = req_schp->k_use_sg;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001665 md->offset = 0;
FUJITA Tomonori97ae77a2008-12-18 14:49:38 +09001666 md->null_mapped = hp->dxferp ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001668
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001669 if (iov_count)
1670 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1671 hp->dxfer_len, GFP_ATOMIC);
1672 else
1673 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1674 hp->dxfer_len, GFP_ATOMIC);
1675
1676 if (!res) {
1677 srp->bio = rq->bio;
1678
1679 if (!md) {
1680 req_schp->dio_in_use = 1;
1681 hp->info |= SG_INFO_DIRECT_IO;
1682 }
1683 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001684 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687static void
1688sg_finish_rem_req(Sg_request * srp)
1689{
1690 Sg_fd *sfp = srp->parentfp;
1691 Sg_scatter_hold *req_schp = &srp->data;
1692
1693 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1694 if (srp->res_used)
1695 sg_unlink_reserve(sfp, srp);
1696 else
1697 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001698
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001699 if (srp->rq) {
1700 if (srp->bio)
1701 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001702
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001703 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001704 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 sg_remove_request(sfp, srp);
1707}
1708
1709static int
1710sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1711{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001712 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001713 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001715 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1716 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001719 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static int
1723sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1724{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001725 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001726 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001727 int blk_size = buff_size, order;
1728 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Eric Sesterhennfb119932007-05-23 14:41:36 -07001730 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EFAULT;
1732 if (0 == blk_size)
1733 ++blk_size; /* don't know why */
1734/* round request up to next highest SG_SECTOR_SZ byte boundary */
1735 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1736 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1737 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001738
1739 /* N.B. ret_sz carried into this block ... */
1740 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1741 if (mx_sc_elems < 0)
1742 return mx_sc_elems; /* most likely -ENOMEM */
1743
Douglas Gilbert6460e752006-09-20 18:20:49 -04001744 num = scatter_elem_sz;
1745 if (unlikely(num != scatter_elem_sz_prev)) {
1746 if (num < PAGE_SIZE) {
1747 scatter_elem_sz = PAGE_SIZE;
1748 scatter_elem_sz_prev = PAGE_SIZE;
1749 } else
1750 scatter_elem_sz_prev = num;
1751 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001752
1753 if (sfp->low_dma)
1754 gfp_mask |= GFP_DMA;
1755
1756 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1757 gfp_mask |= __GFP_ZERO;
1758
1759 order = get_order(num);
1760retry:
1761 ret_sz = 1 << (PAGE_SHIFT + order);
1762
1763 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1764 k++, rem_sz -= ret_sz) {
1765
Douglas Gilbert6460e752006-09-20 18:20:49 -04001766 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001767 scatter_elem_sz_prev : rem_sz;
1768
1769 schp->pages[k] = alloc_pages(gfp_mask, order);
1770 if (!schp->pages[k])
1771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Douglas Gilbert6460e752006-09-20 18:20:49 -04001773 if (num == scatter_elem_sz_prev) {
1774 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1775 scatter_elem_sz = ret_sz;
1776 scatter_elem_sz_prev = ret_sz;
1777 }
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001780 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1781 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001782 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001784 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001785 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001786 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1787 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001788
1789 schp->bufflen = blk_size;
1790 if (rem_sz > 0) /* must have failed */
1791 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001793out:
1794 for (i = 0; i < k; i++)
1795 __free_pages(schp->pages[k], order);
1796
1797 if (--order >= 0)
1798 goto retry;
1799
1800 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803static void
1804sg_remove_scat(Sg_scatter_hold * schp)
1805{
1806 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001807 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001808 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int k;
1810
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001811 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001813 "sg_remove_scat: k=%d, pg=0x%p\n",
1814 k, schp->pages[k]));
1815 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001817
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001818 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Mike Christied6b10342005-11-08 04:06:41 -06001820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 memset(schp, 0, sizeof (*schp));
1822}
1823
1824static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1826{
1827 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001828 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1831 num_read_xfer));
1832 if ((!outp) || (num_read_xfer <= 0))
1833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001835 num = 1 << (PAGE_SHIFT + schp->page_order);
1836 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001837 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001838 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001839 num_read_xfer))
1840 return -EFAULT;
1841 break;
1842 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001843 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001844 num))
1845 return -EFAULT;
1846 num_read_xfer -= num;
1847 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 break;
Mike Christied6b10342005-11-08 04:06:41 -06001849 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Mike Christied6b10342005-11-08 04:06:41 -06001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854}
1855
1856static void
1857sg_build_reserve(Sg_fd * sfp, int req_size)
1858{
1859 Sg_scatter_hold *schp = &sfp->reserve;
1860
1861 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1862 do {
1863 if (req_size < PAGE_SIZE)
1864 req_size = PAGE_SIZE;
1865 if (0 == sg_build_indirect(schp, sfp, req_size))
1866 return;
1867 else
1868 sg_remove_scat(schp);
1869 req_size >>= 1; /* divide by 2 */
1870 } while (req_size > (PAGE_SIZE / 2));
1871}
1872
1873static void
1874sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1875{
1876 Sg_scatter_hold *req_schp = &srp->data;
1877 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001878 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 srp->res_used = 1;
1881 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001882 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001884 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1885 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001886 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001887 req_schp->k_use_sg = k + 1;
1888 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001889 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001890
1891 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001892 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001893 break;
1894 } else
1895 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Mike Christied6b10342005-11-08 04:06:41 -06001897
1898 if (k >= rsv_schp->k_use_sg)
1899 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902static void
1903sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1904{
1905 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1908 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 req_schp->k_use_sg = 0;
1910 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001911 req_schp->pages = NULL;
1912 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 req_schp->sglist_len = 0;
1914 sfp->save_scat_len = 0;
1915 srp->res_used = 0;
1916}
1917
1918static Sg_request *
1919sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1920{
1921 Sg_request *resp;
1922 unsigned long iflags;
1923
1924 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1925 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1926 /* look for requests that are ready + not SG_IO owned */
1927 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1928 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1929 resp->done = 2; /* guard against other readers */
1930 break;
1931 }
1932 }
1933 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1934 return resp;
1935}
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/* always adds to end of list */
1938static Sg_request *
1939sg_add_request(Sg_fd * sfp)
1940{
1941 int k;
1942 unsigned long iflags;
1943 Sg_request *resp;
1944 Sg_request *rp = sfp->req_arr;
1945
1946 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1947 resp = sfp->headrp;
1948 if (!resp) {
1949 memset(rp, 0, sizeof (Sg_request));
1950 rp->parentfp = sfp;
1951 resp = rp;
1952 sfp->headrp = resp;
1953 } else {
1954 if (0 == sfp->cmd_q)
1955 resp = NULL; /* command queuing disallowed */
1956 else {
1957 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1958 if (!rp->parentfp)
1959 break;
1960 }
1961 if (k < SG_MAX_QUEUE) {
1962 memset(rp, 0, sizeof (Sg_request));
1963 rp->parentfp = sfp;
1964 while (resp->nextrp)
1965 resp = resp->nextrp;
1966 resp->nextrp = rp;
1967 resp = rp;
1968 } else
1969 resp = NULL;
1970 }
1971 }
1972 if (resp) {
1973 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06001974 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1977 return resp;
1978}
1979
1980/* Return of 1 for found; 0 for not found */
1981static int
1982sg_remove_request(Sg_fd * sfp, Sg_request * srp)
1983{
1984 Sg_request *prev_rp;
1985 Sg_request *rp;
1986 unsigned long iflags;
1987 int res = 0;
1988
1989 if ((!sfp) || (!srp) || (!sfp->headrp))
1990 return res;
1991 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1992 prev_rp = sfp->headrp;
1993 if (srp == prev_rp) {
1994 sfp->headrp = prev_rp->nextrp;
1995 prev_rp->parentfp = NULL;
1996 res = 1;
1997 } else {
1998 while ((rp = prev_rp->nextrp)) {
1999 if (srp == rp) {
2000 prev_rp->nextrp = rp->nextrp;
2001 rp->parentfp = NULL;
2002 res = 1;
2003 break;
2004 }
2005 prev_rp = rp;
2006 }
2007 }
2008 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2009 return res;
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static Sg_fd *
2013sg_add_sfp(Sg_device * sdp, int dev)
2014{
2015 Sg_fd *sfp;
2016 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002017 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Mike Christied6b10342005-11-08 04:06:41 -06002019 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (!sfp)
2021 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 init_waitqueue_head(&sfp->read_wait);
2024 rwlock_init(&sfp->rq_list_lock);
2025
Tony Battersbyc6517b792009-01-21 14:45:50 -05002026 kref_init(&sfp->f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 sfp->timeout = SG_DEFAULT_TIMEOUT;
2028 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2029 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2030 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2031 sdp->device->host->unchecked_isa_dma : 1;
2032 sfp->cmd_q = SG_DEF_COMMAND_Q;
2033 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2034 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002035 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (!sdp->headfp)
2037 sdp->headfp = sfp;
2038 else { /* add to tail of existing list */
2039 Sg_fd *pfp = sdp->headfp;
2040 while (pfp->nextfp)
2041 pfp = pfp->nextfp;
2042 pfp->nextfp = sfp;
2043 }
James Bottomley7c07d612007-08-05 13:36:11 -05002044 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002046 if (unlikely(sg_big_buff != def_reserved_size))
2047 sg_big_buff = def_reserved_size;
2048
Alan Stern44ec9542007-02-20 11:01:57 -05002049 bufflen = min_t(int, sg_big_buff,
2050 sdp->device->request_queue->max_sectors * 512);
2051 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2053 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
Tony Battersbyc6517b792009-01-21 14:45:50 -05002054
2055 kref_get(&sdp->d_ref);
2056 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return sfp;
2058}
2059
Tony Battersbyc6517b792009-01-21 14:45:50 -05002060static void sg_remove_sfp_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002062 struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2063 struct sg_device *sdp = sfp->parentdp;
2064
2065 /* Cleanup any responses which were never read(). */
2066 while (sfp->headrp)
2067 sg_finish_rem_req(sfp->headrp);
2068
2069 if (sfp->reserve.bufflen > 0) {
2070 SCSI_LOG_TIMEOUT(6,
2071 printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2072 (int) sfp->reserve.bufflen,
2073 (int) sfp->reserve.k_use_sg));
2074 sg_remove_scat(&sfp->reserve);
2075 }
2076
2077 SCSI_LOG_TIMEOUT(6,
2078 printk("sg_remove_sfp: %s, sfp=0x%p\n",
2079 sdp->disk->disk_name,
2080 sfp));
2081 kfree(sfp);
2082
2083 scsi_device_put(sdp->device);
2084 sg_put_dev(sdp);
2085 module_put(THIS_MODULE);
2086}
2087
2088static void sg_remove_sfp(struct kref *kref)
2089{
2090 struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
2091 struct sg_device *sdp = sfp->parentdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 Sg_fd *fp;
2093 Sg_fd *prev_fp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002094 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Tony Battersbyc6517b792009-01-21 14:45:50 -05002096 /* CAUTION! Note that sfp can still be found by walking sdp->headfp
2097 * even though the refcount is now 0. Therefore, unlink sfp from
2098 * sdp->headfp BEFORE doing any other cleanup.
2099 */
2100
2101 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 prev_fp = sdp->headfp;
2103 if (sfp == prev_fp)
2104 sdp->headfp = prev_fp->nextfp;
2105 else {
2106 while ((fp = prev_fp->nextfp)) {
2107 if (sfp == fp) {
2108 prev_fp->nextfp = fp->nextfp;
2109 break;
2110 }
2111 prev_fp = fp;
2112 }
2113 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002114 write_unlock_irqrestore(&sg_index_lock, iflags);
2115 wake_up_interruptible(&sdp->o_excl_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Tony Battersbyc6517b792009-01-21 14:45:50 -05002117 execute_in_process_context(sg_remove_sfp_usercontext, &sfp->ew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120static int
2121sg_res_in_use(Sg_fd * sfp)
2122{
2123 const Sg_request *srp;
2124 unsigned long iflags;
2125
2126 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2127 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2128 if (srp->res_used)
2129 break;
2130 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2131 return srp ? 1 : 0;
2132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134#ifdef CONFIG_SCSI_PROC_FS
2135static int
James Bottomley7c07d612007-08-05 13:36:11 -05002136sg_idr_max_id(int id, void *p, void *data)
2137{
2138 int *k = data;
2139
2140 if (*k < id)
2141 *k = id;
2142
2143 return 0;
2144}
2145
2146static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147sg_last_dev(void)
2148{
Tony Battersby53474c02008-01-22 15:25:49 -05002149 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 unsigned long iflags;
2151
James Bottomley7c07d612007-08-05 13:36:11 -05002152 read_lock_irqsave(&sg_index_lock, iflags);
2153 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2154 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return k + 1; /* origin 1 */
2156}
2157#endif
2158
Tony Battersbyc6517b792009-01-21 14:45:50 -05002159/* must be called with sg_index_lock held */
2160static Sg_device *sg_lookup_dev(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002162 return idr_find(&sg_index_idr, dev);
2163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Tony Battersbyc6517b792009-01-21 14:45:50 -05002165static Sg_device *sg_get_dev(int dev)
2166{
2167 struct sg_device *sdp;
2168 unsigned long flags;
2169
2170 read_lock_irqsave(&sg_index_lock, flags);
2171 sdp = sg_lookup_dev(dev);
2172 if (!sdp)
2173 sdp = ERR_PTR(-ENXIO);
2174 else if (sdp->detached) {
2175 /* If sdp->detached, then the refcount may already be 0, in
2176 * which case it would be a bug to do kref_get().
2177 */
2178 sdp = ERR_PTR(-ENODEV);
2179 } else
2180 kref_get(&sdp->d_ref);
2181 read_unlock_irqrestore(&sg_index_lock, flags);
James Bottomley7c07d612007-08-05 13:36:11 -05002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return sdp;
2184}
2185
Tony Battersbyc6517b792009-01-21 14:45:50 -05002186static void sg_put_dev(struct sg_device *sdp)
2187{
2188 kref_put(&sdp->d_ref, sg_device_destroy);
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifdef CONFIG_SCSI_PROC_FS
2192
2193static struct proc_dir_entry *sg_proc_sgp = NULL;
2194
2195static char sg_proc_sg_dirname[] = "scsi/sg";
2196
2197static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2198
2199static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2200static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2201 size_t count, loff_t *off);
2202static struct file_operations adio_fops = {
2203 /* .owner, .read and .llseek added in sg_proc_init() */
2204 .open = sg_proc_single_open_adio,
2205 .write = sg_proc_write_adio,
2206 .release = single_release,
2207};
2208
2209static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2210static ssize_t sg_proc_write_dressz(struct file *filp,
2211 const char __user *buffer, size_t count, loff_t *off);
2212static struct file_operations dressz_fops = {
2213 .open = sg_proc_single_open_dressz,
2214 .write = sg_proc_write_dressz,
2215 .release = single_release,
2216};
2217
2218static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2219static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2220static struct file_operations version_fops = {
2221 .open = sg_proc_single_open_version,
2222 .release = single_release,
2223};
2224
2225static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2226static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2227static struct file_operations devhdr_fops = {
2228 .open = sg_proc_single_open_devhdr,
2229 .release = single_release,
2230};
2231
2232static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2233static int sg_proc_open_dev(struct inode *inode, struct file *file);
2234static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2235static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2236static void dev_seq_stop(struct seq_file *s, void *v);
2237static struct file_operations dev_fops = {
2238 .open = sg_proc_open_dev,
2239 .release = seq_release,
2240};
2241static struct seq_operations dev_seq_ops = {
2242 .start = dev_seq_start,
2243 .next = dev_seq_next,
2244 .stop = dev_seq_stop,
2245 .show = sg_proc_seq_show_dev,
2246};
2247
2248static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2249static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2250static struct file_operations devstrs_fops = {
2251 .open = sg_proc_open_devstrs,
2252 .release = seq_release,
2253};
2254static struct seq_operations devstrs_seq_ops = {
2255 .start = dev_seq_start,
2256 .next = dev_seq_next,
2257 .stop = dev_seq_stop,
2258 .show = sg_proc_seq_show_devstrs,
2259};
2260
2261static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2262static int sg_proc_open_debug(struct inode *inode, struct file *file);
2263static struct file_operations debug_fops = {
2264 .open = sg_proc_open_debug,
2265 .release = seq_release,
2266};
2267static struct seq_operations debug_seq_ops = {
2268 .start = dev_seq_start,
2269 .next = dev_seq_next,
2270 .stop = dev_seq_stop,
2271 .show = sg_proc_seq_show_debug,
2272};
2273
2274
2275struct sg_proc_leaf {
2276 const char * name;
2277 struct file_operations * fops;
2278};
2279
2280static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2281 {"allow_dio", &adio_fops},
2282 {"debug", &debug_fops},
2283 {"def_reserved_size", &dressz_fops},
2284 {"device_hdr", &devhdr_fops},
2285 {"devices", &dev_fops},
2286 {"device_strs", &devstrs_fops},
2287 {"version", &version_fops}
2288};
2289
2290static int
2291sg_proc_init(void)
2292{
2293 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002294 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 struct sg_proc_leaf * leaf;
2296
Al Viro66600222005-09-28 22:32:57 +01002297 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (!sg_proc_sgp)
2299 return 1;
2300 for (k = 0; k < num_leaves; ++k) {
2301 leaf = &sg_proc_leaf_arr[k];
2302 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002303 leaf->fops->owner = THIS_MODULE;
2304 leaf->fops->read = seq_read;
2305 leaf->fops->llseek = seq_lseek;
2306 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308 return 0;
2309}
2310
2311static void
2312sg_proc_cleanup(void)
2313{
2314 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002315 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 if (!sg_proc_sgp)
2318 return;
2319 for (k = 0; k < num_leaves; ++k)
2320 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2321 remove_proc_entry(sg_proc_sg_dirname, NULL);
2322}
2323
2324
2325static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2326{
2327 seq_printf(s, "%d\n", *((int *)s->private));
2328 return 0;
2329}
2330
2331static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2332{
2333 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2334}
2335
2336static ssize_t
2337sg_proc_write_adio(struct file *filp, const char __user *buffer,
2338 size_t count, loff_t *off)
2339{
2340 int num;
2341 char buff[11];
2342
2343 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2344 return -EACCES;
2345 num = (count < 10) ? count : 10;
2346 if (copy_from_user(buff, buffer, num))
2347 return -EFAULT;
2348 buff[num] = '\0';
2349 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2350 return count;
2351}
2352
2353static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2354{
2355 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2356}
2357
2358static ssize_t
2359sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2360 size_t count, loff_t *off)
2361{
2362 int num;
2363 unsigned long k = ULONG_MAX;
2364 char buff[11];
2365
2366 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2367 return -EACCES;
2368 num = (count < 10) ? count : 10;
2369 if (copy_from_user(buff, buffer, num))
2370 return -EFAULT;
2371 buff[num] = '\0';
2372 k = simple_strtoul(buff, NULL, 10);
2373 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2374 sg_big_buff = k;
2375 return count;
2376 }
2377 return -ERANGE;
2378}
2379
2380static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2381{
2382 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2383 sg_version_date);
2384 return 0;
2385}
2386
2387static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2388{
2389 return single_open(file, sg_proc_seq_show_version, NULL);
2390}
2391
2392static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2393{
2394 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2395 "online\n");
2396 return 0;
2397}
2398
2399static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2400{
2401 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2402}
2403
2404struct sg_proc_deviter {
2405 loff_t index;
2406 size_t max;
2407};
2408
2409static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2410{
2411 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2412
Jan Blunck729d70f2005-08-27 11:07:52 -07002413 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (! it)
2415 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 it->index = *pos;
2418 it->max = sg_last_dev();
2419 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002420 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2425{
Jan Blunck729d70f2005-08-27 11:07:52 -07002426 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 *pos = ++it->index;
2429 return (it->index < it->max) ? it : NULL;
2430}
2431
2432static void dev_seq_stop(struct seq_file *s, void *v)
2433{
Jan Blunck729d70f2005-08-27 11:07:52 -07002434 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
2437static int sg_proc_open_dev(struct inode *inode, struct file *file)
2438{
2439 return seq_open(file, &dev_seq_ops);
2440}
2441
2442static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2443{
2444 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2445 Sg_device *sdp;
2446 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002447 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Tony Battersbyc6517b792009-01-21 14:45:50 -05002449 read_lock_irqsave(&sg_index_lock, iflags);
2450 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2452 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2453 scsidp->host->host_no, scsidp->channel,
2454 scsidp->id, scsidp->lun, (int) scsidp->type,
2455 1,
2456 (int) scsidp->queue_depth,
2457 (int) scsidp->device_busy,
2458 (int) scsi_device_online(scsidp));
2459 else
2460 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 -05002461 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
2463}
2464
2465static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2466{
2467 return seq_open(file, &devstrs_seq_ops);
2468}
2469
2470static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2471{
2472 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2473 Sg_device *sdp;
2474 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002475 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Tony Battersbyc6517b792009-01-21 14:45:50 -05002477 read_lock_irqsave(&sg_index_lock, iflags);
2478 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2480 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2481 scsidp->vendor, scsidp->model, scsidp->rev);
2482 else
2483 seq_printf(s, "<no active device>\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002484 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return 0;
2486}
2487
Tony Battersbyc6517b792009-01-21 14:45:50 -05002488/* must be called while holding sg_index_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2490{
2491 int k, m, new_interface, blen, usg;
2492 Sg_request *srp;
2493 Sg_fd *fp;
2494 const sg_io_hdr_t *hp;
2495 const char * cp;
cb59e842005-04-02 13:51:23 -06002496 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Tony Battersbyc6517b792009-01-21 14:45:50 -05002498 for (k = 0, fp = sdp->headfp; fp != NULL; ++k, fp = fp->nextfp) {
2499 read_lock(&fp->rq_list_lock); /* irqs already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2501 "(res)sgat=%d low_dma=%d\n", k + 1,
2502 jiffies_to_msecs(fp->timeout),
2503 fp->reserve.bufflen,
2504 (int) fp->reserve.k_use_sg,
2505 (int) fp->low_dma);
2506 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2507 (int) fp->cmd_q, (int) fp->force_packid,
2508 (int) fp->keep_orphan, (int) fp->closed);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002509 for (m = 0, srp = fp->headrp;
2510 srp != NULL;
2511 ++m, srp = srp->nextrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 hp = &srp->header;
2513 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2514 if (srp->res_used) {
2515 if (new_interface &&
2516 (SG_FLAG_MMAP_IO & hp->flags))
2517 cp = " mmap>> ";
2518 else
2519 cp = " rb>> ";
2520 } else {
2521 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2522 cp = " dio>> ";
2523 else
2524 cp = " ";
2525 }
2526 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002527 blen = srp->data.bufflen;
2528 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 seq_printf(s, srp->done ?
2530 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002531 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 seq_printf(s, " id=%d blen=%d",
2533 srp->header.pack_id, blen);
2534 if (srp->done)
2535 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002536 else {
2537 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002539 (new_interface ? hp->timeout :
2540 jiffies_to_msecs(fp->timeout)),
2541 (ms > hp->duration ? ms - hp->duration : 0));
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2544 (int) srp->data.cmd_opcode);
2545 }
2546 if (0 == m)
2547 seq_printf(s, " No requests active\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002548 read_unlock(&fp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550}
2551
2552static int sg_proc_open_debug(struct inode *inode, struct file *file)
2553{
2554 return seq_open(file, &debug_seq_ops);
2555}
2556
2557static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2558{
2559 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2560 Sg_device *sdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002561 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002564 seq_printf(s, "max_active_device=%d(origin 1)\n",
2565 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2567 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002568
2569 read_lock_irqsave(&sg_index_lock, iflags);
2570 sdp = it ? sg_lookup_dev(it->index) : NULL;
2571 if (sdp && sdp->headfp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct scsi_device *scsidp = sdp->device;
2573
Tony Battersbyc6517b792009-01-21 14:45:50 -05002574 seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2575 if (sdp->detached)
2576 seq_printf(s, "detached pending close ");
2577 else
2578 seq_printf
2579 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2580 scsidp->host->host_no,
2581 scsidp->channel, scsidp->id,
2582 scsidp->lun,
2583 scsidp->host->hostt->emulated);
2584 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2585 sdp->sg_tablesize, sdp->exclude);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 sg_proc_debug_helper(s, sdp);
2587 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002588 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return 0;
2590}
2591
2592#endif /* CONFIG_SCSI_PROC_FS */
2593
2594module_init(init_sg);
2595module_exit(exit_sg);