blob: 4284625ed035d042a530669b05c8c5ceeb146fe6 [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);
186static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
187 int blocking, int read_only, Sg_request ** o_srp);
188static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
189 unsigned char *cmnd, int timeout, int blocking);
190static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
191 int wr_xf, int *countp, unsigned char __user **up);
192static int sg_write_xfer(Sg_request * srp);
193static int sg_read_xfer(Sg_request * srp);
194static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
195static void sg_remove_scat(Sg_scatter_hold * schp);
196static void sg_build_reserve(Sg_fd * sfp, int req_size);
197static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
198static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600199static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
200static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
202static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
203static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
205static Sg_request *sg_add_request(Sg_fd * sfp);
206static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
207static int sg_res_in_use(Sg_fd * sfp);
208static int sg_allow_access(unsigned char opcode, char dev_type);
209static 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
220static int
221sg_open(struct inode *inode, struct file *filp)
222{
223 int dev = iminor(inode);
224 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600225 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 Sg_device *sdp;
227 Sg_fd *sfp;
228 int res;
229 int retval;
230
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600231 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 nonseekable_open(inode, filp);
233 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
234 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600235 if ((!sdp) || (!sdp->device)) {
236 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600238 }
239 if (sdp->detached) {
240 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* This driver's module count bumped by fops_get in <linux/fs.h> */
245 /* Prevent the device driver from vanishing while we sleep */
246 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600247 if (retval) {
248 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (!((flags & O_NONBLOCK) ||
253 scsi_block_when_processing_errors(sdp->device))) {
254 retval = -ENXIO;
255 /* we are in error recovery for this device */
256 goto error_out;
257 }
258
259 if (flags & O_EXCL) {
260 if (O_RDONLY == (flags & O_ACCMODE)) {
261 retval = -EPERM; /* Can't lock it with read only access */
262 goto error_out;
263 }
264 if (sdp->headfp && (flags & O_NONBLOCK)) {
265 retval = -EBUSY;
266 goto error_out;
267 }
268 res = 0;
269 __wait_event_interruptible(sdp->o_excl_wait,
270 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
271 if (res) {
272 retval = res; /* -ERESTARTSYS because signal hit process */
273 goto error_out;
274 }
275 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
276 if (flags & O_NONBLOCK) {
277 retval = -EBUSY;
278 goto error_out;
279 }
280 res = 0;
281 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
282 res);
283 if (res) {
284 retval = res; /* -ERESTARTSYS because signal hit process */
285 goto error_out;
286 }
287 }
288 if (sdp->detached) {
289 retval = -ENODEV;
290 goto error_out;
291 }
292 if (!sdp->headfp) { /* no existing opens on this device */
293 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600294 q = sdp->device->request_queue;
295 sdp->sg_tablesize = min(q->max_hw_segments,
296 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 if ((sfp = sg_add_sfp(sdp, dev)))
299 filp->private_data = sfp;
300 else {
301 if (flags & O_EXCL)
302 sdp->exclude = 0; /* undo if error */
303 retval = -ENOMEM;
304 goto error_out;
305 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600306 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308
309 error_out:
310 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600311 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return retval;
313}
314
315/* Following function was formerly called 'sg_close' */
316static int
317sg_release(struct inode *inode, struct file *filp)
318{
319 Sg_device *sdp;
320 Sg_fd *sfp;
321
322 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
323 return -ENXIO;
324 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
325 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
326 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
327 if (!sdp->detached) {
328 scsi_device_put(sdp->device);
329 }
330 sdp->exclude = 0;
331 wake_up_interruptible(&sdp->o_excl_wait);
332 }
333 return 0;
334}
335
336static ssize_t
337sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 Sg_device *sdp;
340 Sg_fd *sfp;
341 Sg_request *srp;
342 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600344 struct sg_header *old_hdr = NULL;
345 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
348 return -ENXIO;
349 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
350 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!access_ok(VERIFY_WRITE, buf, count))
353 return -EFAULT;
354 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600355 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
356 if (!old_hdr)
357 return -ENOMEM;
358 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
359 retval = -EFAULT;
360 goto free_old_hdr;
361 }
362 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600364 sg_io_hdr_t *new_hdr;
365 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
366 if (!new_hdr) {
367 retval = -ENOMEM;
368 goto free_old_hdr;
369 }
370 retval =__copy_from_user
371 (new_hdr, buf, SZ_SG_IO_HDR);
372 req_pack_id = new_hdr->pack_id;
373 kfree(new_hdr);
374 if (retval) {
375 retval = -EFAULT;
376 goto free_old_hdr;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 } else
cb59e842005-04-02 13:51:23 -0600380 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 srp = sg_get_rq_mark(sfp, req_pack_id);
383 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600384 if (sdp->detached) {
385 retval = -ENODEV;
386 goto free_old_hdr;
387 }
388 if (filp->f_flags & O_NONBLOCK) {
389 retval = -EAGAIN;
390 goto free_old_hdr;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 while (1) {
cb59e842005-04-02 13:51:23 -0600393 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600395 (sdp->detached ||
396 (srp = sg_get_rq_mark(sfp, req_pack_id))),
397 retval);
398 if (sdp->detached) {
399 retval = -ENODEV;
400 goto free_old_hdr;
401 }
402 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
cb59e842005-04-02 13:51:23 -0600404
405 /* -ERESTARTSYS as signal hit process */
406 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 }
cb59e842005-04-02 13:51:23 -0600409 if (srp->header.interface_id != '\0') {
410 retval = sg_new_read(sfp, buf, count, srp);
411 goto free_old_hdr;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600415 if (old_hdr == NULL) {
416 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
417 if (! old_hdr) {
418 retval = -ENOMEM;
419 goto free_old_hdr;
420 }
421 }
422 memset(old_hdr, 0, SZ_SG_HEADER);
423 old_hdr->reply_len = (int) hp->timeout;
424 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
425 old_hdr->pack_id = hp->pack_id;
426 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600428 old_hdr->target_status = hp->masked_status;
429 old_hdr->host_status = hp->host_status;
430 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ((CHECK_CONDITION & hp->masked_status) ||
432 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600433 memcpy(old_hdr->sense_buffer, srp->sense_b,
434 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 switch (hp->host_status) {
436 /* This setup of 'result' is for backward compatibility and is best
437 ignored by the user who should use target, host + driver status */
438 case DID_OK:
439 case DID_PASSTHROUGH:
440 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600441 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443 case DID_NO_CONNECT:
444 case DID_BUS_BUSY:
445 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case DID_BAD_TARGET:
449 case DID_ABORT:
450 case DID_PARITY:
451 case DID_RESET:
452 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600453 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600456 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 hp->masked_status == GOOD) ? 0 : EIO;
458 break;
459 default:
cb59e842005-04-02 13:51:23 -0600460 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 }
463
464 /* Now copy the result back to the user buffer. */
465 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600466 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
467 retval = -EFAULT;
468 goto free_old_hdr;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600471 if (count > old_hdr->reply_len)
472 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600474 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
475 retval = -EFAULT;
476 goto free_old_hdr;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 } else
cb59e842005-04-02 13:51:23 -0600480 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600482 retval = count;
483free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800484 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600485 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static ssize_t
489sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
490{
491 sg_io_hdr_t *hp = &srp->header;
492 int err = 0;
493 int len;
494
495 if (count < SZ_SG_IO_HDR) {
496 err = -EINVAL;
497 goto err_out;
498 }
499 hp->sb_len_wr = 0;
500 if ((hp->mx_sb_len > 0) && hp->sbp) {
501 if ((CHECK_CONDITION & hp->masked_status) ||
502 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600503 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
505 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
506 len = (len > sb_len) ? sb_len : len;
507 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
508 err = -EFAULT;
509 goto err_out;
510 }
511 hp->sb_len_wr = len;
512 }
513 }
514 if (hp->masked_status || hp->host_status || hp->driver_status)
515 hp->info |= SG_INFO_CHECK;
516 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
517 err = -EFAULT;
518 goto err_out;
519 }
520 err = sg_read_xfer(srp);
521 err_out:
522 sg_finish_rem_req(srp);
523 return (0 == err) ? count : err;
524}
525
526static ssize_t
527sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
528{
529 int mxsize, cmd_size, k;
530 int input_size, blocking;
531 unsigned char opcode;
532 Sg_device *sdp;
533 Sg_fd *sfp;
534 Sg_request *srp;
535 struct sg_header old_hdr;
536 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600537 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
540 return -ENXIO;
541 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
542 sdp->disk->disk_name, (int) count));
543 if (sdp->detached)
544 return -ENODEV;
545 if (!((filp->f_flags & O_NONBLOCK) ||
546 scsi_block_when_processing_errors(sdp->device)))
547 return -ENXIO;
548
549 if (!access_ok(VERIFY_READ, buf, count))
550 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
551 if (count < SZ_SG_HEADER)
552 return -EIO;
553 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
554 return -EFAULT;
555 blocking = !(filp->f_flags & O_NONBLOCK);
556 if (old_hdr.reply_len < 0)
557 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
558 if (count < (SZ_SG_HEADER + 6))
559 return -EIO; /* The minimum scsi command length is 6 bytes. */
560
561 if (!(srp = sg_add_request(sfp))) {
562 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
563 return -EDOM;
564 }
565 buf += SZ_SG_HEADER;
566 __get_user(opcode, buf);
567 if (sfp->next_cmd_len > 0) {
568 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
569 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
570 sfp->next_cmd_len = 0;
571 sg_remove_request(sfp, srp);
572 return -EIO;
573 }
574 cmd_size = sfp->next_cmd_len;
575 sfp->next_cmd_len = 0; /* reset so only this write() effected */
576 } else {
577 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
578 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
579 cmd_size = 12;
580 }
581 SCSI_LOG_TIMEOUT(4, printk(
582 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
583/* Determine buffer size. */
584 input_size = count - cmd_size;
585 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
586 mxsize -= SZ_SG_HEADER;
587 input_size -= SZ_SG_HEADER;
588 if (input_size < 0) {
589 sg_remove_request(sfp, srp);
590 return -EIO; /* User did not pass enough bytes for this command. */
591 }
592 hp = &srp->header;
593 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
594 hp->cmd_len = (unsigned char) cmd_size;
595 hp->iovec_count = 0;
596 hp->mx_sb_len = 0;
597 if (input_size > 0)
598 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
599 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
600 else
601 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
602 hp->dxfer_len = mxsize;
603 hp->dxferp = (char __user *)buf + cmd_size;
604 hp->sbp = NULL;
605 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
606 hp->flags = input_size; /* structure abuse ... */
607 hp->pack_id = old_hdr.pack_id;
608 hp->usr_ptr = NULL;
609 if (__copy_from_user(cmnd, buf, cmd_size))
610 return -EFAULT;
611 /*
612 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
613 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
614 * is a non-zero input_size, so emit a warning.
615 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100616 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
617 static char cmd[TASK_COMM_LEN];
618 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 printk(KERN_WARNING
620 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
621 "guessing data in;\n" KERN_WARNING " "
622 "program %s not setting count and/or reply_len properly\n",
623 old_hdr.reply_len - (int)SZ_SG_HEADER,
624 input_size, (unsigned int) cmnd[0],
625 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100626 strcpy(cmd, current->comm);
627 }
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
630 return (k < 0) ? k : count;
631}
632
633static ssize_t
634sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
635 int blocking, int read_only, Sg_request ** o_srp)
636{
637 int k;
638 Sg_request *srp;
639 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600640 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int timeout;
642 unsigned long ul_timeout;
643
644 if (count < SZ_SG_IO_HDR)
645 return -EINVAL;
646 if (!access_ok(VERIFY_READ, buf, count))
647 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
648
649 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
650 if (!(srp = sg_add_request(sfp))) {
651 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
652 return -EDOM;
653 }
654 hp = &srp->header;
655 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
656 sg_remove_request(sfp, srp);
657 return -EFAULT;
658 }
659 if (hp->interface_id != 'S') {
660 sg_remove_request(sfp, srp);
661 return -ENOSYS;
662 }
663 if (hp->flags & SG_FLAG_MMAP_IO) {
664 if (hp->dxfer_len > sfp->reserve.bufflen) {
665 sg_remove_request(sfp, srp);
666 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
667 }
668 if (hp->flags & SG_FLAG_DIRECT_IO) {
669 sg_remove_request(sfp, srp);
670 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
671 }
672 if (sg_res_in_use(sfp)) {
673 sg_remove_request(sfp, srp);
674 return -EBUSY; /* reserve buffer already being used */
675 }
676 }
677 ul_timeout = msecs_to_jiffies(srp->header.timeout);
678 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
679 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
680 sg_remove_request(sfp, srp);
681 return -EMSGSIZE;
682 }
683 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
684 sg_remove_request(sfp, srp);
685 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
686 }
687 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
688 sg_remove_request(sfp, srp);
689 return -EFAULT;
690 }
691 if (read_only &&
692 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
693 sg_remove_request(sfp, srp);
694 return -EPERM;
695 }
696 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
697 if (k < 0)
698 return k;
699 if (o_srp)
700 *o_srp = srp;
701 return count;
702}
703
704static int
705sg_common_write(Sg_fd * sfp, Sg_request * srp,
706 unsigned char *cmnd, int timeout, int blocking)
707{
Mike Christied6b10342005-11-08 04:06:41 -0600708 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 Sg_device *sdp = sfp->parentdp;
710 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
713 hp->status = 0;
714 hp->masked_status = 0;
715 hp->msg_status = 0;
716 hp->info = 0;
717 hp->host_status = 0;
718 hp->driver_status = 0;
719 hp->resid = 0;
720 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
721 (int) cmnd[0], (int) hp->cmd_len));
722
723 if ((k = sg_start_req(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400724 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 sg_finish_rem_req(srp);
726 return k; /* probably out of space --> ENOMEM */
727 }
728 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400729 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sg_finish_rem_req(srp);
731 return k;
732 }
733 if (sdp->detached) {
734 sg_finish_rem_req(srp);
735 return -ENODEV;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 switch (hp->dxfer_direction) {
739 case SG_DXFER_TO_FROM_DEV:
740 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600741 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600744 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600747 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 default:
Mike Christied6b10342005-11-08 04:06:41 -0600750 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752 }
cb59e842005-04-02 13:51:23 -0600753 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/* Now send everything of to mid-level. The next time we hear about this
755 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600756 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600757 hp->dxfer_len, srp->data.k_use_sg, timeout,
758 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
759 GFP_ATOMIC)) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400760 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
Mike Christied6b10342005-11-08 04:06:41 -0600761 /*
762 * most likely out of mem, but could also be a bad map
763 */
Mike Christie18c49b82006-03-22 16:04:38 -0600764 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600765 return -ENOMEM;
766 } else
767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static int
771sg_srp_done(Sg_request *srp, Sg_fd *sfp)
772{
773 unsigned long iflags;
774 int done;
775
776 read_lock_irqsave(&sfp->rq_list_lock, iflags);
777 done = srp->done;
778 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
779 return done;
780}
781
782static int
783sg_ioctl(struct inode *inode, struct file *filp,
784 unsigned int cmd_in, unsigned long arg)
785{
786 void __user *p = (void __user *)arg;
787 int __user *ip = p;
788 int result, val, read_only;
789 Sg_device *sdp;
790 Sg_fd *sfp;
791 Sg_request *srp;
792 unsigned long iflags;
793
794 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
795 return -ENXIO;
796 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
797 sdp->disk->disk_name, (int) cmd_in));
798 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
799
800 switch (cmd_in) {
801 case SG_IO:
802 {
803 int blocking = 1; /* ignore O_NONBLOCK flag */
804
805 if (sdp->detached)
806 return -ENODEV;
807 if (!scsi_block_when_processing_errors(sdp->device))
808 return -ENXIO;
809 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
810 return -EFAULT;
811 result =
812 sg_new_write(sfp, p, SZ_SG_IO_HDR,
813 blocking, read_only, &srp);
814 if (result < 0)
815 return result;
816 srp->sg_io_owned = 1;
817 while (1) {
818 result = 0; /* following macro to beat race condition */
819 __wait_event_interruptible(sfp->read_wait,
820 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
821 result);
822 if (sdp->detached)
823 return -ENODEV;
824 if (sfp->closed)
825 return 0; /* request packet dropped already */
826 if (0 == result)
827 break;
828 srp->orphan = 1;
829 return result; /* -ERESTARTSYS because signal hit process */
830 }
831 write_lock_irqsave(&sfp->rq_list_lock, iflags);
832 srp->done = 2;
833 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
834 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
835 return (result < 0) ? result : 0;
836 }
837 case SG_SET_TIMEOUT:
838 result = get_user(val, ip);
839 if (result)
840 return result;
841 if (val < 0)
842 return -EIO;
843 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
844 val = MULDIV (INT_MAX, USER_HZ, HZ);
845 sfp->timeout_user = val;
846 sfp->timeout = MULDIV (val, HZ, USER_HZ);
847
848 return 0;
849 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
850 /* strange ..., for backward compatibility */
851 return sfp->timeout_user;
852 case SG_SET_FORCE_LOW_DMA:
853 result = get_user(val, ip);
854 if (result)
855 return result;
856 if (val) {
857 sfp->low_dma = 1;
858 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
859 val = (int) sfp->reserve.bufflen;
860 sg_remove_scat(&sfp->reserve);
861 sg_build_reserve(sfp, val);
862 }
863 } else {
864 if (sdp->detached)
865 return -ENODEV;
866 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
867 }
868 return 0;
869 case SG_GET_LOW_DMA:
870 return put_user((int) sfp->low_dma, ip);
871 case SG_GET_SCSI_ID:
872 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
873 return -EFAULT;
874 else {
875 sg_scsi_id_t __user *sg_idp = p;
876
877 if (sdp->detached)
878 return -ENODEV;
879 __put_user((int) sdp->device->host->host_no,
880 &sg_idp->host_no);
881 __put_user((int) sdp->device->channel,
882 &sg_idp->channel);
883 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
884 __put_user((int) sdp->device->lun, &sg_idp->lun);
885 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
886 __put_user((short) sdp->device->host->cmd_per_lun,
887 &sg_idp->h_cmd_per_lun);
888 __put_user((short) sdp->device->queue_depth,
889 &sg_idp->d_queue_depth);
890 __put_user(0, &sg_idp->unused[0]);
891 __put_user(0, &sg_idp->unused[1]);
892 return 0;
893 }
894 case SG_SET_FORCE_PACK_ID:
895 result = get_user(val, ip);
896 if (result)
897 return result;
898 sfp->force_packid = val ? 1 : 0;
899 return 0;
900 case SG_GET_PACK_ID:
901 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
902 return -EFAULT;
903 read_lock_irqsave(&sfp->rq_list_lock, iflags);
904 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
905 if ((1 == srp->done) && (!srp->sg_io_owned)) {
906 read_unlock_irqrestore(&sfp->rq_list_lock,
907 iflags);
908 __put_user(srp->header.pack_id, ip);
909 return 0;
910 }
911 }
912 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
913 __put_user(-1, ip);
914 return 0;
915 case SG_GET_NUM_WAITING:
916 read_lock_irqsave(&sfp->rq_list_lock, iflags);
917 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
918 if ((1 == srp->done) && (!srp->sg_io_owned))
919 ++val;
920 }
921 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
922 return put_user(val, ip);
923 case SG_GET_SG_TABLESIZE:
924 return put_user(sdp->sg_tablesize, ip);
925 case SG_SET_RESERVED_SIZE:
926 result = get_user(val, ip);
927 if (result)
928 return result;
929 if (val < 0)
930 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500931 val = min_t(int, val,
932 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (val != sfp->reserve.bufflen) {
934 if (sg_res_in_use(sfp) || sfp->mmap_called)
935 return -EBUSY;
936 sg_remove_scat(&sfp->reserve);
937 sg_build_reserve(sfp, val);
938 }
939 return 0;
940 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500941 val = min_t(int, sfp->reserve.bufflen,
942 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return put_user(val, ip);
944 case SG_SET_COMMAND_Q:
945 result = get_user(val, ip);
946 if (result)
947 return result;
948 sfp->cmd_q = val ? 1 : 0;
949 return 0;
950 case SG_GET_COMMAND_Q:
951 return put_user((int) sfp->cmd_q, ip);
952 case SG_SET_KEEP_ORPHAN:
953 result = get_user(val, ip);
954 if (result)
955 return result;
956 sfp->keep_orphan = val;
957 return 0;
958 case SG_GET_KEEP_ORPHAN:
959 return put_user((int) sfp->keep_orphan, ip);
960 case SG_NEXT_CMD_LEN:
961 result = get_user(val, ip);
962 if (result)
963 return result;
964 sfp->next_cmd_len = (val > 0) ? val : 0;
965 return 0;
966 case SG_GET_VERSION_NUM:
967 return put_user(sg_version_num, ip);
968 case SG_GET_ACCESS_COUNT:
969 /* faked - we don't have a real access count anymore */
970 val = (sdp->device ? 1 : 0);
971 return put_user(val, ip);
972 case SG_GET_REQUEST_TABLE:
973 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
974 return -EFAULT;
975 else {
cb59e842005-04-02 13:51:23 -0600976 sg_req_info_t *rinfo;
977 unsigned int ms;
978
979 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
980 GFP_KERNEL);
981 if (!rinfo)
982 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 read_lock_irqsave(&sfp->rq_list_lock, iflags);
984 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
985 ++val, srp = srp ? srp->nextrp : srp) {
986 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
987 if (srp) {
988 rinfo[val].req_state = srp->done + 1;
989 rinfo[val].problem =
990 srp->header.masked_status &
991 srp->header.host_status &
992 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600993 if (srp->done)
994 rinfo[val].duration =
995 srp->header.duration;
996 else {
997 ms = jiffies_to_msecs(jiffies);
998 rinfo[val].duration =
999 (ms > srp->header.duration) ?
1000 (ms - srp->header.duration) : 0;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001003 rinfo[val].sg_io_owned =
1004 srp->sg_io_owned;
1005 rinfo[val].pack_id =
1006 srp->header.pack_id;
1007 rinfo[val].usr_ptr =
1008 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 }
1011 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001012 result = __copy_to_user(p, rinfo,
1013 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1014 result = result ? -EFAULT : 0;
1015 kfree(rinfo);
1016 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 case SG_EMULATED_HOST:
1019 if (sdp->detached)
1020 return -ENODEV;
1021 return put_user(sdp->device->host->hostt->emulated, ip);
1022 case SG_SCSI_RESET:
1023 if (sdp->detached)
1024 return -ENODEV;
1025 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001026 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EBUSY;
1028 } else if (!scsi_block_when_processing_errors(sdp->device))
1029 return -EBUSY;
1030 result = get_user(val, ip);
1031 if (result)
1032 return result;
1033 if (SG_SCSI_RESET_NOTHING == val)
1034 return 0;
1035 switch (val) {
1036 case SG_SCSI_RESET_DEVICE:
1037 val = SCSI_TRY_RESET_DEVICE;
1038 break;
1039 case SG_SCSI_RESET_BUS:
1040 val = SCSI_TRY_RESET_BUS;
1041 break;
1042 case SG_SCSI_RESET_HOST:
1043 val = SCSI_TRY_RESET_HOST;
1044 break;
1045 default:
1046 return -EINVAL;
1047 }
1048 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1049 return -EACCES;
1050 return (scsi_reset_provider(sdp->device, val) ==
1051 SUCCESS) ? 0 : -EIO;
1052 case SCSI_IOCTL_SEND_COMMAND:
1053 if (sdp->detached)
1054 return -ENODEV;
1055 if (read_only) {
1056 unsigned char opcode = WRITE_6;
1057 Scsi_Ioctl_Command __user *siocp = p;
1058
1059 if (copy_from_user(&opcode, siocp->data, 1))
1060 return -EFAULT;
1061 if (!sg_allow_access(opcode, sdp->device->type))
1062 return -EPERM;
1063 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001064 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 case SG_SET_DEBUG:
1066 result = get_user(val, ip);
1067 if (result)
1068 return result;
1069 sdp->sgdebug = (char) val;
1070 return 0;
1071 case SCSI_IOCTL_GET_IDLUN:
1072 case SCSI_IOCTL_GET_BUS_NUMBER:
1073 case SCSI_IOCTL_PROBE_HOST:
1074 case SG_GET_TRANSFORM:
1075 if (sdp->detached)
1076 return -ENODEV;
1077 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001078 case BLKSECTGET:
1079 return put_user(sdp->device->request_queue->max_sectors * 512,
1080 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001081 case BLKTRACESETUP:
1082 return blk_trace_setup(sdp->device->request_queue,
1083 sdp->disk->disk_name,
1084 sdp->device->sdev_gendev.devt,
1085 (char *)arg);
1086 case BLKTRACESTART:
1087 return blk_trace_startstop(sdp->device->request_queue, 1);
1088 case BLKTRACESTOP:
1089 return blk_trace_startstop(sdp->device->request_queue, 0);
1090 case BLKTRACETEARDOWN:
1091 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 default:
1093 if (read_only)
1094 return -EPERM; /* don't know so take safe approach */
1095 return scsi_ioctl(sdp->device, cmd_in, p);
1096 }
1097}
1098
1099#ifdef CONFIG_COMPAT
1100static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1101{
1102 Sg_device *sdp;
1103 Sg_fd *sfp;
1104 struct scsi_device *sdev;
1105
1106 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1107 return -ENXIO;
1108
1109 sdev = sdp->device;
1110 if (sdev->host->hostt->compat_ioctl) {
1111 int ret;
1112
1113 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1114
1115 return ret;
1116 }
1117
1118 return -ENOIOCTLCMD;
1119}
1120#endif
1121
1122static unsigned int
1123sg_poll(struct file *filp, poll_table * wait)
1124{
1125 unsigned int res = 0;
1126 Sg_device *sdp;
1127 Sg_fd *sfp;
1128 Sg_request *srp;
1129 int count = 0;
1130 unsigned long iflags;
1131
1132 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1133 || sfp->closed)
1134 return POLLERR;
1135 poll_wait(filp, &sfp->read_wait, wait);
1136 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1137 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1138 /* if any read waiting, flag it */
1139 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1140 res = POLLIN | POLLRDNORM;
1141 ++count;
1142 }
1143 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1144
1145 if (sdp->detached)
1146 res |= POLLHUP;
1147 else if (!sfp->cmd_q) {
1148 if (0 == count)
1149 res |= POLLOUT | POLLWRNORM;
1150 } else if (count < SG_MAX_QUEUE)
1151 res |= POLLOUT | POLLWRNORM;
1152 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1153 sdp->disk->disk_name, (int) res));
1154 return res;
1155}
1156
1157static int
1158sg_fasync(int fd, struct file *filp, int mode)
1159{
1160 int retval;
1161 Sg_device *sdp;
1162 Sg_fd *sfp;
1163
1164 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1165 return -ENXIO;
1166 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1167 sdp->disk->disk_name, mode));
1168
1169 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1170 return (retval < 0) ? retval : 0;
1171}
1172
Nick Piggina13ff0b2008-02-07 18:46:06 -08001173static int
1174sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001177 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001179 struct scatterlist *sg;
1180 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001183 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001185 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001187 return VM_FAULT_SIGBUS;
1188 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001190 sg = rsv_schp->buffer;
1191 sa = vma->vm_start;
1192 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001193 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001194 len = vma->vm_end - sa;
1195 len = (len < sg->length) ? len : sg->length;
1196 if (offset < len) {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001197 struct page *page;
Jens Axboe45711f12007-10-22 21:19:53 +02001198 page = virt_to_page(page_address(sg_page(sg)) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001199 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001200 vmf->page = page;
1201 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Mike Christied6b10342005-11-08 04:06:41 -06001203 sa += len;
1204 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Mike Christied6b10342005-11-08 04:06:41 -06001206
Nick Piggina13ff0b2008-02-07 18:46:06 -08001207 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001211 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212};
1213
1214static int
1215sg_mmap(struct file *filp, struct vm_area_struct *vma)
1216{
1217 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001218 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001220 int k;
1221 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1224 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001225 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1227 (void *) vma->vm_start, (int) req_sz));
1228 if (vma->vm_pgoff)
1229 return -EINVAL; /* want no offset */
1230 rsv_schp = &sfp->reserve;
1231 if (req_sz > rsv_schp->bufflen)
1232 return -ENOMEM; /* cannot map more than reserved buffer */
1233
Mike Christied6b10342005-11-08 04:06:41 -06001234 sa = vma->vm_start;
1235 sg = rsv_schp->buffer;
1236 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001237 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001238 len = vma->vm_end - sa;
1239 len = (len < sg->length) ? len : sg->length;
1240 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Mike Christied6b10342005-11-08 04:06:41 -06001242
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001243 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001244 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 vma->vm_private_data = sfp;
1246 vma->vm_ops = &sg_mmap_vm_ops;
1247 return 0;
1248}
1249
1250/* This function is a "bottom half" handler that is called by the
1251 * mid level when a command is completed (or has failed). */
1252static void
Mike Christied6b10342005-11-08 04:06:41 -06001253sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Mike Christied6b10342005-11-08 04:06:41 -06001255 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 Sg_device *sdp = NULL;
1257 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001259 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (NULL == srp) {
1262 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
1264 }
1265 sfp = srp->parentfp;
1266 if (sfp)
1267 sdp = sfp->parentdp;
1268 if ((NULL == sdp) || sdp->detached) {
1269 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return;
1271 }
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001275 sdp->disk->disk_name, srp->header.pack_id, result));
1276 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001277 ms = jiffies_to_msecs(jiffies);
1278 srp->header.duration = (ms > srp->header.duration) ?
1279 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001280 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct scsi_sense_hdr sshdr;
1282
Mike Christied6b10342005-11-08 04:06:41 -06001283 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1284 srp->header.status = 0xff & result;
1285 srp->header.masked_status = status_byte(result);
1286 srp->header.msg_status = msg_byte(result);
1287 srp->header.host_status = host_byte(result);
1288 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if ((sdp->sgdebug > 0) &&
1290 ((CHECK_CONDITION == srp->header.masked_status) ||
1291 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001292 __scsi_print_sense("sg_cmd_done", sense,
1293 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001296 if (driver_byte(result) != 0
1297 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 && !scsi_sense_is_deferred(&sshdr)
1299 && sshdr.sense_key == UNIT_ATTENTION
1300 && sdp->device->removable) {
1301 /* Detected possible disc change. Set the bit - this */
1302 /* may be used if there are filesystems using this device */
1303 sdp->device->changed = 1;
1304 }
1305 }
1306 /* Rely on write phase to clean out srp status values, so no "else" */
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (sfp->closed) { /* whoops this fd already released, cleanup */
1309 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1310 sg_finish_rem_req(srp);
1311 srp = NULL;
1312 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001313 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1315 scsi_device_put(sdp->device);
1316 }
1317 sfp = NULL;
1318 }
1319 } else if (srp && srp->orphan) {
1320 if (sfp->keep_orphan)
1321 srp->sg_io_owned = 0;
1322 else {
1323 sg_finish_rem_req(srp);
1324 srp = NULL;
1325 }
1326 }
1327 if (sfp && srp) {
1328 /* Now wake up any sg_read() that is waiting for this packet. */
1329 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1330 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1331 srp->done = 1;
1332 wake_up_interruptible(&sfp->read_wait);
1333 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1334 }
1335}
1336
1337static struct file_operations sg_fops = {
1338 .owner = THIS_MODULE,
1339 .read = sg_read,
1340 .write = sg_write,
1341 .poll = sg_poll,
1342 .ioctl = sg_ioctl,
1343#ifdef CONFIG_COMPAT
1344 .compat_ioctl = sg_compat_ioctl,
1345#endif
1346 .open = sg_open,
1347 .mmap = sg_mmap,
1348 .release = sg_release,
1349 .fasync = sg_fasync,
1350};
1351
gregkh@suse.ded2538782005-03-23 09:55:22 -08001352static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354static int sg_sysfs_valid = 0;
1355
James Bottomley7c07d612007-08-05 13:36:11 -05001356static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Mike Christied6b10342005-11-08 04:06:41 -06001358 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 Sg_device *sdp;
1360 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001361 int error;
1362 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Jes Sorensen24669f752006-01-16 10:31:18 -05001364 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!sdp) {
1366 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001367 return ERR_PTR(-ENOMEM);
1368 }
1369 error = -ENOMEM;
1370 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1371 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
James Bottomley7c07d612007-08-05 13:36:11 -05001375 write_lock_irqsave(&sg_index_lock, iflags);
1376 error = idr_get_new(&sg_index_idr, sdp, &k);
1377 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
James Bottomley7c07d612007-08-05 13:36:11 -05001379 if (error) {
1380 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1381 error);
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (unlikely(k >= SG_MAX_DEVS))
1386 goto overflow;
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1389 sprintf(disk->disk_name, "sg%d", k);
1390 disk->first_minor = k;
1391 sdp->disk = disk;
1392 sdp->device = scsidp;
1393 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001394 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001395 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
James Bottomley7c07d612007-08-05 13:36:11 -05001397 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001399 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001401 return ERR_PTR(error);
1402 }
1403 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001406 sdev_printk(KERN_WARNING, scsidp,
1407 "Unable to attach sg device type=%d, minor "
1408 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 error = -ENODEV;
1410 goto out;
1411}
1412
1413static int
Tony Jonesee959b02008-02-22 00:13:36 +01001414sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Tony Jonesee959b02008-02-22 00:13:36 +01001416 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct gendisk *disk;
1418 Sg_device *sdp = NULL;
1419 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001420 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001421 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 disk = alloc_disk(1);
1424 if (!disk) {
1425 printk(KERN_WARNING "alloc_disk failed\n");
1426 return -ENOMEM;
1427 }
1428 disk->major = SCSI_GENERIC_MAJOR;
1429
1430 error = -ENOMEM;
1431 cdev = cdev_alloc();
1432 if (!cdev) {
1433 printk(KERN_WARNING "cdev_alloc failed\n");
1434 goto out;
1435 }
1436 cdev->owner = THIS_MODULE;
1437 cdev->ops = &sg_fops;
1438
James Bottomley7c07d612007-08-05 13:36:11 -05001439 sdp = sg_alloc(disk, scsidp);
1440 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001442 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto out;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
James Bottomley7c07d612007-08-05 13:36:11 -05001446 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001447 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001448 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 sdp->cdev = cdev;
1451 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001452 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Tony Jonesee959b02008-02-22 00:13:36 +01001454 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1455 MKDEV(SCSI_GENERIC_MAJOR,
1456 sdp->index),
1457 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001458 if (IS_ERR(sg_class_member)) {
1459 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001460 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001461 error = PTR_ERR(sg_class_member);
1462 goto cdev_add_err;
1463 }
Tony Jonesee959b02008-02-22 00:13:36 +01001464 dev_set_drvdata(sg_class_member, sdp);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001465 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 &sg_class_member->kobj, "generic");
1467 if (error)
1468 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001469 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001471 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
James Bottomley9ccfc752005-10-02 11:45:08 -05001473 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001474 "Attached scsi generic sg%d type %d\n", sdp->index,
1475 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Tony Jonesee959b02008-02-22 00:13:36 +01001477 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001481cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001482 write_lock_irqsave(&sg_index_lock, iflags);
1483 idr_remove(&sg_index_idr, sdp->index);
1484 write_unlock_irqrestore(&sg_index_lock, iflags);
1485 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487out:
1488 put_disk(disk);
1489 if (cdev)
1490 cdev_del(cdev);
1491 return error;
1492}
1493
1494static void
Tony Jonesee959b02008-02-22 00:13:36 +01001495sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Tony Jonesee959b02008-02-22 00:13:36 +01001497 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1498 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 unsigned long iflags;
1500 Sg_fd *sfp;
1501 Sg_fd *tsfp;
1502 Sg_request *srp;
1503 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001504 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
James Bottomley7c07d612007-08-05 13:36:11 -05001506 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
James Bottomley7c07d612007-08-05 13:36:11 -05001509 delay = 0;
1510 write_lock_irqsave(&sg_index_lock, iflags);
1511 if (sdp->headfp) {
1512 sdp->detached = 1;
1513 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1514 tsfp = sfp->nextfp;
1515 for (srp = sfp->headrp; srp; srp = tsrp) {
1516 tsrp = srp->nextrp;
1517 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1518 sg_finish_rem_req(srp);
1519 }
1520 if (sfp->closed) {
1521 scsi_device_put(sdp->device);
1522 __sg_remove_sfp(sdp, sfp);
1523 } else {
1524 delay = 1;
1525 wake_up_interruptible(&sfp->read_wait);
1526 kill_fasync(&sfp->async_qp, SIGPOLL,
1527 POLL_HUP);
1528 }
1529 }
1530 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1531 if (NULL == sdp->headfp) {
1532 idr_remove(&sg_index_idr, sdp->index);
1533 }
1534 } else { /* nothing active, simple case */
1535 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1536 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
James Bottomley7c07d612007-08-05 13:36:11 -05001538 write_unlock_irqrestore(&sg_index_lock, iflags);
1539
1540 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001541 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001542 cdev_del(sdp->cdev);
1543 sdp->cdev = NULL;
1544 put_disk(sdp->disk);
1545 sdp->disk = NULL;
1546 if (NULL == sdp->headfp)
1547 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (delay)
1550 msleep(10); /* dirty detach so delay device destruction */
1551}
1552
Douglas Gilbert6460e752006-09-20 18:20:49 -04001553module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1554module_param_named(def_reserved_size, def_reserved_size, int,
1555 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1557
1558MODULE_AUTHOR("Douglas Gilbert");
1559MODULE_DESCRIPTION("SCSI generic (sg) driver");
1560MODULE_LICENSE("GPL");
1561MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001562MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Douglas Gilbert6460e752006-09-20 18:20:49 -04001564MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1565 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1567MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1568
1569static int __init
1570init_sg(void)
1571{
1572 int rc;
1573
Douglas Gilbert6460e752006-09-20 18:20:49 -04001574 if (scatter_elem_sz < PAGE_SIZE) {
1575 scatter_elem_sz = PAGE_SIZE;
1576 scatter_elem_sz_prev = scatter_elem_sz;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (def_reserved_size >= 0)
1579 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001580 else
1581 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1584 SG_MAX_DEVS, "sg");
1585 if (rc)
1586 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001587 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if ( IS_ERR(sg_sysfs_class) ) {
1589 rc = PTR_ERR(sg_sysfs_class);
1590 goto err_out;
1591 }
1592 sg_sysfs_valid = 1;
1593 rc = scsi_register_interface(&sg_interface);
1594 if (0 == rc) {
1595#ifdef CONFIG_SCSI_PROC_FS
1596 sg_proc_init();
1597#endif /* CONFIG_SCSI_PROC_FS */
1598 return 0;
1599 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001600 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601err_out:
1602 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1603 return rc;
1604}
1605
1606static void __exit
1607exit_sg(void)
1608{
1609#ifdef CONFIG_SCSI_PROC_FS
1610 sg_proc_cleanup();
1611#endif /* CONFIG_SCSI_PROC_FS */
1612 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001613 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 sg_sysfs_valid = 0;
1615 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1616 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001617 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620static int
1621sg_start_req(Sg_request * srp)
1622{
1623 int res;
1624 Sg_fd *sfp = srp->parentfp;
1625 sg_io_hdr_t *hp = &srp->header;
1626 int dxfer_len = (int) hp->dxfer_len;
1627 int dxfer_dir = hp->dxfer_direction;
1628 Sg_scatter_hold *req_schp = &srp->data;
1629 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1630
1631 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1632 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1633 return 0;
1634 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1635 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1636 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1637 res = sg_build_direct(srp, sfp, dxfer_len);
1638 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1639 return res;
1640 }
1641 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1642 sg_link_reserve(sfp, srp, dxfer_len);
1643 else {
1644 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1645 if (res) {
1646 sg_remove_scat(req_schp);
1647 return res;
1648 }
1649 }
1650 return 0;
1651}
1652
1653static void
1654sg_finish_rem_req(Sg_request * srp)
1655{
1656 Sg_fd *sfp = srp->parentfp;
1657 Sg_scatter_hold *req_schp = &srp->data;
1658
1659 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1660 if (srp->res_used)
1661 sg_unlink_reserve(sfp, srp);
1662 else
1663 sg_remove_scat(req_schp);
1664 sg_remove_request(sfp, srp);
1665}
1666
1667static int
1668sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1669{
Mike Christied6b10342005-11-08 04:06:41 -06001670 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001671 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Mike Christied6b10342005-11-08 04:06:41 -06001673 /*
1674 * TODO: test without low_dma, we should not need it since
1675 * the block layer will bounce the buffer for us
1676 *
1677 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1678 */
1679 if (sfp->low_dma)
1680 gfp_flags |= GFP_DMA;
1681 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (!schp->buffer)
1683 return -ENOMEM;
Anton Blanchard30fa0d02007-10-26 14:00:14 +02001684 sg_init_table(schp->buffer, tablesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001686 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689#ifdef SG_ALLOW_DIO_CODE
1690/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001691 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1694 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 (i.e., either completely successful or fails)
1696*/
1697static int
1698st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001699 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001701 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1702 unsigned long start = uaddr >> PAGE_SHIFT;
1703 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct page **pages;
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /* User attempted Overflow! */
1708 if ((uaddr + count) < uaddr)
1709 return -EINVAL;
1710
1711 /* Too big */
1712 if (nr_pages > max_pages)
1713 return -ENOMEM;
1714
1715 /* Hmm? */
1716 if (count == 0)
1717 return 0;
1718
1719 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1720 return -ENOMEM;
1721
1722 /* Try to fault in all of the necessary pages */
1723 down_read(&current->mm->mmap_sem);
1724 /* rw==READ means read from drive, write into memory area */
1725 res = get_user_pages(
1726 current,
1727 current->mm,
1728 uaddr,
1729 nr_pages,
1730 rw == READ,
1731 0, /* don't force */
1732 pages,
1733 NULL);
1734 up_read(&current->mm->mmap_sem);
1735
1736 /* Errors and no page mapped should return here */
1737 if (res < nr_pages)
1738 goto out_unmap;
1739
1740 for (i=0; i < nr_pages; i++) {
1741 /* FIXME: flush superflous for rw==READ,
1742 * probably wrong function for rw==WRITE
1743 */
1744 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* ?? Is locking needed? I don't think so */
1746 /* if (TestSetPageLocked(pages[i]))
1747 goto out_unlock; */
1748 }
1749
Jens Axboe642f1492007-10-24 11:20:47 +02001750 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (nr_pages > 1) {
1752 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1753 count -= sgl[0].length;
Jens Axboe642f1492007-10-24 11:20:47 +02001754 for (i=1; i < nr_pages ; i++)
1755 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 else {
1758 sgl[0].length = count;
1759 }
1760
1761 kfree(pages);
1762 return nr_pages;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001765 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 for (j=0; j < res; j++)
1767 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001768 res = 0;
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 kfree(pages);
1771 return res;
1772}
1773
1774
1775/* And unmap them... */
1776static int
1777st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1778 int dirtied)
1779{
1780 int i;
1781
1782 for (i=0; i < nr_pages; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +02001783 struct page *page = sg_page(&sgl[i]);
Nick Pigginb5810032005-10-29 18:16:12 -07001784
Nick Pigginb5810032005-10-29 18:16:12 -07001785 if (dirtied)
1786 SetPageDirty(page);
1787 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /* FIXME: cache flush missing for rw==READ
1789 * FIXME: call the correct reference counting function
1790 */
Nick Pigginb5810032005-10-29 18:16:12 -07001791 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
1794 return 0;
1795}
1796
1797/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1798#endif
1799
1800
1801/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1802static int
1803sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1804{
1805#ifdef SG_ALLOW_DIO_CODE
1806 sg_io_hdr_t *hp = &srp->header;
1807 Sg_scatter_hold *schp = &srp->data;
1808 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int mx_sc_elems, res;
1810 struct scsi_device *sdev = sfp->parentdp->device;
1811
1812 if (((unsigned long)hp->dxferp &
1813 queue_dma_alignment(sdev->request_queue)) != 0)
1814 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1817 if (mx_sc_elems <= 0) {
1818 return 1;
1819 }
Mike Christied6b10342005-11-08 04:06:41 -06001820 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1821 (unsigned long)hp->dxferp, dxfer_len,
1822 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001823 if (res <= 0) {
1824 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 schp->k_use_sg = res;
1828 schp->dio_in_use = 1;
1829 hp->info |= SG_INFO_DIRECT_IO;
1830 return 0;
1831#else
1832 return 1;
1833#endif
1834}
1835
1836static int
1837sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1838{
Mike Christied6b10342005-11-08 04:06:41 -06001839 struct scatterlist *sg;
1840 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1841 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001843 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Eric Sesterhennfb119932007-05-23 14:41:36 -07001845 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EFAULT;
1847 if (0 == blk_size)
1848 ++blk_size; /* don't know why */
1849/* round request up to next highest SG_SECTOR_SZ byte boundary */
1850 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1851 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1852 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001853
1854 /* N.B. ret_sz carried into this block ... */
1855 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1856 if (mx_sc_elems < 0)
1857 return mx_sc_elems; /* most likely -ENOMEM */
1858
Douglas Gilbert6460e752006-09-20 18:20:49 -04001859 num = scatter_elem_sz;
1860 if (unlikely(num != scatter_elem_sz_prev)) {
1861 if (num < PAGE_SIZE) {
1862 scatter_elem_sz = PAGE_SIZE;
1863 scatter_elem_sz_prev = PAGE_SIZE;
1864 } else
1865 scatter_elem_sz_prev = num;
1866 }
Mike Christied6b10342005-11-08 04:06:41 -06001867 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1868 (rem_sz > 0) && (k < mx_sc_elems);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001869 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001870
Douglas Gilbert6460e752006-09-20 18:20:49 -04001871 num = (rem_sz > scatter_elem_sz_prev) ?
1872 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001873 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (!p)
1875 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Douglas Gilbert6460e752006-09-20 18:20:49 -04001877 if (num == scatter_elem_sz_prev) {
1878 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1879 scatter_elem_sz = ret_sz;
1880 scatter_elem_sz_prev = ret_sz;
1881 }
1882 }
Jens Axboe642f1492007-10-24 11:20:47 +02001883 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001885 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1886 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001887 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Mike Christied6b10342005-11-08 04:06:41 -06001889 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001890 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1891 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001892
1893 schp->bufflen = blk_size;
1894 if (rem_sz > 0) /* must have failed */
1895 return -ENOMEM;
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return 0;
1898}
1899
1900static int
1901sg_write_xfer(Sg_request * srp)
1902{
1903 sg_io_hdr_t *hp = &srp->header;
1904 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001905 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int num_xfer = 0;
1907 int j, k, onum, usglen, ksglen, res;
1908 int iovec_count = (int) hp->iovec_count;
1909 int dxfer_dir = hp->dxfer_direction;
1910 unsigned char *p;
1911 unsigned char __user *up;
1912 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1913
1914 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1915 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1916 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1917 if (schp->bufflen < num_xfer)
1918 num_xfer = schp->bufflen;
1919 }
1920 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1921 (new_interface
1922 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1923 return 0;
1924
1925 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1926 num_xfer, iovec_count, schp->k_use_sg));
1927 if (iovec_count) {
1928 onum = iovec_count;
1929 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1930 return -EFAULT;
1931 } else
1932 onum = 1;
1933
Mike Christied6b10342005-11-08 04:06:41 -06001934 ksglen = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001935 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06001936 for (j = 0, k = 0; j < onum; ++j) {
1937 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1938 if (res)
1939 return res;
1940
Jens Axboeb0f655d2007-05-11 15:01:01 +02001941 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02001942 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06001943 if (usglen <= 0)
1944 break;
1945 if (ksglen > usglen) {
1946 if (usglen >= num_xfer) {
1947 if (__copy_from_user(p, up, num_xfer))
1948 return -EFAULT;
1949 return 0;
1950 }
1951 if (__copy_from_user(p, up, usglen))
1952 return -EFAULT;
1953 p += usglen;
1954 ksglen -= usglen;
1955 break;
1956 } else {
1957 if (ksglen >= num_xfer) {
1958 if (__copy_from_user(p, up, num_xfer))
1959 return -EFAULT;
1960 return 0;
1961 }
1962 if (__copy_from_user(p, up, ksglen))
1963 return -EFAULT;
1964 up += ksglen;
1965 usglen -= ksglen;
1966 }
1967 ++k;
1968 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return 0;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Mike Christied6b10342005-11-08 04:06:41 -06001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974}
1975
1976static int
1977sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1978 int wr_xf, int *countp, unsigned char __user **up)
1979{
1980 int num_xfer = (int) hp->dxfer_len;
1981 unsigned char __user *p = hp->dxferp;
1982 int count;
1983
1984 if (0 == sg_num) {
1985 if (wr_xf && ('\0' == hp->interface_id))
1986 count = (int) hp->flags; /* holds "old" input_size */
1987 else
1988 count = num_xfer;
1989 } else {
1990 sg_iovec_t iovec;
1991 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1992 return -EFAULT;
1993 p = iovec.iov_base;
1994 count = (int) iovec.iov_len;
1995 }
1996 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1997 return -EFAULT;
1998 if (up)
1999 *up = p;
2000 if (countp)
2001 *countp = count;
2002 return 0;
2003}
2004
2005static void
2006sg_remove_scat(Sg_scatter_hold * schp)
2007{
2008 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2009 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002010 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 if (schp->dio_in_use) {
2013#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06002014 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015#endif
2016 } else {
2017 int k;
2018
Jens Axboe45711f12007-10-22 21:19:53 +02002019 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
Jens Axboeb0f655d2007-05-11 15:01:01 +02002020 ++k, sg = sg_next(sg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 SCSI_LOG_TIMEOUT(5, printk(
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04002022 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
Jens Axboe45711f12007-10-22 21:19:53 +02002023 k, sg_page(sg), sg->length));
2024 sg_page_free(sg_page(sg), sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 }
Mike Christied6b10342005-11-08 04:06:41 -06002027 kfree(schp->buffer);
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 memset(schp, 0, sizeof (*schp));
2030}
2031
2032static int
2033sg_read_xfer(Sg_request * srp)
2034{
2035 sg_io_hdr_t *hp = &srp->header;
2036 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002037 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int num_xfer = 0;
2039 int j, k, onum, usglen, ksglen, res;
2040 int iovec_count = (int) hp->iovec_count;
2041 int dxfer_dir = hp->dxfer_direction;
2042 unsigned char *p;
2043 unsigned char __user *up;
2044 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2045
2046 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2047 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2048 num_xfer = hp->dxfer_len;
2049 if (schp->bufflen < num_xfer)
2050 num_xfer = schp->bufflen;
2051 }
2052 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2053 (new_interface
2054 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2055 return 0;
2056
2057 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2058 num_xfer, iovec_count, schp->k_use_sg));
2059 if (iovec_count) {
2060 onum = iovec_count;
2061 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2062 return -EFAULT;
2063 } else
2064 onum = 1;
2065
Jens Axboe45711f12007-10-22 21:19:53 +02002066 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06002067 ksglen = sg->length;
2068 for (j = 0, k = 0; j < onum; ++j) {
2069 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2070 if (res)
2071 return res;
2072
Jens Axboeb0f655d2007-05-11 15:01:01 +02002073 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02002074 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06002075 if (usglen <= 0)
2076 break;
2077 if (ksglen > usglen) {
2078 if (usglen >= num_xfer) {
2079 if (__copy_to_user(up, p, num_xfer))
2080 return -EFAULT;
2081 return 0;
2082 }
2083 if (__copy_to_user(up, p, usglen))
2084 return -EFAULT;
2085 p += usglen;
2086 ksglen -= usglen;
2087 break;
2088 } else {
2089 if (ksglen >= num_xfer) {
2090 if (__copy_to_user(up, p, num_xfer))
2091 return -EFAULT;
2092 return 0;
2093 }
2094 if (__copy_to_user(up, p, ksglen))
2095 return -EFAULT;
2096 up += ksglen;
2097 usglen -= ksglen;
2098 }
2099 ++k;
2100 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return 0;
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Mike Christied6b10342005-11-08 04:06:41 -06002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
2106}
2107
2108static int
2109sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2110{
2111 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002112 struct scatterlist *sg = schp->buffer;
2113 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2116 num_read_xfer));
2117 if ((!outp) || (num_read_xfer <= 0))
2118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jens Axboe45711f12007-10-22 21:19:53 +02002120 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002121 num = sg->length;
2122 if (num > num_read_xfer) {
Jens Axboe45711f12007-10-22 21:19:53 +02002123 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002124 num_read_xfer))
2125 return -EFAULT;
2126 break;
2127 } else {
Jens Axboe45711f12007-10-22 21:19:53 +02002128 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002129 num))
2130 return -EFAULT;
2131 num_read_xfer -= num;
2132 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 break;
Mike Christied6b10342005-11-08 04:06:41 -06002134 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
Mike Christied6b10342005-11-08 04:06:41 -06002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
2139}
2140
2141static void
2142sg_build_reserve(Sg_fd * sfp, int req_size)
2143{
2144 Sg_scatter_hold *schp = &sfp->reserve;
2145
2146 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2147 do {
2148 if (req_size < PAGE_SIZE)
2149 req_size = PAGE_SIZE;
2150 if (0 == sg_build_indirect(schp, sfp, req_size))
2151 return;
2152 else
2153 sg_remove_scat(schp);
2154 req_size >>= 1; /* divide by 2 */
2155 } while (req_size > (PAGE_SIZE / 2));
2156}
2157
2158static void
2159sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2160{
2161 Sg_scatter_hold *req_schp = &srp->data;
2162 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002163 struct scatterlist *sg = rsv_schp->buffer;
2164 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 srp->res_used = 1;
2167 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002168 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Jens Axboeb0f655d2007-05-11 15:01:01 +02002170 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002171 num = sg->length;
2172 if (rem <= num) {
2173 sfp->save_scat_len = num;
2174 sg->length = rem;
2175 req_schp->k_use_sg = k + 1;
2176 req_schp->sglist_len = rsv_schp->sglist_len;
2177 req_schp->buffer = rsv_schp->buffer;
2178
2179 req_schp->bufflen = size;
2180 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2181 break;
2182 } else
2183 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Mike Christied6b10342005-11-08 04:06:41 -06002185
2186 if (k >= rsv_schp->k_use_sg)
2187 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
2190static void
2191sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2192{
2193 Sg_scatter_hold *req_schp = &srp->data;
2194 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2195
2196 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2197 (int) req_schp->k_use_sg));
2198 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002199 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002202 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 (unsigned) sfp->save_scat_len;
2204 else
2205 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2206 }
2207 req_schp->k_use_sg = 0;
2208 req_schp->bufflen = 0;
2209 req_schp->buffer = NULL;
2210 req_schp->sglist_len = 0;
2211 sfp->save_scat_len = 0;
2212 srp->res_used = 0;
2213}
2214
2215static Sg_request *
2216sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2217{
2218 Sg_request *resp;
2219 unsigned long iflags;
2220
2221 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2222 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2223 /* look for requests that are ready + not SG_IO owned */
2224 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2225 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2226 resp->done = 2; /* guard against other readers */
2227 break;
2228 }
2229 }
2230 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2231 return resp;
2232}
2233
2234#ifdef CONFIG_SCSI_PROC_FS
2235static Sg_request *
2236sg_get_nth_request(Sg_fd * sfp, int nth)
2237{
2238 Sg_request *resp;
2239 unsigned long iflags;
2240 int k;
2241
2242 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2243 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2244 ++k, resp = resp->nextrp) ;
2245 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2246 return resp;
2247}
2248#endif
2249
2250/* always adds to end of list */
2251static Sg_request *
2252sg_add_request(Sg_fd * sfp)
2253{
2254 int k;
2255 unsigned long iflags;
2256 Sg_request *resp;
2257 Sg_request *rp = sfp->req_arr;
2258
2259 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2260 resp = sfp->headrp;
2261 if (!resp) {
2262 memset(rp, 0, sizeof (Sg_request));
2263 rp->parentfp = sfp;
2264 resp = rp;
2265 sfp->headrp = resp;
2266 } else {
2267 if (0 == sfp->cmd_q)
2268 resp = NULL; /* command queuing disallowed */
2269 else {
2270 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2271 if (!rp->parentfp)
2272 break;
2273 }
2274 if (k < SG_MAX_QUEUE) {
2275 memset(rp, 0, sizeof (Sg_request));
2276 rp->parentfp = sfp;
2277 while (resp->nextrp)
2278 resp = resp->nextrp;
2279 resp->nextrp = rp;
2280 resp = rp;
2281 } else
2282 resp = NULL;
2283 }
2284 }
2285 if (resp) {
2286 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002287 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2290 return resp;
2291}
2292
2293/* Return of 1 for found; 0 for not found */
2294static int
2295sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2296{
2297 Sg_request *prev_rp;
2298 Sg_request *rp;
2299 unsigned long iflags;
2300 int res = 0;
2301
2302 if ((!sfp) || (!srp) || (!sfp->headrp))
2303 return res;
2304 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2305 prev_rp = sfp->headrp;
2306 if (srp == prev_rp) {
2307 sfp->headrp = prev_rp->nextrp;
2308 prev_rp->parentfp = NULL;
2309 res = 1;
2310 } else {
2311 while ((rp = prev_rp->nextrp)) {
2312 if (srp == rp) {
2313 prev_rp->nextrp = rp->nextrp;
2314 rp->parentfp = NULL;
2315 res = 1;
2316 break;
2317 }
2318 prev_rp = rp;
2319 }
2320 }
2321 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2322 return res;
2323}
2324
2325#ifdef CONFIG_SCSI_PROC_FS
2326static Sg_fd *
2327sg_get_nth_sfp(Sg_device * sdp, int nth)
2328{
2329 Sg_fd *resp;
2330 unsigned long iflags;
2331 int k;
2332
James Bottomley7c07d612007-08-05 13:36:11 -05002333 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2335 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002336 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return resp;
2338}
2339#endif
2340
2341static Sg_fd *
2342sg_add_sfp(Sg_device * sdp, int dev)
2343{
2344 Sg_fd *sfp;
2345 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002346 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Mike Christied6b10342005-11-08 04:06:41 -06002348 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (!sfp)
2350 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 init_waitqueue_head(&sfp->read_wait);
2353 rwlock_init(&sfp->rq_list_lock);
2354
2355 sfp->timeout = SG_DEFAULT_TIMEOUT;
2356 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2357 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2358 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2359 sdp->device->host->unchecked_isa_dma : 1;
2360 sfp->cmd_q = SG_DEF_COMMAND_Q;
2361 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2362 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002363 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (!sdp->headfp)
2365 sdp->headfp = sfp;
2366 else { /* add to tail of existing list */
2367 Sg_fd *pfp = sdp->headfp;
2368 while (pfp->nextfp)
2369 pfp = pfp->nextfp;
2370 pfp->nextfp = sfp;
2371 }
James Bottomley7c07d612007-08-05 13:36:11 -05002372 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002374 if (unlikely(sg_big_buff != def_reserved_size))
2375 sg_big_buff = def_reserved_size;
2376
Alan Stern44ec9542007-02-20 11:01:57 -05002377 bufflen = min_t(int, sg_big_buff,
2378 sdp->device->request_queue->max_sectors * 512);
2379 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2381 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2382 return sfp;
2383}
2384
2385static void
2386__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2387{
2388 Sg_fd *fp;
2389 Sg_fd *prev_fp;
2390
2391 prev_fp = sdp->headfp;
2392 if (sfp == prev_fp)
2393 sdp->headfp = prev_fp->nextfp;
2394 else {
2395 while ((fp = prev_fp->nextfp)) {
2396 if (sfp == fp) {
2397 prev_fp->nextfp = fp->nextfp;
2398 break;
2399 }
2400 prev_fp = fp;
2401 }
2402 }
2403 if (sfp->reserve.bufflen > 0) {
2404 SCSI_LOG_TIMEOUT(6,
2405 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2406 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 sg_remove_scat(&sfp->reserve);
2408 }
2409 sfp->parentdp = NULL;
2410 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002411 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
2414/* Returns 0 in normal case, 1 when detached and sdp object removed */
2415static int
2416sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2417{
2418 Sg_request *srp;
2419 Sg_request *tsrp;
2420 int dirty = 0;
2421 int res = 0;
2422
2423 for (srp = sfp->headrp; srp; srp = tsrp) {
2424 tsrp = srp->nextrp;
2425 if (sg_srp_done(srp, sfp))
2426 sg_finish_rem_req(srp);
2427 else
2428 ++dirty;
2429 }
2430 if (0 == dirty) {
2431 unsigned long iflags;
2432
James Bottomley7c07d612007-08-05 13:36:11 -05002433 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 __sg_remove_sfp(sdp, sfp);
2435 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002436 idr_remove(&sg_index_idr, sdp->index);
2437 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 res = 1;
2439 }
James Bottomley7c07d612007-08-05 13:36:11 -05002440 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 } else {
2442 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2443 /* only bump the access_count if we actually succeeded in
2444 * throwing another counter on the host module */
2445 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2446 sfp->closed = 1; /* flag dirty state on this fd */
2447 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2448 dirty));
2449 }
2450 return res;
2451}
2452
2453static int
2454sg_res_in_use(Sg_fd * sfp)
2455{
2456 const Sg_request *srp;
2457 unsigned long iflags;
2458
2459 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2460 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2461 if (srp->res_used)
2462 break;
2463 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2464 return srp ? 1 : 0;
2465}
2466
Douglas Gilbert6460e752006-09-20 18:20:49 -04002467/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002468static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2470{
Mike Christied6b10342005-11-08 04:06:41 -06002471 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002472 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002474 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Douglas Gilbert6460e752006-09-20 18:20:49 -04002476 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return resp;
2478
2479 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002480 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002482 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2485 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002486 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002487 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002488 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 --order;
2490 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002491 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 resSz = a_size;
2493 }
2494 if (resp) {
2495 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002496 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002497 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499 return resp;
2500}
2501
2502static void
Mike Christied6b10342005-11-08 04:06:41 -06002503sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 int order, a_size;
2506
Mike Christied6b10342005-11-08 04:06:41 -06002507 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return;
2509 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2510 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002511 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514#ifndef MAINTENANCE_IN_CMD
2515#define MAINTENANCE_IN_CMD 0xa3
2516#endif
2517
2518static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2519 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2520 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2521 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2522};
2523
2524static int
2525sg_allow_access(unsigned char opcode, char dev_type)
2526{
2527 int k;
2528
2529 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2530 return 1;
2531 for (k = 0; k < sizeof (allow_ops); ++k) {
2532 if (opcode == allow_ops[k])
2533 return 1;
2534 }
2535 return 0;
2536}
2537
2538#ifdef CONFIG_SCSI_PROC_FS
2539static int
James Bottomley7c07d612007-08-05 13:36:11 -05002540sg_idr_max_id(int id, void *p, void *data)
2541{
2542 int *k = data;
2543
2544 if (*k < id)
2545 *k = id;
2546
2547 return 0;
2548}
2549
2550static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551sg_last_dev(void)
2552{
Tony Battersby53474c02008-01-22 15:25:49 -05002553 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 unsigned long iflags;
2555
James Bottomley7c07d612007-08-05 13:36:11 -05002556 read_lock_irqsave(&sg_index_lock, iflags);
2557 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2558 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return k + 1; /* origin 1 */
2560}
2561#endif
2562
2563static Sg_device *
2564sg_get_dev(int dev)
2565{
James Bottomley7c07d612007-08-05 13:36:11 -05002566 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 unsigned long iflags;
2568
James Bottomley7c07d612007-08-05 13:36:11 -05002569 read_lock_irqsave(&sg_index_lock, iflags);
2570 sdp = idr_find(&sg_index_idr, dev);
2571 read_unlock_irqrestore(&sg_index_lock, iflags);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return sdp;
2574}
2575
2576#ifdef CONFIG_SCSI_PROC_FS
2577
2578static struct proc_dir_entry *sg_proc_sgp = NULL;
2579
2580static char sg_proc_sg_dirname[] = "scsi/sg";
2581
2582static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2583
2584static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2585static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2586 size_t count, loff_t *off);
2587static struct file_operations adio_fops = {
2588 /* .owner, .read and .llseek added in sg_proc_init() */
2589 .open = sg_proc_single_open_adio,
2590 .write = sg_proc_write_adio,
2591 .release = single_release,
2592};
2593
2594static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2595static ssize_t sg_proc_write_dressz(struct file *filp,
2596 const char __user *buffer, size_t count, loff_t *off);
2597static struct file_operations dressz_fops = {
2598 .open = sg_proc_single_open_dressz,
2599 .write = sg_proc_write_dressz,
2600 .release = single_release,
2601};
2602
2603static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2604static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2605static struct file_operations version_fops = {
2606 .open = sg_proc_single_open_version,
2607 .release = single_release,
2608};
2609
2610static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2611static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2612static struct file_operations devhdr_fops = {
2613 .open = sg_proc_single_open_devhdr,
2614 .release = single_release,
2615};
2616
2617static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2618static int sg_proc_open_dev(struct inode *inode, struct file *file);
2619static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2620static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2621static void dev_seq_stop(struct seq_file *s, void *v);
2622static struct file_operations dev_fops = {
2623 .open = sg_proc_open_dev,
2624 .release = seq_release,
2625};
2626static struct seq_operations dev_seq_ops = {
2627 .start = dev_seq_start,
2628 .next = dev_seq_next,
2629 .stop = dev_seq_stop,
2630 .show = sg_proc_seq_show_dev,
2631};
2632
2633static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2634static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2635static struct file_operations devstrs_fops = {
2636 .open = sg_proc_open_devstrs,
2637 .release = seq_release,
2638};
2639static struct seq_operations devstrs_seq_ops = {
2640 .start = dev_seq_start,
2641 .next = dev_seq_next,
2642 .stop = dev_seq_stop,
2643 .show = sg_proc_seq_show_devstrs,
2644};
2645
2646static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2647static int sg_proc_open_debug(struct inode *inode, struct file *file);
2648static struct file_operations debug_fops = {
2649 .open = sg_proc_open_debug,
2650 .release = seq_release,
2651};
2652static struct seq_operations debug_seq_ops = {
2653 .start = dev_seq_start,
2654 .next = dev_seq_next,
2655 .stop = dev_seq_stop,
2656 .show = sg_proc_seq_show_debug,
2657};
2658
2659
2660struct sg_proc_leaf {
2661 const char * name;
2662 struct file_operations * fops;
2663};
2664
2665static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2666 {"allow_dio", &adio_fops},
2667 {"debug", &debug_fops},
2668 {"def_reserved_size", &dressz_fops},
2669 {"device_hdr", &devhdr_fops},
2670 {"devices", &dev_fops},
2671 {"device_strs", &devstrs_fops},
2672 {"version", &version_fops}
2673};
2674
2675static int
2676sg_proc_init(void)
2677{
2678 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002679 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 struct sg_proc_leaf * leaf;
2681
Al Viro66600222005-09-28 22:32:57 +01002682 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (!sg_proc_sgp)
2684 return 1;
2685 for (k = 0; k < num_leaves; ++k) {
2686 leaf = &sg_proc_leaf_arr[k];
2687 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002688 leaf->fops->owner = THIS_MODULE;
2689 leaf->fops->read = seq_read;
2690 leaf->fops->llseek = seq_lseek;
2691 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693 return 0;
2694}
2695
2696static void
2697sg_proc_cleanup(void)
2698{
2699 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002700 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 if (!sg_proc_sgp)
2703 return;
2704 for (k = 0; k < num_leaves; ++k)
2705 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2706 remove_proc_entry(sg_proc_sg_dirname, NULL);
2707}
2708
2709
2710static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2711{
2712 seq_printf(s, "%d\n", *((int *)s->private));
2713 return 0;
2714}
2715
2716static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2717{
2718 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2719}
2720
2721static ssize_t
2722sg_proc_write_adio(struct file *filp, const char __user *buffer,
2723 size_t count, loff_t *off)
2724{
2725 int num;
2726 char buff[11];
2727
2728 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2729 return -EACCES;
2730 num = (count < 10) ? count : 10;
2731 if (copy_from_user(buff, buffer, num))
2732 return -EFAULT;
2733 buff[num] = '\0';
2734 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2735 return count;
2736}
2737
2738static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2739{
2740 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2741}
2742
2743static ssize_t
2744sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2745 size_t count, loff_t *off)
2746{
2747 int num;
2748 unsigned long k = ULONG_MAX;
2749 char buff[11];
2750
2751 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2752 return -EACCES;
2753 num = (count < 10) ? count : 10;
2754 if (copy_from_user(buff, buffer, num))
2755 return -EFAULT;
2756 buff[num] = '\0';
2757 k = simple_strtoul(buff, NULL, 10);
2758 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2759 sg_big_buff = k;
2760 return count;
2761 }
2762 return -ERANGE;
2763}
2764
2765static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2766{
2767 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2768 sg_version_date);
2769 return 0;
2770}
2771
2772static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2773{
2774 return single_open(file, sg_proc_seq_show_version, NULL);
2775}
2776
2777static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2778{
2779 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2780 "online\n");
2781 return 0;
2782}
2783
2784static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2785{
2786 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2787}
2788
2789struct sg_proc_deviter {
2790 loff_t index;
2791 size_t max;
2792};
2793
2794static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2795{
2796 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2797
Jan Blunck729d70f2005-08-27 11:07:52 -07002798 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (! it)
2800 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 it->index = *pos;
2803 it->max = sg_last_dev();
2804 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002805 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
2809static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2810{
Jan Blunck729d70f2005-08-27 11:07:52 -07002811 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 *pos = ++it->index;
2814 return (it->index < it->max) ? it : NULL;
2815}
2816
2817static void dev_seq_stop(struct seq_file *s, void *v)
2818{
Jan Blunck729d70f2005-08-27 11:07:52 -07002819 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
2822static int sg_proc_open_dev(struct inode *inode, struct file *file)
2823{
2824 return seq_open(file, &dev_seq_ops);
2825}
2826
2827static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2828{
2829 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2830 Sg_device *sdp;
2831 struct scsi_device *scsidp;
2832
2833 sdp = it ? sg_get_dev(it->index) : NULL;
2834 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2835 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2836 scsidp->host->host_no, scsidp->channel,
2837 scsidp->id, scsidp->lun, (int) scsidp->type,
2838 1,
2839 (int) scsidp->queue_depth,
2840 (int) scsidp->device_busy,
2841 (int) scsi_device_online(scsidp));
2842 else
2843 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2844 return 0;
2845}
2846
2847static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2848{
2849 return seq_open(file, &devstrs_seq_ops);
2850}
2851
2852static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2853{
2854 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2855 Sg_device *sdp;
2856 struct scsi_device *scsidp;
2857
2858 sdp = it ? sg_get_dev(it->index) : NULL;
2859 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2860 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2861 scsidp->vendor, scsidp->model, scsidp->rev);
2862 else
2863 seq_printf(s, "<no active device>\n");
2864 return 0;
2865}
2866
2867static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2868{
2869 int k, m, new_interface, blen, usg;
2870 Sg_request *srp;
2871 Sg_fd *fp;
2872 const sg_io_hdr_t *hp;
2873 const char * cp;
cb59e842005-04-02 13:51:23 -06002874 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2877 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2878 "(res)sgat=%d low_dma=%d\n", k + 1,
2879 jiffies_to_msecs(fp->timeout),
2880 fp->reserve.bufflen,
2881 (int) fp->reserve.k_use_sg,
2882 (int) fp->low_dma);
2883 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2884 (int) fp->cmd_q, (int) fp->force_packid,
2885 (int) fp->keep_orphan, (int) fp->closed);
2886 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2887 hp = &srp->header;
2888 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2889 if (srp->res_used) {
2890 if (new_interface &&
2891 (SG_FLAG_MMAP_IO & hp->flags))
2892 cp = " mmap>> ";
2893 else
2894 cp = " rb>> ";
2895 } else {
2896 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2897 cp = " dio>> ";
2898 else
2899 cp = " ";
2900 }
2901 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002902 blen = srp->data.bufflen;
2903 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 seq_printf(s, srp->done ?
2905 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002906 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 seq_printf(s, " id=%d blen=%d",
2908 srp->header.pack_id, blen);
2909 if (srp->done)
2910 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002911 else {
2912 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002914 (new_interface ? hp->timeout :
2915 jiffies_to_msecs(fp->timeout)),
2916 (ms > hp->duration ? ms - hp->duration : 0));
2917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2919 (int) srp->data.cmd_opcode);
2920 }
2921 if (0 == m)
2922 seq_printf(s, " No requests active\n");
2923 }
2924}
2925
2926static int sg_proc_open_debug(struct inode *inode, struct file *file)
2927{
2928 return seq_open(file, &debug_seq_ops);
2929}
2930
2931static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2932{
2933 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2934 Sg_device *sdp;
2935
2936 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002937 seq_printf(s, "max_active_device=%d(origin 1)\n",
2938 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2940 }
2941 sdp = it ? sg_get_dev(it->index) : NULL;
2942 if (sdp) {
2943 struct scsi_device *scsidp = sdp->device;
2944
2945 if (NULL == scsidp) {
2946 seq_printf(s, "device %d detached ??\n",
2947 (int)it->index);
2948 return 0;
2949 }
2950
2951 if (sg_get_nth_sfp(sdp, 0)) {
2952 seq_printf(s, " >>> device=%s ",
2953 sdp->disk->disk_name);
2954 if (sdp->detached)
2955 seq_printf(s, "detached pending close ");
2956 else
2957 seq_printf
2958 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2959 scsidp->host->host_no,
2960 scsidp->channel, scsidp->id,
2961 scsidp->lun,
2962 scsidp->host->hostt->emulated);
2963 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2964 sdp->sg_tablesize, sdp->exclude);
2965 }
2966 sg_proc_debug_helper(s, sdp);
2967 }
2968 return 0;
2969}
2970
2971#endif /* CONFIG_SCSI_PROC_FS */
2972
2973module_init(init_sg);
2974module_exit(exit_sg);