blob: 661f9f21650a57f6361650ec5b685bb01d595da9 [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>
David Hardeman378f0582005-09-17 17:55:31 +100050#include <linux/scatterlist.h>
Christof Schmitt6da127a2008-01-11 10:09:43 +010051#include <linux/blktrace_api.h>
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -060052#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "scsi.h"
db9dff32005-04-03 14:53:59 -050055#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <scsi/scsi_driver.h>
58#include <scsi/scsi_ioctl.h>
59#include <scsi/sg.h>
60
61#include "scsi_logging.h"
62
63#ifdef CONFIG_SCSI_PROC_FS
64#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040065static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int sg_proc_init(void);
68static void sg_proc_cleanup(void);
69#endif
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SG_ALLOW_DIO_DEF 0
72#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
73
74#define SG_MAX_DEVS 32768
75
76/*
77 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
78 * Then when using 32 bit integers x * m may overflow during the calculation.
79 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
80 * calculates the same, but prevents the overflow when both m and d
81 * are "small" numbers (like HZ and USER_HZ).
82 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
83 * in 32 bits.
84 */
85#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
86
87#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
88
89int sg_big_buff = SG_DEF_RESERVED_SIZE;
90/* N.B. This variable is readable and writeable via
91 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
92 of this size (or less if there is not enough memory) will be reserved
93 for use by this file descriptor. [Deprecated usage: this variable is also
94 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
95 the kernel (i.e. it is not a module).] */
96static int def_reserved_size = -1; /* picks up init parameter */
97static int sg_allow_dio = SG_ALLOW_DIO_DEF;
98
Douglas Gilbert6460e752006-09-20 18:20:49 -040099static int scatter_elem_sz = SG_SCATTER_SZ;
100static int scatter_elem_sz_prev = SG_SCATTER_SZ;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define SG_SECTOR_SZ 512
103#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
104
Tony Jonesee959b02008-02-22 00:13:36 +0100105static int sg_add(struct device *, struct class_interface *);
106static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
James Bottomley7c07d612007-08-05 13:36:11 -0500108static DEFINE_IDR(sg_index_idr);
109static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 file descriptor list for device */
111
112static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100113 .add_dev = sg_add,
114 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
118 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900119 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned bufflen; /* Size of (aggregate) data buffer */
121 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
Mike Christied6b10342005-11-08 04:06:41 -0600122 struct scatterlist *buffer;/* scatter list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
124 unsigned char cmd_opcode; /* first byte of command */
125} Sg_scatter_hold;
126
127struct sg_device; /* forward declarations */
128struct sg_fd;
129
130typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct sg_request *nextrp; /* NULL -> tail request (slist) */
132 struct sg_fd *parentfp; /* NULL -> not in use */
133 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
134 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600135 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
137 char orphan; /* 1 -> drop on sight, 0 -> normal */
138 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
139 volatile char done; /* 0->before bh, 1->before read, 2->read */
140} 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 */
178static void sg_cmd_done(void *data, char *sense, int result, int resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int sg_start_req(Sg_request * srp);
180static 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);
191static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
192 int wr_xf, int *countp, unsigned char __user **up);
193static int sg_write_xfer(Sg_request * srp);
194static int sg_read_xfer(Sg_request * srp);
195static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196static void sg_remove_scat(Sg_scatter_hold * schp);
197static void sg_build_reserve(Sg_fd * sfp, int req_size);
198static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600200static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
201static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
203static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
205static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
206static Sg_request *sg_add_request(Sg_fd * sfp);
207static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
208static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
210static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef CONFIG_SCSI_PROC_FS
212static int sg_last_dev(void);
213#endif
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#define SZ_SG_HEADER sizeof(struct sg_header)
216#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
217#define SZ_SG_IOVEC sizeof(sg_iovec_t)
218#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
219
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900220static int sg_allow_access(struct file *filp, unsigned char *cmd)
221{
222 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
223 struct request_queue *q = sfp->parentdp->device->request_queue;
224
225 if (sfp->parentdp->device->type == TYPE_SCANNER)
226 return 0;
227
228 return blk_verify_command(&q->cmd_filter,
229 cmd, filp->f_mode & FMODE_WRITE);
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int
233sg_open(struct inode *inode, struct file *filp)
234{
235 int dev = iminor(inode);
236 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600237 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 Sg_device *sdp;
239 Sg_fd *sfp;
240 int res;
241 int retval;
242
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600243 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 nonseekable_open(inode, filp);
245 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
246 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600247 if ((!sdp) || (!sdp->device)) {
248 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600250 }
251 if (sdp->detached) {
252 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* This driver's module count bumped by fops_get in <linux/fs.h> */
257 /* Prevent the device driver from vanishing while we sleep */
258 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600259 if (retval) {
260 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (!((flags & O_NONBLOCK) ||
265 scsi_block_when_processing_errors(sdp->device))) {
266 retval = -ENXIO;
267 /* we are in error recovery for this device */
268 goto error_out;
269 }
270
271 if (flags & O_EXCL) {
272 if (O_RDONLY == (flags & O_ACCMODE)) {
273 retval = -EPERM; /* Can't lock it with read only access */
274 goto error_out;
275 }
276 if (sdp->headfp && (flags & O_NONBLOCK)) {
277 retval = -EBUSY;
278 goto error_out;
279 }
280 res = 0;
281 __wait_event_interruptible(sdp->o_excl_wait,
282 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
283 if (res) {
284 retval = res; /* -ERESTARTSYS because signal hit process */
285 goto error_out;
286 }
287 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
288 if (flags & O_NONBLOCK) {
289 retval = -EBUSY;
290 goto error_out;
291 }
292 res = 0;
293 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
294 res);
295 if (res) {
296 retval = res; /* -ERESTARTSYS because signal hit process */
297 goto error_out;
298 }
299 }
300 if (sdp->detached) {
301 retval = -ENODEV;
302 goto error_out;
303 }
304 if (!sdp->headfp) { /* no existing opens on this device */
305 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600306 q = sdp->device->request_queue;
307 sdp->sg_tablesize = min(q->max_hw_segments,
308 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 if ((sfp = sg_add_sfp(sdp, dev)))
311 filp->private_data = sfp;
312 else {
313 if (flags & O_EXCL)
314 sdp->exclude = 0; /* undo if error */
315 retval = -ENOMEM;
316 goto error_out;
317 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600318 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
320
321 error_out:
322 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600323 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return retval;
325}
326
327/* Following function was formerly called 'sg_close' */
328static int
329sg_release(struct inode *inode, struct file *filp)
330{
331 Sg_device *sdp;
332 Sg_fd *sfp;
333
334 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
335 return -ENXIO;
336 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
337 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
338 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
339 if (!sdp->detached) {
340 scsi_device_put(sdp->device);
341 }
342 sdp->exclude = 0;
343 wake_up_interruptible(&sdp->o_excl_wait);
344 }
345 return 0;
346}
347
348static ssize_t
349sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 Sg_device *sdp;
352 Sg_fd *sfp;
353 Sg_request *srp;
354 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600356 struct sg_header *old_hdr = NULL;
357 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
360 return -ENXIO;
361 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
362 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!access_ok(VERIFY_WRITE, buf, count))
365 return -EFAULT;
366 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600367 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
368 if (!old_hdr)
369 return -ENOMEM;
370 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
371 retval = -EFAULT;
372 goto free_old_hdr;
373 }
374 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600376 sg_io_hdr_t *new_hdr;
377 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
378 if (!new_hdr) {
379 retval = -ENOMEM;
380 goto free_old_hdr;
381 }
382 retval =__copy_from_user
383 (new_hdr, buf, SZ_SG_IO_HDR);
384 req_pack_id = new_hdr->pack_id;
385 kfree(new_hdr);
386 if (retval) {
387 retval = -EFAULT;
388 goto free_old_hdr;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 } else
cb59e842005-04-02 13:51:23 -0600392 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 srp = sg_get_rq_mark(sfp, req_pack_id);
395 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600396 if (sdp->detached) {
397 retval = -ENODEV;
398 goto free_old_hdr;
399 }
400 if (filp->f_flags & O_NONBLOCK) {
401 retval = -EAGAIN;
402 goto free_old_hdr;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 while (1) {
cb59e842005-04-02 13:51:23 -0600405 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600407 (sdp->detached ||
408 (srp = sg_get_rq_mark(sfp, req_pack_id))),
409 retval);
410 if (sdp->detached) {
411 retval = -ENODEV;
412 goto free_old_hdr;
413 }
414 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
cb59e842005-04-02 13:51:23 -0600416
417 /* -ERESTARTSYS as signal hit process */
418 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
cb59e842005-04-02 13:51:23 -0600421 if (srp->header.interface_id != '\0') {
422 retval = sg_new_read(sfp, buf, count, srp);
423 goto free_old_hdr;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600427 if (old_hdr == NULL) {
428 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
429 if (! old_hdr) {
430 retval = -ENOMEM;
431 goto free_old_hdr;
432 }
433 }
434 memset(old_hdr, 0, SZ_SG_HEADER);
435 old_hdr->reply_len = (int) hp->timeout;
436 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
437 old_hdr->pack_id = hp->pack_id;
438 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600440 old_hdr->target_status = hp->masked_status;
441 old_hdr->host_status = hp->host_status;
442 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if ((CHECK_CONDITION & hp->masked_status) ||
444 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600445 memcpy(old_hdr->sense_buffer, srp->sense_b,
446 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 switch (hp->host_status) {
448 /* This setup of 'result' is for backward compatibility and is best
449 ignored by the user who should use target, host + driver status */
450 case DID_OK:
451 case DID_PASSTHROUGH:
452 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600453 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 case DID_NO_CONNECT:
456 case DID_BUS_BUSY:
457 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600458 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 case DID_BAD_TARGET:
461 case DID_ABORT:
462 case DID_PARITY:
463 case DID_RESET:
464 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600465 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600468 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 hp->masked_status == GOOD) ? 0 : EIO;
470 break;
471 default:
cb59e842005-04-02 13:51:23 -0600472 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 }
475
476 /* Now copy the result back to the user buffer. */
477 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600478 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
479 retval = -EFAULT;
480 goto free_old_hdr;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600483 if (count > old_hdr->reply_len)
484 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600486 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
487 retval = -EFAULT;
488 goto free_old_hdr;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 } else
cb59e842005-04-02 13:51:23 -0600492 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600494 retval = count;
495free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800496 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600497 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500static ssize_t
501sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
502{
503 sg_io_hdr_t *hp = &srp->header;
504 int err = 0;
505 int len;
506
507 if (count < SZ_SG_IO_HDR) {
508 err = -EINVAL;
509 goto err_out;
510 }
511 hp->sb_len_wr = 0;
512 if ((hp->mx_sb_len > 0) && hp->sbp) {
513 if ((CHECK_CONDITION & hp->masked_status) ||
514 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600515 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
517 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
518 len = (len > sb_len) ? sb_len : len;
519 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
520 err = -EFAULT;
521 goto err_out;
522 }
523 hp->sb_len_wr = len;
524 }
525 }
526 if (hp->masked_status || hp->host_status || hp->driver_status)
527 hp->info |= SG_INFO_CHECK;
528 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
529 err = -EFAULT;
530 goto err_out;
531 }
532 err = sg_read_xfer(srp);
533 err_out:
534 sg_finish_rem_req(srp);
535 return (0 == err) ? count : err;
536}
537
538static ssize_t
539sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
540{
541 int mxsize, cmd_size, k;
542 int input_size, blocking;
543 unsigned char opcode;
544 Sg_device *sdp;
545 Sg_fd *sfp;
546 Sg_request *srp;
547 struct sg_header old_hdr;
548 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600549 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
552 return -ENXIO;
553 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
554 sdp->disk->disk_name, (int) count));
555 if (sdp->detached)
556 return -ENODEV;
557 if (!((filp->f_flags & O_NONBLOCK) ||
558 scsi_block_when_processing_errors(sdp->device)))
559 return -ENXIO;
560
561 if (!access_ok(VERIFY_READ, buf, count))
562 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
563 if (count < SZ_SG_HEADER)
564 return -EIO;
565 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
566 return -EFAULT;
567 blocking = !(filp->f_flags & O_NONBLOCK);
568 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200569 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (count < (SZ_SG_HEADER + 6))
571 return -EIO; /* The minimum scsi command length is 6 bytes. */
572
573 if (!(srp = sg_add_request(sfp))) {
574 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
575 return -EDOM;
576 }
577 buf += SZ_SG_HEADER;
578 __get_user(opcode, buf);
579 if (sfp->next_cmd_len > 0) {
580 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
581 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
582 sfp->next_cmd_len = 0;
583 sg_remove_request(sfp, srp);
584 return -EIO;
585 }
586 cmd_size = sfp->next_cmd_len;
587 sfp->next_cmd_len = 0; /* reset so only this write() effected */
588 } else {
589 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
590 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
591 cmd_size = 12;
592 }
593 SCSI_LOG_TIMEOUT(4, printk(
594 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
595/* Determine buffer size. */
596 input_size = count - cmd_size;
597 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
598 mxsize -= SZ_SG_HEADER;
599 input_size -= SZ_SG_HEADER;
600 if (input_size < 0) {
601 sg_remove_request(sfp, srp);
602 return -EIO; /* User did not pass enough bytes for this command. */
603 }
604 hp = &srp->header;
605 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
606 hp->cmd_len = (unsigned char) cmd_size;
607 hp->iovec_count = 0;
608 hp->mx_sb_len = 0;
609 if (input_size > 0)
610 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
611 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
612 else
613 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
614 hp->dxfer_len = mxsize;
615 hp->dxferp = (char __user *)buf + cmd_size;
616 hp->sbp = NULL;
617 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
618 hp->flags = input_size; /* structure abuse ... */
619 hp->pack_id = old_hdr.pack_id;
620 hp->usr_ptr = NULL;
621 if (__copy_from_user(cmnd, buf, cmd_size))
622 return -EFAULT;
623 /*
624 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
625 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
626 * is a non-zero input_size, so emit a warning.
627 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100628 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
629 static char cmd[TASK_COMM_LEN];
630 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 printk(KERN_WARNING
632 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
633 "guessing data in;\n" KERN_WARNING " "
634 "program %s not setting count and/or reply_len properly\n",
635 old_hdr.reply_len - (int)SZ_SG_HEADER,
636 input_size, (unsigned int) cmnd[0],
637 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100638 strcpy(cmd, current->comm);
639 }
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
642 return (k < 0) ? k : count;
643}
644
645static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200646sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
647 size_t count, int blocking, int read_only,
648 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 int k;
651 Sg_request *srp;
652 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600653 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int timeout;
655 unsigned long ul_timeout;
656
657 if (count < SZ_SG_IO_HDR)
658 return -EINVAL;
659 if (!access_ok(VERIFY_READ, buf, count))
660 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
661
662 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
663 if (!(srp = sg_add_request(sfp))) {
664 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
665 return -EDOM;
666 }
667 hp = &srp->header;
668 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
669 sg_remove_request(sfp, srp);
670 return -EFAULT;
671 }
672 if (hp->interface_id != 'S') {
673 sg_remove_request(sfp, srp);
674 return -ENOSYS;
675 }
676 if (hp->flags & SG_FLAG_MMAP_IO) {
677 if (hp->dxfer_len > sfp->reserve.bufflen) {
678 sg_remove_request(sfp, srp);
679 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
680 }
681 if (hp->flags & SG_FLAG_DIRECT_IO) {
682 sg_remove_request(sfp, srp);
683 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
684 }
685 if (sg_res_in_use(sfp)) {
686 sg_remove_request(sfp, srp);
687 return -EBUSY; /* reserve buffer already being used */
688 }
689 }
690 ul_timeout = msecs_to_jiffies(srp->header.timeout);
691 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
692 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
693 sg_remove_request(sfp, srp);
694 return -EMSGSIZE;
695 }
696 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
697 sg_remove_request(sfp, srp);
698 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
699 }
700 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
701 sg_remove_request(sfp, srp);
702 return -EFAULT;
703 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900704 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sg_remove_request(sfp, srp);
706 return -EPERM;
707 }
708 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
709 if (k < 0)
710 return k;
711 if (o_srp)
712 *o_srp = srp;
713 return count;
714}
715
716static int
717sg_common_write(Sg_fd * sfp, Sg_request * srp,
718 unsigned char *cmnd, int timeout, int blocking)
719{
Mike Christied6b10342005-11-08 04:06:41 -0600720 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 Sg_device *sdp = sfp->parentdp;
722 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
725 hp->status = 0;
726 hp->masked_status = 0;
727 hp->msg_status = 0;
728 hp->info = 0;
729 hp->host_status = 0;
730 hp->driver_status = 0;
731 hp->resid = 0;
732 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
733 (int) cmnd[0], (int) hp->cmd_len));
734
735 if ((k = sg_start_req(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400736 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 sg_finish_rem_req(srp);
738 return k; /* probably out of space --> ENOMEM */
739 }
740 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400741 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 sg_finish_rem_req(srp);
743 return k;
744 }
745 if (sdp->detached) {
746 sg_finish_rem_req(srp);
747 return -ENODEV;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 switch (hp->dxfer_direction) {
751 case SG_DXFER_TO_FROM_DEV:
752 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600753 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600756 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600759 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 default:
Mike Christied6b10342005-11-08 04:06:41 -0600762 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
764 }
cb59e842005-04-02 13:51:23 -0600765 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/* Now send everything of to mid-level. The next time we hear about this
767 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600768 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600769 hp->dxfer_len, srp->data.k_use_sg, timeout,
770 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
771 GFP_ATOMIC)) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400772 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
Mike Christied6b10342005-11-08 04:06:41 -0600773 /*
774 * most likely out of mem, but could also be a bad map
775 */
Mike Christie18c49b82006-03-22 16:04:38 -0600776 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600777 return -ENOMEM;
778 } else
779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782static int
783sg_srp_done(Sg_request *srp, Sg_fd *sfp)
784{
785 unsigned long iflags;
786 int done;
787
788 read_lock_irqsave(&sfp->rq_list_lock, iflags);
789 done = srp->done;
790 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
791 return done;
792}
793
794static int
795sg_ioctl(struct inode *inode, struct file *filp,
796 unsigned int cmd_in, unsigned long arg)
797{
798 void __user *p = (void __user *)arg;
799 int __user *ip = p;
800 int result, val, read_only;
801 Sg_device *sdp;
802 Sg_fd *sfp;
803 Sg_request *srp;
804 unsigned long iflags;
805
806 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
807 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
810 sdp->disk->disk_name, (int) cmd_in));
811 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
812
813 switch (cmd_in) {
814 case SG_IO:
815 {
816 int blocking = 1; /* ignore O_NONBLOCK flag */
817
818 if (sdp->detached)
819 return -ENODEV;
820 if (!scsi_block_when_processing_errors(sdp->device))
821 return -ENXIO;
822 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
823 return -EFAULT;
824 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200825 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 blocking, read_only, &srp);
827 if (result < 0)
828 return result;
829 srp->sg_io_owned = 1;
830 while (1) {
831 result = 0; /* following macro to beat race condition */
832 __wait_event_interruptible(sfp->read_wait,
833 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
834 result);
835 if (sdp->detached)
836 return -ENODEV;
837 if (sfp->closed)
838 return 0; /* request packet dropped already */
839 if (0 == result)
840 break;
841 srp->orphan = 1;
842 return result; /* -ERESTARTSYS because signal hit process */
843 }
844 write_lock_irqsave(&sfp->rq_list_lock, iflags);
845 srp->done = 2;
846 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
847 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
848 return (result < 0) ? result : 0;
849 }
850 case SG_SET_TIMEOUT:
851 result = get_user(val, ip);
852 if (result)
853 return result;
854 if (val < 0)
855 return -EIO;
856 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
857 val = MULDIV (INT_MAX, USER_HZ, HZ);
858 sfp->timeout_user = val;
859 sfp->timeout = MULDIV (val, HZ, USER_HZ);
860
861 return 0;
862 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
863 /* strange ..., for backward compatibility */
864 return sfp->timeout_user;
865 case SG_SET_FORCE_LOW_DMA:
866 result = get_user(val, ip);
867 if (result)
868 return result;
869 if (val) {
870 sfp->low_dma = 1;
871 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
872 val = (int) sfp->reserve.bufflen;
873 sg_remove_scat(&sfp->reserve);
874 sg_build_reserve(sfp, val);
875 }
876 } else {
877 if (sdp->detached)
878 return -ENODEV;
879 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
880 }
881 return 0;
882 case SG_GET_LOW_DMA:
883 return put_user((int) sfp->low_dma, ip);
884 case SG_GET_SCSI_ID:
885 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
886 return -EFAULT;
887 else {
888 sg_scsi_id_t __user *sg_idp = p;
889
890 if (sdp->detached)
891 return -ENODEV;
892 __put_user((int) sdp->device->host->host_no,
893 &sg_idp->host_no);
894 __put_user((int) sdp->device->channel,
895 &sg_idp->channel);
896 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
897 __put_user((int) sdp->device->lun, &sg_idp->lun);
898 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
899 __put_user((short) sdp->device->host->cmd_per_lun,
900 &sg_idp->h_cmd_per_lun);
901 __put_user((short) sdp->device->queue_depth,
902 &sg_idp->d_queue_depth);
903 __put_user(0, &sg_idp->unused[0]);
904 __put_user(0, &sg_idp->unused[1]);
905 return 0;
906 }
907 case SG_SET_FORCE_PACK_ID:
908 result = get_user(val, ip);
909 if (result)
910 return result;
911 sfp->force_packid = val ? 1 : 0;
912 return 0;
913 case SG_GET_PACK_ID:
914 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
915 return -EFAULT;
916 read_lock_irqsave(&sfp->rq_list_lock, iflags);
917 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
918 if ((1 == srp->done) && (!srp->sg_io_owned)) {
919 read_unlock_irqrestore(&sfp->rq_list_lock,
920 iflags);
921 __put_user(srp->header.pack_id, ip);
922 return 0;
923 }
924 }
925 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
926 __put_user(-1, ip);
927 return 0;
928 case SG_GET_NUM_WAITING:
929 read_lock_irqsave(&sfp->rq_list_lock, iflags);
930 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
931 if ((1 == srp->done) && (!srp->sg_io_owned))
932 ++val;
933 }
934 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
935 return put_user(val, ip);
936 case SG_GET_SG_TABLESIZE:
937 return put_user(sdp->sg_tablesize, ip);
938 case SG_SET_RESERVED_SIZE:
939 result = get_user(val, ip);
940 if (result)
941 return result;
942 if (val < 0)
943 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500944 val = min_t(int, val,
945 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (val != sfp->reserve.bufflen) {
947 if (sg_res_in_use(sfp) || sfp->mmap_called)
948 return -EBUSY;
949 sg_remove_scat(&sfp->reserve);
950 sg_build_reserve(sfp, val);
951 }
952 return 0;
953 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500954 val = min_t(int, sfp->reserve.bufflen,
955 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return put_user(val, ip);
957 case SG_SET_COMMAND_Q:
958 result = get_user(val, ip);
959 if (result)
960 return result;
961 sfp->cmd_q = val ? 1 : 0;
962 return 0;
963 case SG_GET_COMMAND_Q:
964 return put_user((int) sfp->cmd_q, ip);
965 case SG_SET_KEEP_ORPHAN:
966 result = get_user(val, ip);
967 if (result)
968 return result;
969 sfp->keep_orphan = val;
970 return 0;
971 case SG_GET_KEEP_ORPHAN:
972 return put_user((int) sfp->keep_orphan, ip);
973 case SG_NEXT_CMD_LEN:
974 result = get_user(val, ip);
975 if (result)
976 return result;
977 sfp->next_cmd_len = (val > 0) ? val : 0;
978 return 0;
979 case SG_GET_VERSION_NUM:
980 return put_user(sg_version_num, ip);
981 case SG_GET_ACCESS_COUNT:
982 /* faked - we don't have a real access count anymore */
983 val = (sdp->device ? 1 : 0);
984 return put_user(val, ip);
985 case SG_GET_REQUEST_TABLE:
986 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
987 return -EFAULT;
988 else {
cb59e842005-04-02 13:51:23 -0600989 sg_req_info_t *rinfo;
990 unsigned int ms;
991
992 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
993 GFP_KERNEL);
994 if (!rinfo)
995 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 read_lock_irqsave(&sfp->rq_list_lock, iflags);
997 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
998 ++val, srp = srp ? srp->nextrp : srp) {
999 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
1000 if (srp) {
1001 rinfo[val].req_state = srp->done + 1;
1002 rinfo[val].problem =
1003 srp->header.masked_status &
1004 srp->header.host_status &
1005 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -06001006 if (srp->done)
1007 rinfo[val].duration =
1008 srp->header.duration;
1009 else {
1010 ms = jiffies_to_msecs(jiffies);
1011 rinfo[val].duration =
1012 (ms > srp->header.duration) ?
1013 (ms - srp->header.duration) : 0;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001016 rinfo[val].sg_io_owned =
1017 srp->sg_io_owned;
1018 rinfo[val].pack_id =
1019 srp->header.pack_id;
1020 rinfo[val].usr_ptr =
1021 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 }
1024 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001025 result = __copy_to_user(p, rinfo,
1026 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1027 result = result ? -EFAULT : 0;
1028 kfree(rinfo);
1029 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 case SG_EMULATED_HOST:
1032 if (sdp->detached)
1033 return -ENODEV;
1034 return put_user(sdp->device->host->hostt->emulated, ip);
1035 case SG_SCSI_RESET:
1036 if (sdp->detached)
1037 return -ENODEV;
1038 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001039 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -EBUSY;
1041 } else if (!scsi_block_when_processing_errors(sdp->device))
1042 return -EBUSY;
1043 result = get_user(val, ip);
1044 if (result)
1045 return result;
1046 if (SG_SCSI_RESET_NOTHING == val)
1047 return 0;
1048 switch (val) {
1049 case SG_SCSI_RESET_DEVICE:
1050 val = SCSI_TRY_RESET_DEVICE;
1051 break;
Brian King39120e12008-07-01 13:03:19 -05001052 case SG_SCSI_RESET_TARGET:
1053 val = SCSI_TRY_RESET_TARGET;
1054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 case SG_SCSI_RESET_BUS:
1056 val = SCSI_TRY_RESET_BUS;
1057 break;
1058 case SG_SCSI_RESET_HOST:
1059 val = SCSI_TRY_RESET_HOST;
1060 break;
1061 default:
1062 return -EINVAL;
1063 }
1064 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1065 return -EACCES;
1066 return (scsi_reset_provider(sdp->device, val) ==
1067 SUCCESS) ? 0 : -EIO;
1068 case SCSI_IOCTL_SEND_COMMAND:
1069 if (sdp->detached)
1070 return -ENODEV;
1071 if (read_only) {
1072 unsigned char opcode = WRITE_6;
1073 Scsi_Ioctl_Command __user *siocp = p;
1074
1075 if (copy_from_user(&opcode, siocp->data, 1))
1076 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001077 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -EPERM;
1079 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001080 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 case SG_SET_DEBUG:
1082 result = get_user(val, ip);
1083 if (result)
1084 return result;
1085 sdp->sgdebug = (char) val;
1086 return 0;
1087 case SCSI_IOCTL_GET_IDLUN:
1088 case SCSI_IOCTL_GET_BUS_NUMBER:
1089 case SCSI_IOCTL_PROBE_HOST:
1090 case SG_GET_TRANSFORM:
1091 if (sdp->detached)
1092 return -ENODEV;
1093 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001094 case BLKSECTGET:
1095 return put_user(sdp->device->request_queue->max_sectors * 512,
1096 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001097 case BLKTRACESETUP:
1098 return blk_trace_setup(sdp->device->request_queue,
1099 sdp->disk->disk_name,
1100 sdp->device->sdev_gendev.devt,
1101 (char *)arg);
1102 case BLKTRACESTART:
1103 return blk_trace_startstop(sdp->device->request_queue, 1);
1104 case BLKTRACESTOP:
1105 return blk_trace_startstop(sdp->device->request_queue, 0);
1106 case BLKTRACETEARDOWN:
1107 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 default:
1109 if (read_only)
1110 return -EPERM; /* don't know so take safe approach */
1111 return scsi_ioctl(sdp->device, cmd_in, p);
1112 }
1113}
1114
1115#ifdef CONFIG_COMPAT
1116static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1117{
1118 Sg_device *sdp;
1119 Sg_fd *sfp;
1120 struct scsi_device *sdev;
1121
1122 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1123 return -ENXIO;
1124
1125 sdev = sdp->device;
1126 if (sdev->host->hostt->compat_ioctl) {
1127 int ret;
1128
1129 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1130
1131 return ret;
1132 }
1133
1134 return -ENOIOCTLCMD;
1135}
1136#endif
1137
1138static unsigned int
1139sg_poll(struct file *filp, poll_table * wait)
1140{
1141 unsigned int res = 0;
1142 Sg_device *sdp;
1143 Sg_fd *sfp;
1144 Sg_request *srp;
1145 int count = 0;
1146 unsigned long iflags;
1147
1148 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1149 || sfp->closed)
1150 return POLLERR;
1151 poll_wait(filp, &sfp->read_wait, wait);
1152 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1153 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1154 /* if any read waiting, flag it */
1155 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1156 res = POLLIN | POLLRDNORM;
1157 ++count;
1158 }
1159 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1160
1161 if (sdp->detached)
1162 res |= POLLHUP;
1163 else if (!sfp->cmd_q) {
1164 if (0 == count)
1165 res |= POLLOUT | POLLWRNORM;
1166 } else if (count < SG_MAX_QUEUE)
1167 res |= POLLOUT | POLLWRNORM;
1168 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1169 sdp->disk->disk_name, (int) res));
1170 return res;
1171}
1172
1173static int
1174sg_fasync(int fd, struct file *filp, int mode)
1175{
1176 int retval;
1177 Sg_device *sdp;
1178 Sg_fd *sfp;
1179
1180 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1181 return -ENXIO;
1182 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1183 sdp->disk->disk_name, mode));
1184
1185 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1186 return (retval < 0) ? retval : 0;
1187}
1188
Nick Piggina13ff0b2008-02-07 18:46:06 -08001189static int
1190sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001193 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001195 struct scatterlist *sg;
1196 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001199 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001201 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001203 return VM_FAULT_SIGBUS;
1204 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001206 sg = rsv_schp->buffer;
1207 sa = vma->vm_start;
1208 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001209 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001210 len = vma->vm_end - sa;
1211 len = (len < sg->length) ? len : sg->length;
1212 if (offset < len) {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001213 struct page *page;
Jens Axboe45711f12007-10-22 21:19:53 +02001214 page = virt_to_page(page_address(sg_page(sg)) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001215 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001216 vmf->page = page;
1217 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Mike Christied6b10342005-11-08 04:06:41 -06001219 sa += len;
1220 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Mike Christied6b10342005-11-08 04:06:41 -06001222
Nick Piggina13ff0b2008-02-07 18:46:06 -08001223 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001227 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228};
1229
1230static int
1231sg_mmap(struct file *filp, struct vm_area_struct *vma)
1232{
1233 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001234 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001236 int k;
1237 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1240 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001241 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1243 (void *) vma->vm_start, (int) req_sz));
1244 if (vma->vm_pgoff)
1245 return -EINVAL; /* want no offset */
1246 rsv_schp = &sfp->reserve;
1247 if (req_sz > rsv_schp->bufflen)
1248 return -ENOMEM; /* cannot map more than reserved buffer */
1249
Mike Christied6b10342005-11-08 04:06:41 -06001250 sa = vma->vm_start;
1251 sg = rsv_schp->buffer;
1252 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001253 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001254 len = vma->vm_end - sa;
1255 len = (len < sg->length) ? len : sg->length;
1256 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Mike Christied6b10342005-11-08 04:06:41 -06001258
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001259 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001260 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 vma->vm_private_data = sfp;
1262 vma->vm_ops = &sg_mmap_vm_ops;
1263 return 0;
1264}
1265
1266/* This function is a "bottom half" handler that is called by the
1267 * mid level when a command is completed (or has failed). */
1268static void
Mike Christied6b10342005-11-08 04:06:41 -06001269sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Mike Christied6b10342005-11-08 04:06:41 -06001271 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 Sg_device *sdp = NULL;
1273 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001275 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (NULL == srp) {
1278 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return;
1280 }
1281 sfp = srp->parentfp;
1282 if (sfp)
1283 sdp = sfp->parentdp;
1284 if ((NULL == sdp) || sdp->detached) {
1285 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return;
1287 }
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001291 sdp->disk->disk_name, srp->header.pack_id, result));
1292 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001293 ms = jiffies_to_msecs(jiffies);
1294 srp->header.duration = (ms > srp->header.duration) ?
1295 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001296 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct scsi_sense_hdr sshdr;
1298
Mike Christied6b10342005-11-08 04:06:41 -06001299 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1300 srp->header.status = 0xff & result;
1301 srp->header.masked_status = status_byte(result);
1302 srp->header.msg_status = msg_byte(result);
1303 srp->header.host_status = host_byte(result);
1304 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if ((sdp->sgdebug > 0) &&
1306 ((CHECK_CONDITION == srp->header.masked_status) ||
1307 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001308 __scsi_print_sense("sg_cmd_done", sense,
1309 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001312 if (driver_byte(result) != 0
1313 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 && !scsi_sense_is_deferred(&sshdr)
1315 && sshdr.sense_key == UNIT_ATTENTION
1316 && sdp->device->removable) {
1317 /* Detected possible disc change. Set the bit - this */
1318 /* may be used if there are filesystems using this device */
1319 sdp->device->changed = 1;
1320 }
1321 }
1322 /* Rely on write phase to clean out srp status values, so no "else" */
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (sfp->closed) { /* whoops this fd already released, cleanup */
1325 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1326 sg_finish_rem_req(srp);
1327 srp = NULL;
1328 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001329 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1331 scsi_device_put(sdp->device);
1332 }
1333 sfp = NULL;
1334 }
1335 } else if (srp && srp->orphan) {
1336 if (sfp->keep_orphan)
1337 srp->sg_io_owned = 0;
1338 else {
1339 sg_finish_rem_req(srp);
1340 srp = NULL;
1341 }
1342 }
1343 if (sfp && srp) {
1344 /* Now wake up any sg_read() that is waiting for this packet. */
1345 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1346 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1347 srp->done = 1;
1348 wake_up_interruptible(&sfp->read_wait);
1349 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1350 }
1351}
1352
1353static struct file_operations sg_fops = {
1354 .owner = THIS_MODULE,
1355 .read = sg_read,
1356 .write = sg_write,
1357 .poll = sg_poll,
1358 .ioctl = sg_ioctl,
1359#ifdef CONFIG_COMPAT
1360 .compat_ioctl = sg_compat_ioctl,
1361#endif
1362 .open = sg_open,
1363 .mmap = sg_mmap,
1364 .release = sg_release,
1365 .fasync = sg_fasync,
1366};
1367
gregkh@suse.ded2538782005-03-23 09:55:22 -08001368static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370static int sg_sysfs_valid = 0;
1371
James Bottomley7c07d612007-08-05 13:36:11 -05001372static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Mike Christied6b10342005-11-08 04:06:41 -06001374 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 Sg_device *sdp;
1376 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001377 int error;
1378 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Jes Sorensen24669f752006-01-16 10:31:18 -05001380 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!sdp) {
1382 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001383 return ERR_PTR(-ENOMEM);
1384 }
1385 error = -ENOMEM;
1386 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1387 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
James Bottomley7c07d612007-08-05 13:36:11 -05001391 write_lock_irqsave(&sg_index_lock, iflags);
1392 error = idr_get_new(&sg_index_idr, sdp, &k);
1393 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
James Bottomley7c07d612007-08-05 13:36:11 -05001395 if (error) {
1396 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1397 error);
1398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (unlikely(k >= SG_MAX_DEVS))
1402 goto overflow;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1405 sprintf(disk->disk_name, "sg%d", k);
1406 disk->first_minor = k;
1407 sdp->disk = disk;
1408 sdp->device = scsidp;
1409 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001410 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001411 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
James Bottomley7c07d612007-08-05 13:36:11 -05001413 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001415 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001417 return ERR_PTR(error);
1418 }
1419 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001422 sdev_printk(KERN_WARNING, scsidp,
1423 "Unable to attach sg device type=%d, minor "
1424 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 error = -ENODEV;
1426 goto out;
1427}
1428
1429static int
Tony Jonesee959b02008-02-22 00:13:36 +01001430sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Tony Jonesee959b02008-02-22 00:13:36 +01001432 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct gendisk *disk;
1434 Sg_device *sdp = NULL;
1435 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001436 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001437 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 disk = alloc_disk(1);
1440 if (!disk) {
1441 printk(KERN_WARNING "alloc_disk failed\n");
1442 return -ENOMEM;
1443 }
1444 disk->major = SCSI_GENERIC_MAJOR;
1445
1446 error = -ENOMEM;
1447 cdev = cdev_alloc();
1448 if (!cdev) {
1449 printk(KERN_WARNING "cdev_alloc failed\n");
1450 goto out;
1451 }
1452 cdev->owner = THIS_MODULE;
1453 cdev->ops = &sg_fops;
1454
James Bottomley7c07d612007-08-05 13:36:11 -05001455 sdp = sg_alloc(disk, scsidp);
1456 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001458 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 goto out;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
James Bottomley7c07d612007-08-05 13:36:11 -05001462 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001463 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001464 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 sdp->cdev = cdev;
1467 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001468 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001470 sg_class_member = device_create_drvdata(sg_sysfs_class,
1471 cl_dev->parent,
1472 MKDEV(SCSI_GENERIC_MAJOR,
1473 sdp->index),
1474 sdp,
1475 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001476 if (IS_ERR(sg_class_member)) {
1477 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001478 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001479 error = PTR_ERR(sg_class_member);
1480 goto cdev_add_err;
1481 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001482 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 &sg_class_member->kobj, "generic");
1484 if (error)
1485 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001486 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001488 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
James Bottomley9ccfc752005-10-02 11:45:08 -05001490 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001491 "Attached scsi generic sg%d type %d\n", sdp->index,
1492 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Tony Jonesee959b02008-02-22 00:13:36 +01001494 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return 0;
1497
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001498cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001499 write_lock_irqsave(&sg_index_lock, iflags);
1500 idr_remove(&sg_index_idr, sdp->index);
1501 write_unlock_irqrestore(&sg_index_lock, iflags);
1502 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504out:
1505 put_disk(disk);
1506 if (cdev)
1507 cdev_del(cdev);
1508 return error;
1509}
1510
1511static void
Tony Jonesee959b02008-02-22 00:13:36 +01001512sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Tony Jonesee959b02008-02-22 00:13:36 +01001514 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1515 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned long iflags;
1517 Sg_fd *sfp;
1518 Sg_fd *tsfp;
1519 Sg_request *srp;
1520 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001521 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
James Bottomley7c07d612007-08-05 13:36:11 -05001523 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
James Bottomley7c07d612007-08-05 13:36:11 -05001526 delay = 0;
1527 write_lock_irqsave(&sg_index_lock, iflags);
1528 if (sdp->headfp) {
1529 sdp->detached = 1;
1530 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1531 tsfp = sfp->nextfp;
1532 for (srp = sfp->headrp; srp; srp = tsrp) {
1533 tsrp = srp->nextrp;
1534 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1535 sg_finish_rem_req(srp);
1536 }
1537 if (sfp->closed) {
1538 scsi_device_put(sdp->device);
1539 __sg_remove_sfp(sdp, sfp);
1540 } else {
1541 delay = 1;
1542 wake_up_interruptible(&sfp->read_wait);
1543 kill_fasync(&sfp->async_qp, SIGPOLL,
1544 POLL_HUP);
1545 }
1546 }
1547 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1548 if (NULL == sdp->headfp) {
1549 idr_remove(&sg_index_idr, sdp->index);
1550 }
1551 } else { /* nothing active, simple case */
1552 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1553 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
James Bottomley7c07d612007-08-05 13:36:11 -05001555 write_unlock_irqrestore(&sg_index_lock, iflags);
1556
1557 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001558 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001559 cdev_del(sdp->cdev);
1560 sdp->cdev = NULL;
1561 put_disk(sdp->disk);
1562 sdp->disk = NULL;
1563 if (NULL == sdp->headfp)
1564 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (delay)
1567 msleep(10); /* dirty detach so delay device destruction */
1568}
1569
Douglas Gilbert6460e752006-09-20 18:20:49 -04001570module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1571module_param_named(def_reserved_size, def_reserved_size, int,
1572 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1574
1575MODULE_AUTHOR("Douglas Gilbert");
1576MODULE_DESCRIPTION("SCSI generic (sg) driver");
1577MODULE_LICENSE("GPL");
1578MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001579MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Douglas Gilbert6460e752006-09-20 18:20:49 -04001581MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1582 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1584MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1585
1586static int __init
1587init_sg(void)
1588{
1589 int rc;
1590
Douglas Gilbert6460e752006-09-20 18:20:49 -04001591 if (scatter_elem_sz < PAGE_SIZE) {
1592 scatter_elem_sz = PAGE_SIZE;
1593 scatter_elem_sz_prev = scatter_elem_sz;
1594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (def_reserved_size >= 0)
1596 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001597 else
1598 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1601 SG_MAX_DEVS, "sg");
1602 if (rc)
1603 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001604 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if ( IS_ERR(sg_sysfs_class) ) {
1606 rc = PTR_ERR(sg_sysfs_class);
1607 goto err_out;
1608 }
1609 sg_sysfs_valid = 1;
1610 rc = scsi_register_interface(&sg_interface);
1611 if (0 == rc) {
1612#ifdef CONFIG_SCSI_PROC_FS
1613 sg_proc_init();
1614#endif /* CONFIG_SCSI_PROC_FS */
1615 return 0;
1616 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001617 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618err_out:
1619 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1620 return rc;
1621}
1622
1623static void __exit
1624exit_sg(void)
1625{
1626#ifdef CONFIG_SCSI_PROC_FS
1627 sg_proc_cleanup();
1628#endif /* CONFIG_SCSI_PROC_FS */
1629 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001630 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sg_sysfs_valid = 0;
1632 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1633 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001634 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637static int
1638sg_start_req(Sg_request * srp)
1639{
1640 int res;
1641 Sg_fd *sfp = srp->parentfp;
1642 sg_io_hdr_t *hp = &srp->header;
1643 int dxfer_len = (int) hp->dxfer_len;
1644 int dxfer_dir = hp->dxfer_direction;
1645 Sg_scatter_hold *req_schp = &srp->data;
1646 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1647
1648 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1649 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1650 return 0;
1651 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1652 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1653 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1654 res = sg_build_direct(srp, sfp, dxfer_len);
1655 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1656 return res;
1657 }
1658 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1659 sg_link_reserve(sfp, srp, dxfer_len);
1660 else {
1661 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1662 if (res) {
1663 sg_remove_scat(req_schp);
1664 return res;
1665 }
1666 }
1667 return 0;
1668}
1669
1670static void
1671sg_finish_rem_req(Sg_request * srp)
1672{
1673 Sg_fd *sfp = srp->parentfp;
1674 Sg_scatter_hold *req_schp = &srp->data;
1675
1676 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1677 if (srp->res_used)
1678 sg_unlink_reserve(sfp, srp);
1679 else
1680 sg_remove_scat(req_schp);
1681 sg_remove_request(sfp, srp);
1682}
1683
1684static int
1685sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1686{
Mike Christied6b10342005-11-08 04:06:41 -06001687 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001688 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Mike Christied6b10342005-11-08 04:06:41 -06001690 /*
1691 * TODO: test without low_dma, we should not need it since
1692 * the block layer will bounce the buffer for us
1693 *
1694 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1695 */
1696 if (sfp->low_dma)
1697 gfp_flags |= GFP_DMA;
1698 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (!schp->buffer)
1700 return -ENOMEM;
Anton Blanchard30fa0d02007-10-26 14:00:14 +02001701 sg_init_table(schp->buffer, tablesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001703 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706#ifdef SG_ALLOW_DIO_CODE
1707/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001708 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1711 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 (i.e., either completely successful or fails)
1713*/
1714static int
1715st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001716 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001718 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1719 unsigned long start = uaddr >> PAGE_SHIFT;
1720 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct page **pages;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* User attempted Overflow! */
1725 if ((uaddr + count) < uaddr)
1726 return -EINVAL;
1727
1728 /* Too big */
1729 if (nr_pages > max_pages)
1730 return -ENOMEM;
1731
1732 /* Hmm? */
1733 if (count == 0)
1734 return 0;
1735
1736 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1737 return -ENOMEM;
1738
1739 /* Try to fault in all of the necessary pages */
1740 down_read(&current->mm->mmap_sem);
1741 /* rw==READ means read from drive, write into memory area */
1742 res = get_user_pages(
1743 current,
1744 current->mm,
1745 uaddr,
1746 nr_pages,
1747 rw == READ,
1748 0, /* don't force */
1749 pages,
1750 NULL);
1751 up_read(&current->mm->mmap_sem);
1752
1753 /* Errors and no page mapped should return here */
1754 if (res < nr_pages)
1755 goto out_unmap;
1756
1757 for (i=0; i < nr_pages; i++) {
1758 /* FIXME: flush superflous for rw==READ,
1759 * probably wrong function for rw==WRITE
1760 */
1761 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /* ?? Is locking needed? I don't think so */
Nick Piggin529ae9a2008-08-02 12:01:03 +02001763 /* if (!trylock_page(pages[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto out_unlock; */
1765 }
1766
Jens Axboe642f1492007-10-24 11:20:47 +02001767 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (nr_pages > 1) {
1769 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1770 count -= sgl[0].length;
Jens Axboe642f1492007-10-24 11:20:47 +02001771 for (i=1; i < nr_pages ; i++)
1772 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 else {
1775 sgl[0].length = count;
1776 }
1777
1778 kfree(pages);
1779 return nr_pages;
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001782 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 for (j=0; j < res; j++)
1784 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001785 res = 0;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 kfree(pages);
1788 return res;
1789}
1790
1791
1792/* And unmap them... */
1793static int
1794st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1795 int dirtied)
1796{
1797 int i;
1798
1799 for (i=0; i < nr_pages; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +02001800 struct page *page = sg_page(&sgl[i]);
Nick Pigginb5810032005-10-29 18:16:12 -07001801
Nick Pigginb5810032005-10-29 18:16:12 -07001802 if (dirtied)
1803 SetPageDirty(page);
1804 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* FIXME: cache flush missing for rw==READ
1806 * FIXME: call the correct reference counting function
1807 */
Nick Pigginb5810032005-10-29 18:16:12 -07001808 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
1811 return 0;
1812}
1813
1814/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1815#endif
1816
1817
1818/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1819static int
1820sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1821{
1822#ifdef SG_ALLOW_DIO_CODE
1823 sg_io_hdr_t *hp = &srp->header;
1824 Sg_scatter_hold *schp = &srp->data;
1825 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int mx_sc_elems, res;
1827 struct scsi_device *sdev = sfp->parentdp->device;
1828
1829 if (((unsigned long)hp->dxferp &
1830 queue_dma_alignment(sdev->request_queue)) != 0)
1831 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1834 if (mx_sc_elems <= 0) {
1835 return 1;
1836 }
Mike Christied6b10342005-11-08 04:06:41 -06001837 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1838 (unsigned long)hp->dxferp, dxfer_len,
1839 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001840 if (res <= 0) {
1841 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 schp->k_use_sg = res;
1845 schp->dio_in_use = 1;
1846 hp->info |= SG_INFO_DIRECT_IO;
1847 return 0;
1848#else
1849 return 1;
1850#endif
1851}
1852
1853static int
1854sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1855{
Mike Christied6b10342005-11-08 04:06:41 -06001856 struct scatterlist *sg;
1857 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1858 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001860 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Eric Sesterhennfb119932007-05-23 14:41:36 -07001862 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return -EFAULT;
1864 if (0 == blk_size)
1865 ++blk_size; /* don't know why */
1866/* round request up to next highest SG_SECTOR_SZ byte boundary */
1867 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1868 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1869 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001870
1871 /* N.B. ret_sz carried into this block ... */
1872 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1873 if (mx_sc_elems < 0)
1874 return mx_sc_elems; /* most likely -ENOMEM */
1875
Douglas Gilbert6460e752006-09-20 18:20:49 -04001876 num = scatter_elem_sz;
1877 if (unlikely(num != scatter_elem_sz_prev)) {
1878 if (num < PAGE_SIZE) {
1879 scatter_elem_sz = PAGE_SIZE;
1880 scatter_elem_sz_prev = PAGE_SIZE;
1881 } else
1882 scatter_elem_sz_prev = num;
1883 }
Mike Christied6b10342005-11-08 04:06:41 -06001884 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1885 (rem_sz > 0) && (k < mx_sc_elems);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001886 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001887
Douglas Gilbert6460e752006-09-20 18:20:49 -04001888 num = (rem_sz > scatter_elem_sz_prev) ?
1889 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001890 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (!p)
1892 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Douglas Gilbert6460e752006-09-20 18:20:49 -04001894 if (num == scatter_elem_sz_prev) {
1895 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1896 scatter_elem_sz = ret_sz;
1897 scatter_elem_sz_prev = ret_sz;
1898 }
1899 }
Jens Axboe642f1492007-10-24 11:20:47 +02001900 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001902 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1903 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001904 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Mike Christied6b10342005-11-08 04:06:41 -06001906 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001907 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1908 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001909
1910 schp->bufflen = blk_size;
1911 if (rem_sz > 0) /* must have failed */
1912 return -ENOMEM;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 0;
1915}
1916
1917static int
1918sg_write_xfer(Sg_request * srp)
1919{
1920 sg_io_hdr_t *hp = &srp->header;
1921 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001922 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int num_xfer = 0;
1924 int j, k, onum, usglen, ksglen, res;
1925 int iovec_count = (int) hp->iovec_count;
1926 int dxfer_dir = hp->dxfer_direction;
1927 unsigned char *p;
1928 unsigned char __user *up;
1929 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1930
1931 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1932 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1933 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1934 if (schp->bufflen < num_xfer)
1935 num_xfer = schp->bufflen;
1936 }
1937 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1938 (new_interface
1939 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1940 return 0;
1941
1942 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1943 num_xfer, iovec_count, schp->k_use_sg));
1944 if (iovec_count) {
1945 onum = iovec_count;
1946 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1947 return -EFAULT;
1948 } else
1949 onum = 1;
1950
Mike Christied6b10342005-11-08 04:06:41 -06001951 ksglen = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001952 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06001953 for (j = 0, k = 0; j < onum; ++j) {
1954 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1955 if (res)
1956 return res;
1957
Jens Axboeb0f655d2007-05-11 15:01:01 +02001958 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02001959 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06001960 if (usglen <= 0)
1961 break;
1962 if (ksglen > usglen) {
1963 if (usglen >= num_xfer) {
1964 if (__copy_from_user(p, up, num_xfer))
1965 return -EFAULT;
1966 return 0;
1967 }
1968 if (__copy_from_user(p, up, usglen))
1969 return -EFAULT;
1970 p += usglen;
1971 ksglen -= usglen;
1972 break;
1973 } else {
1974 if (ksglen >= num_xfer) {
1975 if (__copy_from_user(p, up, num_xfer))
1976 return -EFAULT;
1977 return 0;
1978 }
1979 if (__copy_from_user(p, up, ksglen))
1980 return -EFAULT;
1981 up += ksglen;
1982 usglen -= ksglen;
1983 }
1984 ++k;
1985 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Mike Christied6b10342005-11-08 04:06:41 -06001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return 0;
1991}
1992
1993static int
1994sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1995 int wr_xf, int *countp, unsigned char __user **up)
1996{
1997 int num_xfer = (int) hp->dxfer_len;
1998 unsigned char __user *p = hp->dxferp;
1999 int count;
2000
2001 if (0 == sg_num) {
2002 if (wr_xf && ('\0' == hp->interface_id))
2003 count = (int) hp->flags; /* holds "old" input_size */
2004 else
2005 count = num_xfer;
2006 } else {
2007 sg_iovec_t iovec;
2008 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2009 return -EFAULT;
2010 p = iovec.iov_base;
2011 count = (int) iovec.iov_len;
2012 }
2013 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2014 return -EFAULT;
2015 if (up)
2016 *up = p;
2017 if (countp)
2018 *countp = count;
2019 return 0;
2020}
2021
2022static void
2023sg_remove_scat(Sg_scatter_hold * schp)
2024{
2025 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2026 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002027 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if (schp->dio_in_use) {
2030#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06002031 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#endif
2033 } else {
2034 int k;
2035
Jens Axboe45711f12007-10-22 21:19:53 +02002036 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
Jens Axboeb0f655d2007-05-11 15:01:01 +02002037 ++k, sg = sg_next(sg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 SCSI_LOG_TIMEOUT(5, printk(
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04002039 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
Jens Axboe45711f12007-10-22 21:19:53 +02002040 k, sg_page(sg), sg->length));
2041 sg_page_free(sg_page(sg), sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 }
Mike Christied6b10342005-11-08 04:06:41 -06002044 kfree(schp->buffer);
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 memset(schp, 0, sizeof (*schp));
2047}
2048
2049static int
2050sg_read_xfer(Sg_request * srp)
2051{
2052 sg_io_hdr_t *hp = &srp->header;
2053 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002054 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 int num_xfer = 0;
2056 int j, k, onum, usglen, ksglen, res;
2057 int iovec_count = (int) hp->iovec_count;
2058 int dxfer_dir = hp->dxfer_direction;
2059 unsigned char *p;
2060 unsigned char __user *up;
2061 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2062
2063 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2064 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2065 num_xfer = hp->dxfer_len;
2066 if (schp->bufflen < num_xfer)
2067 num_xfer = schp->bufflen;
2068 }
2069 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2070 (new_interface
2071 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2072 return 0;
2073
2074 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2075 num_xfer, iovec_count, schp->k_use_sg));
2076 if (iovec_count) {
2077 onum = iovec_count;
2078 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2079 return -EFAULT;
2080 } else
2081 onum = 1;
2082
Jens Axboe45711f12007-10-22 21:19:53 +02002083 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06002084 ksglen = sg->length;
2085 for (j = 0, k = 0; j < onum; ++j) {
2086 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2087 if (res)
2088 return res;
2089
Jens Axboeb0f655d2007-05-11 15:01:01 +02002090 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02002091 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06002092 if (usglen <= 0)
2093 break;
2094 if (ksglen > usglen) {
2095 if (usglen >= num_xfer) {
2096 if (__copy_to_user(up, p, num_xfer))
2097 return -EFAULT;
2098 return 0;
2099 }
2100 if (__copy_to_user(up, p, usglen))
2101 return -EFAULT;
2102 p += usglen;
2103 ksglen -= usglen;
2104 break;
2105 } else {
2106 if (ksglen >= num_xfer) {
2107 if (__copy_to_user(up, p, num_xfer))
2108 return -EFAULT;
2109 return 0;
2110 }
2111 if (__copy_to_user(up, p, ksglen))
2112 return -EFAULT;
2113 up += ksglen;
2114 usglen -= ksglen;
2115 }
2116 ++k;
2117 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return 0;
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Mike Christied6b10342005-11-08 04:06:41 -06002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return 0;
2123}
2124
2125static int
2126sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2127{
2128 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002129 struct scatterlist *sg = schp->buffer;
2130 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2133 num_read_xfer));
2134 if ((!outp) || (num_read_xfer <= 0))
2135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jens Axboe45711f12007-10-22 21:19:53 +02002137 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002138 num = sg->length;
2139 if (num > num_read_xfer) {
Jens Axboe45711f12007-10-22 21:19:53 +02002140 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002141 num_read_xfer))
2142 return -EFAULT;
2143 break;
2144 } else {
Jens Axboe45711f12007-10-22 21:19:53 +02002145 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002146 num))
2147 return -EFAULT;
2148 num_read_xfer -= num;
2149 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 break;
Mike Christied6b10342005-11-08 04:06:41 -06002151 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Mike Christied6b10342005-11-08 04:06:41 -06002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return 0;
2156}
2157
2158static void
2159sg_build_reserve(Sg_fd * sfp, int req_size)
2160{
2161 Sg_scatter_hold *schp = &sfp->reserve;
2162
2163 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2164 do {
2165 if (req_size < PAGE_SIZE)
2166 req_size = PAGE_SIZE;
2167 if (0 == sg_build_indirect(schp, sfp, req_size))
2168 return;
2169 else
2170 sg_remove_scat(schp);
2171 req_size >>= 1; /* divide by 2 */
2172 } while (req_size > (PAGE_SIZE / 2));
2173}
2174
2175static void
2176sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2177{
2178 Sg_scatter_hold *req_schp = &srp->data;
2179 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002180 struct scatterlist *sg = rsv_schp->buffer;
2181 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 srp->res_used = 1;
2184 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002185 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Jens Axboeb0f655d2007-05-11 15:01:01 +02002187 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002188 num = sg->length;
2189 if (rem <= num) {
2190 sfp->save_scat_len = num;
2191 sg->length = rem;
2192 req_schp->k_use_sg = k + 1;
2193 req_schp->sglist_len = rsv_schp->sglist_len;
2194 req_schp->buffer = rsv_schp->buffer;
2195
2196 req_schp->bufflen = size;
2197 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2198 break;
2199 } else
2200 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
Mike Christied6b10342005-11-08 04:06:41 -06002202
2203 if (k >= rsv_schp->k_use_sg)
2204 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207static void
2208sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2209{
2210 Sg_scatter_hold *req_schp = &srp->data;
2211 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2212
2213 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2214 (int) req_schp->k_use_sg));
2215 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002216 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002219 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 (unsigned) sfp->save_scat_len;
2221 else
2222 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2223 }
2224 req_schp->k_use_sg = 0;
2225 req_schp->bufflen = 0;
2226 req_schp->buffer = NULL;
2227 req_schp->sglist_len = 0;
2228 sfp->save_scat_len = 0;
2229 srp->res_used = 0;
2230}
2231
2232static Sg_request *
2233sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2234{
2235 Sg_request *resp;
2236 unsigned long iflags;
2237
2238 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2239 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2240 /* look for requests that are ready + not SG_IO owned */
2241 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2242 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2243 resp->done = 2; /* guard against other readers */
2244 break;
2245 }
2246 }
2247 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2248 return resp;
2249}
2250
2251#ifdef CONFIG_SCSI_PROC_FS
2252static Sg_request *
2253sg_get_nth_request(Sg_fd * sfp, int nth)
2254{
2255 Sg_request *resp;
2256 unsigned long iflags;
2257 int k;
2258
2259 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2260 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2261 ++k, resp = resp->nextrp) ;
2262 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2263 return resp;
2264}
2265#endif
2266
2267/* always adds to end of list */
2268static Sg_request *
2269sg_add_request(Sg_fd * sfp)
2270{
2271 int k;
2272 unsigned long iflags;
2273 Sg_request *resp;
2274 Sg_request *rp = sfp->req_arr;
2275
2276 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2277 resp = sfp->headrp;
2278 if (!resp) {
2279 memset(rp, 0, sizeof (Sg_request));
2280 rp->parentfp = sfp;
2281 resp = rp;
2282 sfp->headrp = resp;
2283 } else {
2284 if (0 == sfp->cmd_q)
2285 resp = NULL; /* command queuing disallowed */
2286 else {
2287 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2288 if (!rp->parentfp)
2289 break;
2290 }
2291 if (k < SG_MAX_QUEUE) {
2292 memset(rp, 0, sizeof (Sg_request));
2293 rp->parentfp = sfp;
2294 while (resp->nextrp)
2295 resp = resp->nextrp;
2296 resp->nextrp = rp;
2297 resp = rp;
2298 } else
2299 resp = NULL;
2300 }
2301 }
2302 if (resp) {
2303 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002304 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2307 return resp;
2308}
2309
2310/* Return of 1 for found; 0 for not found */
2311static int
2312sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2313{
2314 Sg_request *prev_rp;
2315 Sg_request *rp;
2316 unsigned long iflags;
2317 int res = 0;
2318
2319 if ((!sfp) || (!srp) || (!sfp->headrp))
2320 return res;
2321 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2322 prev_rp = sfp->headrp;
2323 if (srp == prev_rp) {
2324 sfp->headrp = prev_rp->nextrp;
2325 prev_rp->parentfp = NULL;
2326 res = 1;
2327 } else {
2328 while ((rp = prev_rp->nextrp)) {
2329 if (srp == rp) {
2330 prev_rp->nextrp = rp->nextrp;
2331 rp->parentfp = NULL;
2332 res = 1;
2333 break;
2334 }
2335 prev_rp = rp;
2336 }
2337 }
2338 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2339 return res;
2340}
2341
2342#ifdef CONFIG_SCSI_PROC_FS
2343static Sg_fd *
2344sg_get_nth_sfp(Sg_device * sdp, int nth)
2345{
2346 Sg_fd *resp;
2347 unsigned long iflags;
2348 int k;
2349
James Bottomley7c07d612007-08-05 13:36:11 -05002350 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2352 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002353 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return resp;
2355}
2356#endif
2357
2358static Sg_fd *
2359sg_add_sfp(Sg_device * sdp, int dev)
2360{
2361 Sg_fd *sfp;
2362 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002363 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Mike Christied6b10342005-11-08 04:06:41 -06002365 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (!sfp)
2367 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 init_waitqueue_head(&sfp->read_wait);
2370 rwlock_init(&sfp->rq_list_lock);
2371
2372 sfp->timeout = SG_DEFAULT_TIMEOUT;
2373 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2374 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2375 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2376 sdp->device->host->unchecked_isa_dma : 1;
2377 sfp->cmd_q = SG_DEF_COMMAND_Q;
2378 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2379 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002380 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (!sdp->headfp)
2382 sdp->headfp = sfp;
2383 else { /* add to tail of existing list */
2384 Sg_fd *pfp = sdp->headfp;
2385 while (pfp->nextfp)
2386 pfp = pfp->nextfp;
2387 pfp->nextfp = sfp;
2388 }
James Bottomley7c07d612007-08-05 13:36:11 -05002389 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002391 if (unlikely(sg_big_buff != def_reserved_size))
2392 sg_big_buff = def_reserved_size;
2393
Alan Stern44ec9542007-02-20 11:01:57 -05002394 bufflen = min_t(int, sg_big_buff,
2395 sdp->device->request_queue->max_sectors * 512);
2396 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2398 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2399 return sfp;
2400}
2401
2402static void
2403__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2404{
2405 Sg_fd *fp;
2406 Sg_fd *prev_fp;
2407
2408 prev_fp = sdp->headfp;
2409 if (sfp == prev_fp)
2410 sdp->headfp = prev_fp->nextfp;
2411 else {
2412 while ((fp = prev_fp->nextfp)) {
2413 if (sfp == fp) {
2414 prev_fp->nextfp = fp->nextfp;
2415 break;
2416 }
2417 prev_fp = fp;
2418 }
2419 }
2420 if (sfp->reserve.bufflen > 0) {
2421 SCSI_LOG_TIMEOUT(6,
2422 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2423 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 sg_remove_scat(&sfp->reserve);
2425 }
2426 sfp->parentdp = NULL;
2427 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002428 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431/* Returns 0 in normal case, 1 when detached and sdp object removed */
2432static int
2433sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2434{
2435 Sg_request *srp;
2436 Sg_request *tsrp;
2437 int dirty = 0;
2438 int res = 0;
2439
2440 for (srp = sfp->headrp; srp; srp = tsrp) {
2441 tsrp = srp->nextrp;
2442 if (sg_srp_done(srp, sfp))
2443 sg_finish_rem_req(srp);
2444 else
2445 ++dirty;
2446 }
2447 if (0 == dirty) {
2448 unsigned long iflags;
2449
James Bottomley7c07d612007-08-05 13:36:11 -05002450 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 __sg_remove_sfp(sdp, sfp);
2452 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002453 idr_remove(&sg_index_idr, sdp->index);
2454 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 res = 1;
2456 }
James Bottomley7c07d612007-08-05 13:36:11 -05002457 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 } else {
2459 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2460 /* only bump the access_count if we actually succeeded in
2461 * throwing another counter on the host module */
2462 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2463 sfp->closed = 1; /* flag dirty state on this fd */
2464 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2465 dirty));
2466 }
2467 return res;
2468}
2469
2470static int
2471sg_res_in_use(Sg_fd * sfp)
2472{
2473 const Sg_request *srp;
2474 unsigned long iflags;
2475
2476 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2477 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2478 if (srp->res_used)
2479 break;
2480 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2481 return srp ? 1 : 0;
2482}
2483
Douglas Gilbert6460e752006-09-20 18:20:49 -04002484/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002485static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2487{
Mike Christied6b10342005-11-08 04:06:41 -06002488 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002489 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002491 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Douglas Gilbert6460e752006-09-20 18:20:49 -04002493 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return resp;
2495
2496 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002497 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002499 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2502 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002503 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002504 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002505 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 --order;
2507 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002508 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 resSz = a_size;
2510 }
2511 if (resp) {
2512 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002513 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002514 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516 return resp;
2517}
2518
2519static void
Mike Christied6b10342005-11-08 04:06:41 -06002520sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int order, a_size;
2523
Mike Christied6b10342005-11-08 04:06:41 -06002524 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return;
2526 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2527 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002528 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531#ifdef CONFIG_SCSI_PROC_FS
2532static int
James Bottomley7c07d612007-08-05 13:36:11 -05002533sg_idr_max_id(int id, void *p, void *data)
2534{
2535 int *k = data;
2536
2537 if (*k < id)
2538 *k = id;
2539
2540 return 0;
2541}
2542
2543static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544sg_last_dev(void)
2545{
Tony Battersby53474c02008-01-22 15:25:49 -05002546 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 unsigned long iflags;
2548
James Bottomley7c07d612007-08-05 13:36:11 -05002549 read_lock_irqsave(&sg_index_lock, iflags);
2550 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2551 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return k + 1; /* origin 1 */
2553}
2554#endif
2555
2556static Sg_device *
2557sg_get_dev(int dev)
2558{
James Bottomley7c07d612007-08-05 13:36:11 -05002559 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 unsigned long iflags;
2561
James Bottomley7c07d612007-08-05 13:36:11 -05002562 read_lock_irqsave(&sg_index_lock, iflags);
2563 sdp = idr_find(&sg_index_idr, dev);
2564 read_unlock_irqrestore(&sg_index_lock, iflags);
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return sdp;
2567}
2568
2569#ifdef CONFIG_SCSI_PROC_FS
2570
2571static struct proc_dir_entry *sg_proc_sgp = NULL;
2572
2573static char sg_proc_sg_dirname[] = "scsi/sg";
2574
2575static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2576
2577static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2578static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2579 size_t count, loff_t *off);
2580static struct file_operations adio_fops = {
2581 /* .owner, .read and .llseek added in sg_proc_init() */
2582 .open = sg_proc_single_open_adio,
2583 .write = sg_proc_write_adio,
2584 .release = single_release,
2585};
2586
2587static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2588static ssize_t sg_proc_write_dressz(struct file *filp,
2589 const char __user *buffer, size_t count, loff_t *off);
2590static struct file_operations dressz_fops = {
2591 .open = sg_proc_single_open_dressz,
2592 .write = sg_proc_write_dressz,
2593 .release = single_release,
2594};
2595
2596static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2597static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2598static struct file_operations version_fops = {
2599 .open = sg_proc_single_open_version,
2600 .release = single_release,
2601};
2602
2603static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2604static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2605static struct file_operations devhdr_fops = {
2606 .open = sg_proc_single_open_devhdr,
2607 .release = single_release,
2608};
2609
2610static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2611static int sg_proc_open_dev(struct inode *inode, struct file *file);
2612static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2613static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2614static void dev_seq_stop(struct seq_file *s, void *v);
2615static struct file_operations dev_fops = {
2616 .open = sg_proc_open_dev,
2617 .release = seq_release,
2618};
2619static struct seq_operations dev_seq_ops = {
2620 .start = dev_seq_start,
2621 .next = dev_seq_next,
2622 .stop = dev_seq_stop,
2623 .show = sg_proc_seq_show_dev,
2624};
2625
2626static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2627static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2628static struct file_operations devstrs_fops = {
2629 .open = sg_proc_open_devstrs,
2630 .release = seq_release,
2631};
2632static struct seq_operations devstrs_seq_ops = {
2633 .start = dev_seq_start,
2634 .next = dev_seq_next,
2635 .stop = dev_seq_stop,
2636 .show = sg_proc_seq_show_devstrs,
2637};
2638
2639static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2640static int sg_proc_open_debug(struct inode *inode, struct file *file);
2641static struct file_operations debug_fops = {
2642 .open = sg_proc_open_debug,
2643 .release = seq_release,
2644};
2645static struct seq_operations debug_seq_ops = {
2646 .start = dev_seq_start,
2647 .next = dev_seq_next,
2648 .stop = dev_seq_stop,
2649 .show = sg_proc_seq_show_debug,
2650};
2651
2652
2653struct sg_proc_leaf {
2654 const char * name;
2655 struct file_operations * fops;
2656};
2657
2658static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2659 {"allow_dio", &adio_fops},
2660 {"debug", &debug_fops},
2661 {"def_reserved_size", &dressz_fops},
2662 {"device_hdr", &devhdr_fops},
2663 {"devices", &dev_fops},
2664 {"device_strs", &devstrs_fops},
2665 {"version", &version_fops}
2666};
2667
2668static int
2669sg_proc_init(void)
2670{
2671 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002672 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct sg_proc_leaf * leaf;
2674
Al Viro66600222005-09-28 22:32:57 +01002675 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (!sg_proc_sgp)
2677 return 1;
2678 for (k = 0; k < num_leaves; ++k) {
2679 leaf = &sg_proc_leaf_arr[k];
2680 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002681 leaf->fops->owner = THIS_MODULE;
2682 leaf->fops->read = seq_read;
2683 leaf->fops->llseek = seq_lseek;
2684 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
2686 return 0;
2687}
2688
2689static void
2690sg_proc_cleanup(void)
2691{
2692 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002693 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 if (!sg_proc_sgp)
2696 return;
2697 for (k = 0; k < num_leaves; ++k)
2698 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2699 remove_proc_entry(sg_proc_sg_dirname, NULL);
2700}
2701
2702
2703static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2704{
2705 seq_printf(s, "%d\n", *((int *)s->private));
2706 return 0;
2707}
2708
2709static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2710{
2711 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2712}
2713
2714static ssize_t
2715sg_proc_write_adio(struct file *filp, const char __user *buffer,
2716 size_t count, loff_t *off)
2717{
2718 int num;
2719 char buff[11];
2720
2721 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2722 return -EACCES;
2723 num = (count < 10) ? count : 10;
2724 if (copy_from_user(buff, buffer, num))
2725 return -EFAULT;
2726 buff[num] = '\0';
2727 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2728 return count;
2729}
2730
2731static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2732{
2733 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2734}
2735
2736static ssize_t
2737sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2738 size_t count, loff_t *off)
2739{
2740 int num;
2741 unsigned long k = ULONG_MAX;
2742 char buff[11];
2743
2744 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2745 return -EACCES;
2746 num = (count < 10) ? count : 10;
2747 if (copy_from_user(buff, buffer, num))
2748 return -EFAULT;
2749 buff[num] = '\0';
2750 k = simple_strtoul(buff, NULL, 10);
2751 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2752 sg_big_buff = k;
2753 return count;
2754 }
2755 return -ERANGE;
2756}
2757
2758static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2759{
2760 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2761 sg_version_date);
2762 return 0;
2763}
2764
2765static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2766{
2767 return single_open(file, sg_proc_seq_show_version, NULL);
2768}
2769
2770static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2771{
2772 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2773 "online\n");
2774 return 0;
2775}
2776
2777static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2778{
2779 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2780}
2781
2782struct sg_proc_deviter {
2783 loff_t index;
2784 size_t max;
2785};
2786
2787static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2788{
2789 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2790
Jan Blunck729d70f2005-08-27 11:07:52 -07002791 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (! it)
2793 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 it->index = *pos;
2796 it->max = sg_last_dev();
2797 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002798 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
2802static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2803{
Jan Blunck729d70f2005-08-27 11:07:52 -07002804 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 *pos = ++it->index;
2807 return (it->index < it->max) ? it : NULL;
2808}
2809
2810static void dev_seq_stop(struct seq_file *s, void *v)
2811{
Jan Blunck729d70f2005-08-27 11:07:52 -07002812 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
2815static int sg_proc_open_dev(struct inode *inode, struct file *file)
2816{
2817 return seq_open(file, &dev_seq_ops);
2818}
2819
2820static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2821{
2822 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2823 Sg_device *sdp;
2824 struct scsi_device *scsidp;
2825
2826 sdp = it ? sg_get_dev(it->index) : NULL;
2827 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2828 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2829 scsidp->host->host_no, scsidp->channel,
2830 scsidp->id, scsidp->lun, (int) scsidp->type,
2831 1,
2832 (int) scsidp->queue_depth,
2833 (int) scsidp->device_busy,
2834 (int) scsi_device_online(scsidp));
2835 else
2836 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2837 return 0;
2838}
2839
2840static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2841{
2842 return seq_open(file, &devstrs_seq_ops);
2843}
2844
2845static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2846{
2847 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2848 Sg_device *sdp;
2849 struct scsi_device *scsidp;
2850
2851 sdp = it ? sg_get_dev(it->index) : NULL;
2852 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2853 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2854 scsidp->vendor, scsidp->model, scsidp->rev);
2855 else
2856 seq_printf(s, "<no active device>\n");
2857 return 0;
2858}
2859
2860static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2861{
2862 int k, m, new_interface, blen, usg;
2863 Sg_request *srp;
2864 Sg_fd *fp;
2865 const sg_io_hdr_t *hp;
2866 const char * cp;
cb59e842005-04-02 13:51:23 -06002867 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2870 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2871 "(res)sgat=%d low_dma=%d\n", k + 1,
2872 jiffies_to_msecs(fp->timeout),
2873 fp->reserve.bufflen,
2874 (int) fp->reserve.k_use_sg,
2875 (int) fp->low_dma);
2876 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2877 (int) fp->cmd_q, (int) fp->force_packid,
2878 (int) fp->keep_orphan, (int) fp->closed);
2879 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2880 hp = &srp->header;
2881 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2882 if (srp->res_used) {
2883 if (new_interface &&
2884 (SG_FLAG_MMAP_IO & hp->flags))
2885 cp = " mmap>> ";
2886 else
2887 cp = " rb>> ";
2888 } else {
2889 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2890 cp = " dio>> ";
2891 else
2892 cp = " ";
2893 }
2894 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002895 blen = srp->data.bufflen;
2896 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 seq_printf(s, srp->done ?
2898 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002899 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 seq_printf(s, " id=%d blen=%d",
2901 srp->header.pack_id, blen);
2902 if (srp->done)
2903 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002904 else {
2905 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002907 (new_interface ? hp->timeout :
2908 jiffies_to_msecs(fp->timeout)),
2909 (ms > hp->duration ? ms - hp->duration : 0));
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2912 (int) srp->data.cmd_opcode);
2913 }
2914 if (0 == m)
2915 seq_printf(s, " No requests active\n");
2916 }
2917}
2918
2919static int sg_proc_open_debug(struct inode *inode, struct file *file)
2920{
2921 return seq_open(file, &debug_seq_ops);
2922}
2923
2924static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2925{
2926 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2927 Sg_device *sdp;
2928
2929 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002930 seq_printf(s, "max_active_device=%d(origin 1)\n",
2931 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2933 }
2934 sdp = it ? sg_get_dev(it->index) : NULL;
2935 if (sdp) {
2936 struct scsi_device *scsidp = sdp->device;
2937
2938 if (NULL == scsidp) {
2939 seq_printf(s, "device %d detached ??\n",
2940 (int)it->index);
2941 return 0;
2942 }
2943
2944 if (sg_get_nth_sfp(sdp, 0)) {
2945 seq_printf(s, " >>> device=%s ",
2946 sdp->disk->disk_name);
2947 if (sdp->detached)
2948 seq_printf(s, "detached pending close ");
2949 else
2950 seq_printf
2951 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2952 scsidp->host->host_no,
2953 scsidp->channel, scsidp->id,
2954 scsidp->lun,
2955 scsidp->host->hostt->emulated);
2956 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2957 sdp->sg_tablesize, sdp->exclude);
2958 }
2959 sg_proc_debug_helper(s, sdp);
2960 }
2961 return 0;
2962}
2963
2964#endif /* CONFIG_SCSI_PROC_FS */
2965
2966module_init(init_sg);
2967module_exit(exit_sg);