blob: 3f8b93188567909201b12d209030990495405804 [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>
44#include <linux/smp_lock.h>
45#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/cdev.h>
47#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "scsi.h"
db9dff32005-04-03 14:53:59 -050053#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_host.h>
55#include <scsi/scsi_driver.h>
56#include <scsi/scsi_ioctl.h>
57#include <scsi/sg.h>
58
59#include "scsi_logging.h"
60
61#ifdef CONFIG_SCSI_PROC_FS
62#include <linux/proc_fs.h>
Douglas Gilbert6460e752006-09-20 18:20:49 -040063static char *sg_version_date = "20060920";
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int sg_proc_init(void);
66static void sg_proc_cleanup(void);
67#endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define SG_ALLOW_DIO_DEF 0
70#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
71
72#define SG_MAX_DEVS 32768
73
74/*
75 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
76 * Then when using 32 bit integers x * m may overflow during the calculation.
77 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
78 * calculates the same, but prevents the overflow when both m and d
79 * are "small" numbers (like HZ and USER_HZ).
80 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
81 * in 32 bits.
82 */
83#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
84
85#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
86
87int sg_big_buff = SG_DEF_RESERVED_SIZE;
88/* N.B. This variable is readable and writeable via
89 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
90 of this size (or less if there is not enough memory) will be reserved
91 for use by this file descriptor. [Deprecated usage: this variable is also
92 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
93 the kernel (i.e. it is not a module).] */
94static int def_reserved_size = -1; /* picks up init parameter */
95static int sg_allow_dio = SG_ALLOW_DIO_DEF;
96
Douglas Gilbert6460e752006-09-20 18:20:49 -040097static int scatter_elem_sz = SG_SCATTER_SZ;
98static int scatter_elem_sz_prev = SG_SCATTER_SZ;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define SG_SECTOR_SZ 512
101#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
102
103#define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
104
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500105static int sg_add(struct class_device *, struct class_interface *);
106static void sg_remove(struct class_device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static DEFINE_RWLOCK(sg_dev_arr_lock); /* Also used to lock
109 file descriptor list for device */
110
111static struct class_interface sg_interface = {
112 .add = sg_add,
113 .remove = sg_remove,
114};
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 */
118 unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
119 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 */
166 Sg_fd *headfp; /* first open fd belonging to this device */
167 volatile char detached; /* 0->attached, 1->detached pending removal */
168 volatile char exclude; /* opened for exclusive access */
169 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
170 struct gendisk *disk;
171 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
172} Sg_device;
173
174static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600175/* tasklet or soft irq callback */
176static void sg_cmd_done(void *data, char *sense, int result, int resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int sg_start_req(Sg_request * srp);
178static void sg_finish_rem_req(Sg_request * srp);
179static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
180static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
181 int tablesize);
182static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
183 Sg_request * srp);
184static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
185 int blocking, int read_only, Sg_request ** o_srp);
186static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
187 unsigned char *cmnd, int timeout, int blocking);
188static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
189 int wr_xf, int *countp, unsigned char __user **up);
190static int sg_write_xfer(Sg_request * srp);
191static int sg_read_xfer(Sg_request * srp);
192static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
193static void sg_remove_scat(Sg_scatter_hold * schp);
194static void sg_build_reserve(Sg_fd * sfp, int req_size);
195static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
196static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600197static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
198static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
200static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
201static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
203static Sg_request *sg_add_request(Sg_fd * sfp);
204static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
205static int sg_res_in_use(Sg_fd * sfp);
206static int sg_allow_access(unsigned char opcode, char dev_type);
207static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
208static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#ifdef CONFIG_SCSI_PROC_FS
210static int sg_last_dev(void);
211#endif
212
213static Sg_device **sg_dev_arr = NULL;
214static int sg_dev_max;
215static int sg_nr_dev;
216
217#define SZ_SG_HEADER sizeof(struct sg_header)
218#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
219#define SZ_SG_IOVEC sizeof(sg_iovec_t)
220#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
221
222static int
223sg_open(struct inode *inode, struct file *filp)
224{
225 int dev = iminor(inode);
226 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600227 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 Sg_device *sdp;
229 Sg_fd *sfp;
230 int res;
231 int retval;
232
233 nonseekable_open(inode, filp);
234 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
235 sdp = sg_get_dev(dev);
236 if ((!sdp) || (!sdp->device))
237 return -ENXIO;
238 if (sdp->detached)
239 return -ENODEV;
240
241 /* This driver's module count bumped by fops_get in <linux/fs.h> */
242 /* Prevent the device driver from vanishing while we sleep */
243 retval = scsi_device_get(sdp->device);
244 if (retval)
245 return retval;
246
247 if (!((flags & O_NONBLOCK) ||
248 scsi_block_when_processing_errors(sdp->device))) {
249 retval = -ENXIO;
250 /* we are in error recovery for this device */
251 goto error_out;
252 }
253
254 if (flags & O_EXCL) {
255 if (O_RDONLY == (flags & O_ACCMODE)) {
256 retval = -EPERM; /* Can't lock it with read only access */
257 goto error_out;
258 }
259 if (sdp->headfp && (flags & O_NONBLOCK)) {
260 retval = -EBUSY;
261 goto error_out;
262 }
263 res = 0;
264 __wait_event_interruptible(sdp->o_excl_wait,
265 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
266 if (res) {
267 retval = res; /* -ERESTARTSYS because signal hit process */
268 goto error_out;
269 }
270 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
271 if (flags & O_NONBLOCK) {
272 retval = -EBUSY;
273 goto error_out;
274 }
275 res = 0;
276 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
277 res);
278 if (res) {
279 retval = res; /* -ERESTARTSYS because signal hit process */
280 goto error_out;
281 }
282 }
283 if (sdp->detached) {
284 retval = -ENODEV;
285 goto error_out;
286 }
287 if (!sdp->headfp) { /* no existing opens on this device */
288 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600289 q = sdp->device->request_queue;
290 sdp->sg_tablesize = min(q->max_hw_segments,
291 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 if ((sfp = sg_add_sfp(sdp, dev)))
294 filp->private_data = sfp;
295 else {
296 if (flags & O_EXCL)
297 sdp->exclude = 0; /* undo if error */
298 retval = -ENOMEM;
299 goto error_out;
300 }
301 return 0;
302
303 error_out:
304 scsi_device_put(sdp->device);
305 return retval;
306}
307
308/* Following function was formerly called 'sg_close' */
309static int
310sg_release(struct inode *inode, struct file *filp)
311{
312 Sg_device *sdp;
313 Sg_fd *sfp;
314
315 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
316 return -ENXIO;
317 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
318 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
319 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
320 if (!sdp->detached) {
321 scsi_device_put(sdp->device);
322 }
323 sdp->exclude = 0;
324 wake_up_interruptible(&sdp->o_excl_wait);
325 }
326 return 0;
327}
328
329static ssize_t
330sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 Sg_device *sdp;
333 Sg_fd *sfp;
334 Sg_request *srp;
335 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600337 struct sg_header *old_hdr = NULL;
338 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
341 return -ENXIO;
342 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
343 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!access_ok(VERIFY_WRITE, buf, count))
346 return -EFAULT;
347 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600348 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
349 if (!old_hdr)
350 return -ENOMEM;
351 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
352 retval = -EFAULT;
353 goto free_old_hdr;
354 }
355 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600357 sg_io_hdr_t *new_hdr;
358 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
359 if (!new_hdr) {
360 retval = -ENOMEM;
361 goto free_old_hdr;
362 }
363 retval =__copy_from_user
364 (new_hdr, buf, SZ_SG_IO_HDR);
365 req_pack_id = new_hdr->pack_id;
366 kfree(new_hdr);
367 if (retval) {
368 retval = -EFAULT;
369 goto free_old_hdr;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 } else
cb59e842005-04-02 13:51:23 -0600373 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 srp = sg_get_rq_mark(sfp, req_pack_id);
376 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600377 if (sdp->detached) {
378 retval = -ENODEV;
379 goto free_old_hdr;
380 }
381 if (filp->f_flags & O_NONBLOCK) {
382 retval = -EAGAIN;
383 goto free_old_hdr;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 while (1) {
cb59e842005-04-02 13:51:23 -0600386 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600388 (sdp->detached ||
389 (srp = sg_get_rq_mark(sfp, req_pack_id))),
390 retval);
391 if (sdp->detached) {
392 retval = -ENODEV;
393 goto free_old_hdr;
394 }
395 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
cb59e842005-04-02 13:51:23 -0600397
398 /* -ERESTARTSYS as signal hit process */
399 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 }
cb59e842005-04-02 13:51:23 -0600402 if (srp->header.interface_id != '\0') {
403 retval = sg_new_read(sfp, buf, count, srp);
404 goto free_old_hdr;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600408 if (old_hdr == NULL) {
409 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
410 if (! old_hdr) {
411 retval = -ENOMEM;
412 goto free_old_hdr;
413 }
414 }
415 memset(old_hdr, 0, SZ_SG_HEADER);
416 old_hdr->reply_len = (int) hp->timeout;
417 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
418 old_hdr->pack_id = hp->pack_id;
419 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600421 old_hdr->target_status = hp->masked_status;
422 old_hdr->host_status = hp->host_status;
423 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if ((CHECK_CONDITION & hp->masked_status) ||
425 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600426 memcpy(old_hdr->sense_buffer, srp->sense_b,
427 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 switch (hp->host_status) {
429 /* This setup of 'result' is for backward compatibility and is best
430 ignored by the user who should use target, host + driver status */
431 case DID_OK:
432 case DID_PASSTHROUGH:
433 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600434 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436 case DID_NO_CONNECT:
437 case DID_BUS_BUSY:
438 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600439 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 case DID_BAD_TARGET:
442 case DID_ABORT:
443 case DID_PARITY:
444 case DID_RESET:
445 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600449 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 hp->masked_status == GOOD) ? 0 : EIO;
451 break;
452 default:
cb59e842005-04-02 13:51:23 -0600453 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 }
456
457 /* Now copy the result back to the user buffer. */
458 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600459 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
460 retval = -EFAULT;
461 goto free_old_hdr;
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600464 if (count > old_hdr->reply_len)
465 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600467 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
468 retval = -EFAULT;
469 goto free_old_hdr;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 } else
cb59e842005-04-02 13:51:23 -0600473 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600475 retval = count;
476free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800477 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600478 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481static ssize_t
482sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
483{
484 sg_io_hdr_t *hp = &srp->header;
485 int err = 0;
486 int len;
487
488 if (count < SZ_SG_IO_HDR) {
489 err = -EINVAL;
490 goto err_out;
491 }
492 hp->sb_len_wr = 0;
493 if ((hp->mx_sb_len > 0) && hp->sbp) {
494 if ((CHECK_CONDITION & hp->masked_status) ||
495 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600496 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
498 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
499 len = (len > sb_len) ? sb_len : len;
500 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
501 err = -EFAULT;
502 goto err_out;
503 }
504 hp->sb_len_wr = len;
505 }
506 }
507 if (hp->masked_status || hp->host_status || hp->driver_status)
508 hp->info |= SG_INFO_CHECK;
509 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
510 err = -EFAULT;
511 goto err_out;
512 }
513 err = sg_read_xfer(srp);
514 err_out:
515 sg_finish_rem_req(srp);
516 return (0 == err) ? count : err;
517}
518
519static ssize_t
520sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
521{
522 int mxsize, cmd_size, k;
523 int input_size, blocking;
524 unsigned char opcode;
525 Sg_device *sdp;
526 Sg_fd *sfp;
527 Sg_request *srp;
528 struct sg_header old_hdr;
529 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600530 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
533 return -ENXIO;
534 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
535 sdp->disk->disk_name, (int) count));
536 if (sdp->detached)
537 return -ENODEV;
538 if (!((filp->f_flags & O_NONBLOCK) ||
539 scsi_block_when_processing_errors(sdp->device)))
540 return -ENXIO;
541
542 if (!access_ok(VERIFY_READ, buf, count))
543 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
544 if (count < SZ_SG_HEADER)
545 return -EIO;
546 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
547 return -EFAULT;
548 blocking = !(filp->f_flags & O_NONBLOCK);
549 if (old_hdr.reply_len < 0)
550 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
551 if (count < (SZ_SG_HEADER + 6))
552 return -EIO; /* The minimum scsi command length is 6 bytes. */
553
554 if (!(srp = sg_add_request(sfp))) {
555 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
556 return -EDOM;
557 }
558 buf += SZ_SG_HEADER;
559 __get_user(opcode, buf);
560 if (sfp->next_cmd_len > 0) {
561 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
562 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
563 sfp->next_cmd_len = 0;
564 sg_remove_request(sfp, srp);
565 return -EIO;
566 }
567 cmd_size = sfp->next_cmd_len;
568 sfp->next_cmd_len = 0; /* reset so only this write() effected */
569 } else {
570 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
571 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
572 cmd_size = 12;
573 }
574 SCSI_LOG_TIMEOUT(4, printk(
575 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
576/* Determine buffer size. */
577 input_size = count - cmd_size;
578 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
579 mxsize -= SZ_SG_HEADER;
580 input_size -= SZ_SG_HEADER;
581 if (input_size < 0) {
582 sg_remove_request(sfp, srp);
583 return -EIO; /* User did not pass enough bytes for this command. */
584 }
585 hp = &srp->header;
586 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
587 hp->cmd_len = (unsigned char) cmd_size;
588 hp->iovec_count = 0;
589 hp->mx_sb_len = 0;
590 if (input_size > 0)
591 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
592 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
593 else
594 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
595 hp->dxfer_len = mxsize;
596 hp->dxferp = (char __user *)buf + cmd_size;
597 hp->sbp = NULL;
598 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
599 hp->flags = input_size; /* structure abuse ... */
600 hp->pack_id = old_hdr.pack_id;
601 hp->usr_ptr = NULL;
602 if (__copy_from_user(cmnd, buf, cmd_size))
603 return -EFAULT;
604 /*
605 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
606 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
607 * is a non-zero input_size, so emit a warning.
608 */
609 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
610 if (printk_ratelimit())
611 printk(KERN_WARNING
612 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
613 "guessing data in;\n" KERN_WARNING " "
614 "program %s not setting count and/or reply_len properly\n",
615 old_hdr.reply_len - (int)SZ_SG_HEADER,
616 input_size, (unsigned int) cmnd[0],
617 current->comm);
618 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
619 return (k < 0) ? k : count;
620}
621
622static ssize_t
623sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
624 int blocking, int read_only, Sg_request ** o_srp)
625{
626 int k;
627 Sg_request *srp;
628 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600629 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int timeout;
631 unsigned long ul_timeout;
632
633 if (count < SZ_SG_IO_HDR)
634 return -EINVAL;
635 if (!access_ok(VERIFY_READ, buf, count))
636 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
637
638 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
639 if (!(srp = sg_add_request(sfp))) {
640 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
641 return -EDOM;
642 }
643 hp = &srp->header;
644 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
645 sg_remove_request(sfp, srp);
646 return -EFAULT;
647 }
648 if (hp->interface_id != 'S') {
649 sg_remove_request(sfp, srp);
650 return -ENOSYS;
651 }
652 if (hp->flags & SG_FLAG_MMAP_IO) {
653 if (hp->dxfer_len > sfp->reserve.bufflen) {
654 sg_remove_request(sfp, srp);
655 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
656 }
657 if (hp->flags & SG_FLAG_DIRECT_IO) {
658 sg_remove_request(sfp, srp);
659 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
660 }
661 if (sg_res_in_use(sfp)) {
662 sg_remove_request(sfp, srp);
663 return -EBUSY; /* reserve buffer already being used */
664 }
665 }
666 ul_timeout = msecs_to_jiffies(srp->header.timeout);
667 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
668 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
669 sg_remove_request(sfp, srp);
670 return -EMSGSIZE;
671 }
672 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
673 sg_remove_request(sfp, srp);
674 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
675 }
676 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
677 sg_remove_request(sfp, srp);
678 return -EFAULT;
679 }
680 if (read_only &&
681 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
682 sg_remove_request(sfp, srp);
683 return -EPERM;
684 }
685 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
686 if (k < 0)
687 return k;
688 if (o_srp)
689 *o_srp = srp;
690 return count;
691}
692
693static int
694sg_common_write(Sg_fd * sfp, Sg_request * srp,
695 unsigned char *cmnd, int timeout, int blocking)
696{
Mike Christied6b10342005-11-08 04:06:41 -0600697 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 Sg_device *sdp = sfp->parentdp;
699 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
702 hp->status = 0;
703 hp->masked_status = 0;
704 hp->msg_status = 0;
705 hp->info = 0;
706 hp->host_status = 0;
707 hp->driver_status = 0;
708 hp->resid = 0;
709 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
710 (int) cmnd[0], (int) hp->cmd_len));
711
712 if ((k = sg_start_req(srp))) {
713 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
714 sg_finish_rem_req(srp);
715 return k; /* probably out of space --> ENOMEM */
716 }
717 if ((k = sg_write_xfer(srp))) {
718 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
719 sg_finish_rem_req(srp);
720 return k;
721 }
722 if (sdp->detached) {
723 sg_finish_rem_req(srp);
724 return -ENODEV;
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 switch (hp->dxfer_direction) {
728 case SG_DXFER_TO_FROM_DEV:
729 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600730 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
732 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600733 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
735 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600736 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 default:
Mike Christied6b10342005-11-08 04:06:41 -0600739 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 }
cb59e842005-04-02 13:51:23 -0600742 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/* Now send everything of to mid-level. The next time we hear about this
744 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600745 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600746 hp->dxfer_len, srp->data.k_use_sg, timeout,
747 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
748 GFP_ATOMIC)) {
749 SCSI_LOG_TIMEOUT(1, printk("sg_write: scsi_execute_async failed\n"));
750 /*
751 * most likely out of mem, but could also be a bad map
752 */
Mike Christie18c49b82006-03-22 16:04:38 -0600753 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600754 return -ENOMEM;
755 } else
756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static int
760sg_srp_done(Sg_request *srp, Sg_fd *sfp)
761{
762 unsigned long iflags;
763 int done;
764
765 read_lock_irqsave(&sfp->rq_list_lock, iflags);
766 done = srp->done;
767 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
768 return done;
769}
770
771static int
772sg_ioctl(struct inode *inode, struct file *filp,
773 unsigned int cmd_in, unsigned long arg)
774{
775 void __user *p = (void __user *)arg;
776 int __user *ip = p;
777 int result, val, read_only;
778 Sg_device *sdp;
779 Sg_fd *sfp;
780 Sg_request *srp;
781 unsigned long iflags;
782
783 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
784 return -ENXIO;
785 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
786 sdp->disk->disk_name, (int) cmd_in));
787 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
788
789 switch (cmd_in) {
790 case SG_IO:
791 {
792 int blocking = 1; /* ignore O_NONBLOCK flag */
793
794 if (sdp->detached)
795 return -ENODEV;
796 if (!scsi_block_when_processing_errors(sdp->device))
797 return -ENXIO;
798 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
799 return -EFAULT;
800 result =
801 sg_new_write(sfp, p, SZ_SG_IO_HDR,
802 blocking, read_only, &srp);
803 if (result < 0)
804 return result;
805 srp->sg_io_owned = 1;
806 while (1) {
807 result = 0; /* following macro to beat race condition */
808 __wait_event_interruptible(sfp->read_wait,
809 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
810 result);
811 if (sdp->detached)
812 return -ENODEV;
813 if (sfp->closed)
814 return 0; /* request packet dropped already */
815 if (0 == result)
816 break;
817 srp->orphan = 1;
818 return result; /* -ERESTARTSYS because signal hit process */
819 }
820 write_lock_irqsave(&sfp->rq_list_lock, iflags);
821 srp->done = 2;
822 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
823 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
824 return (result < 0) ? result : 0;
825 }
826 case SG_SET_TIMEOUT:
827 result = get_user(val, ip);
828 if (result)
829 return result;
830 if (val < 0)
831 return -EIO;
832 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
833 val = MULDIV (INT_MAX, USER_HZ, HZ);
834 sfp->timeout_user = val;
835 sfp->timeout = MULDIV (val, HZ, USER_HZ);
836
837 return 0;
838 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
839 /* strange ..., for backward compatibility */
840 return sfp->timeout_user;
841 case SG_SET_FORCE_LOW_DMA:
842 result = get_user(val, ip);
843 if (result)
844 return result;
845 if (val) {
846 sfp->low_dma = 1;
847 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
848 val = (int) sfp->reserve.bufflen;
849 sg_remove_scat(&sfp->reserve);
850 sg_build_reserve(sfp, val);
851 }
852 } else {
853 if (sdp->detached)
854 return -ENODEV;
855 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
856 }
857 return 0;
858 case SG_GET_LOW_DMA:
859 return put_user((int) sfp->low_dma, ip);
860 case SG_GET_SCSI_ID:
861 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
862 return -EFAULT;
863 else {
864 sg_scsi_id_t __user *sg_idp = p;
865
866 if (sdp->detached)
867 return -ENODEV;
868 __put_user((int) sdp->device->host->host_no,
869 &sg_idp->host_no);
870 __put_user((int) sdp->device->channel,
871 &sg_idp->channel);
872 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
873 __put_user((int) sdp->device->lun, &sg_idp->lun);
874 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
875 __put_user((short) sdp->device->host->cmd_per_lun,
876 &sg_idp->h_cmd_per_lun);
877 __put_user((short) sdp->device->queue_depth,
878 &sg_idp->d_queue_depth);
879 __put_user(0, &sg_idp->unused[0]);
880 __put_user(0, &sg_idp->unused[1]);
881 return 0;
882 }
883 case SG_SET_FORCE_PACK_ID:
884 result = get_user(val, ip);
885 if (result)
886 return result;
887 sfp->force_packid = val ? 1 : 0;
888 return 0;
889 case SG_GET_PACK_ID:
890 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
891 return -EFAULT;
892 read_lock_irqsave(&sfp->rq_list_lock, iflags);
893 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
894 if ((1 == srp->done) && (!srp->sg_io_owned)) {
895 read_unlock_irqrestore(&sfp->rq_list_lock,
896 iflags);
897 __put_user(srp->header.pack_id, ip);
898 return 0;
899 }
900 }
901 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
902 __put_user(-1, ip);
903 return 0;
904 case SG_GET_NUM_WAITING:
905 read_lock_irqsave(&sfp->rq_list_lock, iflags);
906 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
907 if ((1 == srp->done) && (!srp->sg_io_owned))
908 ++val;
909 }
910 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
911 return put_user(val, ip);
912 case SG_GET_SG_TABLESIZE:
913 return put_user(sdp->sg_tablesize, ip);
914 case SG_SET_RESERVED_SIZE:
915 result = get_user(val, ip);
916 if (result)
917 return result;
918 if (val < 0)
919 return -EINVAL;
920 if (val != sfp->reserve.bufflen) {
921 if (sg_res_in_use(sfp) || sfp->mmap_called)
922 return -EBUSY;
923 sg_remove_scat(&sfp->reserve);
924 sg_build_reserve(sfp, val);
925 }
926 return 0;
927 case SG_GET_RESERVED_SIZE:
928 val = (int) sfp->reserve.bufflen;
929 return put_user(val, ip);
930 case SG_SET_COMMAND_Q:
931 result = get_user(val, ip);
932 if (result)
933 return result;
934 sfp->cmd_q = val ? 1 : 0;
935 return 0;
936 case SG_GET_COMMAND_Q:
937 return put_user((int) sfp->cmd_q, ip);
938 case SG_SET_KEEP_ORPHAN:
939 result = get_user(val, ip);
940 if (result)
941 return result;
942 sfp->keep_orphan = val;
943 return 0;
944 case SG_GET_KEEP_ORPHAN:
945 return put_user((int) sfp->keep_orphan, ip);
946 case SG_NEXT_CMD_LEN:
947 result = get_user(val, ip);
948 if (result)
949 return result;
950 sfp->next_cmd_len = (val > 0) ? val : 0;
951 return 0;
952 case SG_GET_VERSION_NUM:
953 return put_user(sg_version_num, ip);
954 case SG_GET_ACCESS_COUNT:
955 /* faked - we don't have a real access count anymore */
956 val = (sdp->device ? 1 : 0);
957 return put_user(val, ip);
958 case SG_GET_REQUEST_TABLE:
959 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
960 return -EFAULT;
961 else {
cb59e842005-04-02 13:51:23 -0600962 sg_req_info_t *rinfo;
963 unsigned int ms;
964
965 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
966 GFP_KERNEL);
967 if (!rinfo)
968 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 read_lock_irqsave(&sfp->rq_list_lock, iflags);
970 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
971 ++val, srp = srp ? srp->nextrp : srp) {
972 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
973 if (srp) {
974 rinfo[val].req_state = srp->done + 1;
975 rinfo[val].problem =
976 srp->header.masked_status &
977 srp->header.host_status &
978 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600979 if (srp->done)
980 rinfo[val].duration =
981 srp->header.duration;
982 else {
983 ms = jiffies_to_msecs(jiffies);
984 rinfo[val].duration =
985 (ms > srp->header.duration) ?
986 (ms - srp->header.duration) : 0;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600989 rinfo[val].sg_io_owned =
990 srp->sg_io_owned;
991 rinfo[val].pack_id =
992 srp->header.pack_id;
993 rinfo[val].usr_ptr =
994 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 }
997 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -0600998 result = __copy_to_user(p, rinfo,
999 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1000 result = result ? -EFAULT : 0;
1001 kfree(rinfo);
1002 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004 case SG_EMULATED_HOST:
1005 if (sdp->detached)
1006 return -ENODEV;
1007 return put_user(sdp->device->host->hostt->emulated, ip);
1008 case SG_SCSI_RESET:
1009 if (sdp->detached)
1010 return -ENODEV;
1011 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001012 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -EBUSY;
1014 } else if (!scsi_block_when_processing_errors(sdp->device))
1015 return -EBUSY;
1016 result = get_user(val, ip);
1017 if (result)
1018 return result;
1019 if (SG_SCSI_RESET_NOTHING == val)
1020 return 0;
1021 switch (val) {
1022 case SG_SCSI_RESET_DEVICE:
1023 val = SCSI_TRY_RESET_DEVICE;
1024 break;
1025 case SG_SCSI_RESET_BUS:
1026 val = SCSI_TRY_RESET_BUS;
1027 break;
1028 case SG_SCSI_RESET_HOST:
1029 val = SCSI_TRY_RESET_HOST;
1030 break;
1031 default:
1032 return -EINVAL;
1033 }
1034 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1035 return -EACCES;
1036 return (scsi_reset_provider(sdp->device, val) ==
1037 SUCCESS) ? 0 : -EIO;
1038 case SCSI_IOCTL_SEND_COMMAND:
1039 if (sdp->detached)
1040 return -ENODEV;
1041 if (read_only) {
1042 unsigned char opcode = WRITE_6;
1043 Scsi_Ioctl_Command __user *siocp = p;
1044
1045 if (copy_from_user(&opcode, siocp->data, 1))
1046 return -EFAULT;
1047 if (!sg_allow_access(opcode, sdp->device->type))
1048 return -EPERM;
1049 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001050 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 case SG_SET_DEBUG:
1052 result = get_user(val, ip);
1053 if (result)
1054 return result;
1055 sdp->sgdebug = (char) val;
1056 return 0;
1057 case SCSI_IOCTL_GET_IDLUN:
1058 case SCSI_IOCTL_GET_BUS_NUMBER:
1059 case SCSI_IOCTL_PROBE_HOST:
1060 case SG_GET_TRANSFORM:
1061 if (sdp->detached)
1062 return -ENODEV;
1063 return scsi_ioctl(sdp->device, cmd_in, p);
1064 default:
1065 if (read_only)
1066 return -EPERM; /* don't know so take safe approach */
1067 return scsi_ioctl(sdp->device, cmd_in, p);
1068 }
1069}
1070
1071#ifdef CONFIG_COMPAT
1072static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1073{
1074 Sg_device *sdp;
1075 Sg_fd *sfp;
1076 struct scsi_device *sdev;
1077
1078 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1079 return -ENXIO;
1080
1081 sdev = sdp->device;
1082 if (sdev->host->hostt->compat_ioctl) {
1083 int ret;
1084
1085 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1086
1087 return ret;
1088 }
1089
1090 return -ENOIOCTLCMD;
1091}
1092#endif
1093
1094static unsigned int
1095sg_poll(struct file *filp, poll_table * wait)
1096{
1097 unsigned int res = 0;
1098 Sg_device *sdp;
1099 Sg_fd *sfp;
1100 Sg_request *srp;
1101 int count = 0;
1102 unsigned long iflags;
1103
1104 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1105 || sfp->closed)
1106 return POLLERR;
1107 poll_wait(filp, &sfp->read_wait, wait);
1108 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1109 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1110 /* if any read waiting, flag it */
1111 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1112 res = POLLIN | POLLRDNORM;
1113 ++count;
1114 }
1115 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1116
1117 if (sdp->detached)
1118 res |= POLLHUP;
1119 else if (!sfp->cmd_q) {
1120 if (0 == count)
1121 res |= POLLOUT | POLLWRNORM;
1122 } else if (count < SG_MAX_QUEUE)
1123 res |= POLLOUT | POLLWRNORM;
1124 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1125 sdp->disk->disk_name, (int) res));
1126 return res;
1127}
1128
1129static int
1130sg_fasync(int fd, struct file *filp, int mode)
1131{
1132 int retval;
1133 Sg_device *sdp;
1134 Sg_fd *sfp;
1135
1136 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1137 return -ENXIO;
1138 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1139 sdp->disk->disk_name, mode));
1140
1141 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1142 return (retval < 0) ? retval : 0;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static struct page *
1146sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1147{
1148 Sg_fd *sfp;
1149 struct page *page = NOPAGE_SIGBUS;
Mike Christied6b10342005-11-08 04:06:41 -06001150 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001152 struct scatterlist *sg;
1153 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1156 return page;
1157 rsv_schp = &sfp->reserve;
1158 offset = addr - vma->vm_start;
1159 if (offset >= rsv_schp->bufflen)
1160 return page;
1161 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1162 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001163 sg = rsv_schp->buffer;
1164 sa = vma->vm_start;
1165 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1166 ++k, ++sg) {
1167 len = vma->vm_end - sa;
1168 len = (len < sg->length) ? len : sg->length;
1169 if (offset < len) {
Douglas Gilbertb2155d02006-08-19 00:11:34 -04001170 page = virt_to_page(page_address(sg->page) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001171 get_page(page); /* increment page count */
1172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Mike Christied6b10342005-11-08 04:06:41 -06001174 sa += len;
1175 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Mike Christied6b10342005-11-08 04:06:41 -06001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (type)
1179 *type = VM_FAULT_MINOR;
1180 return page;
1181}
1182
1183static struct vm_operations_struct sg_mmap_vm_ops = {
1184 .nopage = sg_vma_nopage,
1185};
1186
1187static int
1188sg_mmap(struct file *filp, struct vm_area_struct *vma)
1189{
1190 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001191 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001193 int k;
1194 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1197 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001198 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1200 (void *) vma->vm_start, (int) req_sz));
1201 if (vma->vm_pgoff)
1202 return -EINVAL; /* want no offset */
1203 rsv_schp = &sfp->reserve;
1204 if (req_sz > rsv_schp->bufflen)
1205 return -ENOMEM; /* cannot map more than reserved buffer */
1206
Mike Christied6b10342005-11-08 04:06:41 -06001207 sa = vma->vm_start;
1208 sg = rsv_schp->buffer;
1209 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1210 ++k, ++sg) {
1211 len = vma->vm_end - sa;
1212 len = (len < sg->length) ? len : sg->length;
1213 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Mike Christied6b10342005-11-08 04:06:41 -06001215
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001216 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001217 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 vma->vm_private_data = sfp;
1219 vma->vm_ops = &sg_mmap_vm_ops;
1220 return 0;
1221}
1222
1223/* This function is a "bottom half" handler that is called by the
1224 * mid level when a command is completed (or has failed). */
1225static void
Mike Christied6b10342005-11-08 04:06:41 -06001226sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Mike Christied6b10342005-11-08 04:06:41 -06001228 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 Sg_device *sdp = NULL;
1230 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001232 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (NULL == srp) {
1235 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return;
1237 }
1238 sfp = srp->parentfp;
1239 if (sfp)
1240 sdp = sfp->parentdp;
1241 if ((NULL == sdp) || sdp->detached) {
1242 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return;
1244 }
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001248 sdp->disk->disk_name, srp->header.pack_id, result));
1249 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001250 ms = jiffies_to_msecs(jiffies);
1251 srp->header.duration = (ms > srp->header.duration) ?
1252 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001253 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct scsi_sense_hdr sshdr;
1255
Mike Christied6b10342005-11-08 04:06:41 -06001256 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1257 srp->header.status = 0xff & result;
1258 srp->header.masked_status = status_byte(result);
1259 srp->header.msg_status = msg_byte(result);
1260 srp->header.host_status = host_byte(result);
1261 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if ((sdp->sgdebug > 0) &&
1263 ((CHECK_CONDITION == srp->header.masked_status) ||
1264 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001265 __scsi_print_sense("sg_cmd_done", sense,
1266 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001269 if (driver_byte(result) != 0
1270 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 && !scsi_sense_is_deferred(&sshdr)
1272 && sshdr.sense_key == UNIT_ATTENTION
1273 && sdp->device->removable) {
1274 /* Detected possible disc change. Set the bit - this */
1275 /* may be used if there are filesystems using this device */
1276 sdp->device->changed = 1;
1277 }
1278 }
1279 /* Rely on write phase to clean out srp status values, so no "else" */
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (sfp->closed) { /* whoops this fd already released, cleanup */
1282 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1283 sg_finish_rem_req(srp);
1284 srp = NULL;
1285 if (NULL == sfp->headrp) {
1286 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1287 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1288 scsi_device_put(sdp->device);
1289 }
1290 sfp = NULL;
1291 }
1292 } else if (srp && srp->orphan) {
1293 if (sfp->keep_orphan)
1294 srp->sg_io_owned = 0;
1295 else {
1296 sg_finish_rem_req(srp);
1297 srp = NULL;
1298 }
1299 }
1300 if (sfp && srp) {
1301 /* Now wake up any sg_read() that is waiting for this packet. */
1302 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1303 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1304 srp->done = 1;
1305 wake_up_interruptible(&sfp->read_wait);
1306 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1307 }
1308}
1309
1310static struct file_operations sg_fops = {
1311 .owner = THIS_MODULE,
1312 .read = sg_read,
1313 .write = sg_write,
1314 .poll = sg_poll,
1315 .ioctl = sg_ioctl,
1316#ifdef CONFIG_COMPAT
1317 .compat_ioctl = sg_compat_ioctl,
1318#endif
1319 .open = sg_open,
1320 .mmap = sg_mmap,
1321 .release = sg_release,
1322 .fasync = sg_fasync,
1323};
1324
gregkh@suse.ded2538782005-03-23 09:55:22 -08001325static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327static int sg_sysfs_valid = 0;
1328
1329static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1330{
Mike Christied6b10342005-11-08 04:06:41 -06001331 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 Sg_device *sdp;
1333 unsigned long iflags;
1334 void *old_sg_dev_arr = NULL;
1335 int k, error;
1336
Jes Sorensen24669f752006-01-16 10:31:18 -05001337 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (!sdp) {
1339 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1340 return -ENOMEM;
1341 }
1342
1343 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1344 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1345 Sg_device **tmp_da;
1346 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1347 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1348
Jes Sorensen24669f752006-01-16 10:31:18 -05001349 tmp_da = kzalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (unlikely(!tmp_da))
1351 goto expand_failed;
1352
1353 write_lock_irqsave(&sg_dev_arr_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1355 old_sg_dev_arr = sg_dev_arr;
1356 sg_dev_arr = tmp_da;
1357 sg_dev_max = tmp_dev_max;
1358 }
1359
1360 for (k = 0; k < sg_dev_max; k++)
1361 if (!sg_dev_arr[k])
1362 break;
1363 if (unlikely(k >= SG_MAX_DEVS))
1364 goto overflow;
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1367 sprintf(disk->disk_name, "sg%d", k);
1368 disk->first_minor = k;
1369 sdp->disk = disk;
1370 sdp->device = scsidp;
1371 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001372 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 sg_nr_dev++;
1375 sg_dev_arr[k] = sdp;
1376 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1377 error = k;
1378
1379 out:
1380 if (error < 0)
1381 kfree(sdp);
1382 kfree(old_sg_dev_arr);
1383 return error;
1384
1385 expand_failed:
1386 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1387 error = -ENOMEM;
1388 goto out;
1389
1390 overflow:
1391 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
James Bottomley9ccfc752005-10-02 11:45:08 -05001392 sdev_printk(KERN_WARNING, scsidp,
1393 "Unable to attach sg device type=%d, minor "
1394 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 error = -ENODEV;
1396 goto out;
1397}
1398
1399static int
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001400sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1403 struct gendisk *disk;
1404 Sg_device *sdp = NULL;
1405 struct cdev * cdev = NULL;
1406 int error, k;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001407 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 disk = alloc_disk(1);
1410 if (!disk) {
1411 printk(KERN_WARNING "alloc_disk failed\n");
1412 return -ENOMEM;
1413 }
1414 disk->major = SCSI_GENERIC_MAJOR;
1415
1416 error = -ENOMEM;
1417 cdev = cdev_alloc();
1418 if (!cdev) {
1419 printk(KERN_WARNING "cdev_alloc failed\n");
1420 goto out;
1421 }
1422 cdev->owner = THIS_MODULE;
1423 cdev->ops = &sg_fops;
1424
1425 error = sg_alloc(disk, scsidp);
1426 if (error < 0) {
1427 printk(KERN_WARNING "sg_alloc failed\n");
1428 goto out;
1429 }
1430 k = error;
1431 sdp = sg_dev_arr[k];
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001434 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001435 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 sdp->cdev = cdev;
1438 if (sg_sysfs_valid) {
1439 struct class_device * sg_class_member;
1440
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07001441 sg_class_member = class_device_create(sg_sysfs_class, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 MKDEV(SCSI_GENERIC_MAJOR, k),
1443 cl_dev->dev, "%s",
1444 disk->disk_name);
1445 if (IS_ERR(sg_class_member))
1446 printk(KERN_WARNING "sg_add: "
gregkh@suse.ded2538782005-03-23 09:55:22 -08001447 "class_device_create failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 class_set_devdata(sg_class_member, sdp);
1449 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1450 &sg_class_member->kobj, "generic");
1451 if (error)
1452 printk(KERN_ERR "sg_add: unable to make symlink "
1453 "'generic' back to sg%d\n", k);
1454 } else
1455 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1456
James Bottomley9ccfc752005-10-02 11:45:08 -05001457 sdev_printk(KERN_NOTICE, scsidp,
1458 "Attached scsi generic sg%d type %d\n", k,scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 return 0;
1461
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001462cdev_add_err:
1463 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1464 kfree(sg_dev_arr[k]);
1465 sg_dev_arr[k] = NULL;
1466 sg_nr_dev--;
1467 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469out:
1470 put_disk(disk);
1471 if (cdev)
1472 cdev_del(cdev);
1473 return error;
1474}
1475
1476static void
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001477sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1480 Sg_device *sdp = NULL;
1481 unsigned long iflags;
1482 Sg_fd *sfp;
1483 Sg_fd *tsfp;
1484 Sg_request *srp;
1485 Sg_request *tsrp;
1486 int k, delay;
1487
1488 if (NULL == sg_dev_arr)
1489 return;
1490 delay = 0;
1491 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1492 for (k = 0; k < sg_dev_max; k++) {
1493 sdp = sg_dev_arr[k];
1494 if ((NULL == sdp) || (sdp->device != scsidp))
1495 continue; /* dirty but lowers nesting */
1496 if (sdp->headfp) {
1497 sdp->detached = 1;
1498 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1499 tsfp = sfp->nextfp;
1500 for (srp = sfp->headrp; srp; srp = tsrp) {
1501 tsrp = srp->nextrp;
1502 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1503 sg_finish_rem_req(srp);
1504 }
1505 if (sfp->closed) {
1506 scsi_device_put(sdp->device);
1507 __sg_remove_sfp(sdp, sfp);
1508 } else {
1509 delay = 1;
1510 wake_up_interruptible(&sfp->read_wait);
1511 kill_fasync(&sfp->async_qp, SIGPOLL,
1512 POLL_HUP);
1513 }
1514 }
1515 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1516 if (NULL == sdp->headfp) {
1517 sg_dev_arr[k] = NULL;
1518 }
1519 } else { /* nothing active, simple case */
1520 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1521 sg_dev_arr[k] = NULL;
1522 }
1523 sg_nr_dev--;
1524 break;
1525 }
1526 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1527
1528 if (sdp) {
1529 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
gregkh@suse.ded2538782005-03-23 09:55:22 -08001530 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, k));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 cdev_del(sdp->cdev);
1532 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 put_disk(sdp->disk);
1534 sdp->disk = NULL;
1535 if (NULL == sdp->headfp)
1536 kfree((char *) sdp);
1537 }
1538
1539 if (delay)
1540 msleep(10); /* dirty detach so delay device destruction */
1541}
1542
Douglas Gilbert6460e752006-09-20 18:20:49 -04001543module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1544module_param_named(def_reserved_size, def_reserved_size, int,
1545 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1547
1548MODULE_AUTHOR("Douglas Gilbert");
1549MODULE_DESCRIPTION("SCSI generic (sg) driver");
1550MODULE_LICENSE("GPL");
1551MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001552MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Douglas Gilbert6460e752006-09-20 18:20:49 -04001554MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1555 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1557MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1558
1559static int __init
1560init_sg(void)
1561{
1562 int rc;
1563
Douglas Gilbert6460e752006-09-20 18:20:49 -04001564 if (scatter_elem_sz < PAGE_SIZE) {
1565 scatter_elem_sz = PAGE_SIZE;
1566 scatter_elem_sz_prev = scatter_elem_sz;
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (def_reserved_size >= 0)
1569 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001570 else
1571 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1574 SG_MAX_DEVS, "sg");
1575 if (rc)
1576 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001577 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if ( IS_ERR(sg_sysfs_class) ) {
1579 rc = PTR_ERR(sg_sysfs_class);
1580 goto err_out;
1581 }
1582 sg_sysfs_valid = 1;
1583 rc = scsi_register_interface(&sg_interface);
1584 if (0 == rc) {
1585#ifdef CONFIG_SCSI_PROC_FS
1586 sg_proc_init();
1587#endif /* CONFIG_SCSI_PROC_FS */
1588 return 0;
1589 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001590 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591err_out:
1592 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1593 return rc;
1594}
1595
1596static void __exit
1597exit_sg(void)
1598{
1599#ifdef CONFIG_SCSI_PROC_FS
1600 sg_proc_cleanup();
1601#endif /* CONFIG_SCSI_PROC_FS */
1602 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001603 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 sg_sysfs_valid = 0;
1605 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1606 SG_MAX_DEVS);
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001607 kfree((char *)sg_dev_arr);
1608 sg_dev_arr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 sg_dev_max = 0;
1610}
1611
1612static int
1613sg_start_req(Sg_request * srp)
1614{
1615 int res;
1616 Sg_fd *sfp = srp->parentfp;
1617 sg_io_hdr_t *hp = &srp->header;
1618 int dxfer_len = (int) hp->dxfer_len;
1619 int dxfer_dir = hp->dxfer_direction;
1620 Sg_scatter_hold *req_schp = &srp->data;
1621 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1622
1623 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1624 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1625 return 0;
1626 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1627 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1628 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1629 res = sg_build_direct(srp, sfp, dxfer_len);
1630 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1631 return res;
1632 }
1633 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1634 sg_link_reserve(sfp, srp, dxfer_len);
1635 else {
1636 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1637 if (res) {
1638 sg_remove_scat(req_schp);
1639 return res;
1640 }
1641 }
1642 return 0;
1643}
1644
1645static void
1646sg_finish_rem_req(Sg_request * srp)
1647{
1648 Sg_fd *sfp = srp->parentfp;
1649 Sg_scatter_hold *req_schp = &srp->data;
1650
1651 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1652 if (srp->res_used)
1653 sg_unlink_reserve(sfp, srp);
1654 else
1655 sg_remove_scat(req_schp);
1656 sg_remove_request(sfp, srp);
1657}
1658
1659static int
1660sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1661{
Mike Christied6b10342005-11-08 04:06:41 -06001662 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001663 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Mike Christied6b10342005-11-08 04:06:41 -06001665 /*
1666 * TODO: test without low_dma, we should not need it since
1667 * the block layer will bounce the buffer for us
1668 *
1669 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1670 */
1671 if (sfp->low_dma)
1672 gfp_flags |= GFP_DMA;
1673 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (!schp->buffer)
1675 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001677 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680#ifdef SG_ALLOW_DIO_CODE
1681/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001682 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1685 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 (i.e., either completely successful or fails)
1687*/
1688static int
1689st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001690 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001692 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1693 unsigned long start = uaddr >> PAGE_SHIFT;
1694 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct page **pages;
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /* User attempted Overflow! */
1699 if ((uaddr + count) < uaddr)
1700 return -EINVAL;
1701
1702 /* Too big */
1703 if (nr_pages > max_pages)
1704 return -ENOMEM;
1705
1706 /* Hmm? */
1707 if (count == 0)
1708 return 0;
1709
1710 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1711 return -ENOMEM;
1712
1713 /* Try to fault in all of the necessary pages */
1714 down_read(&current->mm->mmap_sem);
1715 /* rw==READ means read from drive, write into memory area */
1716 res = get_user_pages(
1717 current,
1718 current->mm,
1719 uaddr,
1720 nr_pages,
1721 rw == READ,
1722 0, /* don't force */
1723 pages,
1724 NULL);
1725 up_read(&current->mm->mmap_sem);
1726
1727 /* Errors and no page mapped should return here */
1728 if (res < nr_pages)
1729 goto out_unmap;
1730
1731 for (i=0; i < nr_pages; i++) {
1732 /* FIXME: flush superflous for rw==READ,
1733 * probably wrong function for rw==WRITE
1734 */
1735 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* ?? Is locking needed? I don't think so */
1737 /* if (TestSetPageLocked(pages[i]))
1738 goto out_unlock; */
1739 }
1740
Mike Christied6b10342005-11-08 04:06:41 -06001741 sgl[0].page = pages[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 sgl[0].offset = uaddr & ~PAGE_MASK;
1743 if (nr_pages > 1) {
1744 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1745 count -= sgl[0].length;
1746 for (i=1; i < nr_pages ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 sgl[i].page = pages[i];
1748 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1749 count -= PAGE_SIZE;
1750 }
1751 }
1752 else {
1753 sgl[0].length = count;
1754 }
1755
1756 kfree(pages);
1757 return nr_pages;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001760 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 for (j=0; j < res; j++)
1762 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001763 res = 0;
1764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 kfree(pages);
1766 return res;
1767}
1768
1769
1770/* And unmap them... */
1771static int
1772st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1773 int dirtied)
1774{
1775 int i;
1776
1777 for (i=0; i < nr_pages; i++) {
Nick Pigginb5810032005-10-29 18:16:12 -07001778 struct page *page = sgl[i].page;
1779
Nick Pigginb5810032005-10-29 18:16:12 -07001780 if (dirtied)
1781 SetPageDirty(page);
1782 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* FIXME: cache flush missing for rw==READ
1784 * FIXME: call the correct reference counting function
1785 */
Nick Pigginb5810032005-10-29 18:16:12 -07001786 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
1789 return 0;
1790}
1791
1792/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1793#endif
1794
1795
1796/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1797static int
1798sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1799{
1800#ifdef SG_ALLOW_DIO_CODE
1801 sg_io_hdr_t *hp = &srp->header;
1802 Sg_scatter_hold *schp = &srp->data;
1803 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 int mx_sc_elems, res;
1805 struct scsi_device *sdev = sfp->parentdp->device;
1806
1807 if (((unsigned long)hp->dxferp &
1808 queue_dma_alignment(sdev->request_queue)) != 0)
1809 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1812 if (mx_sc_elems <= 0) {
1813 return 1;
1814 }
Mike Christied6b10342005-11-08 04:06:41 -06001815 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1816 (unsigned long)hp->dxferp, dxfer_len,
1817 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001818 if (res <= 0) {
1819 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 schp->k_use_sg = res;
1823 schp->dio_in_use = 1;
1824 hp->info |= SG_INFO_DIRECT_IO;
1825 return 0;
1826#else
1827 return 1;
1828#endif
1829}
1830
1831static int
1832sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1833{
Mike Christied6b10342005-11-08 04:06:41 -06001834 struct scatterlist *sg;
1835 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1836 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001838 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if ((blk_size < 0) || (!sfp))
1841 return -EFAULT;
1842 if (0 == blk_size)
1843 ++blk_size; /* don't know why */
1844/* round request up to next highest SG_SECTOR_SZ byte boundary */
1845 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1846 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1847 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001848
1849 /* N.B. ret_sz carried into this block ... */
1850 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1851 if (mx_sc_elems < 0)
1852 return mx_sc_elems; /* most likely -ENOMEM */
1853
Douglas Gilbert6460e752006-09-20 18:20:49 -04001854 num = scatter_elem_sz;
1855 if (unlikely(num != scatter_elem_sz_prev)) {
1856 if (num < PAGE_SIZE) {
1857 scatter_elem_sz = PAGE_SIZE;
1858 scatter_elem_sz_prev = PAGE_SIZE;
1859 } else
1860 scatter_elem_sz_prev = num;
1861 }
Mike Christied6b10342005-11-08 04:06:41 -06001862 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1863 (rem_sz > 0) && (k < mx_sc_elems);
1864 ++k, rem_sz -= ret_sz, ++sg) {
1865
Douglas Gilbert6460e752006-09-20 18:20:49 -04001866 num = (rem_sz > scatter_elem_sz_prev) ?
1867 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001868 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (!p)
1870 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Douglas Gilbert6460e752006-09-20 18:20:49 -04001872 if (num == scatter_elem_sz_prev) {
1873 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1874 scatter_elem_sz = ret_sz;
1875 scatter_elem_sz_prev = ret_sz;
1876 }
1877 }
Mike Christied6b10342005-11-08 04:06:41 -06001878 sg->page = p;
1879 sg->length = ret_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Mike Christied6b10342005-11-08 04:06:41 -06001881 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1882 k, p, ret_sz));
1883 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Mike Christied6b10342005-11-08 04:06:41 -06001885 schp->k_use_sg = k;
1886 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1887
1888 schp->bufflen = blk_size;
1889 if (rem_sz > 0) /* must have failed */
1890 return -ENOMEM;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893}
1894
1895static int
1896sg_write_xfer(Sg_request * srp)
1897{
1898 sg_io_hdr_t *hp = &srp->header;
1899 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001900 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int num_xfer = 0;
1902 int j, k, onum, usglen, ksglen, res;
1903 int iovec_count = (int) hp->iovec_count;
1904 int dxfer_dir = hp->dxfer_direction;
1905 unsigned char *p;
1906 unsigned char __user *up;
1907 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1908
1909 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1910 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1911 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1912 if (schp->bufflen < num_xfer)
1913 num_xfer = schp->bufflen;
1914 }
1915 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1916 (new_interface
1917 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1918 return 0;
1919
1920 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1921 num_xfer, iovec_count, schp->k_use_sg));
1922 if (iovec_count) {
1923 onum = iovec_count;
1924 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1925 return -EFAULT;
1926 } else
1927 onum = 1;
1928
Mike Christied6b10342005-11-08 04:06:41 -06001929 ksglen = sg->length;
1930 p = page_address(sg->page);
1931 for (j = 0, k = 0; j < onum; ++j) {
1932 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1933 if (res)
1934 return res;
1935
1936 for (; p; ++sg, ksglen = sg->length,
1937 p = page_address(sg->page)) {
1938 if (usglen <= 0)
1939 break;
1940 if (ksglen > usglen) {
1941 if (usglen >= num_xfer) {
1942 if (__copy_from_user(p, up, num_xfer))
1943 return -EFAULT;
1944 return 0;
1945 }
1946 if (__copy_from_user(p, up, usglen))
1947 return -EFAULT;
1948 p += usglen;
1949 ksglen -= usglen;
1950 break;
1951 } else {
1952 if (ksglen >= num_xfer) {
1953 if (__copy_from_user(p, up, num_xfer))
1954 return -EFAULT;
1955 return 0;
1956 }
1957 if (__copy_from_user(p, up, ksglen))
1958 return -EFAULT;
1959 up += ksglen;
1960 usglen -= ksglen;
1961 }
1962 ++k;
1963 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Mike Christied6b10342005-11-08 04:06:41 -06001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969}
1970
1971static int
1972sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1973 int wr_xf, int *countp, unsigned char __user **up)
1974{
1975 int num_xfer = (int) hp->dxfer_len;
1976 unsigned char __user *p = hp->dxferp;
1977 int count;
1978
1979 if (0 == sg_num) {
1980 if (wr_xf && ('\0' == hp->interface_id))
1981 count = (int) hp->flags; /* holds "old" input_size */
1982 else
1983 count = num_xfer;
1984 } else {
1985 sg_iovec_t iovec;
1986 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1987 return -EFAULT;
1988 p = iovec.iov_base;
1989 count = (int) iovec.iov_len;
1990 }
1991 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1992 return -EFAULT;
1993 if (up)
1994 *up = p;
1995 if (countp)
1996 *countp = count;
1997 return 0;
1998}
1999
2000static void
2001sg_remove_scat(Sg_scatter_hold * schp)
2002{
2003 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2004 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002005 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 if (schp->dio_in_use) {
2008#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06002009 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010#endif
2011 } else {
2012 int k;
2013
Mike Christied6b10342005-11-08 04:06:41 -06002014 for (k = 0; (k < schp->k_use_sg) && sg->page;
2015 ++k, ++sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 SCSI_LOG_TIMEOUT(5, printk(
2017 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
Mike Christied6b10342005-11-08 04:06:41 -06002018 k, sg->page, sg->length));
2019 sg_page_free(sg->page, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 }
Mike Christied6b10342005-11-08 04:06:41 -06002022 kfree(schp->buffer);
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 memset(schp, 0, sizeof (*schp));
2025}
2026
2027static int
2028sg_read_xfer(Sg_request * srp)
2029{
2030 sg_io_hdr_t *hp = &srp->header;
2031 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002032 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int num_xfer = 0;
2034 int j, k, onum, usglen, ksglen, res;
2035 int iovec_count = (int) hp->iovec_count;
2036 int dxfer_dir = hp->dxfer_direction;
2037 unsigned char *p;
2038 unsigned char __user *up;
2039 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2040
2041 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2042 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2043 num_xfer = hp->dxfer_len;
2044 if (schp->bufflen < num_xfer)
2045 num_xfer = schp->bufflen;
2046 }
2047 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2048 (new_interface
2049 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2050 return 0;
2051
2052 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2053 num_xfer, iovec_count, schp->k_use_sg));
2054 if (iovec_count) {
2055 onum = iovec_count;
2056 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2057 return -EFAULT;
2058 } else
2059 onum = 1;
2060
Mike Christied6b10342005-11-08 04:06:41 -06002061 p = page_address(sg->page);
2062 ksglen = sg->length;
2063 for (j = 0, k = 0; j < onum; ++j) {
2064 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2065 if (res)
2066 return res;
2067
2068 for (; p; ++sg, ksglen = sg->length,
2069 p = page_address(sg->page)) {
2070 if (usglen <= 0)
2071 break;
2072 if (ksglen > usglen) {
2073 if (usglen >= num_xfer) {
2074 if (__copy_to_user(up, p, num_xfer))
2075 return -EFAULT;
2076 return 0;
2077 }
2078 if (__copy_to_user(up, p, usglen))
2079 return -EFAULT;
2080 p += usglen;
2081 ksglen -= usglen;
2082 break;
2083 } else {
2084 if (ksglen >= num_xfer) {
2085 if (__copy_to_user(up, p, num_xfer))
2086 return -EFAULT;
2087 return 0;
2088 }
2089 if (__copy_to_user(up, p, ksglen))
2090 return -EFAULT;
2091 up += ksglen;
2092 usglen -= ksglen;
2093 }
2094 ++k;
2095 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return 0;
2097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Mike Christied6b10342005-11-08 04:06:41 -06002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return 0;
2101}
2102
2103static int
2104sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2105{
2106 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002107 struct scatterlist *sg = schp->buffer;
2108 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2111 num_read_xfer));
2112 if ((!outp) || (num_read_xfer <= 0))
2113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Mike Christied6b10342005-11-08 04:06:41 -06002115 for (k = 0; (k < schp->k_use_sg) && sg->page; ++k, ++sg) {
2116 num = sg->length;
2117 if (num > num_read_xfer) {
2118 if (__copy_to_user(outp, page_address(sg->page),
2119 num_read_xfer))
2120 return -EFAULT;
2121 break;
2122 } else {
2123 if (__copy_to_user(outp, page_address(sg->page),
2124 num))
2125 return -EFAULT;
2126 num_read_xfer -= num;
2127 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 break;
Mike Christied6b10342005-11-08 04:06:41 -06002129 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
Mike Christied6b10342005-11-08 04:06:41 -06002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return 0;
2134}
2135
2136static void
2137sg_build_reserve(Sg_fd * sfp, int req_size)
2138{
2139 Sg_scatter_hold *schp = &sfp->reserve;
2140
2141 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2142 do {
2143 if (req_size < PAGE_SIZE)
2144 req_size = PAGE_SIZE;
2145 if (0 == sg_build_indirect(schp, sfp, req_size))
2146 return;
2147 else
2148 sg_remove_scat(schp);
2149 req_size >>= 1; /* divide by 2 */
2150 } while (req_size > (PAGE_SIZE / 2));
2151}
2152
2153static void
2154sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2155{
2156 Sg_scatter_hold *req_schp = &srp->data;
2157 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002158 struct scatterlist *sg = rsv_schp->buffer;
2159 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 srp->res_used = 1;
2162 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002163 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Mike Christied6b10342005-11-08 04:06:41 -06002165 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sg) {
2166 num = sg->length;
2167 if (rem <= num) {
2168 sfp->save_scat_len = num;
2169 sg->length = rem;
2170 req_schp->k_use_sg = k + 1;
2171 req_schp->sglist_len = rsv_schp->sglist_len;
2172 req_schp->buffer = rsv_schp->buffer;
2173
2174 req_schp->bufflen = size;
2175 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2176 break;
2177 } else
2178 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Mike Christied6b10342005-11-08 04:06:41 -06002180
2181 if (k >= rsv_schp->k_use_sg)
2182 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185static void
2186sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2187{
2188 Sg_scatter_hold *req_schp = &srp->data;
2189 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2190
2191 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2192 (int) req_schp->k_use_sg));
2193 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002194 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002197 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 (unsigned) sfp->save_scat_len;
2199 else
2200 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2201 }
2202 req_schp->k_use_sg = 0;
2203 req_schp->bufflen = 0;
2204 req_schp->buffer = NULL;
2205 req_schp->sglist_len = 0;
2206 sfp->save_scat_len = 0;
2207 srp->res_used = 0;
2208}
2209
2210static Sg_request *
2211sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2212{
2213 Sg_request *resp;
2214 unsigned long iflags;
2215
2216 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2217 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2218 /* look for requests that are ready + not SG_IO owned */
2219 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2220 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2221 resp->done = 2; /* guard against other readers */
2222 break;
2223 }
2224 }
2225 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2226 return resp;
2227}
2228
2229#ifdef CONFIG_SCSI_PROC_FS
2230static Sg_request *
2231sg_get_nth_request(Sg_fd * sfp, int nth)
2232{
2233 Sg_request *resp;
2234 unsigned long iflags;
2235 int k;
2236
2237 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2238 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2239 ++k, resp = resp->nextrp) ;
2240 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2241 return resp;
2242}
2243#endif
2244
2245/* always adds to end of list */
2246static Sg_request *
2247sg_add_request(Sg_fd * sfp)
2248{
2249 int k;
2250 unsigned long iflags;
2251 Sg_request *resp;
2252 Sg_request *rp = sfp->req_arr;
2253
2254 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2255 resp = sfp->headrp;
2256 if (!resp) {
2257 memset(rp, 0, sizeof (Sg_request));
2258 rp->parentfp = sfp;
2259 resp = rp;
2260 sfp->headrp = resp;
2261 } else {
2262 if (0 == sfp->cmd_q)
2263 resp = NULL; /* command queuing disallowed */
2264 else {
2265 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2266 if (!rp->parentfp)
2267 break;
2268 }
2269 if (k < SG_MAX_QUEUE) {
2270 memset(rp, 0, sizeof (Sg_request));
2271 rp->parentfp = sfp;
2272 while (resp->nextrp)
2273 resp = resp->nextrp;
2274 resp->nextrp = rp;
2275 resp = rp;
2276 } else
2277 resp = NULL;
2278 }
2279 }
2280 if (resp) {
2281 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002282 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2285 return resp;
2286}
2287
2288/* Return of 1 for found; 0 for not found */
2289static int
2290sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2291{
2292 Sg_request *prev_rp;
2293 Sg_request *rp;
2294 unsigned long iflags;
2295 int res = 0;
2296
2297 if ((!sfp) || (!srp) || (!sfp->headrp))
2298 return res;
2299 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2300 prev_rp = sfp->headrp;
2301 if (srp == prev_rp) {
2302 sfp->headrp = prev_rp->nextrp;
2303 prev_rp->parentfp = NULL;
2304 res = 1;
2305 } else {
2306 while ((rp = prev_rp->nextrp)) {
2307 if (srp == rp) {
2308 prev_rp->nextrp = rp->nextrp;
2309 rp->parentfp = NULL;
2310 res = 1;
2311 break;
2312 }
2313 prev_rp = rp;
2314 }
2315 }
2316 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2317 return res;
2318}
2319
2320#ifdef CONFIG_SCSI_PROC_FS
2321static Sg_fd *
2322sg_get_nth_sfp(Sg_device * sdp, int nth)
2323{
2324 Sg_fd *resp;
2325 unsigned long iflags;
2326 int k;
2327
2328 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2329 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2330 ++k, resp = resp->nextfp) ;
2331 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2332 return resp;
2333}
2334#endif
2335
2336static Sg_fd *
2337sg_add_sfp(Sg_device * sdp, int dev)
2338{
2339 Sg_fd *sfp;
2340 unsigned long iflags;
2341
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;
2357 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2358 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 }
2366 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2367 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 sg_build_reserve(sfp, sg_big_buff);
2372 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2373 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2374 return sfp;
2375}
2376
2377static void
2378__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2379{
2380 Sg_fd *fp;
2381 Sg_fd *prev_fp;
2382
2383 prev_fp = sdp->headfp;
2384 if (sfp == prev_fp)
2385 sdp->headfp = prev_fp->nextfp;
2386 else {
2387 while ((fp = prev_fp->nextfp)) {
2388 if (sfp == fp) {
2389 prev_fp->nextfp = fp->nextfp;
2390 break;
2391 }
2392 prev_fp = fp;
2393 }
2394 }
2395 if (sfp->reserve.bufflen > 0) {
2396 SCSI_LOG_TIMEOUT(6,
2397 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2398 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 sg_remove_scat(&sfp->reserve);
2400 }
2401 sfp->parentdp = NULL;
2402 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002403 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406/* Returns 0 in normal case, 1 when detached and sdp object removed */
2407static int
2408sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2409{
2410 Sg_request *srp;
2411 Sg_request *tsrp;
2412 int dirty = 0;
2413 int res = 0;
2414
2415 for (srp = sfp->headrp; srp; srp = tsrp) {
2416 tsrp = srp->nextrp;
2417 if (sg_srp_done(srp, sfp))
2418 sg_finish_rem_req(srp);
2419 else
2420 ++dirty;
2421 }
2422 if (0 == dirty) {
2423 unsigned long iflags;
2424
2425 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2426 __sg_remove_sfp(sdp, sfp);
2427 if (sdp->detached && (NULL == sdp->headfp)) {
2428 int k, maxd;
2429
2430 maxd = sg_dev_max;
2431 for (k = 0; k < maxd; ++k) {
2432 if (sdp == sg_dev_arr[k])
2433 break;
2434 }
2435 if (k < maxd)
2436 sg_dev_arr[k] = NULL;
2437 kfree((char *) sdp);
2438 res = 1;
2439 }
2440 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2441 } else {
2442 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2443 /* only bump the access_count if we actually succeeded in
2444 * throwing another counter on the host module */
2445 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2446 sfp->closed = 1; /* flag dirty state on this fd */
2447 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2448 dirty));
2449 }
2450 return res;
2451}
2452
2453static int
2454sg_res_in_use(Sg_fd * sfp)
2455{
2456 const Sg_request *srp;
2457 unsigned long iflags;
2458
2459 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2460 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2461 if (srp->res_used)
2462 break;
2463 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2464 return srp ? 1 : 0;
2465}
2466
Douglas Gilbert6460e752006-09-20 18:20:49 -04002467/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002468static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2470{
Mike Christied6b10342005-11-08 04:06:41 -06002471 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002472 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002474 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Douglas Gilbert6460e752006-09-20 18:20:49 -04002476 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return resp;
2478
2479 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002480 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002482 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2485 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002486 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002487 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002488 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 --order;
2490 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002491 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 resSz = a_size;
2493 }
2494 if (resp) {
2495 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002496 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002497 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499 return resp;
2500}
2501
2502static void
Mike Christied6b10342005-11-08 04:06:41 -06002503sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 int order, a_size;
2506
Mike Christied6b10342005-11-08 04:06:41 -06002507 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return;
2509 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2510 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002511 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514#ifndef MAINTENANCE_IN_CMD
2515#define MAINTENANCE_IN_CMD 0xa3
2516#endif
2517
2518static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2519 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2520 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2521 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2522};
2523
2524static int
2525sg_allow_access(unsigned char opcode, char dev_type)
2526{
2527 int k;
2528
2529 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2530 return 1;
2531 for (k = 0; k < sizeof (allow_ops); ++k) {
2532 if (opcode == allow_ops[k])
2533 return 1;
2534 }
2535 return 0;
2536}
2537
2538#ifdef CONFIG_SCSI_PROC_FS
2539static int
2540sg_last_dev(void)
2541{
2542 int k;
2543 unsigned long iflags;
2544
2545 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2546 for (k = sg_dev_max - 1; k >= 0; --k)
2547 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2548 break;
2549 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2550 return k + 1; /* origin 1 */
2551}
2552#endif
2553
2554static Sg_device *
2555sg_get_dev(int dev)
2556{
2557 Sg_device *sdp = NULL;
2558 unsigned long iflags;
2559
2560 if (sg_dev_arr && (dev >= 0)) {
2561 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2562 if (dev < sg_dev_max)
2563 sdp = sg_dev_arr[dev];
2564 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2565 }
2566 return sdp;
2567}
2568
2569#ifdef CONFIG_SCSI_PROC_FS
2570
2571static struct proc_dir_entry *sg_proc_sgp = NULL;
2572
2573static char sg_proc_sg_dirname[] = "scsi/sg";
2574
2575static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2576
2577static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2578static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2579 size_t count, loff_t *off);
2580static struct file_operations adio_fops = {
2581 /* .owner, .read and .llseek added in sg_proc_init() */
2582 .open = sg_proc_single_open_adio,
2583 .write = sg_proc_write_adio,
2584 .release = single_release,
2585};
2586
2587static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2588static ssize_t sg_proc_write_dressz(struct file *filp,
2589 const char __user *buffer, size_t count, loff_t *off);
2590static struct file_operations dressz_fops = {
2591 .open = sg_proc_single_open_dressz,
2592 .write = sg_proc_write_dressz,
2593 .release = single_release,
2594};
2595
2596static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2597static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2598static struct file_operations version_fops = {
2599 .open = sg_proc_single_open_version,
2600 .release = single_release,
2601};
2602
2603static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2604static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2605static struct file_operations devhdr_fops = {
2606 .open = sg_proc_single_open_devhdr,
2607 .release = single_release,
2608};
2609
2610static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2611static int sg_proc_open_dev(struct inode *inode, struct file *file);
2612static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2613static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2614static void dev_seq_stop(struct seq_file *s, void *v);
2615static struct file_operations dev_fops = {
2616 .open = sg_proc_open_dev,
2617 .release = seq_release,
2618};
2619static struct seq_operations dev_seq_ops = {
2620 .start = dev_seq_start,
2621 .next = dev_seq_next,
2622 .stop = dev_seq_stop,
2623 .show = sg_proc_seq_show_dev,
2624};
2625
2626static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2627static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2628static struct file_operations devstrs_fops = {
2629 .open = sg_proc_open_devstrs,
2630 .release = seq_release,
2631};
2632static struct seq_operations devstrs_seq_ops = {
2633 .start = dev_seq_start,
2634 .next = dev_seq_next,
2635 .stop = dev_seq_stop,
2636 .show = sg_proc_seq_show_devstrs,
2637};
2638
2639static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2640static int sg_proc_open_debug(struct inode *inode, struct file *file);
2641static struct file_operations debug_fops = {
2642 .open = sg_proc_open_debug,
2643 .release = seq_release,
2644};
2645static struct seq_operations debug_seq_ops = {
2646 .start = dev_seq_start,
2647 .next = dev_seq_next,
2648 .stop = dev_seq_stop,
2649 .show = sg_proc_seq_show_debug,
2650};
2651
2652
2653struct sg_proc_leaf {
2654 const char * name;
2655 struct file_operations * fops;
2656};
2657
2658static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2659 {"allow_dio", &adio_fops},
2660 {"debug", &debug_fops},
2661 {"def_reserved_size", &dressz_fops},
2662 {"device_hdr", &devhdr_fops},
2663 {"devices", &dev_fops},
2664 {"device_strs", &devstrs_fops},
2665 {"version", &version_fops}
2666};
2667
2668static int
2669sg_proc_init(void)
2670{
2671 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002672 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct proc_dir_entry *pdep;
2674 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;
2682 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2683 if (pdep) {
2684 leaf->fops->owner = THIS_MODULE,
2685 leaf->fops->read = seq_read,
2686 leaf->fops->llseek = seq_lseek,
2687 pdep->proc_fops = leaf->fops;
2688 }
2689 }
2690 return 0;
2691}
2692
2693static void
2694sg_proc_cleanup(void)
2695{
2696 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002697 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 if (!sg_proc_sgp)
2700 return;
2701 for (k = 0; k < num_leaves; ++k)
2702 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2703 remove_proc_entry(sg_proc_sg_dirname, NULL);
2704}
2705
2706
2707static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2708{
2709 seq_printf(s, "%d\n", *((int *)s->private));
2710 return 0;
2711}
2712
2713static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2714{
2715 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2716}
2717
2718static ssize_t
2719sg_proc_write_adio(struct file *filp, const char __user *buffer,
2720 size_t count, loff_t *off)
2721{
2722 int num;
2723 char buff[11];
2724
2725 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2726 return -EACCES;
2727 num = (count < 10) ? count : 10;
2728 if (copy_from_user(buff, buffer, num))
2729 return -EFAULT;
2730 buff[num] = '\0';
2731 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2732 return count;
2733}
2734
2735static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2736{
2737 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2738}
2739
2740static ssize_t
2741sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2742 size_t count, loff_t *off)
2743{
2744 int num;
2745 unsigned long k = ULONG_MAX;
2746 char buff[11];
2747
2748 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2749 return -EACCES;
2750 num = (count < 10) ? count : 10;
2751 if (copy_from_user(buff, buffer, num))
2752 return -EFAULT;
2753 buff[num] = '\0';
2754 k = simple_strtoul(buff, NULL, 10);
2755 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2756 sg_big_buff = k;
2757 return count;
2758 }
2759 return -ERANGE;
2760}
2761
2762static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2763{
2764 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2765 sg_version_date);
2766 return 0;
2767}
2768
2769static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2770{
2771 return single_open(file, sg_proc_seq_show_version, NULL);
2772}
2773
2774static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2775{
2776 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2777 "online\n");
2778 return 0;
2779}
2780
2781static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2782{
2783 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2784}
2785
2786struct sg_proc_deviter {
2787 loff_t index;
2788 size_t max;
2789};
2790
2791static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2792{
2793 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2794
Jan Blunck729d70f2005-08-27 11:07:52 -07002795 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if (! it)
2797 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (NULL == sg_dev_arr)
Jan Blunck729d70f2005-08-27 11:07:52 -07002800 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 it->index = *pos;
2802 it->max = sg_last_dev();
2803 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002804 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
2808static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2809{
Jan Blunck729d70f2005-08-27 11:07:52 -07002810 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 *pos = ++it->index;
2813 return (it->index < it->max) ? it : NULL;
2814}
2815
2816static void dev_seq_stop(struct seq_file *s, void *v)
2817{
Jan Blunck729d70f2005-08-27 11:07:52 -07002818 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821static int sg_proc_open_dev(struct inode *inode, struct file *file)
2822{
2823 return seq_open(file, &dev_seq_ops);
2824}
2825
2826static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2827{
2828 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2829 Sg_device *sdp;
2830 struct scsi_device *scsidp;
2831
2832 sdp = it ? sg_get_dev(it->index) : NULL;
2833 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2834 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2835 scsidp->host->host_no, scsidp->channel,
2836 scsidp->id, scsidp->lun, (int) scsidp->type,
2837 1,
2838 (int) scsidp->queue_depth,
2839 (int) scsidp->device_busy,
2840 (int) scsi_device_online(scsidp));
2841 else
2842 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2843 return 0;
2844}
2845
2846static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2847{
2848 return seq_open(file, &devstrs_seq_ops);
2849}
2850
2851static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2852{
2853 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2854 Sg_device *sdp;
2855 struct scsi_device *scsidp;
2856
2857 sdp = it ? sg_get_dev(it->index) : NULL;
2858 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2859 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2860 scsidp->vendor, scsidp->model, scsidp->rev);
2861 else
2862 seq_printf(s, "<no active device>\n");
2863 return 0;
2864}
2865
2866static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2867{
2868 int k, m, new_interface, blen, usg;
2869 Sg_request *srp;
2870 Sg_fd *fp;
2871 const sg_io_hdr_t *hp;
2872 const char * cp;
cb59e842005-04-02 13:51:23 -06002873 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2876 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2877 "(res)sgat=%d low_dma=%d\n", k + 1,
2878 jiffies_to_msecs(fp->timeout),
2879 fp->reserve.bufflen,
2880 (int) fp->reserve.k_use_sg,
2881 (int) fp->low_dma);
2882 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2883 (int) fp->cmd_q, (int) fp->force_packid,
2884 (int) fp->keep_orphan, (int) fp->closed);
2885 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2886 hp = &srp->header;
2887 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2888 if (srp->res_used) {
2889 if (new_interface &&
2890 (SG_FLAG_MMAP_IO & hp->flags))
2891 cp = " mmap>> ";
2892 else
2893 cp = " rb>> ";
2894 } else {
2895 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2896 cp = " dio>> ";
2897 else
2898 cp = " ";
2899 }
2900 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002901 blen = srp->data.bufflen;
2902 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 seq_printf(s, srp->done ?
2904 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002905 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 seq_printf(s, " id=%d blen=%d",
2907 srp->header.pack_id, blen);
2908 if (srp->done)
2909 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002910 else {
2911 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002913 (new_interface ? hp->timeout :
2914 jiffies_to_msecs(fp->timeout)),
2915 (ms > hp->duration ? ms - hp->duration : 0));
2916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2918 (int) srp->data.cmd_opcode);
2919 }
2920 if (0 == m)
2921 seq_printf(s, " No requests active\n");
2922 }
2923}
2924
2925static int sg_proc_open_debug(struct inode *inode, struct file *file)
2926{
2927 return seq_open(file, &debug_seq_ops);
2928}
2929
2930static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2931{
2932 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2933 Sg_device *sdp;
2934
2935 if (it && (0 == it->index)) {
2936 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
2937 "(origin 1)\n", sg_dev_max, (int)it->max);
2938 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2939 }
2940 sdp = it ? sg_get_dev(it->index) : NULL;
2941 if (sdp) {
2942 struct scsi_device *scsidp = sdp->device;
2943
2944 if (NULL == scsidp) {
2945 seq_printf(s, "device %d detached ??\n",
2946 (int)it->index);
2947 return 0;
2948 }
2949
2950 if (sg_get_nth_sfp(sdp, 0)) {
2951 seq_printf(s, " >>> device=%s ",
2952 sdp->disk->disk_name);
2953 if (sdp->detached)
2954 seq_printf(s, "detached pending close ");
2955 else
2956 seq_printf
2957 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2958 scsidp->host->host_no,
2959 scsidp->channel, scsidp->id,
2960 scsidp->lun,
2961 scsidp->host->hostt->emulated);
2962 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2963 sdp->sg_tablesize, sdp->exclude);
2964 }
2965 sg_proc_debug_helper(s, sdp);
2966 }
2967 return 0;
2968}
2969
2970#endif /* CONFIG_SCSI_PROC_FS */
2971
2972module_init(init_sg);
2973module_exit(exit_sg);