blob: b4ef2f84ea32ae6a5e3dca480da58ef486345ce2 [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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
331 if (!sdp->detached) {
332 scsi_device_put(sdp->device);
333 }
334 sdp->exclude = 0;
335 wake_up_interruptible(&sdp->o_excl_wait);
336 }
337 return 0;
338}
339
340static ssize_t
341sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 Sg_device *sdp;
344 Sg_fd *sfp;
345 Sg_request *srp;
346 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600348 struct sg_header *old_hdr = NULL;
349 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
352 return -ENXIO;
353 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
354 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!access_ok(VERIFY_WRITE, buf, count))
357 return -EFAULT;
358 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600359 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
360 if (!old_hdr)
361 return -ENOMEM;
362 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
363 retval = -EFAULT;
364 goto free_old_hdr;
365 }
366 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600368 sg_io_hdr_t *new_hdr;
369 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
370 if (!new_hdr) {
371 retval = -ENOMEM;
372 goto free_old_hdr;
373 }
374 retval =__copy_from_user
375 (new_hdr, buf, SZ_SG_IO_HDR);
376 req_pack_id = new_hdr->pack_id;
377 kfree(new_hdr);
378 if (retval) {
379 retval = -EFAULT;
380 goto free_old_hdr;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 } else
cb59e842005-04-02 13:51:23 -0600384 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 srp = sg_get_rq_mark(sfp, req_pack_id);
387 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600388 if (sdp->detached) {
389 retval = -ENODEV;
390 goto free_old_hdr;
391 }
392 if (filp->f_flags & O_NONBLOCK) {
393 retval = -EAGAIN;
394 goto free_old_hdr;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 while (1) {
cb59e842005-04-02 13:51:23 -0600397 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600399 (sdp->detached ||
400 (srp = sg_get_rq_mark(sfp, req_pack_id))),
401 retval);
402 if (sdp->detached) {
403 retval = -ENODEV;
404 goto free_old_hdr;
405 }
406 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
cb59e842005-04-02 13:51:23 -0600408
409 /* -ERESTARTSYS as signal hit process */
410 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
cb59e842005-04-02 13:51:23 -0600413 if (srp->header.interface_id != '\0') {
414 retval = sg_new_read(sfp, buf, count, srp);
415 goto free_old_hdr;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600419 if (old_hdr == NULL) {
420 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
421 if (! old_hdr) {
422 retval = -ENOMEM;
423 goto free_old_hdr;
424 }
425 }
426 memset(old_hdr, 0, SZ_SG_HEADER);
427 old_hdr->reply_len = (int) hp->timeout;
428 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
429 old_hdr->pack_id = hp->pack_id;
430 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600432 old_hdr->target_status = hp->masked_status;
433 old_hdr->host_status = hp->host_status;
434 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if ((CHECK_CONDITION & hp->masked_status) ||
436 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600437 memcpy(old_hdr->sense_buffer, srp->sense_b,
438 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 switch (hp->host_status) {
440 /* This setup of 'result' is for backward compatibility and is best
441 ignored by the user who should use target, host + driver status */
442 case DID_OK:
443 case DID_PASSTHROUGH:
444 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600445 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447 case DID_NO_CONNECT:
448 case DID_BUS_BUSY:
449 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600450 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 case DID_BAD_TARGET:
453 case DID_ABORT:
454 case DID_PARITY:
455 case DID_RESET:
456 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600457 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600460 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 hp->masked_status == GOOD) ? 0 : EIO;
462 break;
463 default:
cb59e842005-04-02 13:51:23 -0600464 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 }
467
468 /* Now copy the result back to the user buffer. */
469 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600470 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
471 retval = -EFAULT;
472 goto free_old_hdr;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600475 if (count > old_hdr->reply_len)
476 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600478 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
479 retval = -EFAULT;
480 goto free_old_hdr;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 } else
cb59e842005-04-02 13:51:23 -0600484 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600486 retval = count;
487free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800488 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600489 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static ssize_t
493sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
494{
495 sg_io_hdr_t *hp = &srp->header;
496 int err = 0;
497 int len;
498
499 if (count < SZ_SG_IO_HDR) {
500 err = -EINVAL;
501 goto err_out;
502 }
503 hp->sb_len_wr = 0;
504 if ((hp->mx_sb_len > 0) && hp->sbp) {
505 if ((CHECK_CONDITION & hp->masked_status) ||
506 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600507 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
509 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
510 len = (len > sb_len) ? sb_len : len;
511 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
512 err = -EFAULT;
513 goto err_out;
514 }
515 hp->sb_len_wr = len;
516 }
517 }
518 if (hp->masked_status || hp->host_status || hp->driver_status)
519 hp->info |= SG_INFO_CHECK;
520 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
521 err = -EFAULT;
522 goto err_out;
523 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900524err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sg_finish_rem_req(srp);
526 return (0 == err) ? count : err;
527}
528
529static ssize_t
530sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
531{
532 int mxsize, cmd_size, k;
533 int input_size, blocking;
534 unsigned char opcode;
535 Sg_device *sdp;
536 Sg_fd *sfp;
537 Sg_request *srp;
538 struct sg_header old_hdr;
539 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600540 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
543 return -ENXIO;
544 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
545 sdp->disk->disk_name, (int) count));
546 if (sdp->detached)
547 return -ENODEV;
548 if (!((filp->f_flags & O_NONBLOCK) ||
549 scsi_block_when_processing_errors(sdp->device)))
550 return -ENXIO;
551
552 if (!access_ok(VERIFY_READ, buf, count))
553 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
554 if (count < SZ_SG_HEADER)
555 return -EIO;
556 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
557 return -EFAULT;
558 blocking = !(filp->f_flags & O_NONBLOCK);
559 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200560 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (count < (SZ_SG_HEADER + 6))
562 return -EIO; /* The minimum scsi command length is 6 bytes. */
563
564 if (!(srp = sg_add_request(sfp))) {
565 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
566 return -EDOM;
567 }
568 buf += SZ_SG_HEADER;
569 __get_user(opcode, buf);
570 if (sfp->next_cmd_len > 0) {
571 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
572 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
573 sfp->next_cmd_len = 0;
574 sg_remove_request(sfp, srp);
575 return -EIO;
576 }
577 cmd_size = sfp->next_cmd_len;
578 sfp->next_cmd_len = 0; /* reset so only this write() effected */
579 } else {
580 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
581 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
582 cmd_size = 12;
583 }
584 SCSI_LOG_TIMEOUT(4, printk(
585 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
586/* Determine buffer size. */
587 input_size = count - cmd_size;
588 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
589 mxsize -= SZ_SG_HEADER;
590 input_size -= SZ_SG_HEADER;
591 if (input_size < 0) {
592 sg_remove_request(sfp, srp);
593 return -EIO; /* User did not pass enough bytes for this command. */
594 }
595 hp = &srp->header;
596 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
597 hp->cmd_len = (unsigned char) cmd_size;
598 hp->iovec_count = 0;
599 hp->mx_sb_len = 0;
600 if (input_size > 0)
601 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
602 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
603 else
604 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
605 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900606 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
607 hp->dxferp = (char __user *)buf + cmd_size;
608 else
609 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 hp->sbp = NULL;
611 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
612 hp->flags = input_size; /* structure abuse ... */
613 hp->pack_id = old_hdr.pack_id;
614 hp->usr_ptr = NULL;
615 if (__copy_from_user(cmnd, buf, cmd_size))
616 return -EFAULT;
617 /*
618 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
619 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
620 * is a non-zero input_size, so emit a warning.
621 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100622 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
623 static char cmd[TASK_COMM_LEN];
624 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 printk(KERN_WARNING
626 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
627 "guessing data in;\n" KERN_WARNING " "
628 "program %s not setting count and/or reply_len properly\n",
629 old_hdr.reply_len - (int)SZ_SG_HEADER,
630 input_size, (unsigned int) cmnd[0],
631 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100632 strcpy(cmd, current->comm);
633 }
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
636 return (k < 0) ? k : count;
637}
638
639static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200640sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
641 size_t count, int blocking, int read_only,
642 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 int k;
645 Sg_request *srp;
646 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600647 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int timeout;
649 unsigned long ul_timeout;
650
651 if (count < SZ_SG_IO_HDR)
652 return -EINVAL;
653 if (!access_ok(VERIFY_READ, buf, count))
654 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
655
656 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
657 if (!(srp = sg_add_request(sfp))) {
658 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
659 return -EDOM;
660 }
661 hp = &srp->header;
662 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
663 sg_remove_request(sfp, srp);
664 return -EFAULT;
665 }
666 if (hp->interface_id != 'S') {
667 sg_remove_request(sfp, srp);
668 return -ENOSYS;
669 }
670 if (hp->flags & SG_FLAG_MMAP_IO) {
671 if (hp->dxfer_len > sfp->reserve.bufflen) {
672 sg_remove_request(sfp, srp);
673 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
674 }
675 if (hp->flags & SG_FLAG_DIRECT_IO) {
676 sg_remove_request(sfp, srp);
677 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
678 }
679 if (sg_res_in_use(sfp)) {
680 sg_remove_request(sfp, srp);
681 return -EBUSY; /* reserve buffer already being used */
682 }
683 }
684 ul_timeout = msecs_to_jiffies(srp->header.timeout);
685 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
686 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
687 sg_remove_request(sfp, srp);
688 return -EMSGSIZE;
689 }
690 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
691 sg_remove_request(sfp, srp);
692 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
693 }
694 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
695 sg_remove_request(sfp, srp);
696 return -EFAULT;
697 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900698 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 sg_remove_request(sfp, srp);
700 return -EPERM;
701 }
702 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
703 if (k < 0)
704 return k;
705 if (o_srp)
706 *o_srp = srp;
707 return count;
708}
709
710static int
711sg_common_write(Sg_fd * sfp, Sg_request * srp,
712 unsigned char *cmnd, int timeout, int blocking)
713{
Mike Christied6b10342005-11-08 04:06:41 -0600714 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 Sg_device *sdp = sfp->parentdp;
716 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
719 hp->status = 0;
720 hp->masked_status = 0;
721 hp->msg_status = 0;
722 hp->info = 0;
723 hp->host_status = 0;
724 hp->driver_status = 0;
725 hp->resid = 0;
726 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
727 (int) cmnd[0], (int) hp->cmd_len));
728
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900729 k = sg_start_req(srp, cmnd);
730 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400731 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 sg_finish_rem_req(srp);
733 return k; /* probably out of space --> ENOMEM */
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (sdp->detached) {
736 sg_finish_rem_req(srp);
737 return -ENODEV;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 switch (hp->dxfer_direction) {
741 case SG_DXFER_TO_FROM_DEV:
742 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600743 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
745 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600746 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600749 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 default:
Mike Christied6b10342005-11-08 04:06:41 -0600752 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 }
cb59e842005-04-02 13:51:23 -0600755 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200756
757 srp->rq->timeout = timeout;
758 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
759 srp->rq, 1, sg_rq_end_io);
760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763static int
764sg_srp_done(Sg_request *srp, Sg_fd *sfp)
765{
766 unsigned long iflags;
767 int done;
768
769 read_lock_irqsave(&sfp->rq_list_lock, iflags);
770 done = srp->done;
771 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
772 return done;
773}
774
775static int
776sg_ioctl(struct inode *inode, struct file *filp,
777 unsigned int cmd_in, unsigned long arg)
778{
779 void __user *p = (void __user *)arg;
780 int __user *ip = p;
781 int result, val, read_only;
782 Sg_device *sdp;
783 Sg_fd *sfp;
784 Sg_request *srp;
785 unsigned long iflags;
786
787 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
788 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
791 sdp->disk->disk_name, (int) cmd_in));
792 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
793
794 switch (cmd_in) {
795 case SG_IO:
796 {
797 int blocking = 1; /* ignore O_NONBLOCK flag */
798
799 if (sdp->detached)
800 return -ENODEV;
801 if (!scsi_block_when_processing_errors(sdp->device))
802 return -ENXIO;
803 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
804 return -EFAULT;
805 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200806 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 blocking, read_only, &srp);
808 if (result < 0)
809 return result;
810 srp->sg_io_owned = 1;
811 while (1) {
812 result = 0; /* following macro to beat race condition */
813 __wait_event_interruptible(sfp->read_wait,
814 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
815 result);
816 if (sdp->detached)
817 return -ENODEV;
818 if (sfp->closed)
819 return 0; /* request packet dropped already */
820 if (0 == result)
821 break;
822 srp->orphan = 1;
823 return result; /* -ERESTARTSYS because signal hit process */
824 }
825 write_lock_irqsave(&sfp->rq_list_lock, iflags);
826 srp->done = 2;
827 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
828 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
829 return (result < 0) ? result : 0;
830 }
831 case SG_SET_TIMEOUT:
832 result = get_user(val, ip);
833 if (result)
834 return result;
835 if (val < 0)
836 return -EIO;
837 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
838 val = MULDIV (INT_MAX, USER_HZ, HZ);
839 sfp->timeout_user = val;
840 sfp->timeout = MULDIV (val, HZ, USER_HZ);
841
842 return 0;
843 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
844 /* strange ..., for backward compatibility */
845 return sfp->timeout_user;
846 case SG_SET_FORCE_LOW_DMA:
847 result = get_user(val, ip);
848 if (result)
849 return result;
850 if (val) {
851 sfp->low_dma = 1;
852 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
853 val = (int) sfp->reserve.bufflen;
854 sg_remove_scat(&sfp->reserve);
855 sg_build_reserve(sfp, val);
856 }
857 } else {
858 if (sdp->detached)
859 return -ENODEV;
860 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
861 }
862 return 0;
863 case SG_GET_LOW_DMA:
864 return put_user((int) sfp->low_dma, ip);
865 case SG_GET_SCSI_ID:
866 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
867 return -EFAULT;
868 else {
869 sg_scsi_id_t __user *sg_idp = p;
870
871 if (sdp->detached)
872 return -ENODEV;
873 __put_user((int) sdp->device->host->host_no,
874 &sg_idp->host_no);
875 __put_user((int) sdp->device->channel,
876 &sg_idp->channel);
877 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
878 __put_user((int) sdp->device->lun, &sg_idp->lun);
879 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
880 __put_user((short) sdp->device->host->cmd_per_lun,
881 &sg_idp->h_cmd_per_lun);
882 __put_user((short) sdp->device->queue_depth,
883 &sg_idp->d_queue_depth);
884 __put_user(0, &sg_idp->unused[0]);
885 __put_user(0, &sg_idp->unused[1]);
886 return 0;
887 }
888 case SG_SET_FORCE_PACK_ID:
889 result = get_user(val, ip);
890 if (result)
891 return result;
892 sfp->force_packid = val ? 1 : 0;
893 return 0;
894 case SG_GET_PACK_ID:
895 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
896 return -EFAULT;
897 read_lock_irqsave(&sfp->rq_list_lock, iflags);
898 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
899 if ((1 == srp->done) && (!srp->sg_io_owned)) {
900 read_unlock_irqrestore(&sfp->rq_list_lock,
901 iflags);
902 __put_user(srp->header.pack_id, ip);
903 return 0;
904 }
905 }
906 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
907 __put_user(-1, ip);
908 return 0;
909 case SG_GET_NUM_WAITING:
910 read_lock_irqsave(&sfp->rq_list_lock, iflags);
911 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
912 if ((1 == srp->done) && (!srp->sg_io_owned))
913 ++val;
914 }
915 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
916 return put_user(val, ip);
917 case SG_GET_SG_TABLESIZE:
918 return put_user(sdp->sg_tablesize, ip);
919 case SG_SET_RESERVED_SIZE:
920 result = get_user(val, ip);
921 if (result)
922 return result;
923 if (val < 0)
924 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500925 val = min_t(int, val,
926 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (val != sfp->reserve.bufflen) {
928 if (sg_res_in_use(sfp) || sfp->mmap_called)
929 return -EBUSY;
930 sg_remove_scat(&sfp->reserve);
931 sg_build_reserve(sfp, val);
932 }
933 return 0;
934 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500935 val = min_t(int, sfp->reserve.bufflen,
936 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return put_user(val, ip);
938 case SG_SET_COMMAND_Q:
939 result = get_user(val, ip);
940 if (result)
941 return result;
942 sfp->cmd_q = val ? 1 : 0;
943 return 0;
944 case SG_GET_COMMAND_Q:
945 return put_user((int) sfp->cmd_q, ip);
946 case SG_SET_KEEP_ORPHAN:
947 result = get_user(val, ip);
948 if (result)
949 return result;
950 sfp->keep_orphan = val;
951 return 0;
952 case SG_GET_KEEP_ORPHAN:
953 return put_user((int) sfp->keep_orphan, ip);
954 case SG_NEXT_CMD_LEN:
955 result = get_user(val, ip);
956 if (result)
957 return result;
958 sfp->next_cmd_len = (val > 0) ? val : 0;
959 return 0;
960 case SG_GET_VERSION_NUM:
961 return put_user(sg_version_num, ip);
962 case SG_GET_ACCESS_COUNT:
963 /* faked - we don't have a real access count anymore */
964 val = (sdp->device ? 1 : 0);
965 return put_user(val, ip);
966 case SG_GET_REQUEST_TABLE:
967 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
968 return -EFAULT;
969 else {
cb59e842005-04-02 13:51:23 -0600970 sg_req_info_t *rinfo;
971 unsigned int ms;
972
973 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
974 GFP_KERNEL);
975 if (!rinfo)
976 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 read_lock_irqsave(&sfp->rq_list_lock, iflags);
978 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
979 ++val, srp = srp ? srp->nextrp : srp) {
980 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
981 if (srp) {
982 rinfo[val].req_state = srp->done + 1;
983 rinfo[val].problem =
984 srp->header.masked_status &
985 srp->header.host_status &
986 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600987 if (srp->done)
988 rinfo[val].duration =
989 srp->header.duration;
990 else {
991 ms = jiffies_to_msecs(jiffies);
992 rinfo[val].duration =
993 (ms > srp->header.duration) ?
994 (ms - srp->header.duration) : 0;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600997 rinfo[val].sg_io_owned =
998 srp->sg_io_owned;
999 rinfo[val].pack_id =
1000 srp->header.pack_id;
1001 rinfo[val].usr_ptr =
1002 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004 }
1005 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001006 result = __copy_to_user(p, rinfo,
1007 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1008 result = result ? -EFAULT : 0;
1009 kfree(rinfo);
1010 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 case SG_EMULATED_HOST:
1013 if (sdp->detached)
1014 return -ENODEV;
1015 return put_user(sdp->device->host->hostt->emulated, ip);
1016 case SG_SCSI_RESET:
1017 if (sdp->detached)
1018 return -ENODEV;
1019 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001020 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EBUSY;
1022 } else if (!scsi_block_when_processing_errors(sdp->device))
1023 return -EBUSY;
1024 result = get_user(val, ip);
1025 if (result)
1026 return result;
1027 if (SG_SCSI_RESET_NOTHING == val)
1028 return 0;
1029 switch (val) {
1030 case SG_SCSI_RESET_DEVICE:
1031 val = SCSI_TRY_RESET_DEVICE;
1032 break;
Brian King39120e12008-07-01 13:03:19 -05001033 case SG_SCSI_RESET_TARGET:
1034 val = SCSI_TRY_RESET_TARGET;
1035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 case SG_SCSI_RESET_BUS:
1037 val = SCSI_TRY_RESET_BUS;
1038 break;
1039 case SG_SCSI_RESET_HOST:
1040 val = SCSI_TRY_RESET_HOST;
1041 break;
1042 default:
1043 return -EINVAL;
1044 }
1045 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1046 return -EACCES;
1047 return (scsi_reset_provider(sdp->device, val) ==
1048 SUCCESS) ? 0 : -EIO;
1049 case SCSI_IOCTL_SEND_COMMAND:
1050 if (sdp->detached)
1051 return -ENODEV;
1052 if (read_only) {
1053 unsigned char opcode = WRITE_6;
1054 Scsi_Ioctl_Command __user *siocp = p;
1055
1056 if (copy_from_user(&opcode, siocp->data, 1))
1057 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001058 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EPERM;
1060 }
Al Viroe915e872008-09-02 17:16:41 -04001061 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 case SG_SET_DEBUG:
1063 result = get_user(val, ip);
1064 if (result)
1065 return result;
1066 sdp->sgdebug = (char) val;
1067 return 0;
1068 case SCSI_IOCTL_GET_IDLUN:
1069 case SCSI_IOCTL_GET_BUS_NUMBER:
1070 case SCSI_IOCTL_PROBE_HOST:
1071 case SG_GET_TRANSFORM:
1072 if (sdp->detached)
1073 return -ENODEV;
1074 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001075 case BLKSECTGET:
1076 return put_user(sdp->device->request_queue->max_sectors * 512,
1077 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001078 case BLKTRACESETUP:
1079 return blk_trace_setup(sdp->device->request_queue,
1080 sdp->disk->disk_name,
Martin Peschke76e3a192009-01-30 15:46:23 +01001081 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
Christof Schmitt6da127a2008-01-11 10:09:43 +01001082 (char *)arg);
1083 case BLKTRACESTART:
1084 return blk_trace_startstop(sdp->device->request_queue, 1);
1085 case BLKTRACESTOP:
1086 return blk_trace_startstop(sdp->device->request_queue, 0);
1087 case BLKTRACETEARDOWN:
1088 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 default:
1090 if (read_only)
1091 return -EPERM; /* don't know so take safe approach */
1092 return scsi_ioctl(sdp->device, cmd_in, p);
1093 }
1094}
1095
1096#ifdef CONFIG_COMPAT
1097static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1098{
1099 Sg_device *sdp;
1100 Sg_fd *sfp;
1101 struct scsi_device *sdev;
1102
1103 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1104 return -ENXIO;
1105
1106 sdev = sdp->device;
1107 if (sdev->host->hostt->compat_ioctl) {
1108 int ret;
1109
1110 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1111
1112 return ret;
1113 }
1114
1115 return -ENOIOCTLCMD;
1116}
1117#endif
1118
1119static unsigned int
1120sg_poll(struct file *filp, poll_table * wait)
1121{
1122 unsigned int res = 0;
1123 Sg_device *sdp;
1124 Sg_fd *sfp;
1125 Sg_request *srp;
1126 int count = 0;
1127 unsigned long iflags;
1128
1129 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1130 || sfp->closed)
1131 return POLLERR;
1132 poll_wait(filp, &sfp->read_wait, wait);
1133 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1134 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1135 /* if any read waiting, flag it */
1136 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1137 res = POLLIN | POLLRDNORM;
1138 ++count;
1139 }
1140 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1141
1142 if (sdp->detached)
1143 res |= POLLHUP;
1144 else if (!sfp->cmd_q) {
1145 if (0 == count)
1146 res |= POLLOUT | POLLWRNORM;
1147 } else if (count < SG_MAX_QUEUE)
1148 res |= POLLOUT | POLLWRNORM;
1149 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1150 sdp->disk->disk_name, (int) res));
1151 return res;
1152}
1153
1154static int
1155sg_fasync(int fd, struct file *filp, int mode)
1156{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 Sg_device *sdp;
1158 Sg_fd *sfp;
1159
1160 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1161 return -ENXIO;
1162 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1163 sdp->disk->disk_name, mode));
1164
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001165 return fasync_helper(fd, filp, mode, &sfp->async_qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Nick Piggina13ff0b2008-02-07 18:46:06 -08001168static int
1169sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001172 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001174 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001177 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001179 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001181 return VM_FAULT_SIGBUS;
1182 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001184 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001185 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1186 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001187 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001188 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001189 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001190 struct page *page = nth_page(rsv_schp->pages[k],
1191 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001192 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001193 vmf->page = page;
1194 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Mike Christied6b10342005-11-08 04:06:41 -06001196 sa += len;
1197 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Mike Christied6b10342005-11-08 04:06:41 -06001199
Nick Piggina13ff0b2008-02-07 18:46:06 -08001200 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001204 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205};
1206
1207static int
1208sg_mmap(struct file *filp, struct vm_area_struct *vma)
1209{
1210 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001211 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001213 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1216 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001217 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1219 (void *) vma->vm_start, (int) req_sz));
1220 if (vma->vm_pgoff)
1221 return -EINVAL; /* want no offset */
1222 rsv_schp = &sfp->reserve;
1223 if (req_sz > rsv_schp->bufflen)
1224 return -ENOMEM; /* cannot map more than reserved buffer */
1225
Mike Christied6b10342005-11-08 04:06:41 -06001226 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001227 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1228 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001229 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001230 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001231 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Mike Christied6b10342005-11-08 04:06:41 -06001233
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001234 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001235 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 vma->vm_private_data = sfp;
1237 vma->vm_ops = &sg_mmap_vm_ops;
1238 return 0;
1239}
1240
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001241/*
1242 * This function is a "bottom half" handler that is called by the mid
1243 * level when a command is completed (or has failed).
1244 */
1245static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001247 struct sg_request *srp = rq->end_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 Sg_device *sdp = NULL;
1249 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001251 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001252 char *sense;
1253 int result, resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (NULL == srp) {
1256 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return;
1258 }
1259 sfp = srp->parentfp;
1260 if (sfp)
1261 sdp = sfp->parentdp;
1262 if ((NULL == sdp) || sdp->detached) {
1263 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return;
1265 }
1266
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001267 sense = rq->sense;
1268 result = rq->errors;
1269 resid = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001272 sdp->disk->disk_name, srp->header.pack_id, result));
1273 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001274 ms = jiffies_to_msecs(jiffies);
1275 srp->header.duration = (ms > srp->header.duration) ?
1276 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001277 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct scsi_sense_hdr sshdr;
1279
Mike Christied6b10342005-11-08 04:06:41 -06001280 srp->header.status = 0xff & result;
1281 srp->header.masked_status = status_byte(result);
1282 srp->header.msg_status = msg_byte(result);
1283 srp->header.host_status = host_byte(result);
1284 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if ((sdp->sgdebug > 0) &&
1286 ((CHECK_CONDITION == srp->header.masked_status) ||
1287 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001288 __scsi_print_sense("sg_cmd_done", sense,
1289 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001292 if (driver_byte(result) != 0
1293 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 && !scsi_sense_is_deferred(&sshdr)
1295 && sshdr.sense_key == UNIT_ATTENTION
1296 && sdp->device->removable) {
1297 /* Detected possible disc change. Set the bit - this */
1298 /* may be used if there are filesystems using this device */
1299 sdp->device->changed = 1;
1300 }
1301 }
1302 /* Rely on write phase to clean out srp status values, so no "else" */
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (sfp->closed) { /* whoops this fd already released, cleanup */
1305 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1306 sg_finish_rem_req(srp);
1307 srp = NULL;
1308 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001309 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1311 scsi_device_put(sdp->device);
1312 }
1313 sfp = NULL;
1314 }
1315 } else if (srp && srp->orphan) {
1316 if (sfp->keep_orphan)
1317 srp->sg_io_owned = 0;
1318 else {
1319 sg_finish_rem_req(srp);
1320 srp = NULL;
1321 }
1322 }
1323 if (sfp && srp) {
1324 /* Now wake up any sg_read() that is waiting for this packet. */
1325 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1326 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1327 srp->done = 1;
1328 wake_up_interruptible(&sfp->read_wait);
1329 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1330 }
1331}
1332
1333static struct file_operations sg_fops = {
1334 .owner = THIS_MODULE,
1335 .read = sg_read,
1336 .write = sg_write,
1337 .poll = sg_poll,
1338 .ioctl = sg_ioctl,
1339#ifdef CONFIG_COMPAT
1340 .compat_ioctl = sg_compat_ioctl,
1341#endif
1342 .open = sg_open,
1343 .mmap = sg_mmap,
1344 .release = sg_release,
1345 .fasync = sg_fasync,
1346};
1347
gregkh@suse.ded2538782005-03-23 09:55:22 -08001348static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350static int sg_sysfs_valid = 0;
1351
James Bottomley7c07d612007-08-05 13:36:11 -05001352static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Mike Christied6b10342005-11-08 04:06:41 -06001354 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 Sg_device *sdp;
1356 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001357 int error;
1358 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Jes Sorensen24669f752006-01-16 10:31:18 -05001360 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!sdp) {
1362 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001363 return ERR_PTR(-ENOMEM);
1364 }
1365 error = -ENOMEM;
1366 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1367 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
James Bottomley7c07d612007-08-05 13:36:11 -05001371 write_lock_irqsave(&sg_index_lock, iflags);
1372 error = idr_get_new(&sg_index_idr, sdp, &k);
1373 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
James Bottomley7c07d612007-08-05 13:36:11 -05001375 if (error) {
1376 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1377 error);
1378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (unlikely(k >= SG_MAX_DEVS))
1382 goto overflow;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1385 sprintf(disk->disk_name, "sg%d", k);
1386 disk->first_minor = k;
1387 sdp->disk = disk;
1388 sdp->device = scsidp;
1389 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001390 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001391 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
James Bottomley7c07d612007-08-05 13:36:11 -05001393 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001395 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001397 return ERR_PTR(error);
1398 }
1399 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001402 sdev_printk(KERN_WARNING, scsidp,
1403 "Unable to attach sg device type=%d, minor "
1404 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 error = -ENODEV;
1406 goto out;
1407}
1408
1409static int
Tony Jonesee959b02008-02-22 00:13:36 +01001410sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Tony Jonesee959b02008-02-22 00:13:36 +01001412 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 struct gendisk *disk;
1414 Sg_device *sdp = NULL;
1415 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001416 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001417 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 disk = alloc_disk(1);
1420 if (!disk) {
1421 printk(KERN_WARNING "alloc_disk failed\n");
1422 return -ENOMEM;
1423 }
1424 disk->major = SCSI_GENERIC_MAJOR;
1425
1426 error = -ENOMEM;
1427 cdev = cdev_alloc();
1428 if (!cdev) {
1429 printk(KERN_WARNING "cdev_alloc failed\n");
1430 goto out;
1431 }
1432 cdev->owner = THIS_MODULE;
1433 cdev->ops = &sg_fops;
1434
James Bottomley7c07d612007-08-05 13:36:11 -05001435 sdp = sg_alloc(disk, scsidp);
1436 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001438 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto out;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
James Bottomley7c07d612007-08-05 13:36:11 -05001442 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001443 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001444 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 sdp->cdev = cdev;
1447 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001448 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Greg Kroah-Hartmand73a1a672008-07-21 20:03:34 -07001450 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1451 MKDEV(SCSI_GENERIC_MAJOR,
1452 sdp->index),
1453 sdp, "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001454 if (IS_ERR(sg_class_member)) {
1455 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001456 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001457 error = PTR_ERR(sg_class_member);
1458 goto cdev_add_err;
1459 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001460 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 &sg_class_member->kobj, "generic");
1462 if (error)
1463 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001464 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001466 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
James Bottomley9ccfc752005-10-02 11:45:08 -05001468 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001469 "Attached scsi generic sg%d type %d\n", sdp->index,
1470 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Tony Jonesee959b02008-02-22 00:13:36 +01001472 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001476cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001477 write_lock_irqsave(&sg_index_lock, iflags);
1478 idr_remove(&sg_index_idr, sdp->index);
1479 write_unlock_irqrestore(&sg_index_lock, iflags);
1480 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482out:
1483 put_disk(disk);
1484 if (cdev)
1485 cdev_del(cdev);
1486 return error;
1487}
1488
1489static void
Tony Jonesee959b02008-02-22 00:13:36 +01001490sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Tony Jonesee959b02008-02-22 00:13:36 +01001492 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1493 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned long iflags;
1495 Sg_fd *sfp;
1496 Sg_fd *tsfp;
1497 Sg_request *srp;
1498 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001499 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
James Bottomley7c07d612007-08-05 13:36:11 -05001501 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
James Bottomley7c07d612007-08-05 13:36:11 -05001504 delay = 0;
1505 write_lock_irqsave(&sg_index_lock, iflags);
1506 if (sdp->headfp) {
1507 sdp->detached = 1;
1508 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1509 tsfp = sfp->nextfp;
1510 for (srp = sfp->headrp; srp; srp = tsrp) {
1511 tsrp = srp->nextrp;
1512 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1513 sg_finish_rem_req(srp);
1514 }
1515 if (sfp->closed) {
1516 scsi_device_put(sdp->device);
1517 __sg_remove_sfp(sdp, sfp);
1518 } else {
1519 delay = 1;
1520 wake_up_interruptible(&sfp->read_wait);
1521 kill_fasync(&sfp->async_qp, SIGPOLL,
1522 POLL_HUP);
1523 }
1524 }
1525 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1526 if (NULL == sdp->headfp) {
1527 idr_remove(&sg_index_idr, sdp->index);
1528 }
1529 } else { /* nothing active, simple case */
1530 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1531 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
James Bottomley7c07d612007-08-05 13:36:11 -05001533 write_unlock_irqrestore(&sg_index_lock, iflags);
1534
1535 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001536 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001537 cdev_del(sdp->cdev);
1538 sdp->cdev = NULL;
1539 put_disk(sdp->disk);
1540 sdp->disk = NULL;
1541 if (NULL == sdp->headfp)
1542 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (delay)
1545 msleep(10); /* dirty detach so delay device destruction */
1546}
1547
Douglas Gilbert6460e752006-09-20 18:20:49 -04001548module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1549module_param_named(def_reserved_size, def_reserved_size, int,
1550 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1552
1553MODULE_AUTHOR("Douglas Gilbert");
1554MODULE_DESCRIPTION("SCSI generic (sg) driver");
1555MODULE_LICENSE("GPL");
1556MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001557MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Douglas Gilbert6460e752006-09-20 18:20:49 -04001559MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1560 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1562MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1563
1564static int __init
1565init_sg(void)
1566{
1567 int rc;
1568
Douglas Gilbert6460e752006-09-20 18:20:49 -04001569 if (scatter_elem_sz < PAGE_SIZE) {
1570 scatter_elem_sz = PAGE_SIZE;
1571 scatter_elem_sz_prev = scatter_elem_sz;
1572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (def_reserved_size >= 0)
1574 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001575 else
1576 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1579 SG_MAX_DEVS, "sg");
1580 if (rc)
1581 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001582 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if ( IS_ERR(sg_sysfs_class) ) {
1584 rc = PTR_ERR(sg_sysfs_class);
1585 goto err_out;
1586 }
1587 sg_sysfs_valid = 1;
1588 rc = scsi_register_interface(&sg_interface);
1589 if (0 == rc) {
1590#ifdef CONFIG_SCSI_PROC_FS
1591 sg_proc_init();
1592#endif /* CONFIG_SCSI_PROC_FS */
1593 return 0;
1594 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001595 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596err_out:
1597 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1598 return rc;
1599}
1600
1601static void __exit
1602exit_sg(void)
1603{
1604#ifdef CONFIG_SCSI_PROC_FS
1605 sg_proc_cleanup();
1606#endif /* CONFIG_SCSI_PROC_FS */
1607 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001608 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 sg_sysfs_valid = 0;
1610 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1611 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001612 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001615static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001616{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001617 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001618 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001619 Sg_fd *sfp = srp->parentfp;
1620 sg_io_hdr_t *hp = &srp->header;
1621 int dxfer_len = (int) hp->dxfer_len;
1622 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001623 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001624 Sg_scatter_hold *req_schp = &srp->data;
1625 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1626 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001627 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001628 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1629
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001630 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1631 dxfer_len));
1632
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001633 rq = blk_get_request(q, rw, GFP_ATOMIC);
1634 if (!rq)
1635 return -ENOMEM;
1636
1637 memcpy(rq->cmd, cmd, hp->cmd_len);
1638
1639 rq->cmd_len = hp->cmd_len;
1640 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1641
1642 srp->rq = rq;
1643 rq->end_io_data = srp;
1644 rq->sense = srp->sense_b;
1645 rq->retries = SG_DEFAULT_RETRIES;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001648 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001649
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001650 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1651 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1652 !sfp->parentdp->device->host->unchecked_isa_dma &&
FUJITA Tomonori01cfcdd2008-08-28 15:05:59 +09001653 blk_rq_aligned(q, hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001654 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001655 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001656 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001657
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001658 if (md) {
1659 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1660 sg_link_reserve(sfp, srp, dxfer_len);
1661 else {
1662 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1663 if (res)
1664 return res;
1665 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001666
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001667 md->pages = req_schp->pages;
1668 md->page_order = req_schp->page_order;
1669 md->nr_entries = req_schp->k_use_sg;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001670 md->offset = 0;
FUJITA Tomonori97ae77a2008-12-18 14:49:38 +09001671 md->null_mapped = hp->dxferp ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001673
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001674 if (iov_count)
1675 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1676 hp->dxfer_len, GFP_ATOMIC);
1677 else
1678 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1679 hp->dxfer_len, GFP_ATOMIC);
1680
1681 if (!res) {
1682 srp->bio = rq->bio;
1683
1684 if (!md) {
1685 req_schp->dio_in_use = 1;
1686 hp->info |= SG_INFO_DIRECT_IO;
1687 }
1688 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001689 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692static void
1693sg_finish_rem_req(Sg_request * srp)
1694{
1695 Sg_fd *sfp = srp->parentfp;
1696 Sg_scatter_hold *req_schp = &srp->data;
1697
1698 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1699 if (srp->res_used)
1700 sg_unlink_reserve(sfp, srp);
1701 else
1702 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001703
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001704 if (srp->rq) {
1705 if (srp->bio)
1706 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001707
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001708 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001709 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 sg_remove_request(sfp, srp);
1712}
1713
1714static int
1715sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1716{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001717 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001718 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001720 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1721 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001724 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static int
1728sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1729{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001730 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001731 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001732 int blk_size = buff_size, order;
1733 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Eric Sesterhennfb119932007-05-23 14:41:36 -07001735 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return -EFAULT;
1737 if (0 == blk_size)
1738 ++blk_size; /* don't know why */
1739/* round request up to next highest SG_SECTOR_SZ byte boundary */
1740 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1741 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1742 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001743
1744 /* N.B. ret_sz carried into this block ... */
1745 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1746 if (mx_sc_elems < 0)
1747 return mx_sc_elems; /* most likely -ENOMEM */
1748
Douglas Gilbert6460e752006-09-20 18:20:49 -04001749 num = scatter_elem_sz;
1750 if (unlikely(num != scatter_elem_sz_prev)) {
1751 if (num < PAGE_SIZE) {
1752 scatter_elem_sz = PAGE_SIZE;
1753 scatter_elem_sz_prev = PAGE_SIZE;
1754 } else
1755 scatter_elem_sz_prev = num;
1756 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001757
1758 if (sfp->low_dma)
1759 gfp_mask |= GFP_DMA;
1760
1761 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1762 gfp_mask |= __GFP_ZERO;
1763
1764 order = get_order(num);
1765retry:
1766 ret_sz = 1 << (PAGE_SHIFT + order);
1767
1768 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1769 k++, rem_sz -= ret_sz) {
1770
Douglas Gilbert6460e752006-09-20 18:20:49 -04001771 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001772 scatter_elem_sz_prev : rem_sz;
1773
1774 schp->pages[k] = alloc_pages(gfp_mask, order);
1775 if (!schp->pages[k])
1776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Douglas Gilbert6460e752006-09-20 18:20:49 -04001778 if (num == scatter_elem_sz_prev) {
1779 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1780 scatter_elem_sz = ret_sz;
1781 scatter_elem_sz_prev = ret_sz;
1782 }
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001785 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1786 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001787 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001789 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001790 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001791 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1792 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001793
1794 schp->bufflen = blk_size;
1795 if (rem_sz > 0) /* must have failed */
1796 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001798out:
1799 for (i = 0; i < k; i++)
1800 __free_pages(schp->pages[k], order);
1801
1802 if (--order >= 0)
1803 goto retry;
1804
1805 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808static void
1809sg_remove_scat(Sg_scatter_hold * schp)
1810{
1811 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001812 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001813 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 int k;
1815
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001816 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001818 "sg_remove_scat: k=%d, pg=0x%p\n",
1819 k, schp->pages[k]));
1820 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001822
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001823 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
Mike Christied6b10342005-11-08 04:06:41 -06001825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 memset(schp, 0, sizeof (*schp));
1827}
1828
1829static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1831{
1832 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001833 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1836 num_read_xfer));
1837 if ((!outp) || (num_read_xfer <= 0))
1838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001840 num = 1 << (PAGE_SHIFT + schp->page_order);
1841 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001842 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001843 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001844 num_read_xfer))
1845 return -EFAULT;
1846 break;
1847 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001848 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001849 num))
1850 return -EFAULT;
1851 num_read_xfer -= num;
1852 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 break;
Mike Christied6b10342005-11-08 04:06:41 -06001854 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Mike Christied6b10342005-11-08 04:06:41 -06001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return 0;
1859}
1860
1861static void
1862sg_build_reserve(Sg_fd * sfp, int req_size)
1863{
1864 Sg_scatter_hold *schp = &sfp->reserve;
1865
1866 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1867 do {
1868 if (req_size < PAGE_SIZE)
1869 req_size = PAGE_SIZE;
1870 if (0 == sg_build_indirect(schp, sfp, req_size))
1871 return;
1872 else
1873 sg_remove_scat(schp);
1874 req_size >>= 1; /* divide by 2 */
1875 } while (req_size > (PAGE_SIZE / 2));
1876}
1877
1878static void
1879sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1880{
1881 Sg_scatter_hold *req_schp = &srp->data;
1882 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001883 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 srp->res_used = 1;
1886 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001887 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001889 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1890 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001891 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001892 req_schp->k_use_sg = k + 1;
1893 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001894 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001895
1896 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001897 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001898 break;
1899 } else
1900 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Mike Christied6b10342005-11-08 04:06:41 -06001902
1903 if (k >= rsv_schp->k_use_sg)
1904 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
1907static void
1908sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1909{
1910 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1913 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 req_schp->k_use_sg = 0;
1915 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001916 req_schp->pages = NULL;
1917 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 req_schp->sglist_len = 0;
1919 sfp->save_scat_len = 0;
1920 srp->res_used = 0;
1921}
1922
1923static Sg_request *
1924sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1925{
1926 Sg_request *resp;
1927 unsigned long iflags;
1928
1929 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1930 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1931 /* look for requests that are ready + not SG_IO owned */
1932 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1933 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1934 resp->done = 2; /* guard against other readers */
1935 break;
1936 }
1937 }
1938 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1939 return resp;
1940}
1941
1942#ifdef CONFIG_SCSI_PROC_FS
1943static Sg_request *
1944sg_get_nth_request(Sg_fd * sfp, int nth)
1945{
1946 Sg_request *resp;
1947 unsigned long iflags;
1948 int k;
1949
1950 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1951 for (k = 0, resp = sfp->headrp; resp && (k < nth);
1952 ++k, resp = resp->nextrp) ;
1953 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1954 return resp;
1955}
1956#endif
1957
1958/* always adds to end of list */
1959static Sg_request *
1960sg_add_request(Sg_fd * sfp)
1961{
1962 int k;
1963 unsigned long iflags;
1964 Sg_request *resp;
1965 Sg_request *rp = sfp->req_arr;
1966
1967 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1968 resp = sfp->headrp;
1969 if (!resp) {
1970 memset(rp, 0, sizeof (Sg_request));
1971 rp->parentfp = sfp;
1972 resp = rp;
1973 sfp->headrp = resp;
1974 } else {
1975 if (0 == sfp->cmd_q)
1976 resp = NULL; /* command queuing disallowed */
1977 else {
1978 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1979 if (!rp->parentfp)
1980 break;
1981 }
1982 if (k < SG_MAX_QUEUE) {
1983 memset(rp, 0, sizeof (Sg_request));
1984 rp->parentfp = sfp;
1985 while (resp->nextrp)
1986 resp = resp->nextrp;
1987 resp->nextrp = rp;
1988 resp = rp;
1989 } else
1990 resp = NULL;
1991 }
1992 }
1993 if (resp) {
1994 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06001995 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1998 return resp;
1999}
2000
2001/* Return of 1 for found; 0 for not found */
2002static int
2003sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2004{
2005 Sg_request *prev_rp;
2006 Sg_request *rp;
2007 unsigned long iflags;
2008 int res = 0;
2009
2010 if ((!sfp) || (!srp) || (!sfp->headrp))
2011 return res;
2012 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2013 prev_rp = sfp->headrp;
2014 if (srp == prev_rp) {
2015 sfp->headrp = prev_rp->nextrp;
2016 prev_rp->parentfp = NULL;
2017 res = 1;
2018 } else {
2019 while ((rp = prev_rp->nextrp)) {
2020 if (srp == rp) {
2021 prev_rp->nextrp = rp->nextrp;
2022 rp->parentfp = NULL;
2023 res = 1;
2024 break;
2025 }
2026 prev_rp = rp;
2027 }
2028 }
2029 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2030 return res;
2031}
2032
2033#ifdef CONFIG_SCSI_PROC_FS
2034static Sg_fd *
2035sg_get_nth_sfp(Sg_device * sdp, int nth)
2036{
2037 Sg_fd *resp;
2038 unsigned long iflags;
2039 int k;
2040
James Bottomley7c07d612007-08-05 13:36:11 -05002041 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2043 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002044 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return resp;
2046}
2047#endif
2048
2049static Sg_fd *
2050sg_add_sfp(Sg_device * sdp, int dev)
2051{
2052 Sg_fd *sfp;
2053 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002054 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Mike Christied6b10342005-11-08 04:06:41 -06002056 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (!sfp)
2058 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 init_waitqueue_head(&sfp->read_wait);
2061 rwlock_init(&sfp->rq_list_lock);
2062
2063 sfp->timeout = SG_DEFAULT_TIMEOUT;
2064 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2065 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2066 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2067 sdp->device->host->unchecked_isa_dma : 1;
2068 sfp->cmd_q = SG_DEF_COMMAND_Q;
2069 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2070 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002071 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (!sdp->headfp)
2073 sdp->headfp = sfp;
2074 else { /* add to tail of existing list */
2075 Sg_fd *pfp = sdp->headfp;
2076 while (pfp->nextfp)
2077 pfp = pfp->nextfp;
2078 pfp->nextfp = sfp;
2079 }
James Bottomley7c07d612007-08-05 13:36:11 -05002080 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002082 if (unlikely(sg_big_buff != def_reserved_size))
2083 sg_big_buff = def_reserved_size;
2084
Alan Stern44ec9542007-02-20 11:01:57 -05002085 bufflen = min_t(int, sg_big_buff,
2086 sdp->device->request_queue->max_sectors * 512);
2087 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2089 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2090 return sfp;
2091}
2092
2093static void
2094__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2095{
2096 Sg_fd *fp;
2097 Sg_fd *prev_fp;
2098
2099 prev_fp = sdp->headfp;
2100 if (sfp == prev_fp)
2101 sdp->headfp = prev_fp->nextfp;
2102 else {
2103 while ((fp = prev_fp->nextfp)) {
2104 if (sfp == fp) {
2105 prev_fp->nextfp = fp->nextfp;
2106 break;
2107 }
2108 prev_fp = fp;
2109 }
2110 }
2111 if (sfp->reserve.bufflen > 0) {
2112 SCSI_LOG_TIMEOUT(6,
2113 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2114 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 sg_remove_scat(&sfp->reserve);
2116 }
2117 sfp->parentdp = NULL;
2118 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002119 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122/* Returns 0 in normal case, 1 when detached and sdp object removed */
2123static int
2124sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2125{
2126 Sg_request *srp;
2127 Sg_request *tsrp;
2128 int dirty = 0;
2129 int res = 0;
2130
2131 for (srp = sfp->headrp; srp; srp = tsrp) {
2132 tsrp = srp->nextrp;
2133 if (sg_srp_done(srp, sfp))
2134 sg_finish_rem_req(srp);
2135 else
2136 ++dirty;
2137 }
2138 if (0 == dirty) {
2139 unsigned long iflags;
2140
James Bottomley7c07d612007-08-05 13:36:11 -05002141 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 __sg_remove_sfp(sdp, sfp);
2143 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002144 idr_remove(&sg_index_idr, sdp->index);
2145 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 res = 1;
2147 }
James Bottomley7c07d612007-08-05 13:36:11 -05002148 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 } else {
2150 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2151 /* only bump the access_count if we actually succeeded in
2152 * throwing another counter on the host module */
2153 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2154 sfp->closed = 1; /* flag dirty state on this fd */
2155 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2156 dirty));
2157 }
2158 return res;
2159}
2160
2161static int
2162sg_res_in_use(Sg_fd * sfp)
2163{
2164 const Sg_request *srp;
2165 unsigned long iflags;
2166
2167 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2168 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2169 if (srp->res_used)
2170 break;
2171 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2172 return srp ? 1 : 0;
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175#ifdef CONFIG_SCSI_PROC_FS
2176static int
James Bottomley7c07d612007-08-05 13:36:11 -05002177sg_idr_max_id(int id, void *p, void *data)
2178{
2179 int *k = data;
2180
2181 if (*k < id)
2182 *k = id;
2183
2184 return 0;
2185}
2186
2187static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188sg_last_dev(void)
2189{
Tony Battersby53474c02008-01-22 15:25:49 -05002190 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 unsigned long iflags;
2192
James Bottomley7c07d612007-08-05 13:36:11 -05002193 read_lock_irqsave(&sg_index_lock, iflags);
2194 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2195 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return k + 1; /* origin 1 */
2197}
2198#endif
2199
2200static Sg_device *
2201sg_get_dev(int dev)
2202{
James Bottomley7c07d612007-08-05 13:36:11 -05002203 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 unsigned long iflags;
2205
James Bottomley7c07d612007-08-05 13:36:11 -05002206 read_lock_irqsave(&sg_index_lock, iflags);
2207 sdp = idr_find(&sg_index_idr, dev);
2208 read_unlock_irqrestore(&sg_index_lock, iflags);
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return sdp;
2211}
2212
2213#ifdef CONFIG_SCSI_PROC_FS
2214
2215static struct proc_dir_entry *sg_proc_sgp = NULL;
2216
2217static char sg_proc_sg_dirname[] = "scsi/sg";
2218
2219static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2220
2221static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2222static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2223 size_t count, loff_t *off);
2224static struct file_operations adio_fops = {
2225 /* .owner, .read and .llseek added in sg_proc_init() */
2226 .open = sg_proc_single_open_adio,
2227 .write = sg_proc_write_adio,
2228 .release = single_release,
2229};
2230
2231static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2232static ssize_t sg_proc_write_dressz(struct file *filp,
2233 const char __user *buffer, size_t count, loff_t *off);
2234static struct file_operations dressz_fops = {
2235 .open = sg_proc_single_open_dressz,
2236 .write = sg_proc_write_dressz,
2237 .release = single_release,
2238};
2239
2240static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2241static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2242static struct file_operations version_fops = {
2243 .open = sg_proc_single_open_version,
2244 .release = single_release,
2245};
2246
2247static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2248static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2249static struct file_operations devhdr_fops = {
2250 .open = sg_proc_single_open_devhdr,
2251 .release = single_release,
2252};
2253
2254static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2255static int sg_proc_open_dev(struct inode *inode, struct file *file);
2256static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2257static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2258static void dev_seq_stop(struct seq_file *s, void *v);
2259static struct file_operations dev_fops = {
2260 .open = sg_proc_open_dev,
2261 .release = seq_release,
2262};
2263static struct seq_operations dev_seq_ops = {
2264 .start = dev_seq_start,
2265 .next = dev_seq_next,
2266 .stop = dev_seq_stop,
2267 .show = sg_proc_seq_show_dev,
2268};
2269
2270static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2271static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2272static struct file_operations devstrs_fops = {
2273 .open = sg_proc_open_devstrs,
2274 .release = seq_release,
2275};
2276static struct seq_operations devstrs_seq_ops = {
2277 .start = dev_seq_start,
2278 .next = dev_seq_next,
2279 .stop = dev_seq_stop,
2280 .show = sg_proc_seq_show_devstrs,
2281};
2282
2283static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2284static int sg_proc_open_debug(struct inode *inode, struct file *file);
2285static struct file_operations debug_fops = {
2286 .open = sg_proc_open_debug,
2287 .release = seq_release,
2288};
2289static struct seq_operations debug_seq_ops = {
2290 .start = dev_seq_start,
2291 .next = dev_seq_next,
2292 .stop = dev_seq_stop,
2293 .show = sg_proc_seq_show_debug,
2294};
2295
2296
2297struct sg_proc_leaf {
2298 const char * name;
2299 struct file_operations * fops;
2300};
2301
2302static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2303 {"allow_dio", &adio_fops},
2304 {"debug", &debug_fops},
2305 {"def_reserved_size", &dressz_fops},
2306 {"device_hdr", &devhdr_fops},
2307 {"devices", &dev_fops},
2308 {"device_strs", &devstrs_fops},
2309 {"version", &version_fops}
2310};
2311
2312static int
2313sg_proc_init(void)
2314{
2315 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002316 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct sg_proc_leaf * leaf;
2318
Al Viro66600222005-09-28 22:32:57 +01002319 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (!sg_proc_sgp)
2321 return 1;
2322 for (k = 0; k < num_leaves; ++k) {
2323 leaf = &sg_proc_leaf_arr[k];
2324 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002325 leaf->fops->owner = THIS_MODULE;
2326 leaf->fops->read = seq_read;
2327 leaf->fops->llseek = seq_lseek;
2328 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 return 0;
2331}
2332
2333static void
2334sg_proc_cleanup(void)
2335{
2336 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002337 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 if (!sg_proc_sgp)
2340 return;
2341 for (k = 0; k < num_leaves; ++k)
2342 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2343 remove_proc_entry(sg_proc_sg_dirname, NULL);
2344}
2345
2346
2347static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2348{
2349 seq_printf(s, "%d\n", *((int *)s->private));
2350 return 0;
2351}
2352
2353static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2354{
2355 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2356}
2357
2358static ssize_t
2359sg_proc_write_adio(struct file *filp, const char __user *buffer,
2360 size_t count, loff_t *off)
2361{
2362 int num;
2363 char buff[11];
2364
2365 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2366 return -EACCES;
2367 num = (count < 10) ? count : 10;
2368 if (copy_from_user(buff, buffer, num))
2369 return -EFAULT;
2370 buff[num] = '\0';
2371 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2372 return count;
2373}
2374
2375static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2376{
2377 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2378}
2379
2380static ssize_t
2381sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2382 size_t count, loff_t *off)
2383{
2384 int num;
2385 unsigned long k = ULONG_MAX;
2386 char buff[11];
2387
2388 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2389 return -EACCES;
2390 num = (count < 10) ? count : 10;
2391 if (copy_from_user(buff, buffer, num))
2392 return -EFAULT;
2393 buff[num] = '\0';
2394 k = simple_strtoul(buff, NULL, 10);
2395 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2396 sg_big_buff = k;
2397 return count;
2398 }
2399 return -ERANGE;
2400}
2401
2402static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2403{
2404 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2405 sg_version_date);
2406 return 0;
2407}
2408
2409static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2410{
2411 return single_open(file, sg_proc_seq_show_version, NULL);
2412}
2413
2414static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2415{
2416 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2417 "online\n");
2418 return 0;
2419}
2420
2421static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2422{
2423 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2424}
2425
2426struct sg_proc_deviter {
2427 loff_t index;
2428 size_t max;
2429};
2430
2431static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2432{
2433 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2434
Jan Blunck729d70f2005-08-27 11:07:52 -07002435 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (! it)
2437 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 it->index = *pos;
2440 it->max = sg_last_dev();
2441 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002442 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2447{
Jan Blunck729d70f2005-08-27 11:07:52 -07002448 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 *pos = ++it->index;
2451 return (it->index < it->max) ? it : NULL;
2452}
2453
2454static void dev_seq_stop(struct seq_file *s, void *v)
2455{
Jan Blunck729d70f2005-08-27 11:07:52 -07002456 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
2459static int sg_proc_open_dev(struct inode *inode, struct file *file)
2460{
2461 return seq_open(file, &dev_seq_ops);
2462}
2463
2464static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2465{
2466 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2467 Sg_device *sdp;
2468 struct scsi_device *scsidp;
2469
2470 sdp = it ? sg_get_dev(it->index) : NULL;
2471 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2472 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2473 scsidp->host->host_no, scsidp->channel,
2474 scsidp->id, scsidp->lun, (int) scsidp->type,
2475 1,
2476 (int) scsidp->queue_depth,
2477 (int) scsidp->device_busy,
2478 (int) scsi_device_online(scsidp));
2479 else
2480 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2481 return 0;
2482}
2483
2484static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2485{
2486 return seq_open(file, &devstrs_seq_ops);
2487}
2488
2489static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2490{
2491 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2492 Sg_device *sdp;
2493 struct scsi_device *scsidp;
2494
2495 sdp = it ? sg_get_dev(it->index) : NULL;
2496 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2497 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2498 scsidp->vendor, scsidp->model, scsidp->rev);
2499 else
2500 seq_printf(s, "<no active device>\n");
2501 return 0;
2502}
2503
2504static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2505{
2506 int k, m, new_interface, blen, usg;
2507 Sg_request *srp;
2508 Sg_fd *fp;
2509 const sg_io_hdr_t *hp;
2510 const char * cp;
cb59e842005-04-02 13:51:23 -06002511 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2514 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2515 "(res)sgat=%d low_dma=%d\n", k + 1,
2516 jiffies_to_msecs(fp->timeout),
2517 fp->reserve.bufflen,
2518 (int) fp->reserve.k_use_sg,
2519 (int) fp->low_dma);
2520 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2521 (int) fp->cmd_q, (int) fp->force_packid,
2522 (int) fp->keep_orphan, (int) fp->closed);
2523 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2524 hp = &srp->header;
2525 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2526 if (srp->res_used) {
2527 if (new_interface &&
2528 (SG_FLAG_MMAP_IO & hp->flags))
2529 cp = " mmap>> ";
2530 else
2531 cp = " rb>> ";
2532 } else {
2533 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2534 cp = " dio>> ";
2535 else
2536 cp = " ";
2537 }
2538 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002539 blen = srp->data.bufflen;
2540 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 seq_printf(s, srp->done ?
2542 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002543 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 seq_printf(s, " id=%d blen=%d",
2545 srp->header.pack_id, blen);
2546 if (srp->done)
2547 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002548 else {
2549 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002551 (new_interface ? hp->timeout :
2552 jiffies_to_msecs(fp->timeout)),
2553 (ms > hp->duration ? ms - hp->duration : 0));
2554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2556 (int) srp->data.cmd_opcode);
2557 }
2558 if (0 == m)
2559 seq_printf(s, " No requests active\n");
2560 }
2561}
2562
2563static int sg_proc_open_debug(struct inode *inode, struct file *file)
2564{
2565 return seq_open(file, &debug_seq_ops);
2566}
2567
2568static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2569{
2570 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2571 Sg_device *sdp;
2572
2573 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002574 seq_printf(s, "max_active_device=%d(origin 1)\n",
2575 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2577 }
2578 sdp = it ? sg_get_dev(it->index) : NULL;
2579 if (sdp) {
2580 struct scsi_device *scsidp = sdp->device;
2581
2582 if (NULL == scsidp) {
2583 seq_printf(s, "device %d detached ??\n",
2584 (int)it->index);
2585 return 0;
2586 }
2587
2588 if (sg_get_nth_sfp(sdp, 0)) {
2589 seq_printf(s, " >>> device=%s ",
2590 sdp->disk->disk_name);
2591 if (sdp->detached)
2592 seq_printf(s, "detached pending close ");
2593 else
2594 seq_printf
2595 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2596 scsidp->host->host_no,
2597 scsidp->channel, scsidp->id,
2598 scsidp->lun,
2599 scsidp->host->hostt->emulated);
2600 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2601 sdp->sg_tablesize, sdp->exclude);
2602 }
2603 sg_proc_debug_helper(s, sdp);
2604 }
2605 return 0;
2606}
2607
2608#endif /* CONFIG_SCSI_PROC_FS */
2609
2610module_init(init_sg);
2611module_exit(exit_sg);