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
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
12 * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
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)
21 static int sg_version_num = 30534; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.34"
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).
31 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/string.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/moduleparam.h>
45 #include <linux/cdev.h>
46 #include <linux/idr.h>
47 #include <linux/seq_file.h>
48 #include <linux/blkdev.h>
49 #include <linux/delay.h>
50 #include <linux/scatterlist.h>
51 #include <linux/blktrace_api.h>
52 #include <linux/smp_lock.h>
55 #include <scsi/scsi_dbg.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_driver.h>
58 #include <scsi/scsi_ioctl.h>
61 #include "scsi_logging.h"
63 #ifdef CONFIG_SCSI_PROC_FS
64 #include <linux/proc_fs.h>
65 static char *sg_version_date = "20061027";
67 static int sg_proc_init(void);
68 static void sg_proc_cleanup(void);
71 #define SG_ALLOW_DIO_DEF 0
72 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
74 #define SG_MAX_DEVS 32768
77 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
78 * Then when using 32 bit integers x * m may overflow during the calculation.
79 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
80 * calculates the same, but prevents the overflow when both m and d
81 * are "small" numbers (like HZ and USER_HZ).
82 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
85 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
87 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
89 int sg_big_buff = SG_DEF_RESERVED_SIZE;
90 /* N.B. This variable is readable and writeable via
91 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
92 of this size (or less if there is not enough memory) will be reserved
93 for use by this file descriptor. [Deprecated usage: this variable is also
94 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
95 the kernel (i.e. it is not a module).] */
96 static int def_reserved_size = -1; /* picks up init parameter */
97 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
99 static int scatter_elem_sz = SG_SCATTER_SZ;
100 static int scatter_elem_sz_prev = SG_SCATTER_SZ;
102 #define SG_SECTOR_SZ 512
103 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
105 static int sg_add(struct device *, struct class_interface *);
106 static void sg_remove(struct device *, struct class_interface *);
108 static DEFINE_IDR(sg_index_idr);
109 static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
110 file descriptor list for device */
112 static struct class_interface sg_interface = {
114 .remove_dev = sg_remove,
117 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
118 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
119 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
120 unsigned bufflen; /* Size of (aggregate) data buffer */
121 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
122 struct scatterlist *buffer;/* scatter list */
123 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
124 unsigned char cmd_opcode; /* first byte of command */
127 struct sg_device; /* forward declarations */
130 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
131 struct sg_request *nextrp; /* NULL -> tail request (slist) */
132 struct sg_fd *parentfp; /* NULL -> not in use */
133 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
134 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
135 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
136 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
137 char orphan; /* 1 -> drop on sight, 0 -> normal */
138 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
139 volatile char done; /* 0->before bh, 1->before read, 2->read */
142 typedef struct sg_fd { /* holds the state of a file descriptor */
143 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
144 struct sg_device *parentdp; /* owning device */
145 wait_queue_head_t read_wait; /* queue read until command done */
146 rwlock_t rq_list_lock; /* protect access to list in req_arr */
147 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
148 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
149 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
150 unsigned save_scat_len; /* original length of trunc. scat. element */
151 Sg_request *headrp; /* head of request slist, NULL->empty */
152 struct fasync_struct *async_qp; /* used by asynchronous notification */
153 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
154 char low_dma; /* as in parent but possibly overridden to 1 */
155 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
156 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
157 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
160 char mmap_called; /* 0 -> mmap() never called on this fd */
163 typedef struct sg_device { /* holds the state of each scsi generic device */
164 struct scsi_device *device;
165 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
166 int sg_tablesize; /* adapter's max scatter-gather table size */
167 u32 index; /* device index number */
168 Sg_fd *headfp; /* first open fd belonging to this device */
169 volatile char detached; /* 0->attached, 1->detached pending removal */
170 volatile char exclude; /* opened for exclusive access */
171 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
172 struct gendisk *disk;
173 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
176 static int sg_fasync(int fd, struct file *filp, int mode);
177 /* tasklet or soft irq callback */
178 static void sg_cmd_done(void *data, char *sense, int result, int resid);
179 static int sg_start_req(Sg_request * srp);
180 static void sg_finish_rem_req(Sg_request * srp);
181 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
184 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
186 static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
187 const char __user *buf, size_t count, int blocking,
188 int read_only, Sg_request **o_srp);
189 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
191 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
192 int wr_xf, int *countp, unsigned char __user **up);
193 static int sg_write_xfer(Sg_request * srp);
194 static int sg_read_xfer(Sg_request * srp);
195 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196 static void sg_remove_scat(Sg_scatter_hold * schp);
197 static void sg_build_reserve(Sg_fd * sfp, int req_size);
198 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
200 static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
201 static void sg_page_free(struct page *page, int size);
202 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
203 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
205 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
206 static Sg_request *sg_add_request(Sg_fd * sfp);
207 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
208 static int sg_res_in_use(Sg_fd * sfp);
209 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
210 static Sg_device *sg_get_dev(int dev);
211 #ifdef CONFIG_SCSI_PROC_FS
212 static int sg_last_dev(void);
215 #define SZ_SG_HEADER sizeof(struct sg_header)
216 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
217 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
218 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
220 static int sg_allow_access(struct file *filp, unsigned char *cmd)
222 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
223 struct request_queue *q = sfp->parentdp->device->request_queue;
225 if (sfp->parentdp->device->type == TYPE_SCANNER)
228 return blk_verify_command(&q->cmd_filter,
229 cmd, filp->f_mode & FMODE_WRITE);
233 sg_open(struct inode *inode, struct file *filp)
235 int dev = iminor(inode);
236 int flags = filp->f_flags;
237 struct request_queue *q;
244 nonseekable_open(inode, filp);
245 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
246 sdp = sg_get_dev(dev);
247 if ((!sdp) || (!sdp->device)) {
256 /* This driver's module count bumped by fops_get in <linux/fs.h> */
257 /* Prevent the device driver from vanishing while we sleep */
258 retval = scsi_device_get(sdp->device);
264 if (!((flags & O_NONBLOCK) ||
265 scsi_block_when_processing_errors(sdp->device))) {
267 /* we are in error recovery for this device */
271 if (flags & O_EXCL) {
272 if (O_RDONLY == (flags & O_ACCMODE)) {
273 retval = -EPERM; /* Can't lock it with read only access */
276 if (sdp->headfp && (flags & O_NONBLOCK)) {
281 __wait_event_interruptible(sdp->o_excl_wait,
282 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
284 retval = res; /* -ERESTARTSYS because signal hit process */
287 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
288 if (flags & O_NONBLOCK) {
293 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
296 retval = res; /* -ERESTARTSYS because signal hit process */
304 if (!sdp->headfp) { /* no existing opens on this device */
306 q = sdp->device->request_queue;
307 sdp->sg_tablesize = min(q->max_hw_segments,
308 q->max_phys_segments);
310 if ((sfp = sg_add_sfp(sdp, dev)))
311 filp->private_data = sfp;
314 sdp->exclude = 0; /* undo if error */
322 scsi_device_put(sdp->device);
327 /* Following function was formerly called 'sg_close' */
329 sg_release(struct inode *inode, struct file *filp)
334 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
336 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
337 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
338 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
339 if (!sdp->detached) {
340 scsi_device_put(sdp->device);
343 wake_up_interruptible(&sdp->o_excl_wait);
349 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
354 int req_pack_id = -1;
356 struct sg_header *old_hdr = NULL;
359 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
361 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
362 sdp->disk->disk_name, (int) count));
364 if (!access_ok(VERIFY_WRITE, buf, count))
366 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
367 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
370 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
374 if (old_hdr->reply_len < 0) {
375 if (count >= SZ_SG_IO_HDR) {
376 sg_io_hdr_t *new_hdr;
377 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
382 retval =__copy_from_user
383 (new_hdr, buf, SZ_SG_IO_HDR);
384 req_pack_id = new_hdr->pack_id;
392 req_pack_id = old_hdr->pack_id;
394 srp = sg_get_rq_mark(sfp, req_pack_id);
395 if (!srp) { /* now wait on packet to arrive */
400 if (filp->f_flags & O_NONBLOCK) {
405 retval = 0; /* following macro beats race condition */
406 __wait_event_interruptible(sfp->read_wait,
408 (srp = sg_get_rq_mark(sfp, req_pack_id))),
417 /* -ERESTARTSYS as signal hit process */
421 if (srp->header.interface_id != '\0') {
422 retval = sg_new_read(sfp, buf, count, srp);
427 if (old_hdr == NULL) {
428 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
434 memset(old_hdr, 0, SZ_SG_HEADER);
435 old_hdr->reply_len = (int) hp->timeout;
436 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
437 old_hdr->pack_id = hp->pack_id;
438 old_hdr->twelve_byte =
439 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
440 old_hdr->target_status = hp->masked_status;
441 old_hdr->host_status = hp->host_status;
442 old_hdr->driver_status = hp->driver_status;
443 if ((CHECK_CONDITION & hp->masked_status) ||
444 (DRIVER_SENSE & hp->driver_status))
445 memcpy(old_hdr->sense_buffer, srp->sense_b,
446 sizeof (old_hdr->sense_buffer));
447 switch (hp->host_status) {
448 /* This setup of 'result' is for backward compatibility and is best
449 ignored by the user who should use target, host + driver status */
451 case DID_PASSTHROUGH:
458 old_hdr->result = EBUSY;
465 old_hdr->result = EIO;
468 old_hdr->result = (srp->sense_b[0] == 0 &&
469 hp->masked_status == GOOD) ? 0 : EIO;
472 old_hdr->result = EIO;
476 /* Now copy the result back to the user buffer. */
477 if (count >= SZ_SG_HEADER) {
478 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
483 if (count > old_hdr->reply_len)
484 count = old_hdr->reply_len;
485 if (count > SZ_SG_HEADER) {
486 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
492 count = (old_hdr->result == 0) ? 0 : -EIO;
493 sg_finish_rem_req(srp);
501 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
503 sg_io_hdr_t *hp = &srp->header;
507 if (count < SZ_SG_IO_HDR) {
512 if ((hp->mx_sb_len > 0) && hp->sbp) {
513 if ((CHECK_CONDITION & hp->masked_status) ||
514 (DRIVER_SENSE & hp->driver_status)) {
515 int sb_len = SCSI_SENSE_BUFFERSIZE;
516 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
517 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
518 len = (len > sb_len) ? sb_len : len;
519 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
526 if (hp->masked_status || hp->host_status || hp->driver_status)
527 hp->info |= SG_INFO_CHECK;
528 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
532 err = sg_read_xfer(srp);
534 sg_finish_rem_req(srp);
535 return (0 == err) ? count : err;
539 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
541 int mxsize, cmd_size, k;
542 int input_size, blocking;
543 unsigned char opcode;
547 struct sg_header old_hdr;
549 unsigned char cmnd[MAX_COMMAND_SIZE];
551 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
553 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
554 sdp->disk->disk_name, (int) count));
557 if (!((filp->f_flags & O_NONBLOCK) ||
558 scsi_block_when_processing_errors(sdp->device)))
561 if (!access_ok(VERIFY_READ, buf, count))
562 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
563 if (count < SZ_SG_HEADER)
565 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
567 blocking = !(filp->f_flags & O_NONBLOCK);
568 if (old_hdr.reply_len < 0)
569 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
570 if (count < (SZ_SG_HEADER + 6))
571 return -EIO; /* The minimum scsi command length is 6 bytes. */
573 if (!(srp = sg_add_request(sfp))) {
574 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
578 __get_user(opcode, buf);
579 if (sfp->next_cmd_len > 0) {
580 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
581 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
582 sfp->next_cmd_len = 0;
583 sg_remove_request(sfp, srp);
586 cmd_size = sfp->next_cmd_len;
587 sfp->next_cmd_len = 0; /* reset so only this write() effected */
589 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
590 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
593 SCSI_LOG_TIMEOUT(4, printk(
594 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
595 /* Determine buffer size. */
596 input_size = count - cmd_size;
597 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
598 mxsize -= SZ_SG_HEADER;
599 input_size -= SZ_SG_HEADER;
600 if (input_size < 0) {
601 sg_remove_request(sfp, srp);
602 return -EIO; /* User did not pass enough bytes for this command. */
605 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
606 hp->cmd_len = (unsigned char) cmd_size;
610 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
611 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
613 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
614 hp->dxfer_len = mxsize;
615 hp->dxferp = (char __user *)buf + cmd_size;
617 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
618 hp->flags = input_size; /* structure abuse ... */
619 hp->pack_id = old_hdr.pack_id;
621 if (__copy_from_user(cmnd, buf, cmd_size))
624 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
625 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
626 * is a non-zero input_size, so emit a warning.
628 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
629 static char cmd[TASK_COMM_LEN];
630 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
632 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
633 "guessing data in;\n" KERN_WARNING " "
634 "program %s not setting count and/or reply_len properly\n",
635 old_hdr.reply_len - (int)SZ_SG_HEADER,
636 input_size, (unsigned int) cmnd[0],
638 strcpy(cmd, current->comm);
641 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
642 return (k < 0) ? k : count;
646 sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
647 size_t count, int blocking, int read_only,
653 unsigned char cmnd[MAX_COMMAND_SIZE];
655 unsigned long ul_timeout;
657 if (count < SZ_SG_IO_HDR)
659 if (!access_ok(VERIFY_READ, buf, count))
660 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
662 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
663 if (!(srp = sg_add_request(sfp))) {
664 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
668 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
669 sg_remove_request(sfp, srp);
672 if (hp->interface_id != 'S') {
673 sg_remove_request(sfp, srp);
676 if (hp->flags & SG_FLAG_MMAP_IO) {
677 if (hp->dxfer_len > sfp->reserve.bufflen) {
678 sg_remove_request(sfp, srp);
679 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
681 if (hp->flags & SG_FLAG_DIRECT_IO) {
682 sg_remove_request(sfp, srp);
683 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
685 if (sg_res_in_use(sfp)) {
686 sg_remove_request(sfp, srp);
687 return -EBUSY; /* reserve buffer already being used */
690 ul_timeout = msecs_to_jiffies(srp->header.timeout);
691 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
692 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
693 sg_remove_request(sfp, srp);
696 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
697 sg_remove_request(sfp, srp);
698 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
700 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
701 sg_remove_request(sfp, srp);
704 if (read_only && sg_allow_access(file, cmnd)) {
705 sg_remove_request(sfp, srp);
708 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
717 sg_common_write(Sg_fd * sfp, Sg_request * srp,
718 unsigned char *cmnd, int timeout, int blocking)
721 Sg_device *sdp = sfp->parentdp;
722 sg_io_hdr_t *hp = &srp->header;
724 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
726 hp->masked_status = 0;
730 hp->driver_status = 0;
732 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
733 (int) cmnd[0], (int) hp->cmd_len));
735 if ((k = sg_start_req(srp))) {
736 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
737 sg_finish_rem_req(srp);
738 return k; /* probably out of space --> ENOMEM */
740 if ((k = sg_write_xfer(srp))) {
741 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
742 sg_finish_rem_req(srp);
746 sg_finish_rem_req(srp);
750 switch (hp->dxfer_direction) {
751 case SG_DXFER_TO_FROM_DEV:
752 case SG_DXFER_FROM_DEV:
753 data_dir = DMA_FROM_DEVICE;
755 case SG_DXFER_TO_DEV:
756 data_dir = DMA_TO_DEVICE;
758 case SG_DXFER_UNKNOWN:
759 data_dir = DMA_BIDIRECTIONAL;
765 hp->duration = jiffies_to_msecs(jiffies);
766 /* Now send everything of to mid-level. The next time we hear about this
767 packet is when sg_cmd_done() is called (i.e. a callback). */
768 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
769 hp->dxfer_len, srp->data.k_use_sg, timeout,
770 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
772 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
774 * most likely out of mem, but could also be a bad map
776 sg_finish_rem_req(srp);
783 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
785 unsigned long iflags;
788 read_lock_irqsave(&sfp->rq_list_lock, iflags);
790 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
795 sg_ioctl(struct inode *inode, struct file *filp,
796 unsigned int cmd_in, unsigned long arg)
798 void __user *p = (void __user *)arg;
800 int result, val, read_only;
804 unsigned long iflags;
806 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
809 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
810 sdp->disk->disk_name, (int) cmd_in));
811 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
816 int blocking = 1; /* ignore O_NONBLOCK flag */
820 if (!scsi_block_when_processing_errors(sdp->device))
822 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
825 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
826 blocking, read_only, &srp);
829 srp->sg_io_owned = 1;
831 result = 0; /* following macro to beat race condition */
832 __wait_event_interruptible(sfp->read_wait,
833 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
838 return 0; /* request packet dropped already */
842 return result; /* -ERESTARTSYS because signal hit process */
844 write_lock_irqsave(&sfp->rq_list_lock, iflags);
846 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
847 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
848 return (result < 0) ? result : 0;
851 result = get_user(val, ip);
856 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
857 val = MULDIV (INT_MAX, USER_HZ, HZ);
858 sfp->timeout_user = val;
859 sfp->timeout = MULDIV (val, HZ, USER_HZ);
862 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
863 /* strange ..., for backward compatibility */
864 return sfp->timeout_user;
865 case SG_SET_FORCE_LOW_DMA:
866 result = get_user(val, ip);
871 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
872 val = (int) sfp->reserve.bufflen;
873 sg_remove_scat(&sfp->reserve);
874 sg_build_reserve(sfp, val);
879 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
883 return put_user((int) sfp->low_dma, ip);
885 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
888 sg_scsi_id_t __user *sg_idp = p;
892 __put_user((int) sdp->device->host->host_no,
894 __put_user((int) sdp->device->channel,
896 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
897 __put_user((int) sdp->device->lun, &sg_idp->lun);
898 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
899 __put_user((short) sdp->device->host->cmd_per_lun,
900 &sg_idp->h_cmd_per_lun);
901 __put_user((short) sdp->device->queue_depth,
902 &sg_idp->d_queue_depth);
903 __put_user(0, &sg_idp->unused[0]);
904 __put_user(0, &sg_idp->unused[1]);
907 case SG_SET_FORCE_PACK_ID:
908 result = get_user(val, ip);
911 sfp->force_packid = val ? 1 : 0;
914 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
916 read_lock_irqsave(&sfp->rq_list_lock, iflags);
917 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
918 if ((1 == srp->done) && (!srp->sg_io_owned)) {
919 read_unlock_irqrestore(&sfp->rq_list_lock,
921 __put_user(srp->header.pack_id, ip);
925 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
928 case SG_GET_NUM_WAITING:
929 read_lock_irqsave(&sfp->rq_list_lock, iflags);
930 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
931 if ((1 == srp->done) && (!srp->sg_io_owned))
934 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
935 return put_user(val, ip);
936 case SG_GET_SG_TABLESIZE:
937 return put_user(sdp->sg_tablesize, ip);
938 case SG_SET_RESERVED_SIZE:
939 result = get_user(val, ip);
944 val = min_t(int, val,
945 sdp->device->request_queue->max_sectors * 512);
946 if (val != sfp->reserve.bufflen) {
947 if (sg_res_in_use(sfp) || sfp->mmap_called)
949 sg_remove_scat(&sfp->reserve);
950 sg_build_reserve(sfp, val);
953 case SG_GET_RESERVED_SIZE:
954 val = min_t(int, sfp->reserve.bufflen,
955 sdp->device->request_queue->max_sectors * 512);
956 return put_user(val, ip);
957 case SG_SET_COMMAND_Q:
958 result = get_user(val, ip);
961 sfp->cmd_q = val ? 1 : 0;
963 case SG_GET_COMMAND_Q:
964 return put_user((int) sfp->cmd_q, ip);
965 case SG_SET_KEEP_ORPHAN:
966 result = get_user(val, ip);
969 sfp->keep_orphan = val;
971 case SG_GET_KEEP_ORPHAN:
972 return put_user((int) sfp->keep_orphan, ip);
973 case SG_NEXT_CMD_LEN:
974 result = get_user(val, ip);
977 sfp->next_cmd_len = (val > 0) ? val : 0;
979 case SG_GET_VERSION_NUM:
980 return put_user(sg_version_num, ip);
981 case SG_GET_ACCESS_COUNT:
982 /* faked - we don't have a real access count anymore */
983 val = (sdp->device ? 1 : 0);
984 return put_user(val, ip);
985 case SG_GET_REQUEST_TABLE:
986 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
989 sg_req_info_t *rinfo;
992 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
996 read_lock_irqsave(&sfp->rq_list_lock, iflags);
997 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
998 ++val, srp = srp ? srp->nextrp : srp) {
999 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
1001 rinfo[val].req_state = srp->done + 1;
1002 rinfo[val].problem =
1003 srp->header.masked_status &
1004 srp->header.host_status &
1005 srp->header.driver_status;
1007 rinfo[val].duration =
1008 srp->header.duration;
1010 ms = jiffies_to_msecs(jiffies);
1011 rinfo[val].duration =
1012 (ms > srp->header.duration) ?
1013 (ms - srp->header.duration) : 0;
1015 rinfo[val].orphan = srp->orphan;
1016 rinfo[val].sg_io_owned =
1018 rinfo[val].pack_id =
1019 srp->header.pack_id;
1020 rinfo[val].usr_ptr =
1021 srp->header.usr_ptr;
1024 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1025 result = __copy_to_user(p, rinfo,
1026 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1027 result = result ? -EFAULT : 0;
1031 case SG_EMULATED_HOST:
1034 return put_user(sdp->device->host->hostt->emulated, ip);
1038 if (filp->f_flags & O_NONBLOCK) {
1039 if (scsi_host_in_recovery(sdp->device->host))
1041 } else if (!scsi_block_when_processing_errors(sdp->device))
1043 result = get_user(val, ip);
1046 if (SG_SCSI_RESET_NOTHING == val)
1049 case SG_SCSI_RESET_DEVICE:
1050 val = SCSI_TRY_RESET_DEVICE;
1052 case SG_SCSI_RESET_TARGET:
1053 val = SCSI_TRY_RESET_TARGET;
1055 case SG_SCSI_RESET_BUS:
1056 val = SCSI_TRY_RESET_BUS;
1058 case SG_SCSI_RESET_HOST:
1059 val = SCSI_TRY_RESET_HOST;
1064 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1066 return (scsi_reset_provider(sdp->device, val) ==
1067 SUCCESS) ? 0 : -EIO;
1068 case SCSI_IOCTL_SEND_COMMAND:
1072 unsigned char opcode = WRITE_6;
1073 Scsi_Ioctl_Command __user *siocp = p;
1075 if (copy_from_user(&opcode, siocp->data, 1))
1077 if (sg_allow_access(filp, &opcode))
1080 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
1082 result = get_user(val, ip);
1085 sdp->sgdebug = (char) val;
1087 case SCSI_IOCTL_GET_IDLUN:
1088 case SCSI_IOCTL_GET_BUS_NUMBER:
1089 case SCSI_IOCTL_PROBE_HOST:
1090 case SG_GET_TRANSFORM:
1093 return scsi_ioctl(sdp->device, cmd_in, p);
1095 return put_user(sdp->device->request_queue->max_sectors * 512,
1098 return blk_trace_setup(sdp->device->request_queue,
1099 sdp->disk->disk_name,
1100 sdp->device->sdev_gendev.devt,
1103 return blk_trace_startstop(sdp->device->request_queue, 1);
1105 return blk_trace_startstop(sdp->device->request_queue, 0);
1106 case BLKTRACETEARDOWN:
1107 return blk_trace_remove(sdp->device->request_queue);
1110 return -EPERM; /* don't know so take safe approach */
1111 return scsi_ioctl(sdp->device, cmd_in, p);
1115 #ifdef CONFIG_COMPAT
1116 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1120 struct scsi_device *sdev;
1122 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1126 if (sdev->host->hostt->compat_ioctl) {
1129 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1134 return -ENOIOCTLCMD;
1139 sg_poll(struct file *filp, poll_table * wait)
1141 unsigned int res = 0;
1146 unsigned long iflags;
1148 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1151 poll_wait(filp, &sfp->read_wait, wait);
1152 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1153 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1154 /* if any read waiting, flag it */
1155 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1156 res = POLLIN | POLLRDNORM;
1159 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1163 else if (!sfp->cmd_q) {
1165 res |= POLLOUT | POLLWRNORM;
1166 } else if (count < SG_MAX_QUEUE)
1167 res |= POLLOUT | POLLWRNORM;
1168 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1169 sdp->disk->disk_name, (int) res));
1174 sg_fasync(int fd, struct file *filp, int mode)
1180 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1182 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1183 sdp->disk->disk_name, mode));
1185 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1186 return (retval < 0) ? retval : 0;
1190 sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1193 unsigned long offset, len, sa;
1194 Sg_scatter_hold *rsv_schp;
1195 struct scatterlist *sg;
1198 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1199 return VM_FAULT_SIGBUS;
1200 rsv_schp = &sfp->reserve;
1201 offset = vmf->pgoff << PAGE_SHIFT;
1202 if (offset >= rsv_schp->bufflen)
1203 return VM_FAULT_SIGBUS;
1204 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
1205 offset, rsv_schp->k_use_sg));
1206 sg = rsv_schp->buffer;
1208 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1209 ++k, sg = sg_next(sg)) {
1210 len = vma->vm_end - sa;
1211 len = (len < sg->length) ? len : sg->length;
1214 page = virt_to_page(page_address(sg_page(sg)) + offset);
1215 get_page(page); /* increment page count */
1217 return 0; /* success */
1223 return VM_FAULT_SIGBUS;
1226 static struct vm_operations_struct sg_mmap_vm_ops = {
1227 .fault = sg_vma_fault,
1231 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1234 unsigned long req_sz, len, sa;
1235 Sg_scatter_hold *rsv_schp;
1237 struct scatterlist *sg;
1239 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1241 req_sz = vma->vm_end - vma->vm_start;
1242 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1243 (void *) vma->vm_start, (int) req_sz));
1245 return -EINVAL; /* want no offset */
1246 rsv_schp = &sfp->reserve;
1247 if (req_sz > rsv_schp->bufflen)
1248 return -ENOMEM; /* cannot map more than reserved buffer */
1251 sg = rsv_schp->buffer;
1252 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1253 ++k, sg = sg_next(sg)) {
1254 len = vma->vm_end - sa;
1255 len = (len < sg->length) ? len : sg->length;
1259 sfp->mmap_called = 1;
1260 vma->vm_flags |= VM_RESERVED;
1261 vma->vm_private_data = sfp;
1262 vma->vm_ops = &sg_mmap_vm_ops;
1266 /* This function is a "bottom half" handler that is called by the
1267 * mid level when a command is completed (or has failed). */
1269 sg_cmd_done(void *data, char *sense, int result, int resid)
1271 Sg_request *srp = data;
1272 Sg_device *sdp = NULL;
1274 unsigned long iflags;
1278 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1281 sfp = srp->parentfp;
1283 sdp = sfp->parentdp;
1284 if ((NULL == sdp) || sdp->detached) {
1285 printk(KERN_INFO "sg_cmd_done: device detached\n");
1290 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1291 sdp->disk->disk_name, srp->header.pack_id, result));
1292 srp->header.resid = resid;
1293 ms = jiffies_to_msecs(jiffies);
1294 srp->header.duration = (ms > srp->header.duration) ?
1295 (ms - srp->header.duration) : 0;
1297 struct scsi_sense_hdr sshdr;
1299 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1300 srp->header.status = 0xff & result;
1301 srp->header.masked_status = status_byte(result);
1302 srp->header.msg_status = msg_byte(result);
1303 srp->header.host_status = host_byte(result);
1304 srp->header.driver_status = driver_byte(result);
1305 if ((sdp->sgdebug > 0) &&
1306 ((CHECK_CONDITION == srp->header.masked_status) ||
1307 (COMMAND_TERMINATED == srp->header.masked_status)))
1308 __scsi_print_sense("sg_cmd_done", sense,
1309 SCSI_SENSE_BUFFERSIZE);
1311 /* Following if statement is a patch supplied by Eric Youngdale */
1312 if (driver_byte(result) != 0
1313 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1314 && !scsi_sense_is_deferred(&sshdr)
1315 && sshdr.sense_key == UNIT_ATTENTION
1316 && sdp->device->removable) {
1317 /* Detected possible disc change. Set the bit - this */
1318 /* may be used if there are filesystems using this device */
1319 sdp->device->changed = 1;
1322 /* Rely on write phase to clean out srp status values, so no "else" */
1324 if (sfp->closed) { /* whoops this fd already released, cleanup */
1325 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1326 sg_finish_rem_req(srp);
1328 if (NULL == sfp->headrp) {
1329 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1330 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1331 scsi_device_put(sdp->device);
1335 } else if (srp && srp->orphan) {
1336 if (sfp->keep_orphan)
1337 srp->sg_io_owned = 0;
1339 sg_finish_rem_req(srp);
1344 /* Now wake up any sg_read() that is waiting for this packet. */
1345 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1346 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1348 wake_up_interruptible(&sfp->read_wait);
1349 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1353 static struct file_operations sg_fops = {
1354 .owner = THIS_MODULE,
1359 #ifdef CONFIG_COMPAT
1360 .compat_ioctl = sg_compat_ioctl,
1364 .release = sg_release,
1365 .fasync = sg_fasync,
1368 static struct class *sg_sysfs_class;
1370 static int sg_sysfs_valid = 0;
1372 static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1374 struct request_queue *q = scsidp->request_queue;
1376 unsigned long iflags;
1380 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1382 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1383 return ERR_PTR(-ENOMEM);
1386 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1387 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1391 write_lock_irqsave(&sg_index_lock, iflags);
1392 error = idr_get_new(&sg_index_idr, sdp, &k);
1393 write_unlock_irqrestore(&sg_index_lock, iflags);
1396 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1401 if (unlikely(k >= SG_MAX_DEVS))
1404 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1405 sprintf(disk->disk_name, "sg%d", k);
1406 disk->first_minor = k;
1408 sdp->device = scsidp;
1409 init_waitqueue_head(&sdp->o_excl_wait);
1410 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1417 return ERR_PTR(error);
1422 sdev_printk(KERN_WARNING, scsidp,
1423 "Unable to attach sg device type=%d, minor "
1424 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1430 sg_add(struct device *cl_dev, struct class_interface *cl_intf)
1432 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1433 struct gendisk *disk;
1434 Sg_device *sdp = NULL;
1435 struct cdev * cdev = NULL;
1437 unsigned long iflags;
1439 disk = alloc_disk(1);
1441 printk(KERN_WARNING "alloc_disk failed\n");
1444 disk->major = SCSI_GENERIC_MAJOR;
1447 cdev = cdev_alloc();
1449 printk(KERN_WARNING "cdev_alloc failed\n");
1452 cdev->owner = THIS_MODULE;
1453 cdev->ops = &sg_fops;
1455 sdp = sg_alloc(disk, scsidp);
1457 printk(KERN_WARNING "sg_alloc failed\n");
1458 error = PTR_ERR(sdp);
1462 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
1467 if (sg_sysfs_valid) {
1468 struct device *sg_class_member;
1470 sg_class_member = device_create_drvdata(sg_sysfs_class,
1472 MKDEV(SCSI_GENERIC_MAJOR,
1475 "%s", disk->disk_name);
1476 if (IS_ERR(sg_class_member)) {
1477 printk(KERN_ERR "sg_add: "
1478 "device_create failed\n");
1479 error = PTR_ERR(sg_class_member);
1482 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1483 &sg_class_member->kobj, "generic");
1485 printk(KERN_ERR "sg_add: unable to make symlink "
1486 "'generic' back to sg%d\n", sdp->index);
1488 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
1490 sdev_printk(KERN_NOTICE, scsidp,
1491 "Attached scsi generic sg%d type %d\n", sdp->index,
1494 dev_set_drvdata(cl_dev, sdp);
1499 write_lock_irqsave(&sg_index_lock, iflags);
1500 idr_remove(&sg_index_idr, sdp->index);
1501 write_unlock_irqrestore(&sg_index_lock, iflags);
1512 sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
1514 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1515 Sg_device *sdp = dev_get_drvdata(cl_dev);
1516 unsigned long iflags;
1527 write_lock_irqsave(&sg_index_lock, iflags);
1530 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1532 for (srp = sfp->headrp; srp; srp = tsrp) {
1534 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1535 sg_finish_rem_req(srp);
1538 scsi_device_put(sdp->device);
1539 __sg_remove_sfp(sdp, sfp);
1542 wake_up_interruptible(&sfp->read_wait);
1543 kill_fasync(&sfp->async_qp, SIGPOLL,
1547 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1548 if (NULL == sdp->headfp) {
1549 idr_remove(&sg_index_idr, sdp->index);
1551 } else { /* nothing active, simple case */
1552 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1553 idr_remove(&sg_index_idr, sdp->index);
1555 write_unlock_irqrestore(&sg_index_lock, iflags);
1557 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1558 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1559 cdev_del(sdp->cdev);
1561 put_disk(sdp->disk);
1563 if (NULL == sdp->headfp)
1567 msleep(10); /* dirty detach so delay device destruction */
1570 module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1571 module_param_named(def_reserved_size, def_reserved_size, int,
1573 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1575 MODULE_AUTHOR("Douglas Gilbert");
1576 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1577 MODULE_LICENSE("GPL");
1578 MODULE_VERSION(SG_VERSION_STR);
1579 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
1581 MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1582 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1583 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1584 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1591 if (scatter_elem_sz < PAGE_SIZE) {
1592 scatter_elem_sz = PAGE_SIZE;
1593 scatter_elem_sz_prev = scatter_elem_sz;
1595 if (def_reserved_size >= 0)
1596 sg_big_buff = def_reserved_size;
1598 def_reserved_size = sg_big_buff;
1600 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1604 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1605 if ( IS_ERR(sg_sysfs_class) ) {
1606 rc = PTR_ERR(sg_sysfs_class);
1610 rc = scsi_register_interface(&sg_interface);
1612 #ifdef CONFIG_SCSI_PROC_FS
1614 #endif /* CONFIG_SCSI_PROC_FS */
1617 class_destroy(sg_sysfs_class);
1619 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1626 #ifdef CONFIG_SCSI_PROC_FS
1628 #endif /* CONFIG_SCSI_PROC_FS */
1629 scsi_unregister_interface(&sg_interface);
1630 class_destroy(sg_sysfs_class);
1632 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1634 idr_destroy(&sg_index_idr);
1638 sg_start_req(Sg_request * srp)
1641 Sg_fd *sfp = srp->parentfp;
1642 sg_io_hdr_t *hp = &srp->header;
1643 int dxfer_len = (int) hp->dxfer_len;
1644 int dxfer_dir = hp->dxfer_direction;
1645 Sg_scatter_hold *req_schp = &srp->data;
1646 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1648 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1649 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1651 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1652 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1653 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1654 res = sg_build_direct(srp, sfp, dxfer_len);
1655 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1658 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1659 sg_link_reserve(sfp, srp, dxfer_len);
1661 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1663 sg_remove_scat(req_schp);
1671 sg_finish_rem_req(Sg_request * srp)
1673 Sg_fd *sfp = srp->parentfp;
1674 Sg_scatter_hold *req_schp = &srp->data;
1676 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1678 sg_unlink_reserve(sfp, srp);
1680 sg_remove_scat(req_schp);
1681 sg_remove_request(sfp, srp);
1685 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1687 int sg_bufflen = tablesize * sizeof(struct scatterlist);
1688 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1691 * TODO: test without low_dma, we should not need it since
1692 * the block layer will bounce the buffer for us
1694 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1697 gfp_flags |= GFP_DMA;
1698 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
1701 sg_init_table(schp->buffer, tablesize);
1702 schp->sglist_len = sg_bufflen;
1703 return tablesize; /* number of scat_gath elements allocated */
1706 #ifdef SG_ALLOW_DIO_CODE
1707 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1708 /* TODO: hopefully we can use the generic block layer code */
1710 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1711 - mapping of all pages not successful
1712 (i.e., either completely successful or fails)
1715 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1716 unsigned long uaddr, size_t count, int rw)
1718 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1719 unsigned long start = uaddr >> PAGE_SHIFT;
1720 const int nr_pages = end - start;
1722 struct page **pages;
1724 /* User attempted Overflow! */
1725 if ((uaddr + count) < uaddr)
1729 if (nr_pages > max_pages)
1736 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1739 /* Try to fault in all of the necessary pages */
1740 down_read(¤t->mm->mmap_sem);
1741 /* rw==READ means read from drive, write into memory area */
1742 res = get_user_pages(
1748 0, /* don't force */
1751 up_read(¤t->mm->mmap_sem);
1753 /* Errors and no page mapped should return here */
1757 for (i=0; i < nr_pages; i++) {
1758 /* FIXME: flush superflous for rw==READ,
1759 * probably wrong function for rw==WRITE
1761 flush_dcache_page(pages[i]);
1762 /* ?? Is locking needed? I don't think so */
1763 /* if (!trylock_page(pages[i]))
1767 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
1769 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1770 count -= sgl[0].length;
1771 for (i=1; i < nr_pages ; i++)
1772 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
1775 sgl[0].length = count;
1783 for (j=0; j < res; j++)
1784 page_cache_release(pages[j]);
1792 /* And unmap them... */
1794 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1799 for (i=0; i < nr_pages; i++) {
1800 struct page *page = sg_page(&sgl[i]);
1804 /* unlock_page(page); */
1805 /* FIXME: cache flush missing for rw==READ
1806 * FIXME: call the correct reference counting function
1808 page_cache_release(page);
1814 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1818 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1820 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1822 #ifdef SG_ALLOW_DIO_CODE
1823 sg_io_hdr_t *hp = &srp->header;
1824 Sg_scatter_hold *schp = &srp->data;
1825 int sg_tablesize = sfp->parentdp->sg_tablesize;
1826 int mx_sc_elems, res;
1827 struct scsi_device *sdev = sfp->parentdp->device;
1829 if (((unsigned long)hp->dxferp &
1830 queue_dma_alignment(sdev->request_queue)) != 0)
1833 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1834 if (mx_sc_elems <= 0) {
1837 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1838 (unsigned long)hp->dxferp, dxfer_len,
1839 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
1841 sg_remove_scat(schp);
1844 schp->k_use_sg = res;
1845 schp->dio_in_use = 1;
1846 hp->info |= SG_INFO_DIRECT_IO;
1854 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1856 struct scatterlist *sg;
1857 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1858 int sg_tablesize = sfp->parentdp->sg_tablesize;
1859 int blk_size = buff_size;
1860 struct page *p = NULL;
1865 ++blk_size; /* don't know why */
1866 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1867 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1868 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1869 buff_size, blk_size));
1871 /* N.B. ret_sz carried into this block ... */
1872 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1873 if (mx_sc_elems < 0)
1874 return mx_sc_elems; /* most likely -ENOMEM */
1876 num = scatter_elem_sz;
1877 if (unlikely(num != scatter_elem_sz_prev)) {
1878 if (num < PAGE_SIZE) {
1879 scatter_elem_sz = PAGE_SIZE;
1880 scatter_elem_sz_prev = PAGE_SIZE;
1882 scatter_elem_sz_prev = num;
1884 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1885 (rem_sz > 0) && (k < mx_sc_elems);
1886 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
1888 num = (rem_sz > scatter_elem_sz_prev) ?
1889 scatter_elem_sz_prev : rem_sz;
1890 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1894 if (num == scatter_elem_sz_prev) {
1895 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1896 scatter_elem_sz = ret_sz;
1897 scatter_elem_sz_prev = ret_sz;
1900 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
1902 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1903 "ret_sz=%d\n", k, num, ret_sz));
1904 } /* end of for loop */
1907 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1908 "rem_sz=%d\n", k, rem_sz));
1910 schp->bufflen = blk_size;
1911 if (rem_sz > 0) /* must have failed */
1918 sg_write_xfer(Sg_request * srp)
1920 sg_io_hdr_t *hp = &srp->header;
1921 Sg_scatter_hold *schp = &srp->data;
1922 struct scatterlist *sg = schp->buffer;
1924 int j, k, onum, usglen, ksglen, res;
1925 int iovec_count = (int) hp->iovec_count;
1926 int dxfer_dir = hp->dxfer_direction;
1928 unsigned char __user *up;
1929 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1931 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1932 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1933 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1934 if (schp->bufflen < num_xfer)
1935 num_xfer = schp->bufflen;
1937 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1939 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1942 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1943 num_xfer, iovec_count, schp->k_use_sg));
1946 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1951 ksglen = sg->length;
1952 p = page_address(sg_page(sg));
1953 for (j = 0, k = 0; j < onum; ++j) {
1954 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1958 for (; p; sg = sg_next(sg), ksglen = sg->length,
1959 p = page_address(sg_page(sg))) {
1962 if (ksglen > usglen) {
1963 if (usglen >= num_xfer) {
1964 if (__copy_from_user(p, up, num_xfer))
1968 if (__copy_from_user(p, up, usglen))
1974 if (ksglen >= num_xfer) {
1975 if (__copy_from_user(p, up, num_xfer))
1979 if (__copy_from_user(p, up, ksglen))
1985 if (k >= schp->k_use_sg)
1994 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1995 int wr_xf, int *countp, unsigned char __user **up)
1997 int num_xfer = (int) hp->dxfer_len;
1998 unsigned char __user *p = hp->dxferp;
2002 if (wr_xf && ('\0' == hp->interface_id))
2003 count = (int) hp->flags; /* holds "old" input_size */
2008 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2011 count = (int) iovec.iov_len;
2013 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2023 sg_remove_scat(Sg_scatter_hold * schp)
2025 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2026 if (schp->buffer && (schp->sglist_len > 0)) {
2027 struct scatterlist *sg = schp->buffer;
2029 if (schp->dio_in_use) {
2030 #ifdef SG_ALLOW_DIO_CODE
2031 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
2036 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
2037 ++k, sg = sg_next(sg)) {
2038 SCSI_LOG_TIMEOUT(5, printk(
2039 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
2040 k, sg_page(sg), sg->length));
2041 sg_page_free(sg_page(sg), sg->length);
2044 kfree(schp->buffer);
2046 memset(schp, 0, sizeof (*schp));
2050 sg_read_xfer(Sg_request * srp)
2052 sg_io_hdr_t *hp = &srp->header;
2053 Sg_scatter_hold *schp = &srp->data;
2054 struct scatterlist *sg = schp->buffer;
2056 int j, k, onum, usglen, ksglen, res;
2057 int iovec_count = (int) hp->iovec_count;
2058 int dxfer_dir = hp->dxfer_direction;
2060 unsigned char __user *up;
2061 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2063 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2064 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2065 num_xfer = hp->dxfer_len;
2066 if (schp->bufflen < num_xfer)
2067 num_xfer = schp->bufflen;
2069 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2071 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2074 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2075 num_xfer, iovec_count, schp->k_use_sg));
2078 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2083 p = page_address(sg_page(sg));
2084 ksglen = sg->length;
2085 for (j = 0, k = 0; j < onum; ++j) {
2086 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2090 for (; p; sg = sg_next(sg), ksglen = sg->length,
2091 p = page_address(sg_page(sg))) {
2094 if (ksglen > usglen) {
2095 if (usglen >= num_xfer) {
2096 if (__copy_to_user(up, p, num_xfer))
2100 if (__copy_to_user(up, p, usglen))
2106 if (ksglen >= num_xfer) {
2107 if (__copy_to_user(up, p, num_xfer))
2111 if (__copy_to_user(up, p, ksglen))
2117 if (k >= schp->k_use_sg)
2126 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2128 Sg_scatter_hold *schp = &srp->data;
2129 struct scatterlist *sg = schp->buffer;
2132 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2134 if ((!outp) || (num_read_xfer <= 0))
2137 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
2139 if (num > num_read_xfer) {
2140 if (__copy_to_user(outp, page_address(sg_page(sg)),
2145 if (__copy_to_user(outp, page_address(sg_page(sg)),
2148 num_read_xfer -= num;
2149 if (num_read_xfer <= 0)
2159 sg_build_reserve(Sg_fd * sfp, int req_size)
2161 Sg_scatter_hold *schp = &sfp->reserve;
2163 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2165 if (req_size < PAGE_SIZE)
2166 req_size = PAGE_SIZE;
2167 if (0 == sg_build_indirect(schp, sfp, req_size))
2170 sg_remove_scat(schp);
2171 req_size >>= 1; /* divide by 2 */
2172 } while (req_size > (PAGE_SIZE / 2));
2176 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2178 Sg_scatter_hold *req_schp = &srp->data;
2179 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2180 struct scatterlist *sg = rsv_schp->buffer;
2184 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2187 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
2190 sfp->save_scat_len = num;
2192 req_schp->k_use_sg = k + 1;
2193 req_schp->sglist_len = rsv_schp->sglist_len;
2194 req_schp->buffer = rsv_schp->buffer;
2196 req_schp->bufflen = size;
2197 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2203 if (k >= rsv_schp->k_use_sg)
2204 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2208 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2210 Sg_scatter_hold *req_schp = &srp->data;
2211 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2213 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2214 (int) req_schp->k_use_sg));
2215 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2216 struct scatterlist *sg = rsv_schp->buffer;
2218 if (sfp->save_scat_len > 0)
2219 (sg + (req_schp->k_use_sg - 1))->length =
2220 (unsigned) sfp->save_scat_len;
2222 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2224 req_schp->k_use_sg = 0;
2225 req_schp->bufflen = 0;
2226 req_schp->buffer = NULL;
2227 req_schp->sglist_len = 0;
2228 sfp->save_scat_len = 0;
2233 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2236 unsigned long iflags;
2238 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2239 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2240 /* look for requests that are ready + not SG_IO owned */
2241 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2242 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2243 resp->done = 2; /* guard against other readers */
2247 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2251 #ifdef CONFIG_SCSI_PROC_FS
2253 sg_get_nth_request(Sg_fd * sfp, int nth)
2256 unsigned long iflags;
2259 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2260 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2261 ++k, resp = resp->nextrp) ;
2262 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2267 /* always adds to end of list */
2269 sg_add_request(Sg_fd * sfp)
2272 unsigned long iflags;
2274 Sg_request *rp = sfp->req_arr;
2276 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2279 memset(rp, 0, sizeof (Sg_request));
2284 if (0 == sfp->cmd_q)
2285 resp = NULL; /* command queuing disallowed */
2287 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2291 if (k < SG_MAX_QUEUE) {
2292 memset(rp, 0, sizeof (Sg_request));
2294 while (resp->nextrp)
2295 resp = resp->nextrp;
2303 resp->nextrp = NULL;
2304 resp->header.duration = jiffies_to_msecs(jiffies);
2306 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2310 /* Return of 1 for found; 0 for not found */
2312 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2314 Sg_request *prev_rp;
2316 unsigned long iflags;
2319 if ((!sfp) || (!srp) || (!sfp->headrp))
2321 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2322 prev_rp = sfp->headrp;
2323 if (srp == prev_rp) {
2324 sfp->headrp = prev_rp->nextrp;
2325 prev_rp->parentfp = NULL;
2328 while ((rp = prev_rp->nextrp)) {
2330 prev_rp->nextrp = rp->nextrp;
2331 rp->parentfp = NULL;
2338 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2342 #ifdef CONFIG_SCSI_PROC_FS
2344 sg_get_nth_sfp(Sg_device * sdp, int nth)
2347 unsigned long iflags;
2350 read_lock_irqsave(&sg_index_lock, iflags);
2351 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2352 ++k, resp = resp->nextfp) ;
2353 read_unlock_irqrestore(&sg_index_lock, iflags);
2359 sg_add_sfp(Sg_device * sdp, int dev)
2362 unsigned long iflags;
2365 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2369 init_waitqueue_head(&sfp->read_wait);
2370 rwlock_init(&sfp->rq_list_lock);
2372 sfp->timeout = SG_DEFAULT_TIMEOUT;
2373 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2374 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2375 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2376 sdp->device->host->unchecked_isa_dma : 1;
2377 sfp->cmd_q = SG_DEF_COMMAND_Q;
2378 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2379 sfp->parentdp = sdp;
2380 write_lock_irqsave(&sg_index_lock, iflags);
2383 else { /* add to tail of existing list */
2384 Sg_fd *pfp = sdp->headfp;
2389 write_unlock_irqrestore(&sg_index_lock, iflags);
2390 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2391 if (unlikely(sg_big_buff != def_reserved_size))
2392 sg_big_buff = def_reserved_size;
2394 bufflen = min_t(int, sg_big_buff,
2395 sdp->device->request_queue->max_sectors * 512);
2396 sg_build_reserve(sfp, bufflen);
2397 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2398 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2403 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2408 prev_fp = sdp->headfp;
2410 sdp->headfp = prev_fp->nextfp;
2412 while ((fp = prev_fp->nextfp)) {
2414 prev_fp->nextfp = fp->nextfp;
2420 if (sfp->reserve.bufflen > 0) {
2422 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2423 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2424 sg_remove_scat(&sfp->reserve);
2426 sfp->parentdp = NULL;
2427 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2431 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2433 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2440 for (srp = sfp->headrp; srp; srp = tsrp) {
2442 if (sg_srp_done(srp, sfp))
2443 sg_finish_rem_req(srp);
2448 unsigned long iflags;
2450 write_lock_irqsave(&sg_index_lock, iflags);
2451 __sg_remove_sfp(sdp, sfp);
2452 if (sdp->detached && (NULL == sdp->headfp)) {
2453 idr_remove(&sg_index_idr, sdp->index);
2457 write_unlock_irqrestore(&sg_index_lock, iflags);
2459 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2460 /* only bump the access_count if we actually succeeded in
2461 * throwing another counter on the host module */
2462 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2463 sfp->closed = 1; /* flag dirty state on this fd */
2464 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2471 sg_res_in_use(Sg_fd * sfp)
2473 const Sg_request *srp;
2474 unsigned long iflags;
2476 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2477 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2480 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2484 /* The size fetched (value output via retSzp) set when non-NULL return */
2485 static struct page *
2486 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2488 struct page *resp = NULL;
2493 if ((rqSz <= 0) || (NULL == retSzp))
2497 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
2499 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
2501 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2502 order++, a_size <<= 1) ;
2503 resSz = a_size; /* rounded up if necessary */
2504 resp = alloc_pages(page_mask, order);
2505 while ((!resp) && order) {
2507 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2508 resp = alloc_pages(page_mask, order); /* try half */
2512 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2513 memset(page_address(resp), 0, resSz);
2520 sg_page_free(struct page *page, int size)
2526 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2527 order++, a_size <<= 1) ;
2528 __free_pages(page, order);
2531 #ifdef CONFIG_SCSI_PROC_FS
2533 sg_idr_max_id(int id, void *p, void *data)
2547 unsigned long iflags;
2549 read_lock_irqsave(&sg_index_lock, iflags);
2550 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2551 read_unlock_irqrestore(&sg_index_lock, iflags);
2552 return k + 1; /* origin 1 */
2560 unsigned long iflags;
2562 read_lock_irqsave(&sg_index_lock, iflags);
2563 sdp = idr_find(&sg_index_idr, dev);
2564 read_unlock_irqrestore(&sg_index_lock, iflags);
2569 #ifdef CONFIG_SCSI_PROC_FS
2571 static struct proc_dir_entry *sg_proc_sgp = NULL;
2573 static char sg_proc_sg_dirname[] = "scsi/sg";
2575 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2577 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2578 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2579 size_t count, loff_t *off);
2580 static struct file_operations adio_fops = {
2581 /* .owner, .read and .llseek added in sg_proc_init() */
2582 .open = sg_proc_single_open_adio,
2583 .write = sg_proc_write_adio,
2584 .release = single_release,
2587 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2588 static ssize_t sg_proc_write_dressz(struct file *filp,
2589 const char __user *buffer, size_t count, loff_t *off);
2590 static struct file_operations dressz_fops = {
2591 .open = sg_proc_single_open_dressz,
2592 .write = sg_proc_write_dressz,
2593 .release = single_release,
2596 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2597 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2598 static struct file_operations version_fops = {
2599 .open = sg_proc_single_open_version,
2600 .release = single_release,
2603 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2604 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2605 static struct file_operations devhdr_fops = {
2606 .open = sg_proc_single_open_devhdr,
2607 .release = single_release,
2610 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2611 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2612 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2613 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2614 static void dev_seq_stop(struct seq_file *s, void *v);
2615 static struct file_operations dev_fops = {
2616 .open = sg_proc_open_dev,
2617 .release = seq_release,
2619 static struct seq_operations dev_seq_ops = {
2620 .start = dev_seq_start,
2621 .next = dev_seq_next,
2622 .stop = dev_seq_stop,
2623 .show = sg_proc_seq_show_dev,
2626 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2627 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2628 static struct file_operations devstrs_fops = {
2629 .open = sg_proc_open_devstrs,
2630 .release = seq_release,
2632 static struct seq_operations devstrs_seq_ops = {
2633 .start = dev_seq_start,
2634 .next = dev_seq_next,
2635 .stop = dev_seq_stop,
2636 .show = sg_proc_seq_show_devstrs,
2639 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2640 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2641 static struct file_operations debug_fops = {
2642 .open = sg_proc_open_debug,
2643 .release = seq_release,
2645 static struct seq_operations debug_seq_ops = {
2646 .start = dev_seq_start,
2647 .next = dev_seq_next,
2648 .stop = dev_seq_stop,
2649 .show = sg_proc_seq_show_debug,
2653 struct sg_proc_leaf {
2655 struct file_operations * fops;
2658 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2659 {"allow_dio", &adio_fops},
2660 {"debug", &debug_fops},
2661 {"def_reserved_size", &dressz_fops},
2662 {"device_hdr", &devhdr_fops},
2663 {"devices", &dev_fops},
2664 {"device_strs", &devstrs_fops},
2665 {"version", &version_fops}
2672 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2673 struct sg_proc_leaf * leaf;
2675 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2678 for (k = 0; k < num_leaves; ++k) {
2679 leaf = &sg_proc_leaf_arr[k];
2680 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2681 leaf->fops->owner = THIS_MODULE;
2682 leaf->fops->read = seq_read;
2683 leaf->fops->llseek = seq_lseek;
2684 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
2690 sg_proc_cleanup(void)
2693 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2697 for (k = 0; k < num_leaves; ++k)
2698 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2699 remove_proc_entry(sg_proc_sg_dirname, NULL);
2703 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2705 seq_printf(s, "%d\n", *((int *)s->private));
2709 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2711 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2715 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2716 size_t count, loff_t *off)
2721 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2723 num = (count < 10) ? count : 10;
2724 if (copy_from_user(buff, buffer, num))
2727 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2731 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2733 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2737 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2738 size_t count, loff_t *off)
2741 unsigned long k = ULONG_MAX;
2744 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2746 num = (count < 10) ? count : 10;
2747 if (copy_from_user(buff, buffer, num))
2750 k = simple_strtoul(buff, NULL, 10);
2751 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2758 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2760 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2765 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2767 return single_open(file, sg_proc_seq_show_version, NULL);
2770 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2772 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2777 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2779 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2782 struct sg_proc_deviter {
2787 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2789 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2796 it->max = sg_last_dev();
2797 if (it->index >= it->max)
2802 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2804 struct sg_proc_deviter * it = s->private;
2807 return (it->index < it->max) ? it : NULL;
2810 static void dev_seq_stop(struct seq_file *s, void *v)
2815 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2817 return seq_open(file, &dev_seq_ops);
2820 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2822 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2824 struct scsi_device *scsidp;
2826 sdp = it ? sg_get_dev(it->index) : NULL;
2827 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2828 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2829 scsidp->host->host_no, scsidp->channel,
2830 scsidp->id, scsidp->lun, (int) scsidp->type,
2832 (int) scsidp->queue_depth,
2833 (int) scsidp->device_busy,
2834 (int) scsi_device_online(scsidp));
2836 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2840 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2842 return seq_open(file, &devstrs_seq_ops);
2845 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2847 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2849 struct scsi_device *scsidp;
2851 sdp = it ? sg_get_dev(it->index) : NULL;
2852 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2853 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2854 scsidp->vendor, scsidp->model, scsidp->rev);
2856 seq_printf(s, "<no active device>\n");
2860 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2862 int k, m, new_interface, blen, usg;
2865 const sg_io_hdr_t *hp;
2869 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2870 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2871 "(res)sgat=%d low_dma=%d\n", k + 1,
2872 jiffies_to_msecs(fp->timeout),
2873 fp->reserve.bufflen,
2874 (int) fp->reserve.k_use_sg,
2876 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2877 (int) fp->cmd_q, (int) fp->force_packid,
2878 (int) fp->keep_orphan, (int) fp->closed);
2879 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2881 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2882 if (srp->res_used) {
2883 if (new_interface &&
2884 (SG_FLAG_MMAP_IO & hp->flags))
2889 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2895 blen = srp->data.bufflen;
2896 usg = srp->data.k_use_sg;
2897 seq_printf(s, srp->done ?
2898 ((1 == srp->done) ? "rcv:" : "fin:")
2900 seq_printf(s, " id=%d blen=%d",
2901 srp->header.pack_id, blen);
2903 seq_printf(s, " dur=%d", hp->duration);
2905 ms = jiffies_to_msecs(jiffies);
2906 seq_printf(s, " t_o/elap=%d/%d",
2907 (new_interface ? hp->timeout :
2908 jiffies_to_msecs(fp->timeout)),
2909 (ms > hp->duration ? ms - hp->duration : 0));
2911 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2912 (int) srp->data.cmd_opcode);
2915 seq_printf(s, " No requests active\n");
2919 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2921 return seq_open(file, &debug_seq_ops);
2924 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2926 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2929 if (it && (0 == it->index)) {
2930 seq_printf(s, "max_active_device=%d(origin 1)\n",
2932 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2934 sdp = it ? sg_get_dev(it->index) : NULL;
2936 struct scsi_device *scsidp = sdp->device;
2938 if (NULL == scsidp) {
2939 seq_printf(s, "device %d detached ??\n",
2944 if (sg_get_nth_sfp(sdp, 0)) {
2945 seq_printf(s, " >>> device=%s ",
2946 sdp->disk->disk_name);
2948 seq_printf(s, "detached pending close ");
2951 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2952 scsidp->host->host_no,
2953 scsidp->channel, scsidp->id,
2955 scsidp->host->hostt->emulated);
2956 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2957 sdp->sg_tablesize, sdp->exclude);
2959 sg_proc_debug_helper(s, sdp);
2964 #endif /* CONFIG_SCSI_PROC_FS */
2966 module_init(init_sg);
2967 module_exit(exit_sg);