blob: 34f9343ed0af58a5cddf55c1379b2181d272ac40 [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 Gilbertb2155d02006-08-19 00:11:34 -040063static char *sg_version_date = "20060818";
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
97#define SG_SECTOR_SZ 512
98#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
99
100#define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
101
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500102static int sg_add(struct class_device *, struct class_interface *);
103static void sg_remove(struct class_device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static DEFINE_RWLOCK(sg_dev_arr_lock); /* Also used to lock
106 file descriptor list for device */
107
108static struct class_interface sg_interface = {
109 .add = sg_add,
110 .remove = sg_remove,
111};
112
113typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
114 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
115 unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
116 unsigned bufflen; /* Size of (aggregate) data buffer */
117 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
Mike Christied6b10342005-11-08 04:06:41 -0600118 struct scatterlist *buffer;/* scatter list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
120 unsigned char cmd_opcode; /* first byte of command */
121} Sg_scatter_hold;
122
123struct sg_device; /* forward declarations */
124struct sg_fd;
125
126typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct sg_request *nextrp; /* NULL -> tail request (slist) */
128 struct sg_fd *parentfp; /* NULL -> not in use */
129 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
130 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600131 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
133 char orphan; /* 1 -> drop on sight, 0 -> normal */
134 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
135 volatile char done; /* 0->before bh, 1->before read, 2->read */
136} Sg_request;
137
138typedef struct sg_fd { /* holds the state of a file descriptor */
139 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
140 struct sg_device *parentdp; /* owning device */
141 wait_queue_head_t read_wait; /* queue read until command done */
142 rwlock_t rq_list_lock; /* protect access to list in req_arr */
143 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
144 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
145 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
146 unsigned save_scat_len; /* original length of trunc. scat. element */
147 Sg_request *headrp; /* head of request slist, NULL->empty */
148 struct fasync_struct *async_qp; /* used by asynchronous notification */
149 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
150 char low_dma; /* as in parent but possibly overridden to 1 */
151 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
152 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
153 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
154 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
155 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
156 char mmap_called; /* 0 -> mmap() never called on this fd */
157} Sg_fd;
158
159typedef struct sg_device { /* holds the state of each scsi generic device */
160 struct scsi_device *device;
161 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
162 int sg_tablesize; /* adapter's max scatter-gather table size */
163 Sg_fd *headfp; /* first open fd belonging to this device */
164 volatile char detached; /* 0->attached, 1->detached pending removal */
165 volatile char exclude; /* opened for exclusive access */
166 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
167 struct gendisk *disk;
168 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
169} Sg_device;
170
171static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600172/* tasklet or soft irq callback */
173static void sg_cmd_done(void *data, char *sense, int result, int resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int sg_start_req(Sg_request * srp);
175static void sg_finish_rem_req(Sg_request * srp);
176static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
177static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
178 int tablesize);
179static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
180 Sg_request * srp);
181static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
182 int blocking, int read_only, Sg_request ** o_srp);
183static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
184 unsigned char *cmnd, int timeout, int blocking);
185static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
186 int wr_xf, int *countp, unsigned char __user **up);
187static int sg_write_xfer(Sg_request * srp);
188static int sg_read_xfer(Sg_request * srp);
189static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
190static void sg_remove_scat(Sg_scatter_hold * schp);
191static void sg_build_reserve(Sg_fd * sfp, int req_size);
192static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
193static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600194static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
195static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
197static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
198static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
199static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
200static Sg_request *sg_add_request(Sg_fd * sfp);
201static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
202static int sg_res_in_use(Sg_fd * sfp);
203static int sg_allow_access(unsigned char opcode, char dev_type);
204static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
205static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#ifdef CONFIG_SCSI_PROC_FS
207static int sg_last_dev(void);
208#endif
209
210static Sg_device **sg_dev_arr = NULL;
211static int sg_dev_max;
212static int sg_nr_dev;
213
214#define SZ_SG_HEADER sizeof(struct sg_header)
215#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
216#define SZ_SG_IOVEC sizeof(sg_iovec_t)
217#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
218
219static int
220sg_open(struct inode *inode, struct file *filp)
221{
222 int dev = iminor(inode);
223 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600224 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 Sg_device *sdp;
226 Sg_fd *sfp;
227 int res;
228 int retval;
229
230 nonseekable_open(inode, filp);
231 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
232 sdp = sg_get_dev(dev);
233 if ((!sdp) || (!sdp->device))
234 return -ENXIO;
235 if (sdp->detached)
236 return -ENODEV;
237
238 /* This driver's module count bumped by fops_get in <linux/fs.h> */
239 /* Prevent the device driver from vanishing while we sleep */
240 retval = scsi_device_get(sdp->device);
241 if (retval)
242 return retval;
243
244 if (!((flags & O_NONBLOCK) ||
245 scsi_block_when_processing_errors(sdp->device))) {
246 retval = -ENXIO;
247 /* we are in error recovery for this device */
248 goto error_out;
249 }
250
251 if (flags & O_EXCL) {
252 if (O_RDONLY == (flags & O_ACCMODE)) {
253 retval = -EPERM; /* Can't lock it with read only access */
254 goto error_out;
255 }
256 if (sdp->headfp && (flags & O_NONBLOCK)) {
257 retval = -EBUSY;
258 goto error_out;
259 }
260 res = 0;
261 __wait_event_interruptible(sdp->o_excl_wait,
262 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
263 if (res) {
264 retval = res; /* -ERESTARTSYS because signal hit process */
265 goto error_out;
266 }
267 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
268 if (flags & O_NONBLOCK) {
269 retval = -EBUSY;
270 goto error_out;
271 }
272 res = 0;
273 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
274 res);
275 if (res) {
276 retval = res; /* -ERESTARTSYS because signal hit process */
277 goto error_out;
278 }
279 }
280 if (sdp->detached) {
281 retval = -ENODEV;
282 goto error_out;
283 }
284 if (!sdp->headfp) { /* no existing opens on this device */
285 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600286 q = sdp->device->request_queue;
287 sdp->sg_tablesize = min(q->max_hw_segments,
288 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 if ((sfp = sg_add_sfp(sdp, dev)))
291 filp->private_data = sfp;
292 else {
293 if (flags & O_EXCL)
294 sdp->exclude = 0; /* undo if error */
295 retval = -ENOMEM;
296 goto error_out;
297 }
298 return 0;
299
300 error_out:
301 scsi_device_put(sdp->device);
302 return retval;
303}
304
305/* Following function was formerly called 'sg_close' */
306static int
307sg_release(struct inode *inode, struct file *filp)
308{
309 Sg_device *sdp;
310 Sg_fd *sfp;
311
312 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
313 return -ENXIO;
314 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
315 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
316 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
317 if (!sdp->detached) {
318 scsi_device_put(sdp->device);
319 }
320 sdp->exclude = 0;
321 wake_up_interruptible(&sdp->o_excl_wait);
322 }
323 return 0;
324}
325
326static ssize_t
327sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 Sg_device *sdp;
330 Sg_fd *sfp;
331 Sg_request *srp;
332 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600334 struct sg_header *old_hdr = NULL;
335 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
338 return -ENXIO;
339 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
340 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!access_ok(VERIFY_WRITE, buf, count))
343 return -EFAULT;
344 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600345 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
346 if (!old_hdr)
347 return -ENOMEM;
348 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
349 retval = -EFAULT;
350 goto free_old_hdr;
351 }
352 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600354 sg_io_hdr_t *new_hdr;
355 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
356 if (!new_hdr) {
357 retval = -ENOMEM;
358 goto free_old_hdr;
359 }
360 retval =__copy_from_user
361 (new_hdr, buf, SZ_SG_IO_HDR);
362 req_pack_id = new_hdr->pack_id;
363 kfree(new_hdr);
364 if (retval) {
365 retval = -EFAULT;
366 goto free_old_hdr;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 } else
cb59e842005-04-02 13:51:23 -0600370 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 srp = sg_get_rq_mark(sfp, req_pack_id);
373 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600374 if (sdp->detached) {
375 retval = -ENODEV;
376 goto free_old_hdr;
377 }
378 if (filp->f_flags & O_NONBLOCK) {
379 retval = -EAGAIN;
380 goto free_old_hdr;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 while (1) {
cb59e842005-04-02 13:51:23 -0600383 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600385 (sdp->detached ||
386 (srp = sg_get_rq_mark(sfp, req_pack_id))),
387 retval);
388 if (sdp->detached) {
389 retval = -ENODEV;
390 goto free_old_hdr;
391 }
392 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
cb59e842005-04-02 13:51:23 -0600394
395 /* -ERESTARTSYS as signal hit process */
396 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
cb59e842005-04-02 13:51:23 -0600399 if (srp->header.interface_id != '\0') {
400 retval = sg_new_read(sfp, buf, count, srp);
401 goto free_old_hdr;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600405 if (old_hdr == NULL) {
406 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
407 if (! old_hdr) {
408 retval = -ENOMEM;
409 goto free_old_hdr;
410 }
411 }
412 memset(old_hdr, 0, SZ_SG_HEADER);
413 old_hdr->reply_len = (int) hp->timeout;
414 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
415 old_hdr->pack_id = hp->pack_id;
416 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600418 old_hdr->target_status = hp->masked_status;
419 old_hdr->host_status = hp->host_status;
420 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if ((CHECK_CONDITION & hp->masked_status) ||
422 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600423 memcpy(old_hdr->sense_buffer, srp->sense_b,
424 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 switch (hp->host_status) {
426 /* This setup of 'result' is for backward compatibility and is best
427 ignored by the user who should use target, host + driver status */
428 case DID_OK:
429 case DID_PASSTHROUGH:
430 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600431 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433 case DID_NO_CONNECT:
434 case DID_BUS_BUSY:
435 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600436 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 case DID_BAD_TARGET:
439 case DID_ABORT:
440 case DID_PARITY:
441 case DID_RESET:
442 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600443 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
445 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 hp->masked_status == GOOD) ? 0 : EIO;
448 break;
449 default:
cb59e842005-04-02 13:51:23 -0600450 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 }
453
454 /* Now copy the result back to the user buffer. */
455 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600456 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
457 retval = -EFAULT;
458 goto free_old_hdr;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600461 if (count > old_hdr->reply_len)
462 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600464 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
465 retval = -EFAULT;
466 goto free_old_hdr;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 } else
cb59e842005-04-02 13:51:23 -0600470 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600472 retval = count;
473free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800474 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600475 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static ssize_t
479sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
480{
481 sg_io_hdr_t *hp = &srp->header;
482 int err = 0;
483 int len;
484
485 if (count < SZ_SG_IO_HDR) {
486 err = -EINVAL;
487 goto err_out;
488 }
489 hp->sb_len_wr = 0;
490 if ((hp->mx_sb_len > 0) && hp->sbp) {
491 if ((CHECK_CONDITION & hp->masked_status) ||
492 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600493 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
495 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
496 len = (len > sb_len) ? sb_len : len;
497 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
498 err = -EFAULT;
499 goto err_out;
500 }
501 hp->sb_len_wr = len;
502 }
503 }
504 if (hp->masked_status || hp->host_status || hp->driver_status)
505 hp->info |= SG_INFO_CHECK;
506 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
507 err = -EFAULT;
508 goto err_out;
509 }
510 err = sg_read_xfer(srp);
511 err_out:
512 sg_finish_rem_req(srp);
513 return (0 == err) ? count : err;
514}
515
516static ssize_t
517sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
518{
519 int mxsize, cmd_size, k;
520 int input_size, blocking;
521 unsigned char opcode;
522 Sg_device *sdp;
523 Sg_fd *sfp;
524 Sg_request *srp;
525 struct sg_header old_hdr;
526 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600527 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
530 return -ENXIO;
531 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
532 sdp->disk->disk_name, (int) count));
533 if (sdp->detached)
534 return -ENODEV;
535 if (!((filp->f_flags & O_NONBLOCK) ||
536 scsi_block_when_processing_errors(sdp->device)))
537 return -ENXIO;
538
539 if (!access_ok(VERIFY_READ, buf, count))
540 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
541 if (count < SZ_SG_HEADER)
542 return -EIO;
543 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
544 return -EFAULT;
545 blocking = !(filp->f_flags & O_NONBLOCK);
546 if (old_hdr.reply_len < 0)
547 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
548 if (count < (SZ_SG_HEADER + 6))
549 return -EIO; /* The minimum scsi command length is 6 bytes. */
550
551 if (!(srp = sg_add_request(sfp))) {
552 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
553 return -EDOM;
554 }
555 buf += SZ_SG_HEADER;
556 __get_user(opcode, buf);
557 if (sfp->next_cmd_len > 0) {
558 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
559 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
560 sfp->next_cmd_len = 0;
561 sg_remove_request(sfp, srp);
562 return -EIO;
563 }
564 cmd_size = sfp->next_cmd_len;
565 sfp->next_cmd_len = 0; /* reset so only this write() effected */
566 } else {
567 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
568 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
569 cmd_size = 12;
570 }
571 SCSI_LOG_TIMEOUT(4, printk(
572 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
573/* Determine buffer size. */
574 input_size = count - cmd_size;
575 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
576 mxsize -= SZ_SG_HEADER;
577 input_size -= SZ_SG_HEADER;
578 if (input_size < 0) {
579 sg_remove_request(sfp, srp);
580 return -EIO; /* User did not pass enough bytes for this command. */
581 }
582 hp = &srp->header;
583 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
584 hp->cmd_len = (unsigned char) cmd_size;
585 hp->iovec_count = 0;
586 hp->mx_sb_len = 0;
587 if (input_size > 0)
588 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
589 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
590 else
591 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
592 hp->dxfer_len = mxsize;
593 hp->dxferp = (char __user *)buf + cmd_size;
594 hp->sbp = NULL;
595 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
596 hp->flags = input_size; /* structure abuse ... */
597 hp->pack_id = old_hdr.pack_id;
598 hp->usr_ptr = NULL;
599 if (__copy_from_user(cmnd, buf, cmd_size))
600 return -EFAULT;
601 /*
602 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
603 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
604 * is a non-zero input_size, so emit a warning.
605 */
606 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
607 if (printk_ratelimit())
608 printk(KERN_WARNING
609 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
610 "guessing data in;\n" KERN_WARNING " "
611 "program %s not setting count and/or reply_len properly\n",
612 old_hdr.reply_len - (int)SZ_SG_HEADER,
613 input_size, (unsigned int) cmnd[0],
614 current->comm);
615 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
616 return (k < 0) ? k : count;
617}
618
619static ssize_t
620sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
621 int blocking, int read_only, Sg_request ** o_srp)
622{
623 int k;
624 Sg_request *srp;
625 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600626 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int timeout;
628 unsigned long ul_timeout;
629
630 if (count < SZ_SG_IO_HDR)
631 return -EINVAL;
632 if (!access_ok(VERIFY_READ, buf, count))
633 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
634
635 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
636 if (!(srp = sg_add_request(sfp))) {
637 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
638 return -EDOM;
639 }
640 hp = &srp->header;
641 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
642 sg_remove_request(sfp, srp);
643 return -EFAULT;
644 }
645 if (hp->interface_id != 'S') {
646 sg_remove_request(sfp, srp);
647 return -ENOSYS;
648 }
649 if (hp->flags & SG_FLAG_MMAP_IO) {
650 if (hp->dxfer_len > sfp->reserve.bufflen) {
651 sg_remove_request(sfp, srp);
652 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
653 }
654 if (hp->flags & SG_FLAG_DIRECT_IO) {
655 sg_remove_request(sfp, srp);
656 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
657 }
658 if (sg_res_in_use(sfp)) {
659 sg_remove_request(sfp, srp);
660 return -EBUSY; /* reserve buffer already being used */
661 }
662 }
663 ul_timeout = msecs_to_jiffies(srp->header.timeout);
664 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
665 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
666 sg_remove_request(sfp, srp);
667 return -EMSGSIZE;
668 }
669 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
670 sg_remove_request(sfp, srp);
671 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
672 }
673 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
674 sg_remove_request(sfp, srp);
675 return -EFAULT;
676 }
677 if (read_only &&
678 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
679 sg_remove_request(sfp, srp);
680 return -EPERM;
681 }
682 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
683 if (k < 0)
684 return k;
685 if (o_srp)
686 *o_srp = srp;
687 return count;
688}
689
690static int
691sg_common_write(Sg_fd * sfp, Sg_request * srp,
692 unsigned char *cmnd, int timeout, int blocking)
693{
Mike Christied6b10342005-11-08 04:06:41 -0600694 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 Sg_device *sdp = sfp->parentdp;
696 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
699 hp->status = 0;
700 hp->masked_status = 0;
701 hp->msg_status = 0;
702 hp->info = 0;
703 hp->host_status = 0;
704 hp->driver_status = 0;
705 hp->resid = 0;
706 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
707 (int) cmnd[0], (int) hp->cmd_len));
708
709 if ((k = sg_start_req(srp))) {
710 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
711 sg_finish_rem_req(srp);
712 return k; /* probably out of space --> ENOMEM */
713 }
714 if ((k = sg_write_xfer(srp))) {
715 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
716 sg_finish_rem_req(srp);
717 return k;
718 }
719 if (sdp->detached) {
720 sg_finish_rem_req(srp);
721 return -ENODEV;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 switch (hp->dxfer_direction) {
725 case SG_DXFER_TO_FROM_DEV:
726 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600727 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600730 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
732 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600733 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
735 default:
Mike Christied6b10342005-11-08 04:06:41 -0600736 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 }
cb59e842005-04-02 13:51:23 -0600739 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* Now send everything of to mid-level. The next time we hear about this
741 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600742 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600743 hp->dxfer_len, srp->data.k_use_sg, timeout,
744 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
745 GFP_ATOMIC)) {
746 SCSI_LOG_TIMEOUT(1, printk("sg_write: scsi_execute_async failed\n"));
747 /*
748 * most likely out of mem, but could also be a bad map
749 */
Mike Christie18c49b82006-03-22 16:04:38 -0600750 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600751 return -ENOMEM;
752 } else
753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756static int
757sg_srp_done(Sg_request *srp, Sg_fd *sfp)
758{
759 unsigned long iflags;
760 int done;
761
762 read_lock_irqsave(&sfp->rq_list_lock, iflags);
763 done = srp->done;
764 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
765 return done;
766}
767
768static int
769sg_ioctl(struct inode *inode, struct file *filp,
770 unsigned int cmd_in, unsigned long arg)
771{
772 void __user *p = (void __user *)arg;
773 int __user *ip = p;
774 int result, val, read_only;
775 Sg_device *sdp;
776 Sg_fd *sfp;
777 Sg_request *srp;
778 unsigned long iflags;
779
780 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
781 return -ENXIO;
782 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
783 sdp->disk->disk_name, (int) cmd_in));
784 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
785
786 switch (cmd_in) {
787 case SG_IO:
788 {
789 int blocking = 1; /* ignore O_NONBLOCK flag */
790
791 if (sdp->detached)
792 return -ENODEV;
793 if (!scsi_block_when_processing_errors(sdp->device))
794 return -ENXIO;
795 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
796 return -EFAULT;
797 result =
798 sg_new_write(sfp, p, SZ_SG_IO_HDR,
799 blocking, read_only, &srp);
800 if (result < 0)
801 return result;
802 srp->sg_io_owned = 1;
803 while (1) {
804 result = 0; /* following macro to beat race condition */
805 __wait_event_interruptible(sfp->read_wait,
806 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
807 result);
808 if (sdp->detached)
809 return -ENODEV;
810 if (sfp->closed)
811 return 0; /* request packet dropped already */
812 if (0 == result)
813 break;
814 srp->orphan = 1;
815 return result; /* -ERESTARTSYS because signal hit process */
816 }
817 write_lock_irqsave(&sfp->rq_list_lock, iflags);
818 srp->done = 2;
819 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
820 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
821 return (result < 0) ? result : 0;
822 }
823 case SG_SET_TIMEOUT:
824 result = get_user(val, ip);
825 if (result)
826 return result;
827 if (val < 0)
828 return -EIO;
829 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
830 val = MULDIV (INT_MAX, USER_HZ, HZ);
831 sfp->timeout_user = val;
832 sfp->timeout = MULDIV (val, HZ, USER_HZ);
833
834 return 0;
835 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
836 /* strange ..., for backward compatibility */
837 return sfp->timeout_user;
838 case SG_SET_FORCE_LOW_DMA:
839 result = get_user(val, ip);
840 if (result)
841 return result;
842 if (val) {
843 sfp->low_dma = 1;
844 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
845 val = (int) sfp->reserve.bufflen;
846 sg_remove_scat(&sfp->reserve);
847 sg_build_reserve(sfp, val);
848 }
849 } else {
850 if (sdp->detached)
851 return -ENODEV;
852 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
853 }
854 return 0;
855 case SG_GET_LOW_DMA:
856 return put_user((int) sfp->low_dma, ip);
857 case SG_GET_SCSI_ID:
858 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
859 return -EFAULT;
860 else {
861 sg_scsi_id_t __user *sg_idp = p;
862
863 if (sdp->detached)
864 return -ENODEV;
865 __put_user((int) sdp->device->host->host_no,
866 &sg_idp->host_no);
867 __put_user((int) sdp->device->channel,
868 &sg_idp->channel);
869 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
870 __put_user((int) sdp->device->lun, &sg_idp->lun);
871 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
872 __put_user((short) sdp->device->host->cmd_per_lun,
873 &sg_idp->h_cmd_per_lun);
874 __put_user((short) sdp->device->queue_depth,
875 &sg_idp->d_queue_depth);
876 __put_user(0, &sg_idp->unused[0]);
877 __put_user(0, &sg_idp->unused[1]);
878 return 0;
879 }
880 case SG_SET_FORCE_PACK_ID:
881 result = get_user(val, ip);
882 if (result)
883 return result;
884 sfp->force_packid = val ? 1 : 0;
885 return 0;
886 case SG_GET_PACK_ID:
887 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
888 return -EFAULT;
889 read_lock_irqsave(&sfp->rq_list_lock, iflags);
890 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
891 if ((1 == srp->done) && (!srp->sg_io_owned)) {
892 read_unlock_irqrestore(&sfp->rq_list_lock,
893 iflags);
894 __put_user(srp->header.pack_id, ip);
895 return 0;
896 }
897 }
898 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
899 __put_user(-1, ip);
900 return 0;
901 case SG_GET_NUM_WAITING:
902 read_lock_irqsave(&sfp->rq_list_lock, iflags);
903 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
904 if ((1 == srp->done) && (!srp->sg_io_owned))
905 ++val;
906 }
907 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
908 return put_user(val, ip);
909 case SG_GET_SG_TABLESIZE:
910 return put_user(sdp->sg_tablesize, ip);
911 case SG_SET_RESERVED_SIZE:
912 result = get_user(val, ip);
913 if (result)
914 return result;
915 if (val < 0)
916 return -EINVAL;
917 if (val != sfp->reserve.bufflen) {
918 if (sg_res_in_use(sfp) || sfp->mmap_called)
919 return -EBUSY;
920 sg_remove_scat(&sfp->reserve);
921 sg_build_reserve(sfp, val);
922 }
923 return 0;
924 case SG_GET_RESERVED_SIZE:
925 val = (int) sfp->reserve.bufflen;
926 return put_user(val, ip);
927 case SG_SET_COMMAND_Q:
928 result = get_user(val, ip);
929 if (result)
930 return result;
931 sfp->cmd_q = val ? 1 : 0;
932 return 0;
933 case SG_GET_COMMAND_Q:
934 return put_user((int) sfp->cmd_q, ip);
935 case SG_SET_KEEP_ORPHAN:
936 result = get_user(val, ip);
937 if (result)
938 return result;
939 sfp->keep_orphan = val;
940 return 0;
941 case SG_GET_KEEP_ORPHAN:
942 return put_user((int) sfp->keep_orphan, ip);
943 case SG_NEXT_CMD_LEN:
944 result = get_user(val, ip);
945 if (result)
946 return result;
947 sfp->next_cmd_len = (val > 0) ? val : 0;
948 return 0;
949 case SG_GET_VERSION_NUM:
950 return put_user(sg_version_num, ip);
951 case SG_GET_ACCESS_COUNT:
952 /* faked - we don't have a real access count anymore */
953 val = (sdp->device ? 1 : 0);
954 return put_user(val, ip);
955 case SG_GET_REQUEST_TABLE:
956 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
957 return -EFAULT;
958 else {
cb59e842005-04-02 13:51:23 -0600959 sg_req_info_t *rinfo;
960 unsigned int ms;
961
962 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
963 GFP_KERNEL);
964 if (!rinfo)
965 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 read_lock_irqsave(&sfp->rq_list_lock, iflags);
967 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
968 ++val, srp = srp ? srp->nextrp : srp) {
969 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
970 if (srp) {
971 rinfo[val].req_state = srp->done + 1;
972 rinfo[val].problem =
973 srp->header.masked_status &
974 srp->header.host_status &
975 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600976 if (srp->done)
977 rinfo[val].duration =
978 srp->header.duration;
979 else {
980 ms = jiffies_to_msecs(jiffies);
981 rinfo[val].duration =
982 (ms > srp->header.duration) ?
983 (ms - srp->header.duration) : 0;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600986 rinfo[val].sg_io_owned =
987 srp->sg_io_owned;
988 rinfo[val].pack_id =
989 srp->header.pack_id;
990 rinfo[val].usr_ptr =
991 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 }
994 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -0600995 result = __copy_to_user(p, rinfo,
996 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
997 result = result ? -EFAULT : 0;
998 kfree(rinfo);
999 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 case SG_EMULATED_HOST:
1002 if (sdp->detached)
1003 return -ENODEV;
1004 return put_user(sdp->device->host->hostt->emulated, ip);
1005 case SG_SCSI_RESET:
1006 if (sdp->detached)
1007 return -ENODEV;
1008 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001009 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EBUSY;
1011 } else if (!scsi_block_when_processing_errors(sdp->device))
1012 return -EBUSY;
1013 result = get_user(val, ip);
1014 if (result)
1015 return result;
1016 if (SG_SCSI_RESET_NOTHING == val)
1017 return 0;
1018 switch (val) {
1019 case SG_SCSI_RESET_DEVICE:
1020 val = SCSI_TRY_RESET_DEVICE;
1021 break;
1022 case SG_SCSI_RESET_BUS:
1023 val = SCSI_TRY_RESET_BUS;
1024 break;
1025 case SG_SCSI_RESET_HOST:
1026 val = SCSI_TRY_RESET_HOST;
1027 break;
1028 default:
1029 return -EINVAL;
1030 }
1031 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1032 return -EACCES;
1033 return (scsi_reset_provider(sdp->device, val) ==
1034 SUCCESS) ? 0 : -EIO;
1035 case SCSI_IOCTL_SEND_COMMAND:
1036 if (sdp->detached)
1037 return -ENODEV;
1038 if (read_only) {
1039 unsigned char opcode = WRITE_6;
1040 Scsi_Ioctl_Command __user *siocp = p;
1041
1042 if (copy_from_user(&opcode, siocp->data, 1))
1043 return -EFAULT;
1044 if (!sg_allow_access(opcode, sdp->device->type))
1045 return -EPERM;
1046 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001047 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 case SG_SET_DEBUG:
1049 result = get_user(val, ip);
1050 if (result)
1051 return result;
1052 sdp->sgdebug = (char) val;
1053 return 0;
1054 case SCSI_IOCTL_GET_IDLUN:
1055 case SCSI_IOCTL_GET_BUS_NUMBER:
1056 case SCSI_IOCTL_PROBE_HOST:
1057 case SG_GET_TRANSFORM:
1058 if (sdp->detached)
1059 return -ENODEV;
1060 return scsi_ioctl(sdp->device, cmd_in, p);
1061 default:
1062 if (read_only)
1063 return -EPERM; /* don't know so take safe approach */
1064 return scsi_ioctl(sdp->device, cmd_in, p);
1065 }
1066}
1067
1068#ifdef CONFIG_COMPAT
1069static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1070{
1071 Sg_device *sdp;
1072 Sg_fd *sfp;
1073 struct scsi_device *sdev;
1074
1075 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1076 return -ENXIO;
1077
1078 sdev = sdp->device;
1079 if (sdev->host->hostt->compat_ioctl) {
1080 int ret;
1081
1082 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1083
1084 return ret;
1085 }
1086
1087 return -ENOIOCTLCMD;
1088}
1089#endif
1090
1091static unsigned int
1092sg_poll(struct file *filp, poll_table * wait)
1093{
1094 unsigned int res = 0;
1095 Sg_device *sdp;
1096 Sg_fd *sfp;
1097 Sg_request *srp;
1098 int count = 0;
1099 unsigned long iflags;
1100
1101 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1102 || sfp->closed)
1103 return POLLERR;
1104 poll_wait(filp, &sfp->read_wait, wait);
1105 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1106 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1107 /* if any read waiting, flag it */
1108 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1109 res = POLLIN | POLLRDNORM;
1110 ++count;
1111 }
1112 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1113
1114 if (sdp->detached)
1115 res |= POLLHUP;
1116 else if (!sfp->cmd_q) {
1117 if (0 == count)
1118 res |= POLLOUT | POLLWRNORM;
1119 } else if (count < SG_MAX_QUEUE)
1120 res |= POLLOUT | POLLWRNORM;
1121 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1122 sdp->disk->disk_name, (int) res));
1123 return res;
1124}
1125
1126static int
1127sg_fasync(int fd, struct file *filp, int mode)
1128{
1129 int retval;
1130 Sg_device *sdp;
1131 Sg_fd *sfp;
1132
1133 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1134 return -ENXIO;
1135 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1136 sdp->disk->disk_name, mode));
1137
1138 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1139 return (retval < 0) ? retval : 0;
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142static struct page *
1143sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1144{
1145 Sg_fd *sfp;
1146 struct page *page = NOPAGE_SIGBUS;
Mike Christied6b10342005-11-08 04:06:41 -06001147 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001149 struct scatterlist *sg;
1150 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1153 return page;
1154 rsv_schp = &sfp->reserve;
1155 offset = addr - vma->vm_start;
1156 if (offset >= rsv_schp->bufflen)
1157 return page;
1158 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1159 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001160 sg = rsv_schp->buffer;
1161 sa = vma->vm_start;
1162 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1163 ++k, ++sg) {
1164 len = vma->vm_end - sa;
1165 len = (len < sg->length) ? len : sg->length;
1166 if (offset < len) {
Douglas Gilbertb2155d02006-08-19 00:11:34 -04001167 page = virt_to_page(page_address(sg->page) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001168 get_page(page); /* increment page count */
1169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Mike Christied6b10342005-11-08 04:06:41 -06001171 sa += len;
1172 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Mike Christied6b10342005-11-08 04:06:41 -06001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (type)
1176 *type = VM_FAULT_MINOR;
1177 return page;
1178}
1179
1180static struct vm_operations_struct sg_mmap_vm_ops = {
1181 .nopage = sg_vma_nopage,
1182};
1183
1184static int
1185sg_mmap(struct file *filp, struct vm_area_struct *vma)
1186{
1187 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001188 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001190 int k;
1191 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1194 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001195 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1197 (void *) vma->vm_start, (int) req_sz));
1198 if (vma->vm_pgoff)
1199 return -EINVAL; /* want no offset */
1200 rsv_schp = &sfp->reserve;
1201 if (req_sz > rsv_schp->bufflen)
1202 return -ENOMEM; /* cannot map more than reserved buffer */
1203
Mike Christied6b10342005-11-08 04:06:41 -06001204 sa = vma->vm_start;
1205 sg = rsv_schp->buffer;
1206 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1207 ++k, ++sg) {
1208 len = vma->vm_end - sa;
1209 len = (len < sg->length) ? len : sg->length;
1210 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Mike Christied6b10342005-11-08 04:06:41 -06001212
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001213 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001214 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 vma->vm_private_data = sfp;
1216 vma->vm_ops = &sg_mmap_vm_ops;
1217 return 0;
1218}
1219
1220/* This function is a "bottom half" handler that is called by the
1221 * mid level when a command is completed (or has failed). */
1222static void
Mike Christied6b10342005-11-08 04:06:41 -06001223sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Mike Christied6b10342005-11-08 04:06:41 -06001225 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 Sg_device *sdp = NULL;
1227 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001229 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (NULL == srp) {
1232 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return;
1234 }
1235 sfp = srp->parentfp;
1236 if (sfp)
1237 sdp = sfp->parentdp;
1238 if ((NULL == sdp) || sdp->detached) {
1239 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001245 sdp->disk->disk_name, srp->header.pack_id, result));
1246 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001247 ms = jiffies_to_msecs(jiffies);
1248 srp->header.duration = (ms > srp->header.duration) ?
1249 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001250 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct scsi_sense_hdr sshdr;
1252
Mike Christied6b10342005-11-08 04:06:41 -06001253 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1254 srp->header.status = 0xff & result;
1255 srp->header.masked_status = status_byte(result);
1256 srp->header.msg_status = msg_byte(result);
1257 srp->header.host_status = host_byte(result);
1258 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if ((sdp->sgdebug > 0) &&
1260 ((CHECK_CONDITION == srp->header.masked_status) ||
1261 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001262 __scsi_print_sense("sg_cmd_done", sense,
1263 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001266 if (driver_byte(result) != 0
1267 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 && !scsi_sense_is_deferred(&sshdr)
1269 && sshdr.sense_key == UNIT_ATTENTION
1270 && sdp->device->removable) {
1271 /* Detected possible disc change. Set the bit - this */
1272 /* may be used if there are filesystems using this device */
1273 sdp->device->changed = 1;
1274 }
1275 }
1276 /* Rely on write phase to clean out srp status values, so no "else" */
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (sfp->closed) { /* whoops this fd already released, cleanup */
1279 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1280 sg_finish_rem_req(srp);
1281 srp = NULL;
1282 if (NULL == sfp->headrp) {
1283 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1284 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1285 scsi_device_put(sdp->device);
1286 }
1287 sfp = NULL;
1288 }
1289 } else if (srp && srp->orphan) {
1290 if (sfp->keep_orphan)
1291 srp->sg_io_owned = 0;
1292 else {
1293 sg_finish_rem_req(srp);
1294 srp = NULL;
1295 }
1296 }
1297 if (sfp && srp) {
1298 /* Now wake up any sg_read() that is waiting for this packet. */
1299 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1300 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1301 srp->done = 1;
1302 wake_up_interruptible(&sfp->read_wait);
1303 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1304 }
1305}
1306
1307static struct file_operations sg_fops = {
1308 .owner = THIS_MODULE,
1309 .read = sg_read,
1310 .write = sg_write,
1311 .poll = sg_poll,
1312 .ioctl = sg_ioctl,
1313#ifdef CONFIG_COMPAT
1314 .compat_ioctl = sg_compat_ioctl,
1315#endif
1316 .open = sg_open,
1317 .mmap = sg_mmap,
1318 .release = sg_release,
1319 .fasync = sg_fasync,
1320};
1321
gregkh@suse.ded2538782005-03-23 09:55:22 -08001322static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324static int sg_sysfs_valid = 0;
1325
1326static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1327{
Mike Christied6b10342005-11-08 04:06:41 -06001328 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 Sg_device *sdp;
1330 unsigned long iflags;
1331 void *old_sg_dev_arr = NULL;
1332 int k, error;
1333
Jes Sorensen24669f752006-01-16 10:31:18 -05001334 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (!sdp) {
1336 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1337 return -ENOMEM;
1338 }
1339
1340 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1341 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1342 Sg_device **tmp_da;
1343 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1344 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1345
Jes Sorensen24669f752006-01-16 10:31:18 -05001346 tmp_da = kzalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (unlikely(!tmp_da))
1348 goto expand_failed;
1349
1350 write_lock_irqsave(&sg_dev_arr_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1352 old_sg_dev_arr = sg_dev_arr;
1353 sg_dev_arr = tmp_da;
1354 sg_dev_max = tmp_dev_max;
1355 }
1356
1357 for (k = 0; k < sg_dev_max; k++)
1358 if (!sg_dev_arr[k])
1359 break;
1360 if (unlikely(k >= SG_MAX_DEVS))
1361 goto overflow;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1364 sprintf(disk->disk_name, "sg%d", k);
1365 disk->first_minor = k;
1366 sdp->disk = disk;
1367 sdp->device = scsidp;
1368 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001369 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 sg_nr_dev++;
1372 sg_dev_arr[k] = sdp;
1373 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1374 error = k;
1375
1376 out:
1377 if (error < 0)
1378 kfree(sdp);
1379 kfree(old_sg_dev_arr);
1380 return error;
1381
1382 expand_failed:
1383 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1384 error = -ENOMEM;
1385 goto out;
1386
1387 overflow:
1388 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
James Bottomley9ccfc752005-10-02 11:45:08 -05001389 sdev_printk(KERN_WARNING, scsidp,
1390 "Unable to attach sg device type=%d, minor "
1391 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 error = -ENODEV;
1393 goto out;
1394}
1395
1396static int
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001397sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1400 struct gendisk *disk;
1401 Sg_device *sdp = NULL;
1402 struct cdev * cdev = NULL;
1403 int error, k;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001404 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 disk = alloc_disk(1);
1407 if (!disk) {
1408 printk(KERN_WARNING "alloc_disk failed\n");
1409 return -ENOMEM;
1410 }
1411 disk->major = SCSI_GENERIC_MAJOR;
1412
1413 error = -ENOMEM;
1414 cdev = cdev_alloc();
1415 if (!cdev) {
1416 printk(KERN_WARNING "cdev_alloc failed\n");
1417 goto out;
1418 }
1419 cdev->owner = THIS_MODULE;
1420 cdev->ops = &sg_fops;
1421
1422 error = sg_alloc(disk, scsidp);
1423 if (error < 0) {
1424 printk(KERN_WARNING "sg_alloc failed\n");
1425 goto out;
1426 }
1427 k = error;
1428 sdp = sg_dev_arr[k];
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001431 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001432 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 sdp->cdev = cdev;
1435 if (sg_sysfs_valid) {
1436 struct class_device * sg_class_member;
1437
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07001438 sg_class_member = class_device_create(sg_sysfs_class, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 MKDEV(SCSI_GENERIC_MAJOR, k),
1440 cl_dev->dev, "%s",
1441 disk->disk_name);
1442 if (IS_ERR(sg_class_member))
1443 printk(KERN_WARNING "sg_add: "
gregkh@suse.ded2538782005-03-23 09:55:22 -08001444 "class_device_create failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 class_set_devdata(sg_class_member, sdp);
1446 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1447 &sg_class_member->kobj, "generic");
1448 if (error)
1449 printk(KERN_ERR "sg_add: unable to make symlink "
1450 "'generic' back to sg%d\n", k);
1451 } else
1452 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1453
James Bottomley9ccfc752005-10-02 11:45:08 -05001454 sdev_printk(KERN_NOTICE, scsidp,
1455 "Attached scsi generic sg%d type %d\n", k,scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 return 0;
1458
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001459cdev_add_err:
1460 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1461 kfree(sg_dev_arr[k]);
1462 sg_dev_arr[k] = NULL;
1463 sg_nr_dev--;
1464 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466out:
1467 put_disk(disk);
1468 if (cdev)
1469 cdev_del(cdev);
1470 return error;
1471}
1472
1473static void
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001474sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1477 Sg_device *sdp = NULL;
1478 unsigned long iflags;
1479 Sg_fd *sfp;
1480 Sg_fd *tsfp;
1481 Sg_request *srp;
1482 Sg_request *tsrp;
1483 int k, delay;
1484
1485 if (NULL == sg_dev_arr)
1486 return;
1487 delay = 0;
1488 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1489 for (k = 0; k < sg_dev_max; k++) {
1490 sdp = sg_dev_arr[k];
1491 if ((NULL == sdp) || (sdp->device != scsidp))
1492 continue; /* dirty but lowers nesting */
1493 if (sdp->headfp) {
1494 sdp->detached = 1;
1495 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1496 tsfp = sfp->nextfp;
1497 for (srp = sfp->headrp; srp; srp = tsrp) {
1498 tsrp = srp->nextrp;
1499 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1500 sg_finish_rem_req(srp);
1501 }
1502 if (sfp->closed) {
1503 scsi_device_put(sdp->device);
1504 __sg_remove_sfp(sdp, sfp);
1505 } else {
1506 delay = 1;
1507 wake_up_interruptible(&sfp->read_wait);
1508 kill_fasync(&sfp->async_qp, SIGPOLL,
1509 POLL_HUP);
1510 }
1511 }
1512 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1513 if (NULL == sdp->headfp) {
1514 sg_dev_arr[k] = NULL;
1515 }
1516 } else { /* nothing active, simple case */
1517 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1518 sg_dev_arr[k] = NULL;
1519 }
1520 sg_nr_dev--;
1521 break;
1522 }
1523 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1524
1525 if (sdp) {
1526 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
gregkh@suse.ded2538782005-03-23 09:55:22 -08001527 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, k));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 cdev_del(sdp->cdev);
1529 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 put_disk(sdp->disk);
1531 sdp->disk = NULL;
1532 if (NULL == sdp->headfp)
1533 kfree((char *) sdp);
1534 }
1535
1536 if (delay)
1537 msleep(10); /* dirty detach so delay device destruction */
1538}
1539
1540/* Set 'perm' (4th argument) to 0 to disable module_param's definition
1541 * of sysfs parameters (which module_param doesn't yet support).
1542 * Sysfs parameters defined explicitly below.
1543 */
1544module_param_named(def_reserved_size, def_reserved_size, int, S_IRUGO);
1545module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1546
1547MODULE_AUTHOR("Douglas Gilbert");
1548MODULE_DESCRIPTION("SCSI generic (sg) driver");
1549MODULE_LICENSE("GPL");
1550MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001551MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1554MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1555
1556static int __init
1557init_sg(void)
1558{
1559 int rc;
1560
1561 if (def_reserved_size >= 0)
1562 sg_big_buff = def_reserved_size;
1563
1564 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1565 SG_MAX_DEVS, "sg");
1566 if (rc)
1567 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001568 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if ( IS_ERR(sg_sysfs_class) ) {
1570 rc = PTR_ERR(sg_sysfs_class);
1571 goto err_out;
1572 }
1573 sg_sysfs_valid = 1;
1574 rc = scsi_register_interface(&sg_interface);
1575 if (0 == rc) {
1576#ifdef CONFIG_SCSI_PROC_FS
1577 sg_proc_init();
1578#endif /* CONFIG_SCSI_PROC_FS */
1579 return 0;
1580 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001581 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582err_out:
1583 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1584 return rc;
1585}
1586
1587static void __exit
1588exit_sg(void)
1589{
1590#ifdef CONFIG_SCSI_PROC_FS
1591 sg_proc_cleanup();
1592#endif /* CONFIG_SCSI_PROC_FS */
1593 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001594 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 sg_sysfs_valid = 0;
1596 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1597 SG_MAX_DEVS);
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001598 kfree((char *)sg_dev_arr);
1599 sg_dev_arr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 sg_dev_max = 0;
1601}
1602
1603static int
1604sg_start_req(Sg_request * srp)
1605{
1606 int res;
1607 Sg_fd *sfp = srp->parentfp;
1608 sg_io_hdr_t *hp = &srp->header;
1609 int dxfer_len = (int) hp->dxfer_len;
1610 int dxfer_dir = hp->dxfer_direction;
1611 Sg_scatter_hold *req_schp = &srp->data;
1612 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1613
1614 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1615 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1616 return 0;
1617 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1618 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1619 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1620 res = sg_build_direct(srp, sfp, dxfer_len);
1621 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1622 return res;
1623 }
1624 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1625 sg_link_reserve(sfp, srp, dxfer_len);
1626 else {
1627 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1628 if (res) {
1629 sg_remove_scat(req_schp);
1630 return res;
1631 }
1632 }
1633 return 0;
1634}
1635
1636static void
1637sg_finish_rem_req(Sg_request * srp)
1638{
1639 Sg_fd *sfp = srp->parentfp;
1640 Sg_scatter_hold *req_schp = &srp->data;
1641
1642 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1643 if (srp->res_used)
1644 sg_unlink_reserve(sfp, srp);
1645 else
1646 sg_remove_scat(req_schp);
1647 sg_remove_request(sfp, srp);
1648}
1649
1650static int
1651sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1652{
Mike Christied6b10342005-11-08 04:06:41 -06001653 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001654 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Mike Christied6b10342005-11-08 04:06:41 -06001656 /*
1657 * TODO: test without low_dma, we should not need it since
1658 * the block layer will bounce the buffer for us
1659 *
1660 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1661 */
1662 if (sfp->low_dma)
1663 gfp_flags |= GFP_DMA;
1664 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (!schp->buffer)
1666 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001668 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671#ifdef SG_ALLOW_DIO_CODE
1672/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001673 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1676 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 (i.e., either completely successful or fails)
1678*/
1679static int
1680st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001681 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001683 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1684 unsigned long start = uaddr >> PAGE_SHIFT;
1685 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 struct page **pages;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* User attempted Overflow! */
1690 if ((uaddr + count) < uaddr)
1691 return -EINVAL;
1692
1693 /* Too big */
1694 if (nr_pages > max_pages)
1695 return -ENOMEM;
1696
1697 /* Hmm? */
1698 if (count == 0)
1699 return 0;
1700
1701 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1702 return -ENOMEM;
1703
1704 /* Try to fault in all of the necessary pages */
1705 down_read(&current->mm->mmap_sem);
1706 /* rw==READ means read from drive, write into memory area */
1707 res = get_user_pages(
1708 current,
1709 current->mm,
1710 uaddr,
1711 nr_pages,
1712 rw == READ,
1713 0, /* don't force */
1714 pages,
1715 NULL);
1716 up_read(&current->mm->mmap_sem);
1717
1718 /* Errors and no page mapped should return here */
1719 if (res < nr_pages)
1720 goto out_unmap;
1721
1722 for (i=0; i < nr_pages; i++) {
1723 /* FIXME: flush superflous for rw==READ,
1724 * probably wrong function for rw==WRITE
1725 */
1726 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* ?? Is locking needed? I don't think so */
1728 /* if (TestSetPageLocked(pages[i]))
1729 goto out_unlock; */
1730 }
1731
Mike Christied6b10342005-11-08 04:06:41 -06001732 sgl[0].page = pages[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 sgl[0].offset = uaddr & ~PAGE_MASK;
1734 if (nr_pages > 1) {
1735 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1736 count -= sgl[0].length;
1737 for (i=1; i < nr_pages ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 sgl[i].page = pages[i];
1739 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1740 count -= PAGE_SIZE;
1741 }
1742 }
1743 else {
1744 sgl[0].length = count;
1745 }
1746
1747 kfree(pages);
1748 return nr_pages;
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001751 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 for (j=0; j < res; j++)
1753 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001754 res = 0;
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 kfree(pages);
1757 return res;
1758}
1759
1760
1761/* And unmap them... */
1762static int
1763st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1764 int dirtied)
1765{
1766 int i;
1767
1768 for (i=0; i < nr_pages; i++) {
Nick Pigginb5810032005-10-29 18:16:12 -07001769 struct page *page = sgl[i].page;
1770
Nick Pigginb5810032005-10-29 18:16:12 -07001771 if (dirtied)
1772 SetPageDirty(page);
1773 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* FIXME: cache flush missing for rw==READ
1775 * FIXME: call the correct reference counting function
1776 */
Nick Pigginb5810032005-10-29 18:16:12 -07001777 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779
1780 return 0;
1781}
1782
1783/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1784#endif
1785
1786
1787/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1788static int
1789sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1790{
1791#ifdef SG_ALLOW_DIO_CODE
1792 sg_io_hdr_t *hp = &srp->header;
1793 Sg_scatter_hold *schp = &srp->data;
1794 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int mx_sc_elems, res;
1796 struct scsi_device *sdev = sfp->parentdp->device;
1797
1798 if (((unsigned long)hp->dxferp &
1799 queue_dma_alignment(sdev->request_queue)) != 0)
1800 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1803 if (mx_sc_elems <= 0) {
1804 return 1;
1805 }
Mike Christied6b10342005-11-08 04:06:41 -06001806 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1807 (unsigned long)hp->dxferp, dxfer_len,
1808 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001809 if (res <= 0) {
1810 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 schp->k_use_sg = res;
1814 schp->dio_in_use = 1;
1815 hp->info |= SG_INFO_DIRECT_IO;
1816 return 0;
1817#else
1818 return 1;
1819#endif
1820}
1821
1822static int
1823sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1824{
Mike Christied6b10342005-11-08 04:06:41 -06001825 struct scatterlist *sg;
1826 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1827 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001829 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 if ((blk_size < 0) || (!sfp))
1832 return -EFAULT;
1833 if (0 == blk_size)
1834 ++blk_size; /* don't know why */
1835/* round request up to next highest SG_SECTOR_SZ byte boundary */
1836 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1837 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1838 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001839
1840 /* N.B. ret_sz carried into this block ... */
1841 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1842 if (mx_sc_elems < 0)
1843 return mx_sc_elems; /* most likely -ENOMEM */
1844
1845 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1846 (rem_sz > 0) && (k < mx_sc_elems);
1847 ++k, rem_sz -= ret_sz, ++sg) {
1848
1849 num = (rem_sz > SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1850 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (!p)
1852 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Mike Christied6b10342005-11-08 04:06:41 -06001854 sg->page = p;
1855 sg->length = ret_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Mike Christied6b10342005-11-08 04:06:41 -06001857 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1858 k, p, ret_sz));
1859 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Mike Christied6b10342005-11-08 04:06:41 -06001861 schp->k_use_sg = k;
1862 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1863
1864 schp->bufflen = blk_size;
1865 if (rem_sz > 0) /* must have failed */
1866 return -ENOMEM;
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return 0;
1869}
1870
1871static int
1872sg_write_xfer(Sg_request * srp)
1873{
1874 sg_io_hdr_t *hp = &srp->header;
1875 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001876 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int num_xfer = 0;
1878 int j, k, onum, usglen, ksglen, res;
1879 int iovec_count = (int) hp->iovec_count;
1880 int dxfer_dir = hp->dxfer_direction;
1881 unsigned char *p;
1882 unsigned char __user *up;
1883 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1884
1885 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1886 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1887 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1888 if (schp->bufflen < num_xfer)
1889 num_xfer = schp->bufflen;
1890 }
1891 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1892 (new_interface
1893 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1894 return 0;
1895
1896 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1897 num_xfer, iovec_count, schp->k_use_sg));
1898 if (iovec_count) {
1899 onum = iovec_count;
1900 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1901 return -EFAULT;
1902 } else
1903 onum = 1;
1904
Mike Christied6b10342005-11-08 04:06:41 -06001905 ksglen = sg->length;
1906 p = page_address(sg->page);
1907 for (j = 0, k = 0; j < onum; ++j) {
1908 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1909 if (res)
1910 return res;
1911
1912 for (; p; ++sg, ksglen = sg->length,
1913 p = page_address(sg->page)) {
1914 if (usglen <= 0)
1915 break;
1916 if (ksglen > usglen) {
1917 if (usglen >= num_xfer) {
1918 if (__copy_from_user(p, up, num_xfer))
1919 return -EFAULT;
1920 return 0;
1921 }
1922 if (__copy_from_user(p, up, usglen))
1923 return -EFAULT;
1924 p += usglen;
1925 ksglen -= usglen;
1926 break;
1927 } else {
1928 if (ksglen >= num_xfer) {
1929 if (__copy_from_user(p, up, num_xfer))
1930 return -EFAULT;
1931 return 0;
1932 }
1933 if (__copy_from_user(p, up, ksglen))
1934 return -EFAULT;
1935 up += ksglen;
1936 usglen -= ksglen;
1937 }
1938 ++k;
1939 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return 0;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Mike Christied6b10342005-11-08 04:06:41 -06001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
1945}
1946
1947static int
1948sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1949 int wr_xf, int *countp, unsigned char __user **up)
1950{
1951 int num_xfer = (int) hp->dxfer_len;
1952 unsigned char __user *p = hp->dxferp;
1953 int count;
1954
1955 if (0 == sg_num) {
1956 if (wr_xf && ('\0' == hp->interface_id))
1957 count = (int) hp->flags; /* holds "old" input_size */
1958 else
1959 count = num_xfer;
1960 } else {
1961 sg_iovec_t iovec;
1962 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1963 return -EFAULT;
1964 p = iovec.iov_base;
1965 count = (int) iovec.iov_len;
1966 }
1967 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1968 return -EFAULT;
1969 if (up)
1970 *up = p;
1971 if (countp)
1972 *countp = count;
1973 return 0;
1974}
1975
1976static void
1977sg_remove_scat(Sg_scatter_hold * schp)
1978{
1979 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1980 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06001981 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 if (schp->dio_in_use) {
1984#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06001985 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#endif
1987 } else {
1988 int k;
1989
Mike Christied6b10342005-11-08 04:06:41 -06001990 for (k = 0; (k < schp->k_use_sg) && sg->page;
1991 ++k, ++sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 SCSI_LOG_TIMEOUT(5, printk(
1993 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
Mike Christied6b10342005-11-08 04:06:41 -06001994 k, sg->page, sg->length));
1995 sg_page_free(sg->page, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 }
Mike Christied6b10342005-11-08 04:06:41 -06001998 kfree(schp->buffer);
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 memset(schp, 0, sizeof (*schp));
2001}
2002
2003static int
2004sg_read_xfer(Sg_request * srp)
2005{
2006 sg_io_hdr_t *hp = &srp->header;
2007 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002008 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 int num_xfer = 0;
2010 int j, k, onum, usglen, ksglen, res;
2011 int iovec_count = (int) hp->iovec_count;
2012 int dxfer_dir = hp->dxfer_direction;
2013 unsigned char *p;
2014 unsigned char __user *up;
2015 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2016
2017 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2018 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2019 num_xfer = hp->dxfer_len;
2020 if (schp->bufflen < num_xfer)
2021 num_xfer = schp->bufflen;
2022 }
2023 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2024 (new_interface
2025 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2026 return 0;
2027
2028 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2029 num_xfer, iovec_count, schp->k_use_sg));
2030 if (iovec_count) {
2031 onum = iovec_count;
2032 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2033 return -EFAULT;
2034 } else
2035 onum = 1;
2036
Mike Christied6b10342005-11-08 04:06:41 -06002037 p = page_address(sg->page);
2038 ksglen = sg->length;
2039 for (j = 0, k = 0; j < onum; ++j) {
2040 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2041 if (res)
2042 return res;
2043
2044 for (; p; ++sg, ksglen = sg->length,
2045 p = page_address(sg->page)) {
2046 if (usglen <= 0)
2047 break;
2048 if (ksglen > usglen) {
2049 if (usglen >= num_xfer) {
2050 if (__copy_to_user(up, p, num_xfer))
2051 return -EFAULT;
2052 return 0;
2053 }
2054 if (__copy_to_user(up, p, usglen))
2055 return -EFAULT;
2056 p += usglen;
2057 ksglen -= usglen;
2058 break;
2059 } else {
2060 if (ksglen >= num_xfer) {
2061 if (__copy_to_user(up, p, num_xfer))
2062 return -EFAULT;
2063 return 0;
2064 }
2065 if (__copy_to_user(up, p, ksglen))
2066 return -EFAULT;
2067 up += ksglen;
2068 usglen -= ksglen;
2069 }
2070 ++k;
2071 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
2073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Mike Christied6b10342005-11-08 04:06:41 -06002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
2077}
2078
2079static int
2080sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2081{
2082 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002083 struct scatterlist *sg = schp->buffer;
2084 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2087 num_read_xfer));
2088 if ((!outp) || (num_read_xfer <= 0))
2089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Mike Christied6b10342005-11-08 04:06:41 -06002091 for (k = 0; (k < schp->k_use_sg) && sg->page; ++k, ++sg) {
2092 num = sg->length;
2093 if (num > num_read_xfer) {
2094 if (__copy_to_user(outp, page_address(sg->page),
2095 num_read_xfer))
2096 return -EFAULT;
2097 break;
2098 } else {
2099 if (__copy_to_user(outp, page_address(sg->page),
2100 num))
2101 return -EFAULT;
2102 num_read_xfer -= num;
2103 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 break;
Mike Christied6b10342005-11-08 04:06:41 -06002105 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Mike Christied6b10342005-11-08 04:06:41 -06002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return 0;
2110}
2111
2112static void
2113sg_build_reserve(Sg_fd * sfp, int req_size)
2114{
2115 Sg_scatter_hold *schp = &sfp->reserve;
2116
2117 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2118 do {
2119 if (req_size < PAGE_SIZE)
2120 req_size = PAGE_SIZE;
2121 if (0 == sg_build_indirect(schp, sfp, req_size))
2122 return;
2123 else
2124 sg_remove_scat(schp);
2125 req_size >>= 1; /* divide by 2 */
2126 } while (req_size > (PAGE_SIZE / 2));
2127}
2128
2129static void
2130sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2131{
2132 Sg_scatter_hold *req_schp = &srp->data;
2133 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002134 struct scatterlist *sg = rsv_schp->buffer;
2135 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 srp->res_used = 1;
2138 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002139 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Mike Christied6b10342005-11-08 04:06:41 -06002141 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sg) {
2142 num = sg->length;
2143 if (rem <= num) {
2144 sfp->save_scat_len = num;
2145 sg->length = rem;
2146 req_schp->k_use_sg = k + 1;
2147 req_schp->sglist_len = rsv_schp->sglist_len;
2148 req_schp->buffer = rsv_schp->buffer;
2149
2150 req_schp->bufflen = size;
2151 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2152 break;
2153 } else
2154 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Mike Christied6b10342005-11-08 04:06:41 -06002156
2157 if (k >= rsv_schp->k_use_sg)
2158 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
2161static void
2162sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2163{
2164 Sg_scatter_hold *req_schp = &srp->data;
2165 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2166
2167 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2168 (int) req_schp->k_use_sg));
2169 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002170 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002173 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 (unsigned) sfp->save_scat_len;
2175 else
2176 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2177 }
2178 req_schp->k_use_sg = 0;
2179 req_schp->bufflen = 0;
2180 req_schp->buffer = NULL;
2181 req_schp->sglist_len = 0;
2182 sfp->save_scat_len = 0;
2183 srp->res_used = 0;
2184}
2185
2186static Sg_request *
2187sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2188{
2189 Sg_request *resp;
2190 unsigned long iflags;
2191
2192 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2193 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2194 /* look for requests that are ready + not SG_IO owned */
2195 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2196 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2197 resp->done = 2; /* guard against other readers */
2198 break;
2199 }
2200 }
2201 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2202 return resp;
2203}
2204
2205#ifdef CONFIG_SCSI_PROC_FS
2206static Sg_request *
2207sg_get_nth_request(Sg_fd * sfp, int nth)
2208{
2209 Sg_request *resp;
2210 unsigned long iflags;
2211 int k;
2212
2213 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2214 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2215 ++k, resp = resp->nextrp) ;
2216 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2217 return resp;
2218}
2219#endif
2220
2221/* always adds to end of list */
2222static Sg_request *
2223sg_add_request(Sg_fd * sfp)
2224{
2225 int k;
2226 unsigned long iflags;
2227 Sg_request *resp;
2228 Sg_request *rp = sfp->req_arr;
2229
2230 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2231 resp = sfp->headrp;
2232 if (!resp) {
2233 memset(rp, 0, sizeof (Sg_request));
2234 rp->parentfp = sfp;
2235 resp = rp;
2236 sfp->headrp = resp;
2237 } else {
2238 if (0 == sfp->cmd_q)
2239 resp = NULL; /* command queuing disallowed */
2240 else {
2241 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2242 if (!rp->parentfp)
2243 break;
2244 }
2245 if (k < SG_MAX_QUEUE) {
2246 memset(rp, 0, sizeof (Sg_request));
2247 rp->parentfp = sfp;
2248 while (resp->nextrp)
2249 resp = resp->nextrp;
2250 resp->nextrp = rp;
2251 resp = rp;
2252 } else
2253 resp = NULL;
2254 }
2255 }
2256 if (resp) {
2257 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002258 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2261 return resp;
2262}
2263
2264/* Return of 1 for found; 0 for not found */
2265static int
2266sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2267{
2268 Sg_request *prev_rp;
2269 Sg_request *rp;
2270 unsigned long iflags;
2271 int res = 0;
2272
2273 if ((!sfp) || (!srp) || (!sfp->headrp))
2274 return res;
2275 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2276 prev_rp = sfp->headrp;
2277 if (srp == prev_rp) {
2278 sfp->headrp = prev_rp->nextrp;
2279 prev_rp->parentfp = NULL;
2280 res = 1;
2281 } else {
2282 while ((rp = prev_rp->nextrp)) {
2283 if (srp == rp) {
2284 prev_rp->nextrp = rp->nextrp;
2285 rp->parentfp = NULL;
2286 res = 1;
2287 break;
2288 }
2289 prev_rp = rp;
2290 }
2291 }
2292 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2293 return res;
2294}
2295
2296#ifdef CONFIG_SCSI_PROC_FS
2297static Sg_fd *
2298sg_get_nth_sfp(Sg_device * sdp, int nth)
2299{
2300 Sg_fd *resp;
2301 unsigned long iflags;
2302 int k;
2303
2304 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2305 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2306 ++k, resp = resp->nextfp) ;
2307 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2308 return resp;
2309}
2310#endif
2311
2312static Sg_fd *
2313sg_add_sfp(Sg_device * sdp, int dev)
2314{
2315 Sg_fd *sfp;
2316 unsigned long iflags;
2317
Mike Christied6b10342005-11-08 04:06:41 -06002318 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!sfp)
2320 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 init_waitqueue_head(&sfp->read_wait);
2323 rwlock_init(&sfp->rq_list_lock);
2324
2325 sfp->timeout = SG_DEFAULT_TIMEOUT;
2326 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2327 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2328 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2329 sdp->device->host->unchecked_isa_dma : 1;
2330 sfp->cmd_q = SG_DEF_COMMAND_Q;
2331 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2332 sfp->parentdp = sdp;
2333 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2334 if (!sdp->headfp)
2335 sdp->headfp = sfp;
2336 else { /* add to tail of existing list */
2337 Sg_fd *pfp = sdp->headfp;
2338 while (pfp->nextfp)
2339 pfp = pfp->nextfp;
2340 pfp->nextfp = sfp;
2341 }
2342 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2343 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2344 sg_build_reserve(sfp, sg_big_buff);
2345 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2346 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2347 return sfp;
2348}
2349
2350static void
2351__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2352{
2353 Sg_fd *fp;
2354 Sg_fd *prev_fp;
2355
2356 prev_fp = sdp->headfp;
2357 if (sfp == prev_fp)
2358 sdp->headfp = prev_fp->nextfp;
2359 else {
2360 while ((fp = prev_fp->nextfp)) {
2361 if (sfp == fp) {
2362 prev_fp->nextfp = fp->nextfp;
2363 break;
2364 }
2365 prev_fp = fp;
2366 }
2367 }
2368 if (sfp->reserve.bufflen > 0) {
2369 SCSI_LOG_TIMEOUT(6,
2370 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2371 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 sg_remove_scat(&sfp->reserve);
2373 }
2374 sfp->parentdp = NULL;
2375 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002376 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379/* Returns 0 in normal case, 1 when detached and sdp object removed */
2380static int
2381sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2382{
2383 Sg_request *srp;
2384 Sg_request *tsrp;
2385 int dirty = 0;
2386 int res = 0;
2387
2388 for (srp = sfp->headrp; srp; srp = tsrp) {
2389 tsrp = srp->nextrp;
2390 if (sg_srp_done(srp, sfp))
2391 sg_finish_rem_req(srp);
2392 else
2393 ++dirty;
2394 }
2395 if (0 == dirty) {
2396 unsigned long iflags;
2397
2398 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2399 __sg_remove_sfp(sdp, sfp);
2400 if (sdp->detached && (NULL == sdp->headfp)) {
2401 int k, maxd;
2402
2403 maxd = sg_dev_max;
2404 for (k = 0; k < maxd; ++k) {
2405 if (sdp == sg_dev_arr[k])
2406 break;
2407 }
2408 if (k < maxd)
2409 sg_dev_arr[k] = NULL;
2410 kfree((char *) sdp);
2411 res = 1;
2412 }
2413 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2414 } else {
2415 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2416 /* only bump the access_count if we actually succeeded in
2417 * throwing another counter on the host module */
2418 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2419 sfp->closed = 1; /* flag dirty state on this fd */
2420 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2421 dirty));
2422 }
2423 return res;
2424}
2425
2426static int
2427sg_res_in_use(Sg_fd * sfp)
2428{
2429 const Sg_request *srp;
2430 unsigned long iflags;
2431
2432 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2433 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2434 if (srp->res_used)
2435 break;
2436 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2437 return srp ? 1 : 0;
2438}
2439
2440/* If retSzp==NULL want exact size or fail */
Mike Christied6b10342005-11-08 04:06:41 -06002441static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2443{
Mike Christied6b10342005-11-08 04:06:41 -06002444 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002445 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 int order, a_size;
2447 int resSz = rqSz;
2448
2449 if (rqSz <= 0)
2450 return resp;
2451
2452 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002453 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002455 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2458 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002459 resp = alloc_pages(page_mask, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 while ((!resp) && order && retSzp) {
2461 --order;
2462 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002463 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 resSz = a_size;
2465 }
2466 if (resp) {
2467 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002468 memset(page_address(resp), 0, resSz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (retSzp)
2470 *retSzp = resSz;
2471 }
2472 return resp;
2473}
2474
2475static void
Mike Christied6b10342005-11-08 04:06:41 -06002476sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 int order, a_size;
2479
Mike Christied6b10342005-11-08 04:06:41 -06002480 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return;
2482 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2483 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002484 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
2487#ifndef MAINTENANCE_IN_CMD
2488#define MAINTENANCE_IN_CMD 0xa3
2489#endif
2490
2491static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2492 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2493 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2494 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2495};
2496
2497static int
2498sg_allow_access(unsigned char opcode, char dev_type)
2499{
2500 int k;
2501
2502 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2503 return 1;
2504 for (k = 0; k < sizeof (allow_ops); ++k) {
2505 if (opcode == allow_ops[k])
2506 return 1;
2507 }
2508 return 0;
2509}
2510
2511#ifdef CONFIG_SCSI_PROC_FS
2512static int
2513sg_last_dev(void)
2514{
2515 int k;
2516 unsigned long iflags;
2517
2518 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2519 for (k = sg_dev_max - 1; k >= 0; --k)
2520 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2521 break;
2522 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2523 return k + 1; /* origin 1 */
2524}
2525#endif
2526
2527static Sg_device *
2528sg_get_dev(int dev)
2529{
2530 Sg_device *sdp = NULL;
2531 unsigned long iflags;
2532
2533 if (sg_dev_arr && (dev >= 0)) {
2534 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2535 if (dev < sg_dev_max)
2536 sdp = sg_dev_arr[dev];
2537 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2538 }
2539 return sdp;
2540}
2541
2542#ifdef CONFIG_SCSI_PROC_FS
2543
2544static struct proc_dir_entry *sg_proc_sgp = NULL;
2545
2546static char sg_proc_sg_dirname[] = "scsi/sg";
2547
2548static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2549
2550static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2551static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2552 size_t count, loff_t *off);
2553static struct file_operations adio_fops = {
2554 /* .owner, .read and .llseek added in sg_proc_init() */
2555 .open = sg_proc_single_open_adio,
2556 .write = sg_proc_write_adio,
2557 .release = single_release,
2558};
2559
2560static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2561static ssize_t sg_proc_write_dressz(struct file *filp,
2562 const char __user *buffer, size_t count, loff_t *off);
2563static struct file_operations dressz_fops = {
2564 .open = sg_proc_single_open_dressz,
2565 .write = sg_proc_write_dressz,
2566 .release = single_release,
2567};
2568
2569static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2570static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2571static struct file_operations version_fops = {
2572 .open = sg_proc_single_open_version,
2573 .release = single_release,
2574};
2575
2576static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2577static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2578static struct file_operations devhdr_fops = {
2579 .open = sg_proc_single_open_devhdr,
2580 .release = single_release,
2581};
2582
2583static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2584static int sg_proc_open_dev(struct inode *inode, struct file *file);
2585static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2586static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2587static void dev_seq_stop(struct seq_file *s, void *v);
2588static struct file_operations dev_fops = {
2589 .open = sg_proc_open_dev,
2590 .release = seq_release,
2591};
2592static struct seq_operations dev_seq_ops = {
2593 .start = dev_seq_start,
2594 .next = dev_seq_next,
2595 .stop = dev_seq_stop,
2596 .show = sg_proc_seq_show_dev,
2597};
2598
2599static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2600static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2601static struct file_operations devstrs_fops = {
2602 .open = sg_proc_open_devstrs,
2603 .release = seq_release,
2604};
2605static struct seq_operations devstrs_seq_ops = {
2606 .start = dev_seq_start,
2607 .next = dev_seq_next,
2608 .stop = dev_seq_stop,
2609 .show = sg_proc_seq_show_devstrs,
2610};
2611
2612static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2613static int sg_proc_open_debug(struct inode *inode, struct file *file);
2614static struct file_operations debug_fops = {
2615 .open = sg_proc_open_debug,
2616 .release = seq_release,
2617};
2618static struct seq_operations debug_seq_ops = {
2619 .start = dev_seq_start,
2620 .next = dev_seq_next,
2621 .stop = dev_seq_stop,
2622 .show = sg_proc_seq_show_debug,
2623};
2624
2625
2626struct sg_proc_leaf {
2627 const char * name;
2628 struct file_operations * fops;
2629};
2630
2631static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2632 {"allow_dio", &adio_fops},
2633 {"debug", &debug_fops},
2634 {"def_reserved_size", &dressz_fops},
2635 {"device_hdr", &devhdr_fops},
2636 {"devices", &dev_fops},
2637 {"device_strs", &devstrs_fops},
2638 {"version", &version_fops}
2639};
2640
2641static int
2642sg_proc_init(void)
2643{
2644 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002645 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct proc_dir_entry *pdep;
2647 struct sg_proc_leaf * leaf;
2648
Al Viro66600222005-09-28 22:32:57 +01002649 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (!sg_proc_sgp)
2651 return 1;
2652 for (k = 0; k < num_leaves; ++k) {
2653 leaf = &sg_proc_leaf_arr[k];
2654 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2655 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2656 if (pdep) {
2657 leaf->fops->owner = THIS_MODULE,
2658 leaf->fops->read = seq_read,
2659 leaf->fops->llseek = seq_lseek,
2660 pdep->proc_fops = leaf->fops;
2661 }
2662 }
2663 return 0;
2664}
2665
2666static void
2667sg_proc_cleanup(void)
2668{
2669 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002670 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 if (!sg_proc_sgp)
2673 return;
2674 for (k = 0; k < num_leaves; ++k)
2675 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2676 remove_proc_entry(sg_proc_sg_dirname, NULL);
2677}
2678
2679
2680static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2681{
2682 seq_printf(s, "%d\n", *((int *)s->private));
2683 return 0;
2684}
2685
2686static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2687{
2688 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2689}
2690
2691static ssize_t
2692sg_proc_write_adio(struct file *filp, const char __user *buffer,
2693 size_t count, loff_t *off)
2694{
2695 int num;
2696 char buff[11];
2697
2698 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2699 return -EACCES;
2700 num = (count < 10) ? count : 10;
2701 if (copy_from_user(buff, buffer, num))
2702 return -EFAULT;
2703 buff[num] = '\0';
2704 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2705 return count;
2706}
2707
2708static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2709{
2710 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2711}
2712
2713static ssize_t
2714sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2715 size_t count, loff_t *off)
2716{
2717 int num;
2718 unsigned long k = ULONG_MAX;
2719 char buff[11];
2720
2721 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2722 return -EACCES;
2723 num = (count < 10) ? count : 10;
2724 if (copy_from_user(buff, buffer, num))
2725 return -EFAULT;
2726 buff[num] = '\0';
2727 k = simple_strtoul(buff, NULL, 10);
2728 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2729 sg_big_buff = k;
2730 return count;
2731 }
2732 return -ERANGE;
2733}
2734
2735static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2736{
2737 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2738 sg_version_date);
2739 return 0;
2740}
2741
2742static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2743{
2744 return single_open(file, sg_proc_seq_show_version, NULL);
2745}
2746
2747static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2748{
2749 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2750 "online\n");
2751 return 0;
2752}
2753
2754static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2755{
2756 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2757}
2758
2759struct sg_proc_deviter {
2760 loff_t index;
2761 size_t max;
2762};
2763
2764static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2765{
2766 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2767
Jan Blunck729d70f2005-08-27 11:07:52 -07002768 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (! it)
2770 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (NULL == sg_dev_arr)
Jan Blunck729d70f2005-08-27 11:07:52 -07002773 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 it->index = *pos;
2775 it->max = sg_last_dev();
2776 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002777 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2782{
Jan Blunck729d70f2005-08-27 11:07:52 -07002783 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 *pos = ++it->index;
2786 return (it->index < it->max) ? it : NULL;
2787}
2788
2789static void dev_seq_stop(struct seq_file *s, void *v)
2790{
Jan Blunck729d70f2005-08-27 11:07:52 -07002791 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794static int sg_proc_open_dev(struct inode *inode, struct file *file)
2795{
2796 return seq_open(file, &dev_seq_ops);
2797}
2798
2799static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2800{
2801 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2802 Sg_device *sdp;
2803 struct scsi_device *scsidp;
2804
2805 sdp = it ? sg_get_dev(it->index) : NULL;
2806 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2807 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2808 scsidp->host->host_no, scsidp->channel,
2809 scsidp->id, scsidp->lun, (int) scsidp->type,
2810 1,
2811 (int) scsidp->queue_depth,
2812 (int) scsidp->device_busy,
2813 (int) scsi_device_online(scsidp));
2814 else
2815 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2816 return 0;
2817}
2818
2819static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2820{
2821 return seq_open(file, &devstrs_seq_ops);
2822}
2823
2824static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2825{
2826 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2827 Sg_device *sdp;
2828 struct scsi_device *scsidp;
2829
2830 sdp = it ? sg_get_dev(it->index) : NULL;
2831 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2832 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2833 scsidp->vendor, scsidp->model, scsidp->rev);
2834 else
2835 seq_printf(s, "<no active device>\n");
2836 return 0;
2837}
2838
2839static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2840{
2841 int k, m, new_interface, blen, usg;
2842 Sg_request *srp;
2843 Sg_fd *fp;
2844 const sg_io_hdr_t *hp;
2845 const char * cp;
cb59e842005-04-02 13:51:23 -06002846 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2849 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2850 "(res)sgat=%d low_dma=%d\n", k + 1,
2851 jiffies_to_msecs(fp->timeout),
2852 fp->reserve.bufflen,
2853 (int) fp->reserve.k_use_sg,
2854 (int) fp->low_dma);
2855 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2856 (int) fp->cmd_q, (int) fp->force_packid,
2857 (int) fp->keep_orphan, (int) fp->closed);
2858 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2859 hp = &srp->header;
2860 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2861 if (srp->res_used) {
2862 if (new_interface &&
2863 (SG_FLAG_MMAP_IO & hp->flags))
2864 cp = " mmap>> ";
2865 else
2866 cp = " rb>> ";
2867 } else {
2868 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2869 cp = " dio>> ";
2870 else
2871 cp = " ";
2872 }
2873 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002874 blen = srp->data.bufflen;
2875 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 seq_printf(s, srp->done ?
2877 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002878 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 seq_printf(s, " id=%d blen=%d",
2880 srp->header.pack_id, blen);
2881 if (srp->done)
2882 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002883 else {
2884 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002886 (new_interface ? hp->timeout :
2887 jiffies_to_msecs(fp->timeout)),
2888 (ms > hp->duration ? ms - hp->duration : 0));
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2891 (int) srp->data.cmd_opcode);
2892 }
2893 if (0 == m)
2894 seq_printf(s, " No requests active\n");
2895 }
2896}
2897
2898static int sg_proc_open_debug(struct inode *inode, struct file *file)
2899{
2900 return seq_open(file, &debug_seq_ops);
2901}
2902
2903static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2904{
2905 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2906 Sg_device *sdp;
2907
2908 if (it && (0 == it->index)) {
2909 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
2910 "(origin 1)\n", sg_dev_max, (int)it->max);
2911 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2912 }
2913 sdp = it ? sg_get_dev(it->index) : NULL;
2914 if (sdp) {
2915 struct scsi_device *scsidp = sdp->device;
2916
2917 if (NULL == scsidp) {
2918 seq_printf(s, "device %d detached ??\n",
2919 (int)it->index);
2920 return 0;
2921 }
2922
2923 if (sg_get_nth_sfp(sdp, 0)) {
2924 seq_printf(s, " >>> device=%s ",
2925 sdp->disk->disk_name);
2926 if (sdp->detached)
2927 seq_printf(s, "detached pending close ");
2928 else
2929 seq_printf
2930 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2931 scsidp->host->host_no,
2932 scsidp->channel, scsidp->id,
2933 scsidp->lun,
2934 scsidp->host->hostt->emulated);
2935 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2936 sdp->sg_tablesize, sdp->exclude);
2937 }
2938 sg_proc_debug_helper(s, sdp);
2939 }
2940 return 0;
2941}
2942
2943#endif /* CONFIG_SCSI_PROC_FS */
2944
2945module_init(init_sg);
2946module_exit(exit_sg);