blob: 2010fa039cfee678a32e4dd0c387dc98abc501b7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "scsi.h"
db9dff32005-04-03 14:53:59 -050054#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <scsi/scsi_host.h>
56#include <scsi/scsi_driver.h>
57#include <scsi/scsi_ioctl.h>
58#include <scsi/sg.h>
59
60#include "scsi_logging.h"
61
62#ifdef CONFIG_SCSI_PROC_FS
63#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040064static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static int sg_proc_init(void);
67static void sg_proc_cleanup(void);
68#endif
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define SG_ALLOW_DIO_DEF 0
71#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
72
73#define SG_MAX_DEVS 32768
74
75/*
76 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
77 * Then when using 32 bit integers x * m may overflow during the calculation.
78 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
79 * calculates the same, but prevents the overflow when both m and d
80 * are "small" numbers (like HZ and USER_HZ).
81 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
82 * in 32 bits.
83 */
84#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
85
86#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
87
88int sg_big_buff = SG_DEF_RESERVED_SIZE;
89/* N.B. This variable is readable and writeable via
90 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
91 of this size (or less if there is not enough memory) will be reserved
92 for use by this file descriptor. [Deprecated usage: this variable is also
93 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
94 the kernel (i.e. it is not a module).] */
95static int def_reserved_size = -1; /* picks up init parameter */
96static int sg_allow_dio = SG_ALLOW_DIO_DEF;
97
Douglas Gilbert6460e752006-09-20 18:20:49 -040098static int scatter_elem_sz = SG_SCATTER_SZ;
99static int scatter_elem_sz_prev = SG_SCATTER_SZ;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define SG_SECTOR_SZ 512
102#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
103
Tony Jonesee959b02008-02-22 00:13:36 +0100104static int sg_add(struct device *, struct class_interface *);
105static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
James Bottomley7c07d612007-08-05 13:36:11 -0500107static DEFINE_IDR(sg_index_idr);
108static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 file descriptor list for device */
110
111static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100112 .add_dev = sg_add,
113 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
117 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900118 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned bufflen; /* Size of (aggregate) data buffer */
120 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
Mike Christied6b10342005-11-08 04:06:41 -0600121 struct scatterlist *buffer;/* scatter list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
123 unsigned char cmd_opcode; /* first byte of command */
124} Sg_scatter_hold;
125
126struct sg_device; /* forward declarations */
127struct sg_fd;
128
129typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct sg_request *nextrp; /* NULL -> tail request (slist) */
131 struct sg_fd *parentfp; /* NULL -> not in use */
132 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
133 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600134 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
136 char orphan; /* 1 -> drop on sight, 0 -> normal */
137 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
138 volatile char done; /* 0->before bh, 1->before read, 2->read */
139} Sg_request;
140
141typedef struct sg_fd { /* holds the state of a file descriptor */
142 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
143 struct sg_device *parentdp; /* owning device */
144 wait_queue_head_t read_wait; /* queue read until command done */
145 rwlock_t rq_list_lock; /* protect access to list in req_arr */
146 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
147 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
148 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
149 unsigned save_scat_len; /* original length of trunc. scat. element */
150 Sg_request *headrp; /* head of request slist, NULL->empty */
151 struct fasync_struct *async_qp; /* used by asynchronous notification */
152 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
153 char low_dma; /* as in parent but possibly overridden to 1 */
154 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
155 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
156 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
157 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
158 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
159 char mmap_called; /* 0 -> mmap() never called on this fd */
160} Sg_fd;
161
162typedef struct sg_device { /* holds the state of each scsi generic device */
163 struct scsi_device *device;
164 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
165 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500166 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 Sg_fd *headfp; /* first open fd belonging to this device */
168 volatile char detached; /* 0->attached, 1->detached pending removal */
169 volatile char exclude; /* opened for exclusive access */
170 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
171 struct gendisk *disk;
172 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
173} Sg_device;
174
175static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600176/* tasklet or soft irq callback */
177static void sg_cmd_done(void *data, char *sense, int result, int resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int sg_start_req(Sg_request * srp);
179static void sg_finish_rem_req(Sg_request * srp);
180static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
181static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
182 int tablesize);
183static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
184 Sg_request * srp);
185static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
186 int blocking, int read_only, Sg_request ** o_srp);
187static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
188 unsigned char *cmnd, int timeout, int blocking);
189static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
190 int wr_xf, int *countp, unsigned char __user **up);
191static int sg_write_xfer(Sg_request * srp);
192static int sg_read_xfer(Sg_request * srp);
193static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
194static void sg_remove_scat(Sg_scatter_hold * schp);
195static void sg_build_reserve(Sg_fd * sfp, int req_size);
196static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
197static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600198static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
199static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
201static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
203static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
204static Sg_request *sg_add_request(Sg_fd * sfp);
205static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
206static int sg_res_in_use(Sg_fd * sfp);
207static int sg_allow_access(unsigned char opcode, char dev_type);
208static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
209static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#ifdef CONFIG_SCSI_PROC_FS
211static int sg_last_dev(void);
212#endif
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define SZ_SG_HEADER sizeof(struct sg_header)
215#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
216#define SZ_SG_IOVEC sizeof(sg_iovec_t)
217#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
218
219static int
220sg_open(struct inode *inode, struct file *filp)
221{
222 int dev = iminor(inode);
223 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600224 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 Sg_device *sdp;
226 Sg_fd *sfp;
227 int res;
228 int retval;
229
230 nonseekable_open(inode, filp);
231 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
232 sdp = sg_get_dev(dev);
233 if ((!sdp) || (!sdp->device))
234 return -ENXIO;
235 if (sdp->detached)
236 return -ENODEV;
237
238 /* This driver's module count bumped by fops_get in <linux/fs.h> */
239 /* Prevent the device driver from vanishing while we sleep */
240 retval = scsi_device_get(sdp->device);
241 if (retval)
242 return retval;
243
244 if (!((flags & O_NONBLOCK) ||
245 scsi_block_when_processing_errors(sdp->device))) {
246 retval = -ENXIO;
247 /* we are in error recovery for this device */
248 goto error_out;
249 }
250
251 if (flags & O_EXCL) {
252 if (O_RDONLY == (flags & O_ACCMODE)) {
253 retval = -EPERM; /* Can't lock it with read only access */
254 goto error_out;
255 }
256 if (sdp->headfp && (flags & O_NONBLOCK)) {
257 retval = -EBUSY;
258 goto error_out;
259 }
260 res = 0;
261 __wait_event_interruptible(sdp->o_excl_wait,
262 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
263 if (res) {
264 retval = res; /* -ERESTARTSYS because signal hit process */
265 goto error_out;
266 }
267 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
268 if (flags & O_NONBLOCK) {
269 retval = -EBUSY;
270 goto error_out;
271 }
272 res = 0;
273 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
274 res);
275 if (res) {
276 retval = res; /* -ERESTARTSYS because signal hit process */
277 goto error_out;
278 }
279 }
280 if (sdp->detached) {
281 retval = -ENODEV;
282 goto error_out;
283 }
284 if (!sdp->headfp) { /* no existing opens on this device */
285 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600286 q = sdp->device->request_queue;
287 sdp->sg_tablesize = min(q->max_hw_segments,
288 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 if ((sfp = sg_add_sfp(sdp, dev)))
291 filp->private_data = sfp;
292 else {
293 if (flags & O_EXCL)
294 sdp->exclude = 0; /* undo if error */
295 retval = -ENOMEM;
296 goto error_out;
297 }
298 return 0;
299
300 error_out:
301 scsi_device_put(sdp->device);
302 return retval;
303}
304
305/* Following function was formerly called 'sg_close' */
306static int
307sg_release(struct inode *inode, struct file *filp)
308{
309 Sg_device *sdp;
310 Sg_fd *sfp;
311
312 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
313 return -ENXIO;
314 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
315 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
316 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
317 if (!sdp->detached) {
318 scsi_device_put(sdp->device);
319 }
320 sdp->exclude = 0;
321 wake_up_interruptible(&sdp->o_excl_wait);
322 }
323 return 0;
324}
325
326static ssize_t
327sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 Sg_device *sdp;
330 Sg_fd *sfp;
331 Sg_request *srp;
332 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600334 struct sg_header *old_hdr = NULL;
335 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
338 return -ENXIO;
339 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
340 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!access_ok(VERIFY_WRITE, buf, count))
343 return -EFAULT;
344 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600345 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
346 if (!old_hdr)
347 return -ENOMEM;
348 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
349 retval = -EFAULT;
350 goto free_old_hdr;
351 }
352 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600354 sg_io_hdr_t *new_hdr;
355 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
356 if (!new_hdr) {
357 retval = -ENOMEM;
358 goto free_old_hdr;
359 }
360 retval =__copy_from_user
361 (new_hdr, buf, SZ_SG_IO_HDR);
362 req_pack_id = new_hdr->pack_id;
363 kfree(new_hdr);
364 if (retval) {
365 retval = -EFAULT;
366 goto free_old_hdr;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 } else
cb59e842005-04-02 13:51:23 -0600370 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 srp = sg_get_rq_mark(sfp, req_pack_id);
373 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600374 if (sdp->detached) {
375 retval = -ENODEV;
376 goto free_old_hdr;
377 }
378 if (filp->f_flags & O_NONBLOCK) {
379 retval = -EAGAIN;
380 goto free_old_hdr;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 while (1) {
cb59e842005-04-02 13:51:23 -0600383 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600385 (sdp->detached ||
386 (srp = sg_get_rq_mark(sfp, req_pack_id))),
387 retval);
388 if (sdp->detached) {
389 retval = -ENODEV;
390 goto free_old_hdr;
391 }
392 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
cb59e842005-04-02 13:51:23 -0600394
395 /* -ERESTARTSYS as signal hit process */
396 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
cb59e842005-04-02 13:51:23 -0600399 if (srp->header.interface_id != '\0') {
400 retval = sg_new_read(sfp, buf, count, srp);
401 goto free_old_hdr;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600405 if (old_hdr == NULL) {
406 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
407 if (! old_hdr) {
408 retval = -ENOMEM;
409 goto free_old_hdr;
410 }
411 }
412 memset(old_hdr, 0, SZ_SG_HEADER);
413 old_hdr->reply_len = (int) hp->timeout;
414 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
415 old_hdr->pack_id = hp->pack_id;
416 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600418 old_hdr->target_status = hp->masked_status;
419 old_hdr->host_status = hp->host_status;
420 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if ((CHECK_CONDITION & hp->masked_status) ||
422 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600423 memcpy(old_hdr->sense_buffer, srp->sense_b,
424 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 switch (hp->host_status) {
426 /* This setup of 'result' is for backward compatibility and is best
427 ignored by the user who should use target, host + driver status */
428 case DID_OK:
429 case DID_PASSTHROUGH:
430 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600431 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433 case DID_NO_CONNECT:
434 case DID_BUS_BUSY:
435 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600436 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 case DID_BAD_TARGET:
439 case DID_ABORT:
440 case DID_PARITY:
441 case DID_RESET:
442 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600443 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
445 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 hp->masked_status == GOOD) ? 0 : EIO;
448 break;
449 default:
cb59e842005-04-02 13:51:23 -0600450 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 }
453
454 /* Now copy the result back to the user buffer. */
455 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600456 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
457 retval = -EFAULT;
458 goto free_old_hdr;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600461 if (count > old_hdr->reply_len)
462 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600464 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
465 retval = -EFAULT;
466 goto free_old_hdr;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 } else
cb59e842005-04-02 13:51:23 -0600470 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600472 retval = count;
473free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800474 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600475 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static ssize_t
479sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
480{
481 sg_io_hdr_t *hp = &srp->header;
482 int err = 0;
483 int len;
484
485 if (count < SZ_SG_IO_HDR) {
486 err = -EINVAL;
487 goto err_out;
488 }
489 hp->sb_len_wr = 0;
490 if ((hp->mx_sb_len > 0) && hp->sbp) {
491 if ((CHECK_CONDITION & hp->masked_status) ||
492 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600493 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
495 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
496 len = (len > sb_len) ? sb_len : len;
497 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
498 err = -EFAULT;
499 goto err_out;
500 }
501 hp->sb_len_wr = len;
502 }
503 }
504 if (hp->masked_status || hp->host_status || hp->driver_status)
505 hp->info |= SG_INFO_CHECK;
506 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
507 err = -EFAULT;
508 goto err_out;
509 }
510 err = sg_read_xfer(srp);
511 err_out:
512 sg_finish_rem_req(srp);
513 return (0 == err) ? count : err;
514}
515
516static ssize_t
517sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
518{
519 int mxsize, cmd_size, k;
520 int input_size, blocking;
521 unsigned char opcode;
522 Sg_device *sdp;
523 Sg_fd *sfp;
524 Sg_request *srp;
525 struct sg_header old_hdr;
526 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600527 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
530 return -ENXIO;
531 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
532 sdp->disk->disk_name, (int) count));
533 if (sdp->detached)
534 return -ENODEV;
535 if (!((filp->f_flags & O_NONBLOCK) ||
536 scsi_block_when_processing_errors(sdp->device)))
537 return -ENXIO;
538
539 if (!access_ok(VERIFY_READ, buf, count))
540 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
541 if (count < SZ_SG_HEADER)
542 return -EIO;
543 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
544 return -EFAULT;
545 blocking = !(filp->f_flags & O_NONBLOCK);
546 if (old_hdr.reply_len < 0)
547 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
548 if (count < (SZ_SG_HEADER + 6))
549 return -EIO; /* The minimum scsi command length is 6 bytes. */
550
551 if (!(srp = sg_add_request(sfp))) {
552 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
553 return -EDOM;
554 }
555 buf += SZ_SG_HEADER;
556 __get_user(opcode, buf);
557 if (sfp->next_cmd_len > 0) {
558 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
559 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
560 sfp->next_cmd_len = 0;
561 sg_remove_request(sfp, srp);
562 return -EIO;
563 }
564 cmd_size = sfp->next_cmd_len;
565 sfp->next_cmd_len = 0; /* reset so only this write() effected */
566 } else {
567 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
568 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
569 cmd_size = 12;
570 }
571 SCSI_LOG_TIMEOUT(4, printk(
572 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
573/* Determine buffer size. */
574 input_size = count - cmd_size;
575 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
576 mxsize -= SZ_SG_HEADER;
577 input_size -= SZ_SG_HEADER;
578 if (input_size < 0) {
579 sg_remove_request(sfp, srp);
580 return -EIO; /* User did not pass enough bytes for this command. */
581 }
582 hp = &srp->header;
583 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
584 hp->cmd_len = (unsigned char) cmd_size;
585 hp->iovec_count = 0;
586 hp->mx_sb_len = 0;
587 if (input_size > 0)
588 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
589 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
590 else
591 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
592 hp->dxfer_len = mxsize;
593 hp->dxferp = (char __user *)buf + cmd_size;
594 hp->sbp = NULL;
595 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
596 hp->flags = input_size; /* structure abuse ... */
597 hp->pack_id = old_hdr.pack_id;
598 hp->usr_ptr = NULL;
599 if (__copy_from_user(cmnd, buf, cmd_size))
600 return -EFAULT;
601 /*
602 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
603 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
604 * is a non-zero input_size, so emit a warning.
605 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100606 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
607 static char cmd[TASK_COMM_LEN];
608 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 printk(KERN_WARNING
610 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
611 "guessing data in;\n" KERN_WARNING " "
612 "program %s not setting count and/or reply_len properly\n",
613 old_hdr.reply_len - (int)SZ_SG_HEADER,
614 input_size, (unsigned int) cmnd[0],
615 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100616 strcpy(cmd, current->comm);
617 }
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
620 return (k < 0) ? k : count;
621}
622
623static ssize_t
624sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
625 int blocking, int read_only, Sg_request ** o_srp)
626{
627 int k;
628 Sg_request *srp;
629 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600630 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int timeout;
632 unsigned long ul_timeout;
633
634 if (count < SZ_SG_IO_HDR)
635 return -EINVAL;
636 if (!access_ok(VERIFY_READ, buf, count))
637 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
638
639 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
640 if (!(srp = sg_add_request(sfp))) {
641 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
642 return -EDOM;
643 }
644 hp = &srp->header;
645 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
646 sg_remove_request(sfp, srp);
647 return -EFAULT;
648 }
649 if (hp->interface_id != 'S') {
650 sg_remove_request(sfp, srp);
651 return -ENOSYS;
652 }
653 if (hp->flags & SG_FLAG_MMAP_IO) {
654 if (hp->dxfer_len > sfp->reserve.bufflen) {
655 sg_remove_request(sfp, srp);
656 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
657 }
658 if (hp->flags & SG_FLAG_DIRECT_IO) {
659 sg_remove_request(sfp, srp);
660 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
661 }
662 if (sg_res_in_use(sfp)) {
663 sg_remove_request(sfp, srp);
664 return -EBUSY; /* reserve buffer already being used */
665 }
666 }
667 ul_timeout = msecs_to_jiffies(srp->header.timeout);
668 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
669 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
670 sg_remove_request(sfp, srp);
671 return -EMSGSIZE;
672 }
673 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
674 sg_remove_request(sfp, srp);
675 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
676 }
677 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
678 sg_remove_request(sfp, srp);
679 return -EFAULT;
680 }
681 if (read_only &&
682 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
683 sg_remove_request(sfp, srp);
684 return -EPERM;
685 }
686 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
687 if (k < 0)
688 return k;
689 if (o_srp)
690 *o_srp = srp;
691 return count;
692}
693
694static int
695sg_common_write(Sg_fd * sfp, Sg_request * srp,
696 unsigned char *cmnd, int timeout, int blocking)
697{
Mike Christied6b10342005-11-08 04:06:41 -0600698 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 Sg_device *sdp = sfp->parentdp;
700 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
703 hp->status = 0;
704 hp->masked_status = 0;
705 hp->msg_status = 0;
706 hp->info = 0;
707 hp->host_status = 0;
708 hp->driver_status = 0;
709 hp->resid = 0;
710 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
711 (int) cmnd[0], (int) hp->cmd_len));
712
713 if ((k = sg_start_req(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400714 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 sg_finish_rem_req(srp);
716 return k; /* probably out of space --> ENOMEM */
717 }
718 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400719 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sg_finish_rem_req(srp);
721 return k;
722 }
723 if (sdp->detached) {
724 sg_finish_rem_req(srp);
725 return -ENODEV;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 switch (hp->dxfer_direction) {
729 case SG_DXFER_TO_FROM_DEV:
730 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600731 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600734 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600737 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739 default:
Mike Christied6b10342005-11-08 04:06:41 -0600740 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 }
cb59e842005-04-02 13:51:23 -0600743 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* Now send everything of to mid-level. The next time we hear about this
745 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600746 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600747 hp->dxfer_len, srp->data.k_use_sg, timeout,
748 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
749 GFP_ATOMIC)) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400750 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
Mike Christied6b10342005-11-08 04:06:41 -0600751 /*
752 * most likely out of mem, but could also be a bad map
753 */
Mike Christie18c49b82006-03-22 16:04:38 -0600754 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600755 return -ENOMEM;
756 } else
757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760static int
761sg_srp_done(Sg_request *srp, Sg_fd *sfp)
762{
763 unsigned long iflags;
764 int done;
765
766 read_lock_irqsave(&sfp->rq_list_lock, iflags);
767 done = srp->done;
768 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
769 return done;
770}
771
772static int
773sg_ioctl(struct inode *inode, struct file *filp,
774 unsigned int cmd_in, unsigned long arg)
775{
776 void __user *p = (void __user *)arg;
777 int __user *ip = p;
778 int result, val, read_only;
779 Sg_device *sdp;
780 Sg_fd *sfp;
781 Sg_request *srp;
782 unsigned long iflags;
783
784 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
785 return -ENXIO;
786 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
787 sdp->disk->disk_name, (int) cmd_in));
788 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
789
790 switch (cmd_in) {
791 case SG_IO:
792 {
793 int blocking = 1; /* ignore O_NONBLOCK flag */
794
795 if (sdp->detached)
796 return -ENODEV;
797 if (!scsi_block_when_processing_errors(sdp->device))
798 return -ENXIO;
799 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
800 return -EFAULT;
801 result =
802 sg_new_write(sfp, p, SZ_SG_IO_HDR,
803 blocking, read_only, &srp);
804 if (result < 0)
805 return result;
806 srp->sg_io_owned = 1;
807 while (1) {
808 result = 0; /* following macro to beat race condition */
809 __wait_event_interruptible(sfp->read_wait,
810 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
811 result);
812 if (sdp->detached)
813 return -ENODEV;
814 if (sfp->closed)
815 return 0; /* request packet dropped already */
816 if (0 == result)
817 break;
818 srp->orphan = 1;
819 return result; /* -ERESTARTSYS because signal hit process */
820 }
821 write_lock_irqsave(&sfp->rq_list_lock, iflags);
822 srp->done = 2;
823 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
824 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
825 return (result < 0) ? result : 0;
826 }
827 case SG_SET_TIMEOUT:
828 result = get_user(val, ip);
829 if (result)
830 return result;
831 if (val < 0)
832 return -EIO;
833 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
834 val = MULDIV (INT_MAX, USER_HZ, HZ);
835 sfp->timeout_user = val;
836 sfp->timeout = MULDIV (val, HZ, USER_HZ);
837
838 return 0;
839 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
840 /* strange ..., for backward compatibility */
841 return sfp->timeout_user;
842 case SG_SET_FORCE_LOW_DMA:
843 result = get_user(val, ip);
844 if (result)
845 return result;
846 if (val) {
847 sfp->low_dma = 1;
848 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
849 val = (int) sfp->reserve.bufflen;
850 sg_remove_scat(&sfp->reserve);
851 sg_build_reserve(sfp, val);
852 }
853 } else {
854 if (sdp->detached)
855 return -ENODEV;
856 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
857 }
858 return 0;
859 case SG_GET_LOW_DMA:
860 return put_user((int) sfp->low_dma, ip);
861 case SG_GET_SCSI_ID:
862 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
863 return -EFAULT;
864 else {
865 sg_scsi_id_t __user *sg_idp = p;
866
867 if (sdp->detached)
868 return -ENODEV;
869 __put_user((int) sdp->device->host->host_no,
870 &sg_idp->host_no);
871 __put_user((int) sdp->device->channel,
872 &sg_idp->channel);
873 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
874 __put_user((int) sdp->device->lun, &sg_idp->lun);
875 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
876 __put_user((short) sdp->device->host->cmd_per_lun,
877 &sg_idp->h_cmd_per_lun);
878 __put_user((short) sdp->device->queue_depth,
879 &sg_idp->d_queue_depth);
880 __put_user(0, &sg_idp->unused[0]);
881 __put_user(0, &sg_idp->unused[1]);
882 return 0;
883 }
884 case SG_SET_FORCE_PACK_ID:
885 result = get_user(val, ip);
886 if (result)
887 return result;
888 sfp->force_packid = val ? 1 : 0;
889 return 0;
890 case SG_GET_PACK_ID:
891 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
892 return -EFAULT;
893 read_lock_irqsave(&sfp->rq_list_lock, iflags);
894 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
895 if ((1 == srp->done) && (!srp->sg_io_owned)) {
896 read_unlock_irqrestore(&sfp->rq_list_lock,
897 iflags);
898 __put_user(srp->header.pack_id, ip);
899 return 0;
900 }
901 }
902 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
903 __put_user(-1, ip);
904 return 0;
905 case SG_GET_NUM_WAITING:
906 read_lock_irqsave(&sfp->rq_list_lock, iflags);
907 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
908 if ((1 == srp->done) && (!srp->sg_io_owned))
909 ++val;
910 }
911 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
912 return put_user(val, ip);
913 case SG_GET_SG_TABLESIZE:
914 return put_user(sdp->sg_tablesize, ip);
915 case SG_SET_RESERVED_SIZE:
916 result = get_user(val, ip);
917 if (result)
918 return result;
919 if (val < 0)
920 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500921 val = min_t(int, val,
922 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (val != sfp->reserve.bufflen) {
924 if (sg_res_in_use(sfp) || sfp->mmap_called)
925 return -EBUSY;
926 sg_remove_scat(&sfp->reserve);
927 sg_build_reserve(sfp, val);
928 }
929 return 0;
930 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500931 val = min_t(int, sfp->reserve.bufflen,
932 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return put_user(val, ip);
934 case SG_SET_COMMAND_Q:
935 result = get_user(val, ip);
936 if (result)
937 return result;
938 sfp->cmd_q = val ? 1 : 0;
939 return 0;
940 case SG_GET_COMMAND_Q:
941 return put_user((int) sfp->cmd_q, ip);
942 case SG_SET_KEEP_ORPHAN:
943 result = get_user(val, ip);
944 if (result)
945 return result;
946 sfp->keep_orphan = val;
947 return 0;
948 case SG_GET_KEEP_ORPHAN:
949 return put_user((int) sfp->keep_orphan, ip);
950 case SG_NEXT_CMD_LEN:
951 result = get_user(val, ip);
952 if (result)
953 return result;
954 sfp->next_cmd_len = (val > 0) ? val : 0;
955 return 0;
956 case SG_GET_VERSION_NUM:
957 return put_user(sg_version_num, ip);
958 case SG_GET_ACCESS_COUNT:
959 /* faked - we don't have a real access count anymore */
960 val = (sdp->device ? 1 : 0);
961 return put_user(val, ip);
962 case SG_GET_REQUEST_TABLE:
963 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
964 return -EFAULT;
965 else {
cb59e842005-04-02 13:51:23 -0600966 sg_req_info_t *rinfo;
967 unsigned int ms;
968
969 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
970 GFP_KERNEL);
971 if (!rinfo)
972 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 read_lock_irqsave(&sfp->rq_list_lock, iflags);
974 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
975 ++val, srp = srp ? srp->nextrp : srp) {
976 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
977 if (srp) {
978 rinfo[val].req_state = srp->done + 1;
979 rinfo[val].problem =
980 srp->header.masked_status &
981 srp->header.host_status &
982 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600983 if (srp->done)
984 rinfo[val].duration =
985 srp->header.duration;
986 else {
987 ms = jiffies_to_msecs(jiffies);
988 rinfo[val].duration =
989 (ms > srp->header.duration) ?
990 (ms - srp->header.duration) : 0;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600993 rinfo[val].sg_io_owned =
994 srp->sg_io_owned;
995 rinfo[val].pack_id =
996 srp->header.pack_id;
997 rinfo[val].usr_ptr =
998 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 }
1001 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001002 result = __copy_to_user(p, rinfo,
1003 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1004 result = result ? -EFAULT : 0;
1005 kfree(rinfo);
1006 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 case SG_EMULATED_HOST:
1009 if (sdp->detached)
1010 return -ENODEV;
1011 return put_user(sdp->device->host->hostt->emulated, ip);
1012 case SG_SCSI_RESET:
1013 if (sdp->detached)
1014 return -ENODEV;
1015 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001016 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -EBUSY;
1018 } else if (!scsi_block_when_processing_errors(sdp->device))
1019 return -EBUSY;
1020 result = get_user(val, ip);
1021 if (result)
1022 return result;
1023 if (SG_SCSI_RESET_NOTHING == val)
1024 return 0;
1025 switch (val) {
1026 case SG_SCSI_RESET_DEVICE:
1027 val = SCSI_TRY_RESET_DEVICE;
1028 break;
Brian King39120e12008-07-01 13:03:19 -05001029 case SG_SCSI_RESET_TARGET:
1030 val = SCSI_TRY_RESET_TARGET;
1031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 case SG_SCSI_RESET_BUS:
1033 val = SCSI_TRY_RESET_BUS;
1034 break;
1035 case SG_SCSI_RESET_HOST:
1036 val = SCSI_TRY_RESET_HOST;
1037 break;
1038 default:
1039 return -EINVAL;
1040 }
1041 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1042 return -EACCES;
1043 return (scsi_reset_provider(sdp->device, val) ==
1044 SUCCESS) ? 0 : -EIO;
1045 case SCSI_IOCTL_SEND_COMMAND:
1046 if (sdp->detached)
1047 return -ENODEV;
1048 if (read_only) {
1049 unsigned char opcode = WRITE_6;
1050 Scsi_Ioctl_Command __user *siocp = p;
1051
1052 if (copy_from_user(&opcode, siocp->data, 1))
1053 return -EFAULT;
1054 if (!sg_allow_access(opcode, sdp->device->type))
1055 return -EPERM;
1056 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001057 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 case SG_SET_DEBUG:
1059 result = get_user(val, ip);
1060 if (result)
1061 return result;
1062 sdp->sgdebug = (char) val;
1063 return 0;
1064 case SCSI_IOCTL_GET_IDLUN:
1065 case SCSI_IOCTL_GET_BUS_NUMBER:
1066 case SCSI_IOCTL_PROBE_HOST:
1067 case SG_GET_TRANSFORM:
1068 if (sdp->detached)
1069 return -ENODEV;
1070 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001071 case BLKSECTGET:
1072 return put_user(sdp->device->request_queue->max_sectors * 512,
1073 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001074 case BLKTRACESETUP:
1075 return blk_trace_setup(sdp->device->request_queue,
1076 sdp->disk->disk_name,
1077 sdp->device->sdev_gendev.devt,
1078 (char *)arg);
1079 case BLKTRACESTART:
1080 return blk_trace_startstop(sdp->device->request_queue, 1);
1081 case BLKTRACESTOP:
1082 return blk_trace_startstop(sdp->device->request_queue, 0);
1083 case BLKTRACETEARDOWN:
1084 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 default:
1086 if (read_only)
1087 return -EPERM; /* don't know so take safe approach */
1088 return scsi_ioctl(sdp->device, cmd_in, p);
1089 }
1090}
1091
1092#ifdef CONFIG_COMPAT
1093static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1094{
1095 Sg_device *sdp;
1096 Sg_fd *sfp;
1097 struct scsi_device *sdev;
1098
1099 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1100 return -ENXIO;
1101
1102 sdev = sdp->device;
1103 if (sdev->host->hostt->compat_ioctl) {
1104 int ret;
1105
1106 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1107
1108 return ret;
1109 }
1110
1111 return -ENOIOCTLCMD;
1112}
1113#endif
1114
1115static unsigned int
1116sg_poll(struct file *filp, poll_table * wait)
1117{
1118 unsigned int res = 0;
1119 Sg_device *sdp;
1120 Sg_fd *sfp;
1121 Sg_request *srp;
1122 int count = 0;
1123 unsigned long iflags;
1124
1125 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1126 || sfp->closed)
1127 return POLLERR;
1128 poll_wait(filp, &sfp->read_wait, wait);
1129 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1130 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1131 /* if any read waiting, flag it */
1132 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1133 res = POLLIN | POLLRDNORM;
1134 ++count;
1135 }
1136 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1137
1138 if (sdp->detached)
1139 res |= POLLHUP;
1140 else if (!sfp->cmd_q) {
1141 if (0 == count)
1142 res |= POLLOUT | POLLWRNORM;
1143 } else if (count < SG_MAX_QUEUE)
1144 res |= POLLOUT | POLLWRNORM;
1145 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1146 sdp->disk->disk_name, (int) res));
1147 return res;
1148}
1149
1150static int
1151sg_fasync(int fd, struct file *filp, int mode)
1152{
1153 int retval;
1154 Sg_device *sdp;
1155 Sg_fd *sfp;
1156
1157 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1158 return -ENXIO;
1159 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1160 sdp->disk->disk_name, mode));
1161
1162 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1163 return (retval < 0) ? retval : 0;
1164}
1165
Nick Piggina13ff0b2008-02-07 18:46:06 -08001166static int
1167sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001170 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001172 struct scatterlist *sg;
1173 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001176 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001178 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001180 return VM_FAULT_SIGBUS;
1181 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001183 sg = rsv_schp->buffer;
1184 sa = vma->vm_start;
1185 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001186 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001187 len = vma->vm_end - sa;
1188 len = (len < sg->length) ? len : sg->length;
1189 if (offset < len) {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001190 struct page *page;
Jens Axboe45711f12007-10-22 21:19:53 +02001191 page = virt_to_page(page_address(sg_page(sg)) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001192 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001193 vmf->page = page;
1194 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Mike Christied6b10342005-11-08 04:06:41 -06001196 sa += len;
1197 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Mike Christied6b10342005-11-08 04:06:41 -06001199
Nick Piggina13ff0b2008-02-07 18:46:06 -08001200 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001204 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205};
1206
1207static int
1208sg_mmap(struct file *filp, struct vm_area_struct *vma)
1209{
1210 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001211 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001213 int k;
1214 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1217 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001218 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1220 (void *) vma->vm_start, (int) req_sz));
1221 if (vma->vm_pgoff)
1222 return -EINVAL; /* want no offset */
1223 rsv_schp = &sfp->reserve;
1224 if (req_sz > rsv_schp->bufflen)
1225 return -ENOMEM; /* cannot map more than reserved buffer */
1226
Mike Christied6b10342005-11-08 04:06:41 -06001227 sa = vma->vm_start;
1228 sg = rsv_schp->buffer;
1229 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001230 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001231 len = vma->vm_end - sa;
1232 len = (len < sg->length) ? len : sg->length;
1233 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Mike Christied6b10342005-11-08 04:06:41 -06001235
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001236 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001237 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 vma->vm_private_data = sfp;
1239 vma->vm_ops = &sg_mmap_vm_ops;
1240 return 0;
1241}
1242
1243/* This function is a "bottom half" handler that is called by the
1244 * mid level when a command is completed (or has failed). */
1245static void
Mike Christied6b10342005-11-08 04:06:41 -06001246sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Mike Christied6b10342005-11-08 04:06:41 -06001248 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 Sg_device *sdp = NULL;
1250 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001252 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (NULL == srp) {
1255 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return;
1257 }
1258 sfp = srp->parentfp;
1259 if (sfp)
1260 sdp = sfp->parentdp;
1261 if ((NULL == sdp) || sdp->detached) {
1262 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
1264 }
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001268 sdp->disk->disk_name, srp->header.pack_id, result));
1269 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001270 ms = jiffies_to_msecs(jiffies);
1271 srp->header.duration = (ms > srp->header.duration) ?
1272 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001273 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct scsi_sense_hdr sshdr;
1275
Mike Christied6b10342005-11-08 04:06:41 -06001276 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1277 srp->header.status = 0xff & result;
1278 srp->header.masked_status = status_byte(result);
1279 srp->header.msg_status = msg_byte(result);
1280 srp->header.host_status = host_byte(result);
1281 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if ((sdp->sgdebug > 0) &&
1283 ((CHECK_CONDITION == srp->header.masked_status) ||
1284 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001285 __scsi_print_sense("sg_cmd_done", sense,
1286 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001289 if (driver_byte(result) != 0
1290 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 && !scsi_sense_is_deferred(&sshdr)
1292 && sshdr.sense_key == UNIT_ATTENTION
1293 && sdp->device->removable) {
1294 /* Detected possible disc change. Set the bit - this */
1295 /* may be used if there are filesystems using this device */
1296 sdp->device->changed = 1;
1297 }
1298 }
1299 /* Rely on write phase to clean out srp status values, so no "else" */
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (sfp->closed) { /* whoops this fd already released, cleanup */
1302 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1303 sg_finish_rem_req(srp);
1304 srp = NULL;
1305 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001306 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1308 scsi_device_put(sdp->device);
1309 }
1310 sfp = NULL;
1311 }
1312 } else if (srp && srp->orphan) {
1313 if (sfp->keep_orphan)
1314 srp->sg_io_owned = 0;
1315 else {
1316 sg_finish_rem_req(srp);
1317 srp = NULL;
1318 }
1319 }
1320 if (sfp && srp) {
1321 /* Now wake up any sg_read() that is waiting for this packet. */
1322 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1323 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1324 srp->done = 1;
1325 wake_up_interruptible(&sfp->read_wait);
1326 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1327 }
1328}
1329
1330static struct file_operations sg_fops = {
1331 .owner = THIS_MODULE,
1332 .read = sg_read,
1333 .write = sg_write,
1334 .poll = sg_poll,
1335 .ioctl = sg_ioctl,
1336#ifdef CONFIG_COMPAT
1337 .compat_ioctl = sg_compat_ioctl,
1338#endif
1339 .open = sg_open,
1340 .mmap = sg_mmap,
1341 .release = sg_release,
1342 .fasync = sg_fasync,
1343};
1344
gregkh@suse.ded2538782005-03-23 09:55:22 -08001345static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347static int sg_sysfs_valid = 0;
1348
James Bottomley7c07d612007-08-05 13:36:11 -05001349static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Mike Christied6b10342005-11-08 04:06:41 -06001351 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 Sg_device *sdp;
1353 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001354 int error;
1355 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jes Sorensen24669f752006-01-16 10:31:18 -05001357 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!sdp) {
1359 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001360 return ERR_PTR(-ENOMEM);
1361 }
1362 error = -ENOMEM;
1363 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1364 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
James Bottomley7c07d612007-08-05 13:36:11 -05001368 write_lock_irqsave(&sg_index_lock, iflags);
1369 error = idr_get_new(&sg_index_idr, sdp, &k);
1370 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
James Bottomley7c07d612007-08-05 13:36:11 -05001372 if (error) {
1373 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1374 error);
1375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (unlikely(k >= SG_MAX_DEVS))
1379 goto overflow;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1382 sprintf(disk->disk_name, "sg%d", k);
1383 disk->first_minor = k;
1384 sdp->disk = disk;
1385 sdp->device = scsidp;
1386 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001387 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001388 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
James Bottomley7c07d612007-08-05 13:36:11 -05001390 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001392 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001394 return ERR_PTR(error);
1395 }
1396 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001399 sdev_printk(KERN_WARNING, scsidp,
1400 "Unable to attach sg device type=%d, minor "
1401 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 error = -ENODEV;
1403 goto out;
1404}
1405
1406static int
Tony Jonesee959b02008-02-22 00:13:36 +01001407sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Tony Jonesee959b02008-02-22 00:13:36 +01001409 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct gendisk *disk;
1411 Sg_device *sdp = NULL;
1412 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001413 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001414 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 disk = alloc_disk(1);
1417 if (!disk) {
1418 printk(KERN_WARNING "alloc_disk failed\n");
1419 return -ENOMEM;
1420 }
1421 disk->major = SCSI_GENERIC_MAJOR;
1422
1423 error = -ENOMEM;
1424 cdev = cdev_alloc();
1425 if (!cdev) {
1426 printk(KERN_WARNING "cdev_alloc failed\n");
1427 goto out;
1428 }
1429 cdev->owner = THIS_MODULE;
1430 cdev->ops = &sg_fops;
1431
James Bottomley7c07d612007-08-05 13:36:11 -05001432 sdp = sg_alloc(disk, scsidp);
1433 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001435 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto out;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
James Bottomley7c07d612007-08-05 13:36:11 -05001439 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001440 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001441 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 sdp->cdev = cdev;
1444 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001445 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001447 sg_class_member = device_create_drvdata(sg_sysfs_class,
1448 cl_dev->parent,
1449 MKDEV(SCSI_GENERIC_MAJOR,
1450 sdp->index),
1451 sdp,
1452 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001453 if (IS_ERR(sg_class_member)) {
1454 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001455 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001456 error = PTR_ERR(sg_class_member);
1457 goto cdev_add_err;
1458 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001459 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 &sg_class_member->kobj, "generic");
1461 if (error)
1462 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001463 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001465 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
James Bottomley9ccfc752005-10-02 11:45:08 -05001467 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001468 "Attached scsi generic sg%d type %d\n", sdp->index,
1469 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Tony Jonesee959b02008-02-22 00:13:36 +01001471 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
1474
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001475cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001476 write_lock_irqsave(&sg_index_lock, iflags);
1477 idr_remove(&sg_index_idr, sdp->index);
1478 write_unlock_irqrestore(&sg_index_lock, iflags);
1479 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481out:
1482 put_disk(disk);
1483 if (cdev)
1484 cdev_del(cdev);
1485 return error;
1486}
1487
1488static void
Tony Jonesee959b02008-02-22 00:13:36 +01001489sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Tony Jonesee959b02008-02-22 00:13:36 +01001491 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1492 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 unsigned long iflags;
1494 Sg_fd *sfp;
1495 Sg_fd *tsfp;
1496 Sg_request *srp;
1497 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001498 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
James Bottomley7c07d612007-08-05 13:36:11 -05001500 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
James Bottomley7c07d612007-08-05 13:36:11 -05001503 delay = 0;
1504 write_lock_irqsave(&sg_index_lock, iflags);
1505 if (sdp->headfp) {
1506 sdp->detached = 1;
1507 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1508 tsfp = sfp->nextfp;
1509 for (srp = sfp->headrp; srp; srp = tsrp) {
1510 tsrp = srp->nextrp;
1511 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1512 sg_finish_rem_req(srp);
1513 }
1514 if (sfp->closed) {
1515 scsi_device_put(sdp->device);
1516 __sg_remove_sfp(sdp, sfp);
1517 } else {
1518 delay = 1;
1519 wake_up_interruptible(&sfp->read_wait);
1520 kill_fasync(&sfp->async_qp, SIGPOLL,
1521 POLL_HUP);
1522 }
1523 }
1524 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1525 if (NULL == sdp->headfp) {
1526 idr_remove(&sg_index_idr, sdp->index);
1527 }
1528 } else { /* nothing active, simple case */
1529 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1530 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
James Bottomley7c07d612007-08-05 13:36:11 -05001532 write_unlock_irqrestore(&sg_index_lock, iflags);
1533
1534 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001535 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001536 cdev_del(sdp->cdev);
1537 sdp->cdev = NULL;
1538 put_disk(sdp->disk);
1539 sdp->disk = NULL;
1540 if (NULL == sdp->headfp)
1541 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (delay)
1544 msleep(10); /* dirty detach so delay device destruction */
1545}
1546
Douglas Gilbert6460e752006-09-20 18:20:49 -04001547module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1548module_param_named(def_reserved_size, def_reserved_size, int,
1549 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1551
1552MODULE_AUTHOR("Douglas Gilbert");
1553MODULE_DESCRIPTION("SCSI generic (sg) driver");
1554MODULE_LICENSE("GPL");
1555MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001556MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Douglas Gilbert6460e752006-09-20 18:20:49 -04001558MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1559 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1561MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1562
1563static int __init
1564init_sg(void)
1565{
1566 int rc;
1567
Douglas Gilbert6460e752006-09-20 18:20:49 -04001568 if (scatter_elem_sz < PAGE_SIZE) {
1569 scatter_elem_sz = PAGE_SIZE;
1570 scatter_elem_sz_prev = scatter_elem_sz;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (def_reserved_size >= 0)
1573 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001574 else
1575 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1578 SG_MAX_DEVS, "sg");
1579 if (rc)
1580 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001581 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if ( IS_ERR(sg_sysfs_class) ) {
1583 rc = PTR_ERR(sg_sysfs_class);
1584 goto err_out;
1585 }
1586 sg_sysfs_valid = 1;
1587 rc = scsi_register_interface(&sg_interface);
1588 if (0 == rc) {
1589#ifdef CONFIG_SCSI_PROC_FS
1590 sg_proc_init();
1591#endif /* CONFIG_SCSI_PROC_FS */
1592 return 0;
1593 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001594 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595err_out:
1596 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1597 return rc;
1598}
1599
1600static void __exit
1601exit_sg(void)
1602{
1603#ifdef CONFIG_SCSI_PROC_FS
1604 sg_proc_cleanup();
1605#endif /* CONFIG_SCSI_PROC_FS */
1606 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001607 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 sg_sysfs_valid = 0;
1609 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1610 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001611 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614static int
1615sg_start_req(Sg_request * srp)
1616{
1617 int res;
1618 Sg_fd *sfp = srp->parentfp;
1619 sg_io_hdr_t *hp = &srp->header;
1620 int dxfer_len = (int) hp->dxfer_len;
1621 int dxfer_dir = hp->dxfer_direction;
1622 Sg_scatter_hold *req_schp = &srp->data;
1623 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1624
1625 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1626 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1627 return 0;
1628 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1629 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1630 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1631 res = sg_build_direct(srp, sfp, dxfer_len);
1632 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1633 return res;
1634 }
1635 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1636 sg_link_reserve(sfp, srp, dxfer_len);
1637 else {
1638 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1639 if (res) {
1640 sg_remove_scat(req_schp);
1641 return res;
1642 }
1643 }
1644 return 0;
1645}
1646
1647static void
1648sg_finish_rem_req(Sg_request * srp)
1649{
1650 Sg_fd *sfp = srp->parentfp;
1651 Sg_scatter_hold *req_schp = &srp->data;
1652
1653 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1654 if (srp->res_used)
1655 sg_unlink_reserve(sfp, srp);
1656 else
1657 sg_remove_scat(req_schp);
1658 sg_remove_request(sfp, srp);
1659}
1660
1661static int
1662sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1663{
Mike Christied6b10342005-11-08 04:06:41 -06001664 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001665 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Mike Christied6b10342005-11-08 04:06:41 -06001667 /*
1668 * TODO: test without low_dma, we should not need it since
1669 * the block layer will bounce the buffer for us
1670 *
1671 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1672 */
1673 if (sfp->low_dma)
1674 gfp_flags |= GFP_DMA;
1675 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (!schp->buffer)
1677 return -ENOMEM;
Anton Blanchard30fa0d02007-10-26 14:00:14 +02001678 sg_init_table(schp->buffer, tablesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001680 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683#ifdef SG_ALLOW_DIO_CODE
1684/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001685 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1688 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 (i.e., either completely successful or fails)
1690*/
1691static int
1692st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001693 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001695 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1696 unsigned long start = uaddr >> PAGE_SHIFT;
1697 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct page **pages;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /* User attempted Overflow! */
1702 if ((uaddr + count) < uaddr)
1703 return -EINVAL;
1704
1705 /* Too big */
1706 if (nr_pages > max_pages)
1707 return -ENOMEM;
1708
1709 /* Hmm? */
1710 if (count == 0)
1711 return 0;
1712
1713 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1714 return -ENOMEM;
1715
1716 /* Try to fault in all of the necessary pages */
1717 down_read(&current->mm->mmap_sem);
1718 /* rw==READ means read from drive, write into memory area */
1719 res = get_user_pages(
1720 current,
1721 current->mm,
1722 uaddr,
1723 nr_pages,
1724 rw == READ,
1725 0, /* don't force */
1726 pages,
1727 NULL);
1728 up_read(&current->mm->mmap_sem);
1729
1730 /* Errors and no page mapped should return here */
1731 if (res < nr_pages)
1732 goto out_unmap;
1733
1734 for (i=0; i < nr_pages; i++) {
1735 /* FIXME: flush superflous for rw==READ,
1736 * probably wrong function for rw==WRITE
1737 */
1738 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* ?? Is locking needed? I don't think so */
1740 /* if (TestSetPageLocked(pages[i]))
1741 goto out_unlock; */
1742 }
1743
Jens Axboe642f149032007-10-24 11:20:47 +02001744 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (nr_pages > 1) {
1746 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1747 count -= sgl[0].length;
Jens Axboe642f149032007-10-24 11:20:47 +02001748 for (i=1; i < nr_pages ; i++)
1749 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751 else {
1752 sgl[0].length = count;
1753 }
1754
1755 kfree(pages);
1756 return nr_pages;
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001759 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 for (j=0; j < res; j++)
1761 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001762 res = 0;
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 kfree(pages);
1765 return res;
1766}
1767
1768
1769/* And unmap them... */
1770static int
1771st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1772 int dirtied)
1773{
1774 int i;
1775
1776 for (i=0; i < nr_pages; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +02001777 struct page *page = sg_page(&sgl[i]);
Nick Pigginb5810032005-10-29 18:16:12 -07001778
Nick Pigginb5810032005-10-29 18:16:12 -07001779 if (dirtied)
1780 SetPageDirty(page);
1781 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* FIXME: cache flush missing for rw==READ
1783 * FIXME: call the correct reference counting function
1784 */
Nick Pigginb5810032005-10-29 18:16:12 -07001785 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787
1788 return 0;
1789}
1790
1791/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1792#endif
1793
1794
1795/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1796static int
1797sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1798{
1799#ifdef SG_ALLOW_DIO_CODE
1800 sg_io_hdr_t *hp = &srp->header;
1801 Sg_scatter_hold *schp = &srp->data;
1802 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 int mx_sc_elems, res;
1804 struct scsi_device *sdev = sfp->parentdp->device;
1805
1806 if (((unsigned long)hp->dxferp &
1807 queue_dma_alignment(sdev->request_queue)) != 0)
1808 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1811 if (mx_sc_elems <= 0) {
1812 return 1;
1813 }
Mike Christied6b10342005-11-08 04:06:41 -06001814 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1815 (unsigned long)hp->dxferp, dxfer_len,
1816 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001817 if (res <= 0) {
1818 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 schp->k_use_sg = res;
1822 schp->dio_in_use = 1;
1823 hp->info |= SG_INFO_DIRECT_IO;
1824 return 0;
1825#else
1826 return 1;
1827#endif
1828}
1829
1830static int
1831sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1832{
Mike Christied6b10342005-11-08 04:06:41 -06001833 struct scatterlist *sg;
1834 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1835 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001837 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Eric Sesterhennfb119932007-05-23 14:41:36 -07001839 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return -EFAULT;
1841 if (0 == blk_size)
1842 ++blk_size; /* don't know why */
1843/* round request up to next highest SG_SECTOR_SZ byte boundary */
1844 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1845 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1846 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001847
1848 /* N.B. ret_sz carried into this block ... */
1849 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1850 if (mx_sc_elems < 0)
1851 return mx_sc_elems; /* most likely -ENOMEM */
1852
Douglas Gilbert6460e752006-09-20 18:20:49 -04001853 num = scatter_elem_sz;
1854 if (unlikely(num != scatter_elem_sz_prev)) {
1855 if (num < PAGE_SIZE) {
1856 scatter_elem_sz = PAGE_SIZE;
1857 scatter_elem_sz_prev = PAGE_SIZE;
1858 } else
1859 scatter_elem_sz_prev = num;
1860 }
Mike Christied6b10342005-11-08 04:06:41 -06001861 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1862 (rem_sz > 0) && (k < mx_sc_elems);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001863 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001864
Douglas Gilbert6460e752006-09-20 18:20:49 -04001865 num = (rem_sz > scatter_elem_sz_prev) ?
1866 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001867 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (!p)
1869 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Douglas Gilbert6460e752006-09-20 18:20:49 -04001871 if (num == scatter_elem_sz_prev) {
1872 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1873 scatter_elem_sz = ret_sz;
1874 scatter_elem_sz_prev = ret_sz;
1875 }
1876 }
Jens Axboe642f149032007-10-24 11:20:47 +02001877 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001879 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1880 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001881 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Mike Christied6b10342005-11-08 04:06:41 -06001883 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001884 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1885 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001886
1887 schp->bufflen = blk_size;
1888 if (rem_sz > 0) /* must have failed */
1889 return -ENOMEM;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892}
1893
1894static int
1895sg_write_xfer(Sg_request * srp)
1896{
1897 sg_io_hdr_t *hp = &srp->header;
1898 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001899 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 int num_xfer = 0;
1901 int j, k, onum, usglen, ksglen, res;
1902 int iovec_count = (int) hp->iovec_count;
1903 int dxfer_dir = hp->dxfer_direction;
1904 unsigned char *p;
1905 unsigned char __user *up;
1906 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1907
1908 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1909 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1910 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1911 if (schp->bufflen < num_xfer)
1912 num_xfer = schp->bufflen;
1913 }
1914 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1915 (new_interface
1916 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1917 return 0;
1918
1919 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1920 num_xfer, iovec_count, schp->k_use_sg));
1921 if (iovec_count) {
1922 onum = iovec_count;
1923 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1924 return -EFAULT;
1925 } else
1926 onum = 1;
1927
Mike Christied6b10342005-11-08 04:06:41 -06001928 ksglen = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001929 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06001930 for (j = 0, k = 0; j < onum; ++j) {
1931 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1932 if (res)
1933 return res;
1934
Jens Axboeb0f655d2007-05-11 15:01:01 +02001935 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02001936 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06001937 if (usglen <= 0)
1938 break;
1939 if (ksglen > usglen) {
1940 if (usglen >= num_xfer) {
1941 if (__copy_from_user(p, up, num_xfer))
1942 return -EFAULT;
1943 return 0;
1944 }
1945 if (__copy_from_user(p, up, usglen))
1946 return -EFAULT;
1947 p += usglen;
1948 ksglen -= usglen;
1949 break;
1950 } else {
1951 if (ksglen >= num_xfer) {
1952 if (__copy_from_user(p, up, num_xfer))
1953 return -EFAULT;
1954 return 0;
1955 }
1956 if (__copy_from_user(p, up, ksglen))
1957 return -EFAULT;
1958 up += ksglen;
1959 usglen -= ksglen;
1960 }
1961 ++k;
1962 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return 0;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Mike Christied6b10342005-11-08 04:06:41 -06001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return 0;
1968}
1969
1970static int
1971sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1972 int wr_xf, int *countp, unsigned char __user **up)
1973{
1974 int num_xfer = (int) hp->dxfer_len;
1975 unsigned char __user *p = hp->dxferp;
1976 int count;
1977
1978 if (0 == sg_num) {
1979 if (wr_xf && ('\0' == hp->interface_id))
1980 count = (int) hp->flags; /* holds "old" input_size */
1981 else
1982 count = num_xfer;
1983 } else {
1984 sg_iovec_t iovec;
1985 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1986 return -EFAULT;
1987 p = iovec.iov_base;
1988 count = (int) iovec.iov_len;
1989 }
1990 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1991 return -EFAULT;
1992 if (up)
1993 *up = p;
1994 if (countp)
1995 *countp = count;
1996 return 0;
1997}
1998
1999static void
2000sg_remove_scat(Sg_scatter_hold * schp)
2001{
2002 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2003 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002004 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 if (schp->dio_in_use) {
2007#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06002008 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009#endif
2010 } else {
2011 int k;
2012
Jens Axboe45711f12007-10-22 21:19:53 +02002013 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
Jens Axboeb0f655d2007-05-11 15:01:01 +02002014 ++k, sg = sg_next(sg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 SCSI_LOG_TIMEOUT(5, printk(
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04002016 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
Jens Axboe45711f12007-10-22 21:19:53 +02002017 k, sg_page(sg), sg->length));
2018 sg_page_free(sg_page(sg), sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 }
Mike Christied6b10342005-11-08 04:06:41 -06002021 kfree(schp->buffer);
2022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 memset(schp, 0, sizeof (*schp));
2024}
2025
2026static int
2027sg_read_xfer(Sg_request * srp)
2028{
2029 sg_io_hdr_t *hp = &srp->header;
2030 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002031 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int num_xfer = 0;
2033 int j, k, onum, usglen, ksglen, res;
2034 int iovec_count = (int) hp->iovec_count;
2035 int dxfer_dir = hp->dxfer_direction;
2036 unsigned char *p;
2037 unsigned char __user *up;
2038 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2039
2040 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2041 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2042 num_xfer = hp->dxfer_len;
2043 if (schp->bufflen < num_xfer)
2044 num_xfer = schp->bufflen;
2045 }
2046 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2047 (new_interface
2048 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2049 return 0;
2050
2051 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2052 num_xfer, iovec_count, schp->k_use_sg));
2053 if (iovec_count) {
2054 onum = iovec_count;
2055 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2056 return -EFAULT;
2057 } else
2058 onum = 1;
2059
Jens Axboe45711f12007-10-22 21:19:53 +02002060 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06002061 ksglen = sg->length;
2062 for (j = 0, k = 0; j < onum; ++j) {
2063 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2064 if (res)
2065 return res;
2066
Jens Axboeb0f655d2007-05-11 15:01:01 +02002067 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02002068 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06002069 if (usglen <= 0)
2070 break;
2071 if (ksglen > usglen) {
2072 if (usglen >= num_xfer) {
2073 if (__copy_to_user(up, p, num_xfer))
2074 return -EFAULT;
2075 return 0;
2076 }
2077 if (__copy_to_user(up, p, usglen))
2078 return -EFAULT;
2079 p += usglen;
2080 ksglen -= usglen;
2081 break;
2082 } else {
2083 if (ksglen >= num_xfer) {
2084 if (__copy_to_user(up, p, num_xfer))
2085 return -EFAULT;
2086 return 0;
2087 }
2088 if (__copy_to_user(up, p, ksglen))
2089 return -EFAULT;
2090 up += ksglen;
2091 usglen -= ksglen;
2092 }
2093 ++k;
2094 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return 0;
2096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Mike Christied6b10342005-11-08 04:06:41 -06002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100}
2101
2102static int
2103sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2104{
2105 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002106 struct scatterlist *sg = schp->buffer;
2107 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2110 num_read_xfer));
2111 if ((!outp) || (num_read_xfer <= 0))
2112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Jens Axboe45711f12007-10-22 21:19:53 +02002114 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002115 num = sg->length;
2116 if (num > num_read_xfer) {
Jens Axboe45711f12007-10-22 21:19:53 +02002117 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002118 num_read_xfer))
2119 return -EFAULT;
2120 break;
2121 } else {
Jens Axboe45711f12007-10-22 21:19:53 +02002122 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002123 num))
2124 return -EFAULT;
2125 num_read_xfer -= num;
2126 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 break;
Mike Christied6b10342005-11-08 04:06:41 -06002128 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Mike Christied6b10342005-11-08 04:06:41 -06002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return 0;
2133}
2134
2135static void
2136sg_build_reserve(Sg_fd * sfp, int req_size)
2137{
2138 Sg_scatter_hold *schp = &sfp->reserve;
2139
2140 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2141 do {
2142 if (req_size < PAGE_SIZE)
2143 req_size = PAGE_SIZE;
2144 if (0 == sg_build_indirect(schp, sfp, req_size))
2145 return;
2146 else
2147 sg_remove_scat(schp);
2148 req_size >>= 1; /* divide by 2 */
2149 } while (req_size > (PAGE_SIZE / 2));
2150}
2151
2152static void
2153sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2154{
2155 Sg_scatter_hold *req_schp = &srp->data;
2156 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002157 struct scatterlist *sg = rsv_schp->buffer;
2158 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 srp->res_used = 1;
2161 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002162 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jens Axboeb0f655d2007-05-11 15:01:01 +02002164 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002165 num = sg->length;
2166 if (rem <= num) {
2167 sfp->save_scat_len = num;
2168 sg->length = rem;
2169 req_schp->k_use_sg = k + 1;
2170 req_schp->sglist_len = rsv_schp->sglist_len;
2171 req_schp->buffer = rsv_schp->buffer;
2172
2173 req_schp->bufflen = size;
2174 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2175 break;
2176 } else
2177 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Mike Christied6b10342005-11-08 04:06:41 -06002179
2180 if (k >= rsv_schp->k_use_sg)
2181 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184static void
2185sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2186{
2187 Sg_scatter_hold *req_schp = &srp->data;
2188 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2189
2190 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2191 (int) req_schp->k_use_sg));
2192 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002193 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002196 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 (unsigned) sfp->save_scat_len;
2198 else
2199 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2200 }
2201 req_schp->k_use_sg = 0;
2202 req_schp->bufflen = 0;
2203 req_schp->buffer = NULL;
2204 req_schp->sglist_len = 0;
2205 sfp->save_scat_len = 0;
2206 srp->res_used = 0;
2207}
2208
2209static Sg_request *
2210sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2211{
2212 Sg_request *resp;
2213 unsigned long iflags;
2214
2215 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2216 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2217 /* look for requests that are ready + not SG_IO owned */
2218 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2219 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2220 resp->done = 2; /* guard against other readers */
2221 break;
2222 }
2223 }
2224 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2225 return resp;
2226}
2227
2228#ifdef CONFIG_SCSI_PROC_FS
2229static Sg_request *
2230sg_get_nth_request(Sg_fd * sfp, int nth)
2231{
2232 Sg_request *resp;
2233 unsigned long iflags;
2234 int k;
2235
2236 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2237 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2238 ++k, resp = resp->nextrp) ;
2239 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2240 return resp;
2241}
2242#endif
2243
2244/* always adds to end of list */
2245static Sg_request *
2246sg_add_request(Sg_fd * sfp)
2247{
2248 int k;
2249 unsigned long iflags;
2250 Sg_request *resp;
2251 Sg_request *rp = sfp->req_arr;
2252
2253 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2254 resp = sfp->headrp;
2255 if (!resp) {
2256 memset(rp, 0, sizeof (Sg_request));
2257 rp->parentfp = sfp;
2258 resp = rp;
2259 sfp->headrp = resp;
2260 } else {
2261 if (0 == sfp->cmd_q)
2262 resp = NULL; /* command queuing disallowed */
2263 else {
2264 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2265 if (!rp->parentfp)
2266 break;
2267 }
2268 if (k < SG_MAX_QUEUE) {
2269 memset(rp, 0, sizeof (Sg_request));
2270 rp->parentfp = sfp;
2271 while (resp->nextrp)
2272 resp = resp->nextrp;
2273 resp->nextrp = rp;
2274 resp = rp;
2275 } else
2276 resp = NULL;
2277 }
2278 }
2279 if (resp) {
2280 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002281 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2284 return resp;
2285}
2286
2287/* Return of 1 for found; 0 for not found */
2288static int
2289sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2290{
2291 Sg_request *prev_rp;
2292 Sg_request *rp;
2293 unsigned long iflags;
2294 int res = 0;
2295
2296 if ((!sfp) || (!srp) || (!sfp->headrp))
2297 return res;
2298 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2299 prev_rp = sfp->headrp;
2300 if (srp == prev_rp) {
2301 sfp->headrp = prev_rp->nextrp;
2302 prev_rp->parentfp = NULL;
2303 res = 1;
2304 } else {
2305 while ((rp = prev_rp->nextrp)) {
2306 if (srp == rp) {
2307 prev_rp->nextrp = rp->nextrp;
2308 rp->parentfp = NULL;
2309 res = 1;
2310 break;
2311 }
2312 prev_rp = rp;
2313 }
2314 }
2315 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2316 return res;
2317}
2318
2319#ifdef CONFIG_SCSI_PROC_FS
2320static Sg_fd *
2321sg_get_nth_sfp(Sg_device * sdp, int nth)
2322{
2323 Sg_fd *resp;
2324 unsigned long iflags;
2325 int k;
2326
James Bottomley7c07d612007-08-05 13:36:11 -05002327 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2329 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002330 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return resp;
2332}
2333#endif
2334
2335static Sg_fd *
2336sg_add_sfp(Sg_device * sdp, int dev)
2337{
2338 Sg_fd *sfp;
2339 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002340 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Mike Christied6b10342005-11-08 04:06:41 -06002342 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (!sfp)
2344 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 init_waitqueue_head(&sfp->read_wait);
2347 rwlock_init(&sfp->rq_list_lock);
2348
2349 sfp->timeout = SG_DEFAULT_TIMEOUT;
2350 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2351 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2352 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2353 sdp->device->host->unchecked_isa_dma : 1;
2354 sfp->cmd_q = SG_DEF_COMMAND_Q;
2355 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2356 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002357 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (!sdp->headfp)
2359 sdp->headfp = sfp;
2360 else { /* add to tail of existing list */
2361 Sg_fd *pfp = sdp->headfp;
2362 while (pfp->nextfp)
2363 pfp = pfp->nextfp;
2364 pfp->nextfp = sfp;
2365 }
James Bottomley7c07d612007-08-05 13:36:11 -05002366 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002368 if (unlikely(sg_big_buff != def_reserved_size))
2369 sg_big_buff = def_reserved_size;
2370
Alan Stern44ec9542007-02-20 11:01:57 -05002371 bufflen = min_t(int, sg_big_buff,
2372 sdp->device->request_queue->max_sectors * 512);
2373 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2375 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2376 return sfp;
2377}
2378
2379static void
2380__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2381{
2382 Sg_fd *fp;
2383 Sg_fd *prev_fp;
2384
2385 prev_fp = sdp->headfp;
2386 if (sfp == prev_fp)
2387 sdp->headfp = prev_fp->nextfp;
2388 else {
2389 while ((fp = prev_fp->nextfp)) {
2390 if (sfp == fp) {
2391 prev_fp->nextfp = fp->nextfp;
2392 break;
2393 }
2394 prev_fp = fp;
2395 }
2396 }
2397 if (sfp->reserve.bufflen > 0) {
2398 SCSI_LOG_TIMEOUT(6,
2399 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2400 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 sg_remove_scat(&sfp->reserve);
2402 }
2403 sfp->parentdp = NULL;
2404 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002405 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408/* Returns 0 in normal case, 1 when detached and sdp object removed */
2409static int
2410sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2411{
2412 Sg_request *srp;
2413 Sg_request *tsrp;
2414 int dirty = 0;
2415 int res = 0;
2416
2417 for (srp = sfp->headrp; srp; srp = tsrp) {
2418 tsrp = srp->nextrp;
2419 if (sg_srp_done(srp, sfp))
2420 sg_finish_rem_req(srp);
2421 else
2422 ++dirty;
2423 }
2424 if (0 == dirty) {
2425 unsigned long iflags;
2426
James Bottomley7c07d612007-08-05 13:36:11 -05002427 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 __sg_remove_sfp(sdp, sfp);
2429 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002430 idr_remove(&sg_index_idr, sdp->index);
2431 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 res = 1;
2433 }
James Bottomley7c07d612007-08-05 13:36:11 -05002434 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 } else {
2436 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2437 /* only bump the access_count if we actually succeeded in
2438 * throwing another counter on the host module */
2439 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2440 sfp->closed = 1; /* flag dirty state on this fd */
2441 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2442 dirty));
2443 }
2444 return res;
2445}
2446
2447static int
2448sg_res_in_use(Sg_fd * sfp)
2449{
2450 const Sg_request *srp;
2451 unsigned long iflags;
2452
2453 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2454 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2455 if (srp->res_used)
2456 break;
2457 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2458 return srp ? 1 : 0;
2459}
2460
Douglas Gilbert6460e752006-09-20 18:20:49 -04002461/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002462static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2464{
Mike Christied6b10342005-11-08 04:06:41 -06002465 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002466 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002468 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Douglas Gilbert6460e752006-09-20 18:20:49 -04002470 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return resp;
2472
2473 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002474 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002476 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2479 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002480 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002481 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002482 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 --order;
2484 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002485 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 resSz = a_size;
2487 }
2488 if (resp) {
2489 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002490 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002491 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493 return resp;
2494}
2495
2496static void
Mike Christied6b10342005-11-08 04:06:41 -06002497sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 int order, a_size;
2500
Mike Christied6b10342005-11-08 04:06:41 -06002501 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return;
2503 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2504 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002505 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508#ifndef MAINTENANCE_IN_CMD
2509#define MAINTENANCE_IN_CMD 0xa3
2510#endif
2511
2512static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2513 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2514 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2515 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2516};
2517
2518static int
2519sg_allow_access(unsigned char opcode, char dev_type)
2520{
2521 int k;
2522
2523 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2524 return 1;
2525 for (k = 0; k < sizeof (allow_ops); ++k) {
2526 if (opcode == allow_ops[k])
2527 return 1;
2528 }
2529 return 0;
2530}
2531
2532#ifdef CONFIG_SCSI_PROC_FS
2533static int
James Bottomley7c07d612007-08-05 13:36:11 -05002534sg_idr_max_id(int id, void *p, void *data)
2535{
2536 int *k = data;
2537
2538 if (*k < id)
2539 *k = id;
2540
2541 return 0;
2542}
2543
2544static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545sg_last_dev(void)
2546{
Tony Battersby53474c02008-01-22 15:25:49 -05002547 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 unsigned long iflags;
2549
James Bottomley7c07d612007-08-05 13:36:11 -05002550 read_lock_irqsave(&sg_index_lock, iflags);
2551 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2552 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return k + 1; /* origin 1 */
2554}
2555#endif
2556
2557static Sg_device *
2558sg_get_dev(int dev)
2559{
James Bottomley7c07d612007-08-05 13:36:11 -05002560 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 unsigned long iflags;
2562
James Bottomley7c07d612007-08-05 13:36:11 -05002563 read_lock_irqsave(&sg_index_lock, iflags);
2564 sdp = idr_find(&sg_index_idr, dev);
2565 read_unlock_irqrestore(&sg_index_lock, iflags);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return sdp;
2568}
2569
2570#ifdef CONFIG_SCSI_PROC_FS
2571
2572static struct proc_dir_entry *sg_proc_sgp = NULL;
2573
2574static char sg_proc_sg_dirname[] = "scsi/sg";
2575
2576static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2577
2578static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2579static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2580 size_t count, loff_t *off);
2581static struct file_operations adio_fops = {
2582 /* .owner, .read and .llseek added in sg_proc_init() */
2583 .open = sg_proc_single_open_adio,
2584 .write = sg_proc_write_adio,
2585 .release = single_release,
2586};
2587
2588static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2589static ssize_t sg_proc_write_dressz(struct file *filp,
2590 const char __user *buffer, size_t count, loff_t *off);
2591static struct file_operations dressz_fops = {
2592 .open = sg_proc_single_open_dressz,
2593 .write = sg_proc_write_dressz,
2594 .release = single_release,
2595};
2596
2597static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2598static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2599static struct file_operations version_fops = {
2600 .open = sg_proc_single_open_version,
2601 .release = single_release,
2602};
2603
2604static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2605static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2606static struct file_operations devhdr_fops = {
2607 .open = sg_proc_single_open_devhdr,
2608 .release = single_release,
2609};
2610
2611static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2612static int sg_proc_open_dev(struct inode *inode, struct file *file);
2613static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2614static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2615static void dev_seq_stop(struct seq_file *s, void *v);
2616static struct file_operations dev_fops = {
2617 .open = sg_proc_open_dev,
2618 .release = seq_release,
2619};
2620static struct seq_operations dev_seq_ops = {
2621 .start = dev_seq_start,
2622 .next = dev_seq_next,
2623 .stop = dev_seq_stop,
2624 .show = sg_proc_seq_show_dev,
2625};
2626
2627static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2628static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2629static struct file_operations devstrs_fops = {
2630 .open = sg_proc_open_devstrs,
2631 .release = seq_release,
2632};
2633static struct seq_operations devstrs_seq_ops = {
2634 .start = dev_seq_start,
2635 .next = dev_seq_next,
2636 .stop = dev_seq_stop,
2637 .show = sg_proc_seq_show_devstrs,
2638};
2639
2640static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2641static int sg_proc_open_debug(struct inode *inode, struct file *file);
2642static struct file_operations debug_fops = {
2643 .open = sg_proc_open_debug,
2644 .release = seq_release,
2645};
2646static struct seq_operations debug_seq_ops = {
2647 .start = dev_seq_start,
2648 .next = dev_seq_next,
2649 .stop = dev_seq_stop,
2650 .show = sg_proc_seq_show_debug,
2651};
2652
2653
2654struct sg_proc_leaf {
2655 const char * name;
2656 struct file_operations * fops;
2657};
2658
2659static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2660 {"allow_dio", &adio_fops},
2661 {"debug", &debug_fops},
2662 {"def_reserved_size", &dressz_fops},
2663 {"device_hdr", &devhdr_fops},
2664 {"devices", &dev_fops},
2665 {"device_strs", &devstrs_fops},
2666 {"version", &version_fops}
2667};
2668
2669static int
2670sg_proc_init(void)
2671{
2672 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002673 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct sg_proc_leaf * leaf;
2675
Al Viro66600222005-09-28 22:32:57 +01002676 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if (!sg_proc_sgp)
2678 return 1;
2679 for (k = 0; k < num_leaves; ++k) {
2680 leaf = &sg_proc_leaf_arr[k];
2681 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002682 leaf->fops->owner = THIS_MODULE;
2683 leaf->fops->read = seq_read;
2684 leaf->fops->llseek = seq_lseek;
2685 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
2687 return 0;
2688}
2689
2690static void
2691sg_proc_cleanup(void)
2692{
2693 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002694 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 if (!sg_proc_sgp)
2697 return;
2698 for (k = 0; k < num_leaves; ++k)
2699 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2700 remove_proc_entry(sg_proc_sg_dirname, NULL);
2701}
2702
2703
2704static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2705{
2706 seq_printf(s, "%d\n", *((int *)s->private));
2707 return 0;
2708}
2709
2710static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2711{
2712 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2713}
2714
2715static ssize_t
2716sg_proc_write_adio(struct file *filp, const char __user *buffer,
2717 size_t count, loff_t *off)
2718{
2719 int num;
2720 char buff[11];
2721
2722 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2723 return -EACCES;
2724 num = (count < 10) ? count : 10;
2725 if (copy_from_user(buff, buffer, num))
2726 return -EFAULT;
2727 buff[num] = '\0';
2728 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2729 return count;
2730}
2731
2732static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2733{
2734 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2735}
2736
2737static ssize_t
2738sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2739 size_t count, loff_t *off)
2740{
2741 int num;
2742 unsigned long k = ULONG_MAX;
2743 char buff[11];
2744
2745 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2746 return -EACCES;
2747 num = (count < 10) ? count : 10;
2748 if (copy_from_user(buff, buffer, num))
2749 return -EFAULT;
2750 buff[num] = '\0';
2751 k = simple_strtoul(buff, NULL, 10);
2752 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2753 sg_big_buff = k;
2754 return count;
2755 }
2756 return -ERANGE;
2757}
2758
2759static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2760{
2761 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2762 sg_version_date);
2763 return 0;
2764}
2765
2766static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2767{
2768 return single_open(file, sg_proc_seq_show_version, NULL);
2769}
2770
2771static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2772{
2773 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2774 "online\n");
2775 return 0;
2776}
2777
2778static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2779{
2780 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2781}
2782
2783struct sg_proc_deviter {
2784 loff_t index;
2785 size_t max;
2786};
2787
2788static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2789{
2790 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2791
Jan Blunck729d70f2005-08-27 11:07:52 -07002792 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (! it)
2794 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 it->index = *pos;
2797 it->max = sg_last_dev();
2798 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002799 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
2803static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2804{
Jan Blunck729d70f2005-08-27 11:07:52 -07002805 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 *pos = ++it->index;
2808 return (it->index < it->max) ? it : NULL;
2809}
2810
2811static void dev_seq_stop(struct seq_file *s, void *v)
2812{
Jan Blunck729d70f2005-08-27 11:07:52 -07002813 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816static int sg_proc_open_dev(struct inode *inode, struct file *file)
2817{
2818 return seq_open(file, &dev_seq_ops);
2819}
2820
2821static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2822{
2823 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2824 Sg_device *sdp;
2825 struct scsi_device *scsidp;
2826
2827 sdp = it ? sg_get_dev(it->index) : NULL;
2828 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2829 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2830 scsidp->host->host_no, scsidp->channel,
2831 scsidp->id, scsidp->lun, (int) scsidp->type,
2832 1,
2833 (int) scsidp->queue_depth,
2834 (int) scsidp->device_busy,
2835 (int) scsi_device_online(scsidp));
2836 else
2837 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2838 return 0;
2839}
2840
2841static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2842{
2843 return seq_open(file, &devstrs_seq_ops);
2844}
2845
2846static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2847{
2848 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2849 Sg_device *sdp;
2850 struct scsi_device *scsidp;
2851
2852 sdp = it ? sg_get_dev(it->index) : NULL;
2853 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2854 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2855 scsidp->vendor, scsidp->model, scsidp->rev);
2856 else
2857 seq_printf(s, "<no active device>\n");
2858 return 0;
2859}
2860
2861static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2862{
2863 int k, m, new_interface, blen, usg;
2864 Sg_request *srp;
2865 Sg_fd *fp;
2866 const sg_io_hdr_t *hp;
2867 const char * cp;
cb59e842005-04-02 13:51:23 -06002868 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2871 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2872 "(res)sgat=%d low_dma=%d\n", k + 1,
2873 jiffies_to_msecs(fp->timeout),
2874 fp->reserve.bufflen,
2875 (int) fp->reserve.k_use_sg,
2876 (int) fp->low_dma);
2877 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2878 (int) fp->cmd_q, (int) fp->force_packid,
2879 (int) fp->keep_orphan, (int) fp->closed);
2880 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2881 hp = &srp->header;
2882 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2883 if (srp->res_used) {
2884 if (new_interface &&
2885 (SG_FLAG_MMAP_IO & hp->flags))
2886 cp = " mmap>> ";
2887 else
2888 cp = " rb>> ";
2889 } else {
2890 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2891 cp = " dio>> ";
2892 else
2893 cp = " ";
2894 }
2895 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002896 blen = srp->data.bufflen;
2897 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 seq_printf(s, srp->done ?
2899 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002900 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 seq_printf(s, " id=%d blen=%d",
2902 srp->header.pack_id, blen);
2903 if (srp->done)
2904 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002905 else {
2906 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002908 (new_interface ? hp->timeout :
2909 jiffies_to_msecs(fp->timeout)),
2910 (ms > hp->duration ? ms - hp->duration : 0));
2911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2913 (int) srp->data.cmd_opcode);
2914 }
2915 if (0 == m)
2916 seq_printf(s, " No requests active\n");
2917 }
2918}
2919
2920static int sg_proc_open_debug(struct inode *inode, struct file *file)
2921{
2922 return seq_open(file, &debug_seq_ops);
2923}
2924
2925static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2926{
2927 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2928 Sg_device *sdp;
2929
2930 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002931 seq_printf(s, "max_active_device=%d(origin 1)\n",
2932 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2934 }
2935 sdp = it ? sg_get_dev(it->index) : NULL;
2936 if (sdp) {
2937 struct scsi_device *scsidp = sdp->device;
2938
2939 if (NULL == scsidp) {
2940 seq_printf(s, "device %d detached ??\n",
2941 (int)it->index);
2942 return 0;
2943 }
2944
2945 if (sg_get_nth_sfp(sdp, 0)) {
2946 seq_printf(s, " >>> device=%s ",
2947 sdp->disk->disk_name);
2948 if (sdp->detached)
2949 seq_printf(s, "detached pending close ");
2950 else
2951 seq_printf
2952 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2953 scsidp->host->host_no,
2954 scsidp->channel, scsidp->id,
2955 scsidp->lun,
2956 scsidp->host->hostt->emulated);
2957 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2958 sdp->sg_tablesize, sdp->exclude);
2959 }
2960 sg_proc_debug_helper(s, sdp);
2961 }
2962 return 0;
2963}
2964
2965#endif /* CONFIG_SCSI_PROC_FS */
2966
2967module_init(init_sg);
2968module_exit(exit_sg);