blob: df8bf67b171ad1569461a44b05ab7843bb868636 [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 *);
104static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
James Bottomley7c07d612007-08-05 13:36:11 -0500106static DEFINE_IDR(sg_index_idr);
107static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 file descriptor list for device */
109
110static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100111 .add_dev = sg_add,
112 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
116 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900117 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200119 struct page **pages;
120 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
122 unsigned char cmd_opcode; /* first byte of command */
123} Sg_scatter_hold;
124
125struct sg_device; /* forward declarations */
126struct sg_fd;
127
128typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct sg_request *nextrp; /* NULL -> tail request (slist) */
130 struct sg_fd *parentfp; /* NULL -> not in use */
131 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
132 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600133 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
135 char orphan; /* 1 -> drop on sight, 0 -> normal */
136 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
137 volatile char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900138 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900139 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140} Sg_request;
141
142typedef struct sg_fd { /* holds the state of a file descriptor */
143 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
144 struct sg_device *parentdp; /* owning device */
145 wait_queue_head_t read_wait; /* queue read until command done */
146 rwlock_t rq_list_lock; /* protect access to list in req_arr */
147 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
148 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
149 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
150 unsigned save_scat_len; /* original length of trunc. scat. element */
151 Sg_request *headrp; /* head of request slist, NULL->empty */
152 struct fasync_struct *async_qp; /* used by asynchronous notification */
153 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
154 char low_dma; /* as in parent but possibly overridden to 1 */
155 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
156 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
157 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
160 char mmap_called; /* 0 -> mmap() never called on this fd */
161} Sg_fd;
162
163typedef struct sg_device { /* holds the state of each scsi generic device */
164 struct scsi_device *device;
165 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
166 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500167 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 Sg_fd *headfp; /* first open fd belonging to this device */
169 volatile char detached; /* 0->attached, 1->detached pending removal */
170 volatile char exclude; /* opened for exclusive access */
171 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
172 struct gendisk *disk;
173 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
174} Sg_device;
175
176static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600177/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900178static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900179static int sg_start_req(Sg_request *srp, unsigned char *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void sg_finish_rem_req(Sg_request * srp);
181static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
183 int tablesize);
184static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
185 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200186static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
187 const char __user *buf, size_t count, int blocking,
188 int read_only, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
192static void sg_remove_scat(Sg_scatter_hold * schp);
193static void sg_build_reserve(Sg_fd * sfp, int req_size);
194static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
195static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
197static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
198static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
199static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
200static Sg_request *sg_add_request(Sg_fd * sfp);
201static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
202static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#ifdef CONFIG_SCSI_PROC_FS
205static int sg_last_dev(void);
206#endif
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define SZ_SG_HEADER sizeof(struct sg_header)
209#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
210#define SZ_SG_IOVEC sizeof(sg_iovec_t)
211#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
212
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900213static int sg_allow_access(struct file *filp, unsigned char *cmd)
214{
215 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
216 struct request_queue *q = sfp->parentdp->device->request_queue;
217
218 if (sfp->parentdp->device->type == TYPE_SCANNER)
219 return 0;
220
221 return blk_verify_command(&q->cmd_filter,
222 cmd, filp->f_mode & FMODE_WRITE);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int
226sg_open(struct inode *inode, struct file *filp)
227{
228 int dev = iminor(inode);
229 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600230 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 Sg_device *sdp;
232 Sg_fd *sfp;
233 int res;
234 int retval;
235
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600236 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 nonseekable_open(inode, filp);
238 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
239 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600240 if ((!sdp) || (!sdp->device)) {
241 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600243 }
244 if (sdp->detached) {
245 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* This driver's module count bumped by fops_get in <linux/fs.h> */
250 /* Prevent the device driver from vanishing while we sleep */
251 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600252 if (retval) {
253 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (!((flags & O_NONBLOCK) ||
258 scsi_block_when_processing_errors(sdp->device))) {
259 retval = -ENXIO;
260 /* we are in error recovery for this device */
261 goto error_out;
262 }
263
264 if (flags & O_EXCL) {
265 if (O_RDONLY == (flags & O_ACCMODE)) {
266 retval = -EPERM; /* Can't lock it with read only access */
267 goto error_out;
268 }
269 if (sdp->headfp && (flags & O_NONBLOCK)) {
270 retval = -EBUSY;
271 goto error_out;
272 }
273 res = 0;
274 __wait_event_interruptible(sdp->o_excl_wait,
275 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
276 if (res) {
277 retval = res; /* -ERESTARTSYS because signal hit process */
278 goto error_out;
279 }
280 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
281 if (flags & O_NONBLOCK) {
282 retval = -EBUSY;
283 goto error_out;
284 }
285 res = 0;
286 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
287 res);
288 if (res) {
289 retval = res; /* -ERESTARTSYS because signal hit process */
290 goto error_out;
291 }
292 }
293 if (sdp->detached) {
294 retval = -ENODEV;
295 goto error_out;
296 }
297 if (!sdp->headfp) { /* no existing opens on this device */
298 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600299 q = sdp->device->request_queue;
300 sdp->sg_tablesize = min(q->max_hw_segments,
301 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 if ((sfp = sg_add_sfp(sdp, dev)))
304 filp->private_data = sfp;
305 else {
306 if (flags & O_EXCL)
307 sdp->exclude = 0; /* undo if error */
308 retval = -ENOMEM;
309 goto error_out;
310 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600311 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313
314 error_out:
315 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600316 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return retval;
318}
319
320/* Following function was formerly called 'sg_close' */
321static int
322sg_release(struct inode *inode, struct file *filp)
323{
324 Sg_device *sdp;
325 Sg_fd *sfp;
326
327 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
328 return -ENXIO;
329 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
330 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
331 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
332 if (!sdp->detached) {
333 scsi_device_put(sdp->device);
334 }
335 sdp->exclude = 0;
336 wake_up_interruptible(&sdp->o_excl_wait);
337 }
338 return 0;
339}
340
341static ssize_t
342sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 Sg_device *sdp;
345 Sg_fd *sfp;
346 Sg_request *srp;
347 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600349 struct sg_header *old_hdr = NULL;
350 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
353 return -ENXIO;
354 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
355 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!access_ok(VERIFY_WRITE, buf, count))
358 return -EFAULT;
359 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600360 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
361 if (!old_hdr)
362 return -ENOMEM;
363 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
364 retval = -EFAULT;
365 goto free_old_hdr;
366 }
367 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600369 sg_io_hdr_t *new_hdr;
370 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
371 if (!new_hdr) {
372 retval = -ENOMEM;
373 goto free_old_hdr;
374 }
375 retval =__copy_from_user
376 (new_hdr, buf, SZ_SG_IO_HDR);
377 req_pack_id = new_hdr->pack_id;
378 kfree(new_hdr);
379 if (retval) {
380 retval = -EFAULT;
381 goto free_old_hdr;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 } else
cb59e842005-04-02 13:51:23 -0600385 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 srp = sg_get_rq_mark(sfp, req_pack_id);
388 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600389 if (sdp->detached) {
390 retval = -ENODEV;
391 goto free_old_hdr;
392 }
393 if (filp->f_flags & O_NONBLOCK) {
394 retval = -EAGAIN;
395 goto free_old_hdr;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 while (1) {
cb59e842005-04-02 13:51:23 -0600398 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600400 (sdp->detached ||
401 (srp = sg_get_rq_mark(sfp, req_pack_id))),
402 retval);
403 if (sdp->detached) {
404 retval = -ENODEV;
405 goto free_old_hdr;
406 }
407 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
cb59e842005-04-02 13:51:23 -0600409
410 /* -ERESTARTSYS as signal hit process */
411 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
cb59e842005-04-02 13:51:23 -0600414 if (srp->header.interface_id != '\0') {
415 retval = sg_new_read(sfp, buf, count, srp);
416 goto free_old_hdr;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600420 if (old_hdr == NULL) {
421 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
422 if (! old_hdr) {
423 retval = -ENOMEM;
424 goto free_old_hdr;
425 }
426 }
427 memset(old_hdr, 0, SZ_SG_HEADER);
428 old_hdr->reply_len = (int) hp->timeout;
429 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
430 old_hdr->pack_id = hp->pack_id;
431 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600433 old_hdr->target_status = hp->masked_status;
434 old_hdr->host_status = hp->host_status;
435 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if ((CHECK_CONDITION & hp->masked_status) ||
437 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600438 memcpy(old_hdr->sense_buffer, srp->sense_b,
439 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 switch (hp->host_status) {
441 /* This setup of 'result' is for backward compatibility and is best
442 ignored by the user who should use target, host + driver status */
443 case DID_OK:
444 case DID_PASSTHROUGH:
445 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case DID_NO_CONNECT:
449 case DID_BUS_BUSY:
450 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600451 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case DID_BAD_TARGET:
454 case DID_ABORT:
455 case DID_PARITY:
456 case DID_RESET:
457 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600458 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600461 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 hp->masked_status == GOOD) ? 0 : EIO;
463 break;
464 default:
cb59e842005-04-02 13:51:23 -0600465 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 }
468
469 /* Now copy the result back to the user buffer. */
470 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600471 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
472 retval = -EFAULT;
473 goto free_old_hdr;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600476 if (count > old_hdr->reply_len)
477 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600479 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
480 retval = -EFAULT;
481 goto free_old_hdr;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 } else
cb59e842005-04-02 13:51:23 -0600485 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600487 retval = count;
488free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800489 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600490 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static ssize_t
494sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
495{
496 sg_io_hdr_t *hp = &srp->header;
497 int err = 0;
498 int len;
499
500 if (count < SZ_SG_IO_HDR) {
501 err = -EINVAL;
502 goto err_out;
503 }
504 hp->sb_len_wr = 0;
505 if ((hp->mx_sb_len > 0) && hp->sbp) {
506 if ((CHECK_CONDITION & hp->masked_status) ||
507 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600508 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
510 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
511 len = (len > sb_len) ? sb_len : len;
512 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
513 err = -EFAULT;
514 goto err_out;
515 }
516 hp->sb_len_wr = len;
517 }
518 }
519 if (hp->masked_status || hp->host_status || hp->driver_status)
520 hp->info |= SG_INFO_CHECK;
521 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
522 err = -EFAULT;
523 goto err_out;
524 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900525 if (srp->bio) {
526 err = blk_rq_unmap_user(srp->bio);
527 srp->bio = NULL;
528 }
529err_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)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200565 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (count < (SZ_SG_HEADER + 6))
567 return -EIO; /* The minimum scsi command length is 6 bytes. */
568
569 if (!(srp = sg_add_request(sfp))) {
570 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
571 return -EDOM;
572 }
573 buf += SZ_SG_HEADER;
574 __get_user(opcode, buf);
575 if (sfp->next_cmd_len > 0) {
576 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
577 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
578 sfp->next_cmd_len = 0;
579 sg_remove_request(sfp, srp);
580 return -EIO;
581 }
582 cmd_size = sfp->next_cmd_len;
583 sfp->next_cmd_len = 0; /* reset so only this write() effected */
584 } else {
585 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
586 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
587 cmd_size = 12;
588 }
589 SCSI_LOG_TIMEOUT(4, printk(
590 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
591/* Determine buffer size. */
592 input_size = count - cmd_size;
593 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
594 mxsize -= SZ_SG_HEADER;
595 input_size -= SZ_SG_HEADER;
596 if (input_size < 0) {
597 sg_remove_request(sfp, srp);
598 return -EIO; /* User did not pass enough bytes for this command. */
599 }
600 hp = &srp->header;
601 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
602 hp->cmd_len = (unsigned char) cmd_size;
603 hp->iovec_count = 0;
604 hp->mx_sb_len = 0;
605 if (input_size > 0)
606 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
607 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
608 else
609 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
610 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900611 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
612 hp->dxferp = (char __user *)buf + cmd_size;
613 else
614 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 hp->sbp = NULL;
616 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
617 hp->flags = input_size; /* structure abuse ... */
618 hp->pack_id = old_hdr.pack_id;
619 hp->usr_ptr = NULL;
620 if (__copy_from_user(cmnd, buf, cmd_size))
621 return -EFAULT;
622 /*
623 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
624 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
625 * is a non-zero input_size, so emit a warning.
626 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100627 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
628 static char cmd[TASK_COMM_LEN];
629 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 printk(KERN_WARNING
631 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
632 "guessing data in;\n" KERN_WARNING " "
633 "program %s not setting count and/or reply_len properly\n",
634 old_hdr.reply_len - (int)SZ_SG_HEADER,
635 input_size, (unsigned int) cmnd[0],
636 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100637 strcpy(cmd, current->comm);
638 }
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
641 return (k < 0) ? k : count;
642}
643
644static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200645sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
646 size_t count, int blocking, int read_only,
647 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int k;
650 Sg_request *srp;
651 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600652 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int timeout;
654 unsigned long ul_timeout;
655
656 if (count < SZ_SG_IO_HDR)
657 return -EINVAL;
658 if (!access_ok(VERIFY_READ, buf, count))
659 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
660
661 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
662 if (!(srp = sg_add_request(sfp))) {
663 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
664 return -EDOM;
665 }
666 hp = &srp->header;
667 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
668 sg_remove_request(sfp, srp);
669 return -EFAULT;
670 }
671 if (hp->interface_id != 'S') {
672 sg_remove_request(sfp, srp);
673 return -ENOSYS;
674 }
675 if (hp->flags & SG_FLAG_MMAP_IO) {
676 if (hp->dxfer_len > sfp->reserve.bufflen) {
677 sg_remove_request(sfp, srp);
678 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
679 }
680 if (hp->flags & SG_FLAG_DIRECT_IO) {
681 sg_remove_request(sfp, srp);
682 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
683 }
684 if (sg_res_in_use(sfp)) {
685 sg_remove_request(sfp, srp);
686 return -EBUSY; /* reserve buffer already being used */
687 }
688 }
689 ul_timeout = msecs_to_jiffies(srp->header.timeout);
690 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
691 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
692 sg_remove_request(sfp, srp);
693 return -EMSGSIZE;
694 }
695 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
696 sg_remove_request(sfp, srp);
697 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
698 }
699 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
700 sg_remove_request(sfp, srp);
701 return -EFAULT;
702 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900703 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 sg_remove_request(sfp, srp);
705 return -EPERM;
706 }
707 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
708 if (k < 0)
709 return k;
710 if (o_srp)
711 *o_srp = srp;
712 return count;
713}
714
715static int
716sg_common_write(Sg_fd * sfp, Sg_request * srp,
717 unsigned char *cmnd, int timeout, int blocking)
718{
Mike Christied6b10342005-11-08 04:06:41 -0600719 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 Sg_device *sdp = sfp->parentdp;
721 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
724 hp->status = 0;
725 hp->masked_status = 0;
726 hp->msg_status = 0;
727 hp->info = 0;
728 hp->host_status = 0;
729 hp->driver_status = 0;
730 hp->resid = 0;
731 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
732 (int) cmnd[0], (int) hp->cmd_len));
733
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900734 k = sg_start_req(srp, cmnd);
735 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400736 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 sg_finish_rem_req(srp);
738 return k; /* probably out of space --> ENOMEM */
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (sdp->detached) {
741 sg_finish_rem_req(srp);
742 return -ENODEV;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 switch (hp->dxfer_direction) {
746 case SG_DXFER_TO_FROM_DEV:
747 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600748 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600751 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600754 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 default:
Mike Christied6b10342005-11-08 04:06:41 -0600757 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 break;
759 }
cb59e842005-04-02 13:51:23 -0600760 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200761
762 srp->rq->timeout = timeout;
763 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
764 srp->rq, 1, sg_rq_end_io);
765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static int
769sg_srp_done(Sg_request *srp, Sg_fd *sfp)
770{
771 unsigned long iflags;
772 int done;
773
774 read_lock_irqsave(&sfp->rq_list_lock, iflags);
775 done = srp->done;
776 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
777 return done;
778}
779
780static int
781sg_ioctl(struct inode *inode, struct file *filp,
782 unsigned int cmd_in, unsigned long arg)
783{
784 void __user *p = (void __user *)arg;
785 int __user *ip = p;
786 int result, val, read_only;
787 Sg_device *sdp;
788 Sg_fd *sfp;
789 Sg_request *srp;
790 unsigned long iflags;
791
792 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
793 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
796 sdp->disk->disk_name, (int) cmd_in));
797 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
798
799 switch (cmd_in) {
800 case SG_IO:
801 {
802 int blocking = 1; /* ignore O_NONBLOCK flag */
803
804 if (sdp->detached)
805 return -ENODEV;
806 if (!scsi_block_when_processing_errors(sdp->device))
807 return -ENXIO;
808 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
809 return -EFAULT;
810 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200811 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 blocking, read_only, &srp);
813 if (result < 0)
814 return result;
815 srp->sg_io_owned = 1;
816 while (1) {
817 result = 0; /* following macro to beat race condition */
818 __wait_event_interruptible(sfp->read_wait,
819 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
820 result);
821 if (sdp->detached)
822 return -ENODEV;
823 if (sfp->closed)
824 return 0; /* request packet dropped already */
825 if (0 == result)
826 break;
827 srp->orphan = 1;
828 return result; /* -ERESTARTSYS because signal hit process */
829 }
830 write_lock_irqsave(&sfp->rq_list_lock, iflags);
831 srp->done = 2;
832 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
833 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
834 return (result < 0) ? result : 0;
835 }
836 case SG_SET_TIMEOUT:
837 result = get_user(val, ip);
838 if (result)
839 return result;
840 if (val < 0)
841 return -EIO;
842 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
843 val = MULDIV (INT_MAX, USER_HZ, HZ);
844 sfp->timeout_user = val;
845 sfp->timeout = MULDIV (val, HZ, USER_HZ);
846
847 return 0;
848 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
849 /* strange ..., for backward compatibility */
850 return sfp->timeout_user;
851 case SG_SET_FORCE_LOW_DMA:
852 result = get_user(val, ip);
853 if (result)
854 return result;
855 if (val) {
856 sfp->low_dma = 1;
857 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
858 val = (int) sfp->reserve.bufflen;
859 sg_remove_scat(&sfp->reserve);
860 sg_build_reserve(sfp, val);
861 }
862 } else {
863 if (sdp->detached)
864 return -ENODEV;
865 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
866 }
867 return 0;
868 case SG_GET_LOW_DMA:
869 return put_user((int) sfp->low_dma, ip);
870 case SG_GET_SCSI_ID:
871 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
872 return -EFAULT;
873 else {
874 sg_scsi_id_t __user *sg_idp = p;
875
876 if (sdp->detached)
877 return -ENODEV;
878 __put_user((int) sdp->device->host->host_no,
879 &sg_idp->host_no);
880 __put_user((int) sdp->device->channel,
881 &sg_idp->channel);
882 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
883 __put_user((int) sdp->device->lun, &sg_idp->lun);
884 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
885 __put_user((short) sdp->device->host->cmd_per_lun,
886 &sg_idp->h_cmd_per_lun);
887 __put_user((short) sdp->device->queue_depth,
888 &sg_idp->d_queue_depth);
889 __put_user(0, &sg_idp->unused[0]);
890 __put_user(0, &sg_idp->unused[1]);
891 return 0;
892 }
893 case SG_SET_FORCE_PACK_ID:
894 result = get_user(val, ip);
895 if (result)
896 return result;
897 sfp->force_packid = val ? 1 : 0;
898 return 0;
899 case SG_GET_PACK_ID:
900 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
901 return -EFAULT;
902 read_lock_irqsave(&sfp->rq_list_lock, iflags);
903 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
904 if ((1 == srp->done) && (!srp->sg_io_owned)) {
905 read_unlock_irqrestore(&sfp->rq_list_lock,
906 iflags);
907 __put_user(srp->header.pack_id, ip);
908 return 0;
909 }
910 }
911 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
912 __put_user(-1, ip);
913 return 0;
914 case SG_GET_NUM_WAITING:
915 read_lock_irqsave(&sfp->rq_list_lock, iflags);
916 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
917 if ((1 == srp->done) && (!srp->sg_io_owned))
918 ++val;
919 }
920 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
921 return put_user(val, ip);
922 case SG_GET_SG_TABLESIZE:
923 return put_user(sdp->sg_tablesize, ip);
924 case SG_SET_RESERVED_SIZE:
925 result = get_user(val, ip);
926 if (result)
927 return result;
928 if (val < 0)
929 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500930 val = min_t(int, val,
931 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (val != sfp->reserve.bufflen) {
933 if (sg_res_in_use(sfp) || sfp->mmap_called)
934 return -EBUSY;
935 sg_remove_scat(&sfp->reserve);
936 sg_build_reserve(sfp, val);
937 }
938 return 0;
939 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500940 val = min_t(int, sfp->reserve.bufflen,
941 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return put_user(val, ip);
943 case SG_SET_COMMAND_Q:
944 result = get_user(val, ip);
945 if (result)
946 return result;
947 sfp->cmd_q = val ? 1 : 0;
948 return 0;
949 case SG_GET_COMMAND_Q:
950 return put_user((int) sfp->cmd_q, ip);
951 case SG_SET_KEEP_ORPHAN:
952 result = get_user(val, ip);
953 if (result)
954 return result;
955 sfp->keep_orphan = val;
956 return 0;
957 case SG_GET_KEEP_ORPHAN:
958 return put_user((int) sfp->keep_orphan, ip);
959 case SG_NEXT_CMD_LEN:
960 result = get_user(val, ip);
961 if (result)
962 return result;
963 sfp->next_cmd_len = (val > 0) ? val : 0;
964 return 0;
965 case SG_GET_VERSION_NUM:
966 return put_user(sg_version_num, ip);
967 case SG_GET_ACCESS_COUNT:
968 /* faked - we don't have a real access count anymore */
969 val = (sdp->device ? 1 : 0);
970 return put_user(val, ip);
971 case SG_GET_REQUEST_TABLE:
972 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
973 return -EFAULT;
974 else {
cb59e842005-04-02 13:51:23 -0600975 sg_req_info_t *rinfo;
976 unsigned int ms;
977
978 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
979 GFP_KERNEL);
980 if (!rinfo)
981 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 read_lock_irqsave(&sfp->rq_list_lock, iflags);
983 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
984 ++val, srp = srp ? srp->nextrp : srp) {
985 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
986 if (srp) {
987 rinfo[val].req_state = srp->done + 1;
988 rinfo[val].problem =
989 srp->header.masked_status &
990 srp->header.host_status &
991 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600992 if (srp->done)
993 rinfo[val].duration =
994 srp->header.duration;
995 else {
996 ms = jiffies_to_msecs(jiffies);
997 rinfo[val].duration =
998 (ms > srp->header.duration) ?
999 (ms - srp->header.duration) : 0;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001002 rinfo[val].sg_io_owned =
1003 srp->sg_io_owned;
1004 rinfo[val].pack_id =
1005 srp->header.pack_id;
1006 rinfo[val].usr_ptr =
1007 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 }
1010 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001011 result = __copy_to_user(p, rinfo,
1012 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1013 result = result ? -EFAULT : 0;
1014 kfree(rinfo);
1015 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 case SG_EMULATED_HOST:
1018 if (sdp->detached)
1019 return -ENODEV;
1020 return put_user(sdp->device->host->hostt->emulated, ip);
1021 case SG_SCSI_RESET:
1022 if (sdp->detached)
1023 return -ENODEV;
1024 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001025 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EBUSY;
1027 } else if (!scsi_block_when_processing_errors(sdp->device))
1028 return -EBUSY;
1029 result = get_user(val, ip);
1030 if (result)
1031 return result;
1032 if (SG_SCSI_RESET_NOTHING == val)
1033 return 0;
1034 switch (val) {
1035 case SG_SCSI_RESET_DEVICE:
1036 val = SCSI_TRY_RESET_DEVICE;
1037 break;
Brian King39120e12008-07-01 13:03:19 -05001038 case SG_SCSI_RESET_TARGET:
1039 val = SCSI_TRY_RESET_TARGET;
1040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 case SG_SCSI_RESET_BUS:
1042 val = SCSI_TRY_RESET_BUS;
1043 break;
1044 case SG_SCSI_RESET_HOST:
1045 val = SCSI_TRY_RESET_HOST;
1046 break;
1047 default:
1048 return -EINVAL;
1049 }
1050 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1051 return -EACCES;
1052 return (scsi_reset_provider(sdp->device, val) ==
1053 SUCCESS) ? 0 : -EIO;
1054 case SCSI_IOCTL_SEND_COMMAND:
1055 if (sdp->detached)
1056 return -ENODEV;
1057 if (read_only) {
1058 unsigned char opcode = WRITE_6;
1059 Scsi_Ioctl_Command __user *siocp = p;
1060
1061 if (copy_from_user(&opcode, siocp->data, 1))
1062 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001063 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -EPERM;
1065 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001066 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 case SG_SET_DEBUG:
1068 result = get_user(val, ip);
1069 if (result)
1070 return result;
1071 sdp->sgdebug = (char) val;
1072 return 0;
1073 case SCSI_IOCTL_GET_IDLUN:
1074 case SCSI_IOCTL_GET_BUS_NUMBER:
1075 case SCSI_IOCTL_PROBE_HOST:
1076 case SG_GET_TRANSFORM:
1077 if (sdp->detached)
1078 return -ENODEV;
1079 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001080 case BLKSECTGET:
1081 return put_user(sdp->device->request_queue->max_sectors * 512,
1082 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001083 case BLKTRACESETUP:
1084 return blk_trace_setup(sdp->device->request_queue,
1085 sdp->disk->disk_name,
1086 sdp->device->sdev_gendev.devt,
1087 (char *)arg);
1088 case BLKTRACESTART:
1089 return blk_trace_startstop(sdp->device->request_queue, 1);
1090 case BLKTRACESTOP:
1091 return blk_trace_startstop(sdp->device->request_queue, 0);
1092 case BLKTRACETEARDOWN:
1093 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 default:
1095 if (read_only)
1096 return -EPERM; /* don't know so take safe approach */
1097 return scsi_ioctl(sdp->device, cmd_in, p);
1098 }
1099}
1100
1101#ifdef CONFIG_COMPAT
1102static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1103{
1104 Sg_device *sdp;
1105 Sg_fd *sfp;
1106 struct scsi_device *sdev;
1107
1108 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1109 return -ENXIO;
1110
1111 sdev = sdp->device;
1112 if (sdev->host->hostt->compat_ioctl) {
1113 int ret;
1114
1115 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1116
1117 return ret;
1118 }
1119
1120 return -ENOIOCTLCMD;
1121}
1122#endif
1123
1124static unsigned int
1125sg_poll(struct file *filp, poll_table * wait)
1126{
1127 unsigned int res = 0;
1128 Sg_device *sdp;
1129 Sg_fd *sfp;
1130 Sg_request *srp;
1131 int count = 0;
1132 unsigned long iflags;
1133
1134 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1135 || sfp->closed)
1136 return POLLERR;
1137 poll_wait(filp, &sfp->read_wait, wait);
1138 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1139 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1140 /* if any read waiting, flag it */
1141 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1142 res = POLLIN | POLLRDNORM;
1143 ++count;
1144 }
1145 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1146
1147 if (sdp->detached)
1148 res |= POLLHUP;
1149 else if (!sfp->cmd_q) {
1150 if (0 == count)
1151 res |= POLLOUT | POLLWRNORM;
1152 } else if (count < SG_MAX_QUEUE)
1153 res |= POLLOUT | POLLWRNORM;
1154 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1155 sdp->disk->disk_name, (int) res));
1156 return res;
1157}
1158
1159static int
1160sg_fasync(int fd, struct file *filp, int mode)
1161{
1162 int retval;
1163 Sg_device *sdp;
1164 Sg_fd *sfp;
1165
1166 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1167 return -ENXIO;
1168 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1169 sdp->disk->disk_name, mode));
1170
1171 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1172 return (retval < 0) ? retval : 0;
1173}
1174
Nick Piggina13ff0b2008-02-07 18:46:06 -08001175static int
1176sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001179 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001181 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001184 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001186 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001188 return VM_FAULT_SIGBUS;
1189 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001191 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001192 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1193 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001194 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001195 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001196 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001197 struct page *page = nth_page(rsv_schp->pages[k],
1198 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001199 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001200 vmf->page = page;
1201 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Mike Christied6b10342005-11-08 04:06:41 -06001203 sa += len;
1204 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Mike Christied6b10342005-11-08 04:06:41 -06001206
Nick Piggina13ff0b2008-02-07 18:46:06 -08001207 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001211 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212};
1213
1214static int
1215sg_mmap(struct file *filp, struct vm_area_struct *vma)
1216{
1217 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001218 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001220 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1223 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001224 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1226 (void *) vma->vm_start, (int) req_sz));
1227 if (vma->vm_pgoff)
1228 return -EINVAL; /* want no offset */
1229 rsv_schp = &sfp->reserve;
1230 if (req_sz > rsv_schp->bufflen)
1231 return -ENOMEM; /* cannot map more than reserved buffer */
1232
Mike Christied6b10342005-11-08 04:06:41 -06001233 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001234 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1235 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001236 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001237 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001238 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Mike Christied6b10342005-11-08 04:06:41 -06001240
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001241 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001242 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 vma->vm_private_data = sfp;
1244 vma->vm_ops = &sg_mmap_vm_ops;
1245 return 0;
1246}
1247
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001248/*
1249 * This function is a "bottom half" handler that is called by the mid
1250 * level when a command is completed (or has failed).
1251 */
1252static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001254 struct sg_request *srp = rq->end_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 Sg_device *sdp = NULL;
1256 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001258 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001259 char *sense;
1260 int result, resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (NULL == srp) {
1263 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return;
1265 }
1266 sfp = srp->parentfp;
1267 if (sfp)
1268 sdp = sfp->parentdp;
1269 if ((NULL == sdp) || sdp->detached) {
1270 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return;
1272 }
1273
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001274 sense = rq->sense;
1275 result = rq->errors;
1276 resid = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001279 sdp->disk->disk_name, srp->header.pack_id, result));
1280 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001281 ms = jiffies_to_msecs(jiffies);
1282 srp->header.duration = (ms > srp->header.duration) ?
1283 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001284 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct scsi_sense_hdr sshdr;
1286
Mike Christied6b10342005-11-08 04:06:41 -06001287 srp->header.status = 0xff & result;
1288 srp->header.masked_status = status_byte(result);
1289 srp->header.msg_status = msg_byte(result);
1290 srp->header.host_status = host_byte(result);
1291 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if ((sdp->sgdebug > 0) &&
1293 ((CHECK_CONDITION == srp->header.masked_status) ||
1294 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001295 __scsi_print_sense("sg_cmd_done", sense,
1296 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001299 if (driver_byte(result) != 0
1300 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 && !scsi_sense_is_deferred(&sshdr)
1302 && sshdr.sense_key == UNIT_ATTENTION
1303 && sdp->device->removable) {
1304 /* Detected possible disc change. Set the bit - this */
1305 /* may be used if there are filesystems using this device */
1306 sdp->device->changed = 1;
1307 }
1308 }
1309 /* Rely on write phase to clean out srp status values, so no "else" */
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (sfp->closed) { /* whoops this fd already released, cleanup */
1312 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1313 sg_finish_rem_req(srp);
1314 srp = NULL;
1315 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001316 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1318 scsi_device_put(sdp->device);
1319 }
1320 sfp = NULL;
1321 }
1322 } else if (srp && srp->orphan) {
1323 if (sfp->keep_orphan)
1324 srp->sg_io_owned = 0;
1325 else {
1326 sg_finish_rem_req(srp);
1327 srp = NULL;
1328 }
1329 }
1330 if (sfp && srp) {
1331 /* Now wake up any sg_read() that is waiting for this packet. */
1332 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1333 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1334 srp->done = 1;
1335 wake_up_interruptible(&sfp->read_wait);
1336 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1337 }
1338}
1339
1340static struct file_operations sg_fops = {
1341 .owner = THIS_MODULE,
1342 .read = sg_read,
1343 .write = sg_write,
1344 .poll = sg_poll,
1345 .ioctl = sg_ioctl,
1346#ifdef CONFIG_COMPAT
1347 .compat_ioctl = sg_compat_ioctl,
1348#endif
1349 .open = sg_open,
1350 .mmap = sg_mmap,
1351 .release = sg_release,
1352 .fasync = sg_fasync,
1353};
1354
gregkh@suse.ded2538782005-03-23 09:55:22 -08001355static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357static int sg_sysfs_valid = 0;
1358
James Bottomley7c07d612007-08-05 13:36:11 -05001359static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Mike Christied6b10342005-11-08 04:06:41 -06001361 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 Sg_device *sdp;
1363 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001364 int error;
1365 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jes Sorensen24669f752006-01-16 10:31:18 -05001367 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (!sdp) {
1369 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001370 return ERR_PTR(-ENOMEM);
1371 }
1372 error = -ENOMEM;
1373 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1374 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
James Bottomley7c07d612007-08-05 13:36:11 -05001378 write_lock_irqsave(&sg_index_lock, iflags);
1379 error = idr_get_new(&sg_index_idr, sdp, &k);
1380 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
James Bottomley7c07d612007-08-05 13:36:11 -05001382 if (error) {
1383 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1384 error);
1385 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (unlikely(k >= SG_MAX_DEVS))
1389 goto overflow;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1392 sprintf(disk->disk_name, "sg%d", k);
1393 disk->first_minor = k;
1394 sdp->disk = disk;
1395 sdp->device = scsidp;
1396 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001397 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001398 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
James Bottomley7c07d612007-08-05 13:36:11 -05001400 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001402 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001404 return ERR_PTR(error);
1405 }
1406 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001409 sdev_printk(KERN_WARNING, scsidp,
1410 "Unable to attach sg device type=%d, minor "
1411 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 error = -ENODEV;
1413 goto out;
1414}
1415
1416static int
Tony Jonesee959b02008-02-22 00:13:36 +01001417sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Tony Jonesee959b02008-02-22 00:13:36 +01001419 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct gendisk *disk;
1421 Sg_device *sdp = NULL;
1422 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001423 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001424 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 disk = alloc_disk(1);
1427 if (!disk) {
1428 printk(KERN_WARNING "alloc_disk failed\n");
1429 return -ENOMEM;
1430 }
1431 disk->major = SCSI_GENERIC_MAJOR;
1432
1433 error = -ENOMEM;
1434 cdev = cdev_alloc();
1435 if (!cdev) {
1436 printk(KERN_WARNING "cdev_alloc failed\n");
1437 goto out;
1438 }
1439 cdev->owner = THIS_MODULE;
1440 cdev->ops = &sg_fops;
1441
James Bottomley7c07d612007-08-05 13:36:11 -05001442 sdp = sg_alloc(disk, scsidp);
1443 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001445 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
James Bottomley7c07d612007-08-05 13:36:11 -05001449 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001450 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001451 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 sdp->cdev = cdev;
1454 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001455 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001457 sg_class_member = device_create_drvdata(sg_sysfs_class,
1458 cl_dev->parent,
1459 MKDEV(SCSI_GENERIC_MAJOR,
1460 sdp->index),
1461 sdp,
1462 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001463 if (IS_ERR(sg_class_member)) {
1464 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001465 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001466 error = PTR_ERR(sg_class_member);
1467 goto cdev_add_err;
1468 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001469 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 &sg_class_member->kobj, "generic");
1471 if (error)
1472 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001473 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001475 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
James Bottomley9ccfc752005-10-02 11:45:08 -05001477 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001478 "Attached scsi generic sg%d type %d\n", sdp->index,
1479 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Tony Jonesee959b02008-02-22 00:13:36 +01001481 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001485cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001486 write_lock_irqsave(&sg_index_lock, iflags);
1487 idr_remove(&sg_index_idr, sdp->index);
1488 write_unlock_irqrestore(&sg_index_lock, iflags);
1489 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out:
1492 put_disk(disk);
1493 if (cdev)
1494 cdev_del(cdev);
1495 return error;
1496}
1497
1498static void
Tony Jonesee959b02008-02-22 00:13:36 +01001499sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Tony Jonesee959b02008-02-22 00:13:36 +01001501 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1502 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned long iflags;
1504 Sg_fd *sfp;
1505 Sg_fd *tsfp;
1506 Sg_request *srp;
1507 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001508 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
James Bottomley7c07d612007-08-05 13:36:11 -05001510 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
James Bottomley7c07d612007-08-05 13:36:11 -05001513 delay = 0;
1514 write_lock_irqsave(&sg_index_lock, iflags);
1515 if (sdp->headfp) {
1516 sdp->detached = 1;
1517 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1518 tsfp = sfp->nextfp;
1519 for (srp = sfp->headrp; srp; srp = tsrp) {
1520 tsrp = srp->nextrp;
1521 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1522 sg_finish_rem_req(srp);
1523 }
1524 if (sfp->closed) {
1525 scsi_device_put(sdp->device);
1526 __sg_remove_sfp(sdp, sfp);
1527 } else {
1528 delay = 1;
1529 wake_up_interruptible(&sfp->read_wait);
1530 kill_fasync(&sfp->async_qp, SIGPOLL,
1531 POLL_HUP);
1532 }
1533 }
1534 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1535 if (NULL == sdp->headfp) {
1536 idr_remove(&sg_index_idr, sdp->index);
1537 }
1538 } else { /* nothing active, simple case */
1539 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1540 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
James Bottomley7c07d612007-08-05 13:36:11 -05001542 write_unlock_irqrestore(&sg_index_lock, iflags);
1543
1544 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001545 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001546 cdev_del(sdp->cdev);
1547 sdp->cdev = NULL;
1548 put_disk(sdp->disk);
1549 sdp->disk = NULL;
1550 if (NULL == sdp->headfp)
1551 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 if (delay)
1554 msleep(10); /* dirty detach so delay device destruction */
1555}
1556
Douglas Gilbert6460e752006-09-20 18:20:49 -04001557module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1558module_param_named(def_reserved_size, def_reserved_size, int,
1559 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1561
1562MODULE_AUTHOR("Douglas Gilbert");
1563MODULE_DESCRIPTION("SCSI generic (sg) driver");
1564MODULE_LICENSE("GPL");
1565MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001566MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Douglas Gilbert6460e752006-09-20 18:20:49 -04001568MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1569 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1571MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1572
1573static int __init
1574init_sg(void)
1575{
1576 int rc;
1577
Douglas Gilbert6460e752006-09-20 18:20:49 -04001578 if (scatter_elem_sz < PAGE_SIZE) {
1579 scatter_elem_sz = PAGE_SIZE;
1580 scatter_elem_sz_prev = scatter_elem_sz;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (def_reserved_size >= 0)
1583 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001584 else
1585 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1588 SG_MAX_DEVS, "sg");
1589 if (rc)
1590 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001591 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if ( IS_ERR(sg_sysfs_class) ) {
1593 rc = PTR_ERR(sg_sysfs_class);
1594 goto err_out;
1595 }
1596 sg_sysfs_valid = 1;
1597 rc = scsi_register_interface(&sg_interface);
1598 if (0 == rc) {
1599#ifdef CONFIG_SCSI_PROC_FS
1600 sg_proc_init();
1601#endif /* CONFIG_SCSI_PROC_FS */
1602 return 0;
1603 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001604 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605err_out:
1606 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1607 return rc;
1608}
1609
1610static void __exit
1611exit_sg(void)
1612{
1613#ifdef CONFIG_SCSI_PROC_FS
1614 sg_proc_cleanup();
1615#endif /* CONFIG_SCSI_PROC_FS */
1616 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001617 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 sg_sysfs_valid = 0;
1619 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1620 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001621 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001624static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001625{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001626 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001627 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001628 Sg_fd *sfp = srp->parentfp;
1629 sg_io_hdr_t *hp = &srp->header;
1630 int dxfer_len = (int) hp->dxfer_len;
1631 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001632 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001633 Sg_scatter_hold *req_schp = &srp->data;
1634 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1635 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001636 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001637 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1638
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001639 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1640 dxfer_len));
1641
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001642 rq = blk_get_request(q, rw, GFP_ATOMIC);
1643 if (!rq)
1644 return -ENOMEM;
1645
1646 memcpy(rq->cmd, cmd, hp->cmd_len);
1647
1648 rq->cmd_len = hp->cmd_len;
1649 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1650
1651 srp->rq = rq;
1652 rq->end_io_data = srp;
1653 rq->sense = srp->sense_b;
1654 rq->retries = SG_DEFAULT_RETRIES;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001657 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001658
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001659 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1660 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1661 !sfp->parentdp->device->host->unchecked_isa_dma &&
FUJITA Tomonori01cfcdd2008-08-28 15:05:59 +09001662 blk_rq_aligned(q, hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001663 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001664 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001665 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001666
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001667 if (md) {
1668 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1669 sg_link_reserve(sfp, srp, dxfer_len);
1670 else {
1671 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1672 if (res)
1673 return res;
1674 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001675
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001676 md->pages = req_schp->pages;
1677 md->page_order = req_schp->page_order;
1678 md->nr_entries = req_schp->k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001680
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001681 if (iov_count)
1682 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1683 hp->dxfer_len, GFP_ATOMIC);
1684 else
1685 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1686 hp->dxfer_len, GFP_ATOMIC);
1687
1688 if (!res) {
1689 srp->bio = rq->bio;
1690
1691 if (!md) {
1692 req_schp->dio_in_use = 1;
1693 hp->info |= SG_INFO_DIRECT_IO;
1694 }
1695 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001696 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static void
1700sg_finish_rem_req(Sg_request * srp)
1701{
1702 Sg_fd *sfp = srp->parentfp;
1703 Sg_scatter_hold *req_schp = &srp->data;
1704
1705 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1706 if (srp->res_used)
1707 sg_unlink_reserve(sfp, srp);
1708 else
1709 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001710
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001711 if (srp->rq) {
1712 if (srp->bio)
1713 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001714
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001715 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001716 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 sg_remove_request(sfp, srp);
1719}
1720
1721static int
1722sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1723{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001724 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001725 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001727 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1728 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001731 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734static int
1735sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1736{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001737 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001738 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001739 int blk_size = buff_size, order;
1740 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Eric Sesterhennfb119932007-05-23 14:41:36 -07001742 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return -EFAULT;
1744 if (0 == blk_size)
1745 ++blk_size; /* don't know why */
1746/* round request up to next highest SG_SECTOR_SZ byte boundary */
1747 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1748 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1749 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001750
1751 /* N.B. ret_sz carried into this block ... */
1752 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1753 if (mx_sc_elems < 0)
1754 return mx_sc_elems; /* most likely -ENOMEM */
1755
Douglas Gilbert6460e752006-09-20 18:20:49 -04001756 num = scatter_elem_sz;
1757 if (unlikely(num != scatter_elem_sz_prev)) {
1758 if (num < PAGE_SIZE) {
1759 scatter_elem_sz = PAGE_SIZE;
1760 scatter_elem_sz_prev = PAGE_SIZE;
1761 } else
1762 scatter_elem_sz_prev = num;
1763 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001764
1765 if (sfp->low_dma)
1766 gfp_mask |= GFP_DMA;
1767
1768 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1769 gfp_mask |= __GFP_ZERO;
1770
1771 order = get_order(num);
1772retry:
1773 ret_sz = 1 << (PAGE_SHIFT + order);
1774
1775 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1776 k++, rem_sz -= ret_sz) {
1777
Douglas Gilbert6460e752006-09-20 18:20:49 -04001778 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001779 scatter_elem_sz_prev : rem_sz;
1780
1781 schp->pages[k] = alloc_pages(gfp_mask, order);
1782 if (!schp->pages[k])
1783 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Douglas Gilbert6460e752006-09-20 18:20:49 -04001785 if (num == scatter_elem_sz_prev) {
1786 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1787 scatter_elem_sz = ret_sz;
1788 scatter_elem_sz_prev = ret_sz;
1789 }
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001792 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1793 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001794 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001796 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001797 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001798 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1799 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001800
1801 schp->bufflen = blk_size;
1802 if (rem_sz > 0) /* must have failed */
1803 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001805out:
1806 for (i = 0; i < k; i++)
1807 __free_pages(schp->pages[k], order);
1808
1809 if (--order >= 0)
1810 goto retry;
1811
1812 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815static void
1816sg_remove_scat(Sg_scatter_hold * schp)
1817{
1818 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001819 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001820 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int k;
1822
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001823 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001825 "sg_remove_scat: k=%d, pg=0x%p\n",
1826 k, schp->pages[k]));
1827 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001829
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001830 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Mike Christied6b10342005-11-08 04:06:41 -06001832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 memset(schp, 0, sizeof (*schp));
1834}
1835
1836static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1838{
1839 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001840 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1843 num_read_xfer));
1844 if ((!outp) || (num_read_xfer <= 0))
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001847 blk_rq_unmap_user(srp->bio);
1848 srp->bio = NULL;
1849
1850 num = 1 << (PAGE_SHIFT + schp->page_order);
1851 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001852 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001853 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001854 num_read_xfer))
1855 return -EFAULT;
1856 break;
1857 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001858 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001859 num))
1860 return -EFAULT;
1861 num_read_xfer -= num;
1862 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 break;
Mike Christied6b10342005-11-08 04:06:41 -06001864 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Mike Christied6b10342005-11-08 04:06:41 -06001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return 0;
1869}
1870
1871static void
1872sg_build_reserve(Sg_fd * sfp, int req_size)
1873{
1874 Sg_scatter_hold *schp = &sfp->reserve;
1875
1876 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1877 do {
1878 if (req_size < PAGE_SIZE)
1879 req_size = PAGE_SIZE;
1880 if (0 == sg_build_indirect(schp, sfp, req_size))
1881 return;
1882 else
1883 sg_remove_scat(schp);
1884 req_size >>= 1; /* divide by 2 */
1885 } while (req_size > (PAGE_SIZE / 2));
1886}
1887
1888static void
1889sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1890{
1891 Sg_scatter_hold *req_schp = &srp->data;
1892 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001893 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 srp->res_used = 1;
1896 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001897 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001899 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1900 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001901 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001902 req_schp->k_use_sg = k + 1;
1903 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001904 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001905
1906 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001907 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001908 break;
1909 } else
1910 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Mike Christied6b10342005-11-08 04:06:41 -06001912
1913 if (k >= rsv_schp->k_use_sg)
1914 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917static void
1918sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1919{
1920 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1923 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 req_schp->k_use_sg = 0;
1925 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001926 req_schp->pages = NULL;
1927 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 req_schp->sglist_len = 0;
1929 sfp->save_scat_len = 0;
1930 srp->res_used = 0;
1931}
1932
1933static Sg_request *
1934sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1935{
1936 Sg_request *resp;
1937 unsigned long iflags;
1938
1939 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1940 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1941 /* look for requests that are ready + not SG_IO owned */
1942 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1943 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1944 resp->done = 2; /* guard against other readers */
1945 break;
1946 }
1947 }
1948 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1949 return resp;
1950}
1951
1952#ifdef CONFIG_SCSI_PROC_FS
1953static Sg_request *
1954sg_get_nth_request(Sg_fd * sfp, int nth)
1955{
1956 Sg_request *resp;
1957 unsigned long iflags;
1958 int k;
1959
1960 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1961 for (k = 0, resp = sfp->headrp; resp && (k < nth);
1962 ++k, resp = resp->nextrp) ;
1963 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1964 return resp;
1965}
1966#endif
1967
1968/* always adds to end of list */
1969static Sg_request *
1970sg_add_request(Sg_fd * sfp)
1971{
1972 int k;
1973 unsigned long iflags;
1974 Sg_request *resp;
1975 Sg_request *rp = sfp->req_arr;
1976
1977 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1978 resp = sfp->headrp;
1979 if (!resp) {
1980 memset(rp, 0, sizeof (Sg_request));
1981 rp->parentfp = sfp;
1982 resp = rp;
1983 sfp->headrp = resp;
1984 } else {
1985 if (0 == sfp->cmd_q)
1986 resp = NULL; /* command queuing disallowed */
1987 else {
1988 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1989 if (!rp->parentfp)
1990 break;
1991 }
1992 if (k < SG_MAX_QUEUE) {
1993 memset(rp, 0, sizeof (Sg_request));
1994 rp->parentfp = sfp;
1995 while (resp->nextrp)
1996 resp = resp->nextrp;
1997 resp->nextrp = rp;
1998 resp = rp;
1999 } else
2000 resp = NULL;
2001 }
2002 }
2003 if (resp) {
2004 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002005 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2008 return resp;
2009}
2010
2011/* Return of 1 for found; 0 for not found */
2012static int
2013sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2014{
2015 Sg_request *prev_rp;
2016 Sg_request *rp;
2017 unsigned long iflags;
2018 int res = 0;
2019
2020 if ((!sfp) || (!srp) || (!sfp->headrp))
2021 return res;
2022 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2023 prev_rp = sfp->headrp;
2024 if (srp == prev_rp) {
2025 sfp->headrp = prev_rp->nextrp;
2026 prev_rp->parentfp = NULL;
2027 res = 1;
2028 } else {
2029 while ((rp = prev_rp->nextrp)) {
2030 if (srp == rp) {
2031 prev_rp->nextrp = rp->nextrp;
2032 rp->parentfp = NULL;
2033 res = 1;
2034 break;
2035 }
2036 prev_rp = rp;
2037 }
2038 }
2039 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2040 return res;
2041}
2042
2043#ifdef CONFIG_SCSI_PROC_FS
2044static Sg_fd *
2045sg_get_nth_sfp(Sg_device * sdp, int nth)
2046{
2047 Sg_fd *resp;
2048 unsigned long iflags;
2049 int k;
2050
James Bottomley7c07d612007-08-05 13:36:11 -05002051 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2053 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002054 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return resp;
2056}
2057#endif
2058
2059static Sg_fd *
2060sg_add_sfp(Sg_device * sdp, int dev)
2061{
2062 Sg_fd *sfp;
2063 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002064 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Mike Christied6b10342005-11-08 04:06:41 -06002066 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!sfp)
2068 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 init_waitqueue_head(&sfp->read_wait);
2071 rwlock_init(&sfp->rq_list_lock);
2072
2073 sfp->timeout = SG_DEFAULT_TIMEOUT;
2074 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2075 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2076 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2077 sdp->device->host->unchecked_isa_dma : 1;
2078 sfp->cmd_q = SG_DEF_COMMAND_Q;
2079 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2080 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002081 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (!sdp->headfp)
2083 sdp->headfp = sfp;
2084 else { /* add to tail of existing list */
2085 Sg_fd *pfp = sdp->headfp;
2086 while (pfp->nextfp)
2087 pfp = pfp->nextfp;
2088 pfp->nextfp = sfp;
2089 }
James Bottomley7c07d612007-08-05 13:36:11 -05002090 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002092 if (unlikely(sg_big_buff != def_reserved_size))
2093 sg_big_buff = def_reserved_size;
2094
Alan Stern44ec9542007-02-20 11:01:57 -05002095 bufflen = min_t(int, sg_big_buff,
2096 sdp->device->request_queue->max_sectors * 512);
2097 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2099 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2100 return sfp;
2101}
2102
2103static void
2104__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2105{
2106 Sg_fd *fp;
2107 Sg_fd *prev_fp;
2108
2109 prev_fp = sdp->headfp;
2110 if (sfp == prev_fp)
2111 sdp->headfp = prev_fp->nextfp;
2112 else {
2113 while ((fp = prev_fp->nextfp)) {
2114 if (sfp == fp) {
2115 prev_fp->nextfp = fp->nextfp;
2116 break;
2117 }
2118 prev_fp = fp;
2119 }
2120 }
2121 if (sfp->reserve.bufflen > 0) {
2122 SCSI_LOG_TIMEOUT(6,
2123 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2124 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 sg_remove_scat(&sfp->reserve);
2126 }
2127 sfp->parentdp = NULL;
2128 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002129 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132/* Returns 0 in normal case, 1 when detached and sdp object removed */
2133static int
2134sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2135{
2136 Sg_request *srp;
2137 Sg_request *tsrp;
2138 int dirty = 0;
2139 int res = 0;
2140
2141 for (srp = sfp->headrp; srp; srp = tsrp) {
2142 tsrp = srp->nextrp;
2143 if (sg_srp_done(srp, sfp))
2144 sg_finish_rem_req(srp);
2145 else
2146 ++dirty;
2147 }
2148 if (0 == dirty) {
2149 unsigned long iflags;
2150
James Bottomley7c07d612007-08-05 13:36:11 -05002151 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 __sg_remove_sfp(sdp, sfp);
2153 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002154 idr_remove(&sg_index_idr, sdp->index);
2155 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 res = 1;
2157 }
James Bottomley7c07d612007-08-05 13:36:11 -05002158 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 } else {
2160 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2161 /* only bump the access_count if we actually succeeded in
2162 * throwing another counter on the host module */
2163 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2164 sfp->closed = 1; /* flag dirty state on this fd */
2165 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2166 dirty));
2167 }
2168 return res;
2169}
2170
2171static int
2172sg_res_in_use(Sg_fd * sfp)
2173{
2174 const Sg_request *srp;
2175 unsigned long iflags;
2176
2177 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2178 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2179 if (srp->res_used)
2180 break;
2181 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2182 return srp ? 1 : 0;
2183}
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185#ifdef CONFIG_SCSI_PROC_FS
2186static int
James Bottomley7c07d612007-08-05 13:36:11 -05002187sg_idr_max_id(int id, void *p, void *data)
2188{
2189 int *k = data;
2190
2191 if (*k < id)
2192 *k = id;
2193
2194 return 0;
2195}
2196
2197static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198sg_last_dev(void)
2199{
Tony Battersby53474c02008-01-22 15:25:49 -05002200 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 unsigned long iflags;
2202
James Bottomley7c07d612007-08-05 13:36:11 -05002203 read_lock_irqsave(&sg_index_lock, iflags);
2204 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2205 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return k + 1; /* origin 1 */
2207}
2208#endif
2209
2210static Sg_device *
2211sg_get_dev(int dev)
2212{
James Bottomley7c07d612007-08-05 13:36:11 -05002213 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 unsigned long iflags;
2215
James Bottomley7c07d612007-08-05 13:36:11 -05002216 read_lock_irqsave(&sg_index_lock, iflags);
2217 sdp = idr_find(&sg_index_idr, dev);
2218 read_unlock_irqrestore(&sg_index_lock, iflags);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return sdp;
2221}
2222
2223#ifdef CONFIG_SCSI_PROC_FS
2224
2225static struct proc_dir_entry *sg_proc_sgp = NULL;
2226
2227static char sg_proc_sg_dirname[] = "scsi/sg";
2228
2229static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2230
2231static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2232static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2233 size_t count, loff_t *off);
2234static struct file_operations adio_fops = {
2235 /* .owner, .read and .llseek added in sg_proc_init() */
2236 .open = sg_proc_single_open_adio,
2237 .write = sg_proc_write_adio,
2238 .release = single_release,
2239};
2240
2241static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2242static ssize_t sg_proc_write_dressz(struct file *filp,
2243 const char __user *buffer, size_t count, loff_t *off);
2244static struct file_operations dressz_fops = {
2245 .open = sg_proc_single_open_dressz,
2246 .write = sg_proc_write_dressz,
2247 .release = single_release,
2248};
2249
2250static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2251static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2252static struct file_operations version_fops = {
2253 .open = sg_proc_single_open_version,
2254 .release = single_release,
2255};
2256
2257static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2258static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2259static struct file_operations devhdr_fops = {
2260 .open = sg_proc_single_open_devhdr,
2261 .release = single_release,
2262};
2263
2264static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2265static int sg_proc_open_dev(struct inode *inode, struct file *file);
2266static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2267static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2268static void dev_seq_stop(struct seq_file *s, void *v);
2269static struct file_operations dev_fops = {
2270 .open = sg_proc_open_dev,
2271 .release = seq_release,
2272};
2273static struct seq_operations dev_seq_ops = {
2274 .start = dev_seq_start,
2275 .next = dev_seq_next,
2276 .stop = dev_seq_stop,
2277 .show = sg_proc_seq_show_dev,
2278};
2279
2280static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2281static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2282static struct file_operations devstrs_fops = {
2283 .open = sg_proc_open_devstrs,
2284 .release = seq_release,
2285};
2286static struct seq_operations devstrs_seq_ops = {
2287 .start = dev_seq_start,
2288 .next = dev_seq_next,
2289 .stop = dev_seq_stop,
2290 .show = sg_proc_seq_show_devstrs,
2291};
2292
2293static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2294static int sg_proc_open_debug(struct inode *inode, struct file *file);
2295static struct file_operations debug_fops = {
2296 .open = sg_proc_open_debug,
2297 .release = seq_release,
2298};
2299static struct seq_operations debug_seq_ops = {
2300 .start = dev_seq_start,
2301 .next = dev_seq_next,
2302 .stop = dev_seq_stop,
2303 .show = sg_proc_seq_show_debug,
2304};
2305
2306
2307struct sg_proc_leaf {
2308 const char * name;
2309 struct file_operations * fops;
2310};
2311
2312static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2313 {"allow_dio", &adio_fops},
2314 {"debug", &debug_fops},
2315 {"def_reserved_size", &dressz_fops},
2316 {"device_hdr", &devhdr_fops},
2317 {"devices", &dev_fops},
2318 {"device_strs", &devstrs_fops},
2319 {"version", &version_fops}
2320};
2321
2322static int
2323sg_proc_init(void)
2324{
2325 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002326 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct sg_proc_leaf * leaf;
2328
Al Viro66600222005-09-28 22:32:57 +01002329 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!sg_proc_sgp)
2331 return 1;
2332 for (k = 0; k < num_leaves; ++k) {
2333 leaf = &sg_proc_leaf_arr[k];
2334 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002335 leaf->fops->owner = THIS_MODULE;
2336 leaf->fops->read = seq_read;
2337 leaf->fops->llseek = seq_lseek;
2338 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 return 0;
2341}
2342
2343static void
2344sg_proc_cleanup(void)
2345{
2346 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002347 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 if (!sg_proc_sgp)
2350 return;
2351 for (k = 0; k < num_leaves; ++k)
2352 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2353 remove_proc_entry(sg_proc_sg_dirname, NULL);
2354}
2355
2356
2357static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2358{
2359 seq_printf(s, "%d\n", *((int *)s->private));
2360 return 0;
2361}
2362
2363static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2364{
2365 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2366}
2367
2368static ssize_t
2369sg_proc_write_adio(struct file *filp, const char __user *buffer,
2370 size_t count, loff_t *off)
2371{
2372 int num;
2373 char buff[11];
2374
2375 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2376 return -EACCES;
2377 num = (count < 10) ? count : 10;
2378 if (copy_from_user(buff, buffer, num))
2379 return -EFAULT;
2380 buff[num] = '\0';
2381 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2382 return count;
2383}
2384
2385static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2386{
2387 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2388}
2389
2390static ssize_t
2391sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2392 size_t count, loff_t *off)
2393{
2394 int num;
2395 unsigned long k = ULONG_MAX;
2396 char buff[11];
2397
2398 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2399 return -EACCES;
2400 num = (count < 10) ? count : 10;
2401 if (copy_from_user(buff, buffer, num))
2402 return -EFAULT;
2403 buff[num] = '\0';
2404 k = simple_strtoul(buff, NULL, 10);
2405 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2406 sg_big_buff = k;
2407 return count;
2408 }
2409 return -ERANGE;
2410}
2411
2412static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2413{
2414 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2415 sg_version_date);
2416 return 0;
2417}
2418
2419static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2420{
2421 return single_open(file, sg_proc_seq_show_version, NULL);
2422}
2423
2424static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2425{
2426 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2427 "online\n");
2428 return 0;
2429}
2430
2431static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2432{
2433 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2434}
2435
2436struct sg_proc_deviter {
2437 loff_t index;
2438 size_t max;
2439};
2440
2441static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2442{
2443 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2444
Jan Blunck729d70f2005-08-27 11:07:52 -07002445 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (! it)
2447 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 it->index = *pos;
2450 it->max = sg_last_dev();
2451 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002452 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2457{
Jan Blunck729d70f2005-08-27 11:07:52 -07002458 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 *pos = ++it->index;
2461 return (it->index < it->max) ? it : NULL;
2462}
2463
2464static void dev_seq_stop(struct seq_file *s, void *v)
2465{
Jan Blunck729d70f2005-08-27 11:07:52 -07002466 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
2469static int sg_proc_open_dev(struct inode *inode, struct file *file)
2470{
2471 return seq_open(file, &dev_seq_ops);
2472}
2473
2474static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2475{
2476 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2477 Sg_device *sdp;
2478 struct scsi_device *scsidp;
2479
2480 sdp = it ? sg_get_dev(it->index) : NULL;
2481 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2482 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2483 scsidp->host->host_no, scsidp->channel,
2484 scsidp->id, scsidp->lun, (int) scsidp->type,
2485 1,
2486 (int) scsidp->queue_depth,
2487 (int) scsidp->device_busy,
2488 (int) scsi_device_online(scsidp));
2489 else
2490 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2491 return 0;
2492}
2493
2494static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2495{
2496 return seq_open(file, &devstrs_seq_ops);
2497}
2498
2499static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2500{
2501 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2502 Sg_device *sdp;
2503 struct scsi_device *scsidp;
2504
2505 sdp = it ? sg_get_dev(it->index) : NULL;
2506 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2507 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2508 scsidp->vendor, scsidp->model, scsidp->rev);
2509 else
2510 seq_printf(s, "<no active device>\n");
2511 return 0;
2512}
2513
2514static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2515{
2516 int k, m, new_interface, blen, usg;
2517 Sg_request *srp;
2518 Sg_fd *fp;
2519 const sg_io_hdr_t *hp;
2520 const char * cp;
cb59e842005-04-02 13:51:23 -06002521 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2524 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2525 "(res)sgat=%d low_dma=%d\n", k + 1,
2526 jiffies_to_msecs(fp->timeout),
2527 fp->reserve.bufflen,
2528 (int) fp->reserve.k_use_sg,
2529 (int) fp->low_dma);
2530 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2531 (int) fp->cmd_q, (int) fp->force_packid,
2532 (int) fp->keep_orphan, (int) fp->closed);
2533 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2534 hp = &srp->header;
2535 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2536 if (srp->res_used) {
2537 if (new_interface &&
2538 (SG_FLAG_MMAP_IO & hp->flags))
2539 cp = " mmap>> ";
2540 else
2541 cp = " rb>> ";
2542 } else {
2543 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2544 cp = " dio>> ";
2545 else
2546 cp = " ";
2547 }
2548 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002549 blen = srp->data.bufflen;
2550 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 seq_printf(s, srp->done ?
2552 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002553 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 seq_printf(s, " id=%d blen=%d",
2555 srp->header.pack_id, blen);
2556 if (srp->done)
2557 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002558 else {
2559 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002561 (new_interface ? hp->timeout :
2562 jiffies_to_msecs(fp->timeout)),
2563 (ms > hp->duration ? ms - hp->duration : 0));
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2566 (int) srp->data.cmd_opcode);
2567 }
2568 if (0 == m)
2569 seq_printf(s, " No requests active\n");
2570 }
2571}
2572
2573static int sg_proc_open_debug(struct inode *inode, struct file *file)
2574{
2575 return seq_open(file, &debug_seq_ops);
2576}
2577
2578static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2579{
2580 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2581 Sg_device *sdp;
2582
2583 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002584 seq_printf(s, "max_active_device=%d(origin 1)\n",
2585 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2587 }
2588 sdp = it ? sg_get_dev(it->index) : NULL;
2589 if (sdp) {
2590 struct scsi_device *scsidp = sdp->device;
2591
2592 if (NULL == scsidp) {
2593 seq_printf(s, "device %d detached ??\n",
2594 (int)it->index);
2595 return 0;
2596 }
2597
2598 if (sg_get_nth_sfp(sdp, 0)) {
2599 seq_printf(s, " >>> device=%s ",
2600 sdp->disk->disk_name);
2601 if (sdp->detached)
2602 seq_printf(s, "detached pending close ");
2603 else
2604 seq_printf
2605 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2606 scsidp->host->host_no,
2607 scsidp->channel, scsidp->id,
2608 scsidp->lun,
2609 scsidp->host->hostt->emulated);
2610 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2611 sdp->sg_tablesize, sdp->exclude);
2612 }
2613 sg_proc_debug_helper(s, sdp);
2614 }
2615 return 0;
2616}
2617
2618#endif /* CONFIG_SCSI_PROC_FS */
2619
2620module_init(init_sg);
2621module_exit(exit_sg);