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>
54 #include <scsi/scsi_dbg.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_driver.h>
57 #include <scsi/scsi_ioctl.h>
60 #include "scsi_logging.h"
62 #ifdef CONFIG_SCSI_PROC_FS
63 #include <linux/proc_fs.h>
64 static char *sg_version_date = "20061027";
66 static int sg_proc_init(void);
67 static void sg_proc_cleanup(void);
70 #define SG_ALLOW_DIO_DEF 0
71 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
73 #define SG_MAX_DEVS 32768
76 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
77 * Then when using 32 bit integers x * m may overflow during the calculation.
78 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
79 * calculates the same, but prevents the overflow when both m and d
80 * are "small" numbers (like HZ and USER_HZ).
81 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
84 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
86 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
88 int sg_big_buff = SG_DEF_RESERVED_SIZE;
89 /* N.B. This variable is readable and writeable via
90 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
91 of this size (or less if there is not enough memory) will be reserved
92 for use by this file descriptor. [Deprecated usage: this variable is also
93 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
94 the kernel (i.e. it is not a module).] */
95 static int def_reserved_size = -1; /* picks up init parameter */
96 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
98 static int scatter_elem_sz = SG_SCATTER_SZ;
99 static int scatter_elem_sz_prev = SG_SCATTER_SZ;
101 #define SG_SECTOR_SZ 512
102 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
104 static int sg_add(struct device *, struct class_interface *);
105 static void sg_remove(struct device *, struct class_interface *);
107 static DEFINE_IDR(sg_index_idr);
108 static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
109 file descriptor list for device */
111 static struct class_interface sg_interface = {
113 .remove_dev = sg_remove,
116 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
117 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
118 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
119 unsigned bufflen; /* Size of (aggregate) data buffer */
120 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
121 struct scatterlist *buffer;/* scatter list */
122 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
123 unsigned char cmd_opcode; /* first byte of command */
126 struct sg_device; /* forward declarations */
129 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
130 struct sg_request *nextrp; /* NULL -> tail request (slist) */
131 struct sg_fd *parentfp; /* NULL -> not in use */
132 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
133 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
134 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
135 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
136 char orphan; /* 1 -> drop on sight, 0 -> normal */
137 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
138 volatile char done; /* 0->before bh, 1->before read, 2->read */
141 typedef struct sg_fd { /* holds the state of a file descriptor */
142 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
143 struct sg_device *parentdp; /* owning device */
144 wait_queue_head_t read_wait; /* queue read until command done */
145 rwlock_t rq_list_lock; /* protect access to list in req_arr */
146 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
147 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
148 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
149 unsigned save_scat_len; /* original length of trunc. scat. element */
150 Sg_request *headrp; /* head of request slist, NULL->empty */
151 struct fasync_struct *async_qp; /* used by asynchronous notification */
152 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
153 char low_dma; /* as in parent but possibly overridden to 1 */
154 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
155 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
156 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
157 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
158 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
159 char mmap_called; /* 0 -> mmap() never called on this fd */
162 typedef struct sg_device { /* holds the state of each scsi generic device */
163 struct scsi_device *device;
164 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
165 int sg_tablesize; /* adapter's max scatter-gather table size */
166 u32 index; /* device index number */
167 Sg_fd *headfp; /* first open fd belonging to this device */
168 volatile char detached; /* 0->attached, 1->detached pending removal */
169 volatile char exclude; /* opened for exclusive access */
170 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
171 struct gendisk *disk;
172 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
175 static int sg_fasync(int fd, struct file *filp, int mode);
176 /* tasklet or soft irq callback */
177 static void sg_cmd_done(void *data, char *sense, int result, int resid);
178 static int sg_start_req(Sg_request * srp);
179 static void sg_finish_rem_req(Sg_request * srp);
180 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
181 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
183 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
185 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
186 int blocking, int read_only, Sg_request ** o_srp);
187 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
188 unsigned char *cmnd, int timeout, int blocking);
189 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
190 int wr_xf, int *countp, unsigned char __user **up);
191 static int sg_write_xfer(Sg_request * srp);
192 static int sg_read_xfer(Sg_request * srp);
193 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
194 static void sg_remove_scat(Sg_scatter_hold * schp);
195 static void sg_build_reserve(Sg_fd * sfp, int req_size);
196 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
197 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
198 static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
199 static void sg_page_free(struct page *page, int size);
200 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
201 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
203 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
204 static Sg_request *sg_add_request(Sg_fd * sfp);
205 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
206 static int sg_res_in_use(Sg_fd * sfp);
207 static int sg_allow_access(unsigned char opcode, char dev_type);
208 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
209 static Sg_device *sg_get_dev(int dev);
210 #ifdef CONFIG_SCSI_PROC_FS
211 static int sg_last_dev(void);
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)
220 sg_open(struct inode *inode, struct file *filp)
222 int dev = iminor(inode);
223 int flags = filp->f_flags;
224 struct request_queue *q;
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))
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);
244 if (!((flags & O_NONBLOCK) ||
245 scsi_block_when_processing_errors(sdp->device))) {
247 /* we are in error recovery for this device */
251 if (flags & O_EXCL) {
252 if (O_RDONLY == (flags & O_ACCMODE)) {
253 retval = -EPERM; /* Can't lock it with read only access */
256 if (sdp->headfp && (flags & O_NONBLOCK)) {
261 __wait_event_interruptible(sdp->o_excl_wait,
262 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
264 retval = res; /* -ERESTARTSYS because signal hit process */
267 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
268 if (flags & O_NONBLOCK) {
273 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
276 retval = res; /* -ERESTARTSYS because signal hit process */
284 if (!sdp->headfp) { /* no existing opens on this device */
286 q = sdp->device->request_queue;
287 sdp->sg_tablesize = min(q->max_hw_segments,
288 q->max_phys_segments);
290 if ((sfp = sg_add_sfp(sdp, dev)))
291 filp->private_data = sfp;
294 sdp->exclude = 0; /* undo if error */
301 scsi_device_put(sdp->device);
305 /* Following function was formerly called 'sg_close' */
307 sg_release(struct inode *inode, struct file *filp)
312 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
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);
321 wake_up_interruptible(&sdp->o_excl_wait);
327 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
332 int req_pack_id = -1;
334 struct sg_header *old_hdr = NULL;
337 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
339 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
340 sdp->disk->disk_name, (int) count));
342 if (!access_ok(VERIFY_WRITE, buf, count))
344 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
345 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
348 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
352 if (old_hdr->reply_len < 0) {
353 if (count >= SZ_SG_IO_HDR) {
354 sg_io_hdr_t *new_hdr;
355 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
360 retval =__copy_from_user
361 (new_hdr, buf, SZ_SG_IO_HDR);
362 req_pack_id = new_hdr->pack_id;
370 req_pack_id = old_hdr->pack_id;
372 srp = sg_get_rq_mark(sfp, req_pack_id);
373 if (!srp) { /* now wait on packet to arrive */
378 if (filp->f_flags & O_NONBLOCK) {
383 retval = 0; /* following macro beats race condition */
384 __wait_event_interruptible(sfp->read_wait,
386 (srp = sg_get_rq_mark(sfp, req_pack_id))),
395 /* -ERESTARTSYS as signal hit process */
399 if (srp->header.interface_id != '\0') {
400 retval = sg_new_read(sfp, buf, count, srp);
405 if (old_hdr == NULL) {
406 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
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 =
417 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
418 old_hdr->target_status = hp->masked_status;
419 old_hdr->host_status = hp->host_status;
420 old_hdr->driver_status = hp->driver_status;
421 if ((CHECK_CONDITION & hp->masked_status) ||
422 (DRIVER_SENSE & hp->driver_status))
423 memcpy(old_hdr->sense_buffer, srp->sense_b,
424 sizeof (old_hdr->sense_buffer));
425 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 */
429 case DID_PASSTHROUGH:
436 old_hdr->result = EBUSY;
443 old_hdr->result = EIO;
446 old_hdr->result = (srp->sense_b[0] == 0 &&
447 hp->masked_status == GOOD) ? 0 : EIO;
450 old_hdr->result = EIO;
454 /* Now copy the result back to the user buffer. */
455 if (count >= SZ_SG_HEADER) {
456 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
461 if (count > old_hdr->reply_len)
462 count = old_hdr->reply_len;
463 if (count > SZ_SG_HEADER) {
464 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
470 count = (old_hdr->result == 0) ? 0 : -EIO;
471 sg_finish_rem_req(srp);
479 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
481 sg_io_hdr_t *hp = &srp->header;
485 if (count < SZ_SG_IO_HDR) {
490 if ((hp->mx_sb_len > 0) && hp->sbp) {
491 if ((CHECK_CONDITION & hp->masked_status) ||
492 (DRIVER_SENSE & hp->driver_status)) {
493 int sb_len = SCSI_SENSE_BUFFERSIZE;
494 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)) {
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)) {
510 err = sg_read_xfer(srp);
512 sg_finish_rem_req(srp);
513 return (0 == err) ? count : err;
517 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
519 int mxsize, cmd_size, k;
520 int input_size, blocking;
521 unsigned char opcode;
525 struct sg_header old_hdr;
527 unsigned char cmnd[MAX_COMMAND_SIZE];
529 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
531 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
532 sdp->disk->disk_name, (int) count));
535 if (!((filp->f_flags & O_NONBLOCK) ||
536 scsi_block_when_processing_errors(sdp->device)))
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)
543 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
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. */
551 if (!(srp = sg_add_request(sfp))) {
552 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
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);
564 cmd_size = sfp->next_cmd_len;
565 sfp->next_cmd_len = 0; /* reset so only this write() effected */
567 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
568 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
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. */
583 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
584 hp->cmd_len = (unsigned char) cmd_size;
588 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
589 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
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;
595 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
596 hp->flags = input_size; /* structure abuse ... */
597 hp->pack_id = old_hdr.pack_id;
599 if (__copy_from_user(cmnd, buf, cmd_size))
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.
606 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
607 static char cmd[TASK_COMM_LEN];
608 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
610 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
611 "guessing data in;\n" KERN_WARNING " "
612 "program %s not setting count and/or reply_len properly\n",
613 old_hdr.reply_len - (int)SZ_SG_HEADER,
614 input_size, (unsigned int) cmnd[0],
616 strcpy(cmd, current->comm);
619 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
620 return (k < 0) ? k : count;
624 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
625 int blocking, int read_only, Sg_request ** o_srp)
630 unsigned char cmnd[MAX_COMMAND_SIZE];
632 unsigned long ul_timeout;
634 if (count < SZ_SG_IO_HDR)
636 if (!access_ok(VERIFY_READ, buf, count))
637 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
639 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
640 if (!(srp = sg_add_request(sfp))) {
641 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
645 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
646 sg_remove_request(sfp, srp);
649 if (hp->interface_id != 'S') {
650 sg_remove_request(sfp, srp);
653 if (hp->flags & SG_FLAG_MMAP_IO) {
654 if (hp->dxfer_len > sfp->reserve.bufflen) {
655 sg_remove_request(sfp, srp);
656 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
658 if (hp->flags & SG_FLAG_DIRECT_IO) {
659 sg_remove_request(sfp, srp);
660 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
662 if (sg_res_in_use(sfp)) {
663 sg_remove_request(sfp, srp);
664 return -EBUSY; /* reserve buffer already being used */
667 ul_timeout = msecs_to_jiffies(srp->header.timeout);
668 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
669 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
670 sg_remove_request(sfp, srp);
673 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
674 sg_remove_request(sfp, srp);
675 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
677 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
678 sg_remove_request(sfp, srp);
682 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
683 sg_remove_request(sfp, srp);
686 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
695 sg_common_write(Sg_fd * sfp, Sg_request * srp,
696 unsigned char *cmnd, int timeout, int blocking)
699 Sg_device *sdp = sfp->parentdp;
700 sg_io_hdr_t *hp = &srp->header;
702 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
704 hp->masked_status = 0;
708 hp->driver_status = 0;
710 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
711 (int) cmnd[0], (int) hp->cmd_len));
713 if ((k = sg_start_req(srp))) {
714 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
715 sg_finish_rem_req(srp);
716 return k; /* probably out of space --> ENOMEM */
718 if ((k = sg_write_xfer(srp))) {
719 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
720 sg_finish_rem_req(srp);
724 sg_finish_rem_req(srp);
728 switch (hp->dxfer_direction) {
729 case SG_DXFER_TO_FROM_DEV:
730 case SG_DXFER_FROM_DEV:
731 data_dir = DMA_FROM_DEVICE;
733 case SG_DXFER_TO_DEV:
734 data_dir = DMA_TO_DEVICE;
736 case SG_DXFER_UNKNOWN:
737 data_dir = DMA_BIDIRECTIONAL;
743 hp->duration = jiffies_to_msecs(jiffies);
744 /* Now send everything of to mid-level. The next time we hear about this
745 packet is when sg_cmd_done() is called (i.e. a callback). */
746 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
747 hp->dxfer_len, srp->data.k_use_sg, timeout,
748 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
750 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
752 * most likely out of mem, but could also be a bad map
754 sg_finish_rem_req(srp);
761 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
763 unsigned long iflags;
766 read_lock_irqsave(&sfp->rq_list_lock, iflags);
768 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
773 sg_ioctl(struct inode *inode, struct file *filp,
774 unsigned int cmd_in, unsigned long arg)
776 void __user *p = (void __user *)arg;
778 int result, val, read_only;
782 unsigned long iflags;
784 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
786 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
787 sdp->disk->disk_name, (int) cmd_in));
788 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
793 int blocking = 1; /* ignore O_NONBLOCK flag */
797 if (!scsi_block_when_processing_errors(sdp->device))
799 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
802 sg_new_write(sfp, p, SZ_SG_IO_HDR,
803 blocking, read_only, &srp);
806 srp->sg_io_owned = 1;
808 result = 0; /* following macro to beat race condition */
809 __wait_event_interruptible(sfp->read_wait,
810 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
815 return 0; /* request packet dropped already */
819 return result; /* -ERESTARTSYS because signal hit process */
821 write_lock_irqsave(&sfp->rq_list_lock, iflags);
823 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
824 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
825 return (result < 0) ? result : 0;
828 result = get_user(val, ip);
833 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
834 val = MULDIV (INT_MAX, USER_HZ, HZ);
835 sfp->timeout_user = val;
836 sfp->timeout = MULDIV (val, HZ, USER_HZ);
839 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
840 /* strange ..., for backward compatibility */
841 return sfp->timeout_user;
842 case SG_SET_FORCE_LOW_DMA:
843 result = get_user(val, ip);
848 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
849 val = (int) sfp->reserve.bufflen;
850 sg_remove_scat(&sfp->reserve);
851 sg_build_reserve(sfp, val);
856 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
860 return put_user((int) sfp->low_dma, ip);
862 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
865 sg_scsi_id_t __user *sg_idp = p;
869 __put_user((int) sdp->device->host->host_no,
871 __put_user((int) sdp->device->channel,
873 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
874 __put_user((int) sdp->device->lun, &sg_idp->lun);
875 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
876 __put_user((short) sdp->device->host->cmd_per_lun,
877 &sg_idp->h_cmd_per_lun);
878 __put_user((short) sdp->device->queue_depth,
879 &sg_idp->d_queue_depth);
880 __put_user(0, &sg_idp->unused[0]);
881 __put_user(0, &sg_idp->unused[1]);
884 case SG_SET_FORCE_PACK_ID:
885 result = get_user(val, ip);
888 sfp->force_packid = val ? 1 : 0;
891 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
893 read_lock_irqsave(&sfp->rq_list_lock, iflags);
894 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
895 if ((1 == srp->done) && (!srp->sg_io_owned)) {
896 read_unlock_irqrestore(&sfp->rq_list_lock,
898 __put_user(srp->header.pack_id, ip);
902 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
905 case SG_GET_NUM_WAITING:
906 read_lock_irqsave(&sfp->rq_list_lock, iflags);
907 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
908 if ((1 == srp->done) && (!srp->sg_io_owned))
911 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
912 return put_user(val, ip);
913 case SG_GET_SG_TABLESIZE:
914 return put_user(sdp->sg_tablesize, ip);
915 case SG_SET_RESERVED_SIZE:
916 result = get_user(val, ip);
921 val = min_t(int, val,
922 sdp->device->request_queue->max_sectors * 512);
923 if (val != sfp->reserve.bufflen) {
924 if (sg_res_in_use(sfp) || sfp->mmap_called)
926 sg_remove_scat(&sfp->reserve);
927 sg_build_reserve(sfp, val);
930 case SG_GET_RESERVED_SIZE:
931 val = min_t(int, sfp->reserve.bufflen,
932 sdp->device->request_queue->max_sectors * 512);
933 return put_user(val, ip);
934 case SG_SET_COMMAND_Q:
935 result = get_user(val, ip);
938 sfp->cmd_q = val ? 1 : 0;
940 case SG_GET_COMMAND_Q:
941 return put_user((int) sfp->cmd_q, ip);
942 case SG_SET_KEEP_ORPHAN:
943 result = get_user(val, ip);
946 sfp->keep_orphan = val;
948 case SG_GET_KEEP_ORPHAN:
949 return put_user((int) sfp->keep_orphan, ip);
950 case SG_NEXT_CMD_LEN:
951 result = get_user(val, ip);
954 sfp->next_cmd_len = (val > 0) ? val : 0;
956 case SG_GET_VERSION_NUM:
957 return put_user(sg_version_num, ip);
958 case SG_GET_ACCESS_COUNT:
959 /* faked - we don't have a real access count anymore */
960 val = (sdp->device ? 1 : 0);
961 return put_user(val, ip);
962 case SG_GET_REQUEST_TABLE:
963 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
966 sg_req_info_t *rinfo;
969 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
973 read_lock_irqsave(&sfp->rq_list_lock, iflags);
974 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
975 ++val, srp = srp ? srp->nextrp : srp) {
976 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
978 rinfo[val].req_state = srp->done + 1;
980 srp->header.masked_status &
981 srp->header.host_status &
982 srp->header.driver_status;
984 rinfo[val].duration =
985 srp->header.duration;
987 ms = jiffies_to_msecs(jiffies);
988 rinfo[val].duration =
989 (ms > srp->header.duration) ?
990 (ms - srp->header.duration) : 0;
992 rinfo[val].orphan = srp->orphan;
993 rinfo[val].sg_io_owned =
1001 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1002 result = __copy_to_user(p, rinfo,
1003 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1004 result = result ? -EFAULT : 0;
1008 case SG_EMULATED_HOST:
1011 return put_user(sdp->device->host->hostt->emulated, ip);
1015 if (filp->f_flags & O_NONBLOCK) {
1016 if (scsi_host_in_recovery(sdp->device->host))
1018 } else if (!scsi_block_when_processing_errors(sdp->device))
1020 result = get_user(val, ip);
1023 if (SG_SCSI_RESET_NOTHING == val)
1026 case SG_SCSI_RESET_DEVICE:
1027 val = SCSI_TRY_RESET_DEVICE;
1029 case SG_SCSI_RESET_BUS:
1030 val = SCSI_TRY_RESET_BUS;
1032 case SG_SCSI_RESET_HOST:
1033 val = SCSI_TRY_RESET_HOST;
1038 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1040 return (scsi_reset_provider(sdp->device, val) ==
1041 SUCCESS) ? 0 : -EIO;
1042 case SCSI_IOCTL_SEND_COMMAND:
1046 unsigned char opcode = WRITE_6;
1047 Scsi_Ioctl_Command __user *siocp = p;
1049 if (copy_from_user(&opcode, siocp->data, 1))
1051 if (!sg_allow_access(opcode, sdp->device->type))
1054 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
1056 result = get_user(val, ip);
1059 sdp->sgdebug = (char) val;
1061 case SCSI_IOCTL_GET_IDLUN:
1062 case SCSI_IOCTL_GET_BUS_NUMBER:
1063 case SCSI_IOCTL_PROBE_HOST:
1064 case SG_GET_TRANSFORM:
1067 return scsi_ioctl(sdp->device, cmd_in, p);
1069 return put_user(sdp->device->request_queue->max_sectors * 512,
1072 return blk_trace_setup(sdp->device->request_queue,
1073 sdp->disk->disk_name,
1074 sdp->device->sdev_gendev.devt,
1077 return blk_trace_startstop(sdp->device->request_queue, 1);
1079 return blk_trace_startstop(sdp->device->request_queue, 0);
1080 case BLKTRACETEARDOWN:
1081 return blk_trace_remove(sdp->device->request_queue);
1084 return -EPERM; /* don't know so take safe approach */
1085 return scsi_ioctl(sdp->device, cmd_in, p);
1089 #ifdef CONFIG_COMPAT
1090 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1094 struct scsi_device *sdev;
1096 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1100 if (sdev->host->hostt->compat_ioctl) {
1103 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1108 return -ENOIOCTLCMD;
1113 sg_poll(struct file *filp, poll_table * wait)
1115 unsigned int res = 0;
1120 unsigned long iflags;
1122 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1125 poll_wait(filp, &sfp->read_wait, wait);
1126 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1127 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1128 /* if any read waiting, flag it */
1129 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1130 res = POLLIN | POLLRDNORM;
1133 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1137 else if (!sfp->cmd_q) {
1139 res |= POLLOUT | POLLWRNORM;
1140 } else if (count < SG_MAX_QUEUE)
1141 res |= POLLOUT | POLLWRNORM;
1142 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1143 sdp->disk->disk_name, (int) res));
1148 sg_fasync(int fd, struct file *filp, int mode)
1154 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1156 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1157 sdp->disk->disk_name, mode));
1159 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1160 return (retval < 0) ? retval : 0;
1164 sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1167 unsigned long offset, len, sa;
1168 Sg_scatter_hold *rsv_schp;
1169 struct scatterlist *sg;
1172 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1173 return VM_FAULT_SIGBUS;
1174 rsv_schp = &sfp->reserve;
1175 offset = vmf->pgoff << PAGE_SHIFT;
1176 if (offset >= rsv_schp->bufflen)
1177 return VM_FAULT_SIGBUS;
1178 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
1179 offset, rsv_schp->k_use_sg));
1180 sg = rsv_schp->buffer;
1182 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1183 ++k, sg = sg_next(sg)) {
1184 len = vma->vm_end - sa;
1185 len = (len < sg->length) ? len : sg->length;
1188 page = virt_to_page(page_address(sg_page(sg)) + offset);
1189 get_page(page); /* increment page count */
1191 return 0; /* success */
1197 return VM_FAULT_SIGBUS;
1200 static struct vm_operations_struct sg_mmap_vm_ops = {
1201 .fault = sg_vma_fault,
1205 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1208 unsigned long req_sz, len, sa;
1209 Sg_scatter_hold *rsv_schp;
1211 struct scatterlist *sg;
1213 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1215 req_sz = vma->vm_end - vma->vm_start;
1216 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1217 (void *) vma->vm_start, (int) req_sz));
1219 return -EINVAL; /* want no offset */
1220 rsv_schp = &sfp->reserve;
1221 if (req_sz > rsv_schp->bufflen)
1222 return -ENOMEM; /* cannot map more than reserved buffer */
1225 sg = rsv_schp->buffer;
1226 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1227 ++k, sg = sg_next(sg)) {
1228 len = vma->vm_end - sa;
1229 len = (len < sg->length) ? len : sg->length;
1233 sfp->mmap_called = 1;
1234 vma->vm_flags |= VM_RESERVED;
1235 vma->vm_private_data = sfp;
1236 vma->vm_ops = &sg_mmap_vm_ops;
1240 /* This function is a "bottom half" handler that is called by the
1241 * mid level when a command is completed (or has failed). */
1243 sg_cmd_done(void *data, char *sense, int result, int resid)
1245 Sg_request *srp = data;
1246 Sg_device *sdp = NULL;
1248 unsigned long iflags;
1252 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1255 sfp = srp->parentfp;
1257 sdp = sfp->parentdp;
1258 if ((NULL == sdp) || sdp->detached) {
1259 printk(KERN_INFO "sg_cmd_done: device detached\n");
1264 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1265 sdp->disk->disk_name, srp->header.pack_id, result));
1266 srp->header.resid = resid;
1267 ms = jiffies_to_msecs(jiffies);
1268 srp->header.duration = (ms > srp->header.duration) ?
1269 (ms - srp->header.duration) : 0;
1271 struct scsi_sense_hdr sshdr;
1273 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1274 srp->header.status = 0xff & result;
1275 srp->header.masked_status = status_byte(result);
1276 srp->header.msg_status = msg_byte(result);
1277 srp->header.host_status = host_byte(result);
1278 srp->header.driver_status = driver_byte(result);
1279 if ((sdp->sgdebug > 0) &&
1280 ((CHECK_CONDITION == srp->header.masked_status) ||
1281 (COMMAND_TERMINATED == srp->header.masked_status)))
1282 __scsi_print_sense("sg_cmd_done", sense,
1283 SCSI_SENSE_BUFFERSIZE);
1285 /* Following if statement is a patch supplied by Eric Youngdale */
1286 if (driver_byte(result) != 0
1287 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1288 && !scsi_sense_is_deferred(&sshdr)
1289 && sshdr.sense_key == UNIT_ATTENTION
1290 && sdp->device->removable) {
1291 /* Detected possible disc change. Set the bit - this */
1292 /* may be used if there are filesystems using this device */
1293 sdp->device->changed = 1;
1296 /* Rely on write phase to clean out srp status values, so no "else" */
1298 if (sfp->closed) { /* whoops this fd already released, cleanup */
1299 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1300 sg_finish_rem_req(srp);
1302 if (NULL == sfp->headrp) {
1303 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1304 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1305 scsi_device_put(sdp->device);
1309 } else if (srp && srp->orphan) {
1310 if (sfp->keep_orphan)
1311 srp->sg_io_owned = 0;
1313 sg_finish_rem_req(srp);
1318 /* Now wake up any sg_read() that is waiting for this packet. */
1319 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1320 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1322 wake_up_interruptible(&sfp->read_wait);
1323 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1327 static struct file_operations sg_fops = {
1328 .owner = THIS_MODULE,
1333 #ifdef CONFIG_COMPAT
1334 .compat_ioctl = sg_compat_ioctl,
1338 .release = sg_release,
1339 .fasync = sg_fasync,
1342 static struct class *sg_sysfs_class;
1344 static int sg_sysfs_valid = 0;
1346 static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1348 struct request_queue *q = scsidp->request_queue;
1350 unsigned long iflags;
1354 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1356 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1357 return ERR_PTR(-ENOMEM);
1360 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1361 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1365 write_lock_irqsave(&sg_index_lock, iflags);
1366 error = idr_get_new(&sg_index_idr, sdp, &k);
1367 write_unlock_irqrestore(&sg_index_lock, iflags);
1370 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1375 if (unlikely(k >= SG_MAX_DEVS))
1378 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1379 sprintf(disk->disk_name, "sg%d", k);
1380 disk->first_minor = k;
1382 sdp->device = scsidp;
1383 init_waitqueue_head(&sdp->o_excl_wait);
1384 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1391 return ERR_PTR(error);
1396 sdev_printk(KERN_WARNING, scsidp,
1397 "Unable to attach sg device type=%d, minor "
1398 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1404 sg_add(struct device *cl_dev, struct class_interface *cl_intf)
1406 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1407 struct gendisk *disk;
1408 Sg_device *sdp = NULL;
1409 struct cdev * cdev = NULL;
1411 unsigned long iflags;
1413 disk = alloc_disk(1);
1415 printk(KERN_WARNING "alloc_disk failed\n");
1418 disk->major = SCSI_GENERIC_MAJOR;
1421 cdev = cdev_alloc();
1423 printk(KERN_WARNING "cdev_alloc failed\n");
1426 cdev->owner = THIS_MODULE;
1427 cdev->ops = &sg_fops;
1429 sdp = sg_alloc(disk, scsidp);
1431 printk(KERN_WARNING "sg_alloc failed\n");
1432 error = PTR_ERR(sdp);
1436 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
1441 if (sg_sysfs_valid) {
1442 struct device *sg_class_member;
1444 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1445 MKDEV(SCSI_GENERIC_MAJOR,
1447 "%s", disk->disk_name);
1448 if (IS_ERR(sg_class_member)) {
1449 printk(KERN_ERR "sg_add: "
1450 "device_create failed\n");
1451 error = PTR_ERR(sg_class_member);
1454 dev_set_drvdata(sg_class_member, sdp);
1455 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1456 &sg_class_member->kobj, "generic");
1458 printk(KERN_ERR "sg_add: unable to make symlink "
1459 "'generic' back to sg%d\n", sdp->index);
1461 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
1463 sdev_printk(KERN_NOTICE, scsidp,
1464 "Attached scsi generic sg%d type %d\n", sdp->index,
1467 dev_set_drvdata(cl_dev, sdp);
1472 write_lock_irqsave(&sg_index_lock, iflags);
1473 idr_remove(&sg_index_idr, sdp->index);
1474 write_unlock_irqrestore(&sg_index_lock, iflags);
1485 sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
1487 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1488 Sg_device *sdp = dev_get_drvdata(cl_dev);
1489 unsigned long iflags;
1500 write_lock_irqsave(&sg_index_lock, iflags);
1503 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1505 for (srp = sfp->headrp; srp; srp = tsrp) {
1507 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1508 sg_finish_rem_req(srp);
1511 scsi_device_put(sdp->device);
1512 __sg_remove_sfp(sdp, sfp);
1515 wake_up_interruptible(&sfp->read_wait);
1516 kill_fasync(&sfp->async_qp, SIGPOLL,
1520 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1521 if (NULL == sdp->headfp) {
1522 idr_remove(&sg_index_idr, sdp->index);
1524 } else { /* nothing active, simple case */
1525 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1526 idr_remove(&sg_index_idr, sdp->index);
1528 write_unlock_irqrestore(&sg_index_lock, iflags);
1530 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1531 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1532 cdev_del(sdp->cdev);
1534 put_disk(sdp->disk);
1536 if (NULL == sdp->headfp)
1540 msleep(10); /* dirty detach so delay device destruction */
1543 module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1544 module_param_named(def_reserved_size, def_reserved_size, int,
1546 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1548 MODULE_AUTHOR("Douglas Gilbert");
1549 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1550 MODULE_LICENSE("GPL");
1551 MODULE_VERSION(SG_VERSION_STR);
1552 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
1554 MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1555 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1556 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1557 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1564 if (scatter_elem_sz < PAGE_SIZE) {
1565 scatter_elem_sz = PAGE_SIZE;
1566 scatter_elem_sz_prev = scatter_elem_sz;
1568 if (def_reserved_size >= 0)
1569 sg_big_buff = def_reserved_size;
1571 def_reserved_size = sg_big_buff;
1573 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1577 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1578 if ( IS_ERR(sg_sysfs_class) ) {
1579 rc = PTR_ERR(sg_sysfs_class);
1583 rc = scsi_register_interface(&sg_interface);
1585 #ifdef CONFIG_SCSI_PROC_FS
1587 #endif /* CONFIG_SCSI_PROC_FS */
1590 class_destroy(sg_sysfs_class);
1592 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1599 #ifdef CONFIG_SCSI_PROC_FS
1601 #endif /* CONFIG_SCSI_PROC_FS */
1602 scsi_unregister_interface(&sg_interface);
1603 class_destroy(sg_sysfs_class);
1605 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1607 idr_destroy(&sg_index_idr);
1611 sg_start_req(Sg_request * srp)
1614 Sg_fd *sfp = srp->parentfp;
1615 sg_io_hdr_t *hp = &srp->header;
1616 int dxfer_len = (int) hp->dxfer_len;
1617 int dxfer_dir = hp->dxfer_direction;
1618 Sg_scatter_hold *req_schp = &srp->data;
1619 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1621 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1622 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1624 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1625 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1626 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1627 res = sg_build_direct(srp, sfp, dxfer_len);
1628 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1631 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1632 sg_link_reserve(sfp, srp, dxfer_len);
1634 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1636 sg_remove_scat(req_schp);
1644 sg_finish_rem_req(Sg_request * srp)
1646 Sg_fd *sfp = srp->parentfp;
1647 Sg_scatter_hold *req_schp = &srp->data;
1649 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1651 sg_unlink_reserve(sfp, srp);
1653 sg_remove_scat(req_schp);
1654 sg_remove_request(sfp, srp);
1658 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1660 int sg_bufflen = tablesize * sizeof(struct scatterlist);
1661 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1664 * TODO: test without low_dma, we should not need it since
1665 * the block layer will bounce the buffer for us
1667 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1670 gfp_flags |= GFP_DMA;
1671 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
1674 sg_init_table(schp->buffer, tablesize);
1675 schp->sglist_len = sg_bufflen;
1676 return tablesize; /* number of scat_gath elements allocated */
1679 #ifdef SG_ALLOW_DIO_CODE
1680 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1681 /* TODO: hopefully we can use the generic block layer code */
1683 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1684 - mapping of all pages not successful
1685 (i.e., either completely successful or fails)
1688 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1689 unsigned long uaddr, size_t count, int rw)
1691 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1692 unsigned long start = uaddr >> PAGE_SHIFT;
1693 const int nr_pages = end - start;
1695 struct page **pages;
1697 /* User attempted Overflow! */
1698 if ((uaddr + count) < uaddr)
1702 if (nr_pages > max_pages)
1709 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1712 /* Try to fault in all of the necessary pages */
1713 down_read(¤t->mm->mmap_sem);
1714 /* rw==READ means read from drive, write into memory area */
1715 res = get_user_pages(
1721 0, /* don't force */
1724 up_read(¤t->mm->mmap_sem);
1726 /* Errors and no page mapped should return here */
1730 for (i=0; i < nr_pages; i++) {
1731 /* FIXME: flush superflous for rw==READ,
1732 * probably wrong function for rw==WRITE
1734 flush_dcache_page(pages[i]);
1735 /* ?? Is locking needed? I don't think so */
1736 /* if (TestSetPageLocked(pages[i]))
1740 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
1742 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1743 count -= sgl[0].length;
1744 for (i=1; i < nr_pages ; i++)
1745 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
1748 sgl[0].length = count;
1756 for (j=0; j < res; j++)
1757 page_cache_release(pages[j]);
1765 /* And unmap them... */
1767 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1772 for (i=0; i < nr_pages; i++) {
1773 struct page *page = sg_page(&sgl[i]);
1777 /* unlock_page(page); */
1778 /* FIXME: cache flush missing for rw==READ
1779 * FIXME: call the correct reference counting function
1781 page_cache_release(page);
1787 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1791 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1793 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1795 #ifdef SG_ALLOW_DIO_CODE
1796 sg_io_hdr_t *hp = &srp->header;
1797 Sg_scatter_hold *schp = &srp->data;
1798 int sg_tablesize = sfp->parentdp->sg_tablesize;
1799 int mx_sc_elems, res;
1800 struct scsi_device *sdev = sfp->parentdp->device;
1802 if (((unsigned long)hp->dxferp &
1803 queue_dma_alignment(sdev->request_queue)) != 0)
1806 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1807 if (mx_sc_elems <= 0) {
1810 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1811 (unsigned long)hp->dxferp, dxfer_len,
1812 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
1814 sg_remove_scat(schp);
1817 schp->k_use_sg = res;
1818 schp->dio_in_use = 1;
1819 hp->info |= SG_INFO_DIRECT_IO;
1827 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1829 struct scatterlist *sg;
1830 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1831 int sg_tablesize = sfp->parentdp->sg_tablesize;
1832 int blk_size = buff_size;
1833 struct page *p = NULL;
1838 ++blk_size; /* don't know why */
1839 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1840 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1841 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1842 buff_size, blk_size));
1844 /* N.B. ret_sz carried into this block ... */
1845 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1846 if (mx_sc_elems < 0)
1847 return mx_sc_elems; /* most likely -ENOMEM */
1849 num = scatter_elem_sz;
1850 if (unlikely(num != scatter_elem_sz_prev)) {
1851 if (num < PAGE_SIZE) {
1852 scatter_elem_sz = PAGE_SIZE;
1853 scatter_elem_sz_prev = PAGE_SIZE;
1855 scatter_elem_sz_prev = num;
1857 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1858 (rem_sz > 0) && (k < mx_sc_elems);
1859 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
1861 num = (rem_sz > scatter_elem_sz_prev) ?
1862 scatter_elem_sz_prev : rem_sz;
1863 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1867 if (num == scatter_elem_sz_prev) {
1868 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1869 scatter_elem_sz = ret_sz;
1870 scatter_elem_sz_prev = ret_sz;
1873 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
1875 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1876 "ret_sz=%d\n", k, num, ret_sz));
1877 } /* end of for loop */
1880 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1881 "rem_sz=%d\n", k, rem_sz));
1883 schp->bufflen = blk_size;
1884 if (rem_sz > 0) /* must have failed */
1891 sg_write_xfer(Sg_request * srp)
1893 sg_io_hdr_t *hp = &srp->header;
1894 Sg_scatter_hold *schp = &srp->data;
1895 struct scatterlist *sg = schp->buffer;
1897 int j, k, onum, usglen, ksglen, res;
1898 int iovec_count = (int) hp->iovec_count;
1899 int dxfer_dir = hp->dxfer_direction;
1901 unsigned char __user *up;
1902 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1904 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1905 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1906 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1907 if (schp->bufflen < num_xfer)
1908 num_xfer = schp->bufflen;
1910 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1912 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1915 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1916 num_xfer, iovec_count, schp->k_use_sg));
1919 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1924 ksglen = sg->length;
1925 p = page_address(sg_page(sg));
1926 for (j = 0, k = 0; j < onum; ++j) {
1927 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1931 for (; p; sg = sg_next(sg), ksglen = sg->length,
1932 p = page_address(sg_page(sg))) {
1935 if (ksglen > usglen) {
1936 if (usglen >= num_xfer) {
1937 if (__copy_from_user(p, up, num_xfer))
1941 if (__copy_from_user(p, up, usglen))
1947 if (ksglen >= num_xfer) {
1948 if (__copy_from_user(p, up, num_xfer))
1952 if (__copy_from_user(p, up, ksglen))
1958 if (k >= schp->k_use_sg)
1967 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1968 int wr_xf, int *countp, unsigned char __user **up)
1970 int num_xfer = (int) hp->dxfer_len;
1971 unsigned char __user *p = hp->dxferp;
1975 if (wr_xf && ('\0' == hp->interface_id))
1976 count = (int) hp->flags; /* holds "old" input_size */
1981 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1984 count = (int) iovec.iov_len;
1986 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1996 sg_remove_scat(Sg_scatter_hold * schp)
1998 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1999 if (schp->buffer && (schp->sglist_len > 0)) {
2000 struct scatterlist *sg = schp->buffer;
2002 if (schp->dio_in_use) {
2003 #ifdef SG_ALLOW_DIO_CODE
2004 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
2009 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
2010 ++k, sg = sg_next(sg)) {
2011 SCSI_LOG_TIMEOUT(5, printk(
2012 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
2013 k, sg_page(sg), sg->length));
2014 sg_page_free(sg_page(sg), sg->length);
2017 kfree(schp->buffer);
2019 memset(schp, 0, sizeof (*schp));
2023 sg_read_xfer(Sg_request * srp)
2025 sg_io_hdr_t *hp = &srp->header;
2026 Sg_scatter_hold *schp = &srp->data;
2027 struct scatterlist *sg = schp->buffer;
2029 int j, k, onum, usglen, ksglen, res;
2030 int iovec_count = (int) hp->iovec_count;
2031 int dxfer_dir = hp->dxfer_direction;
2033 unsigned char __user *up;
2034 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2036 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2037 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2038 num_xfer = hp->dxfer_len;
2039 if (schp->bufflen < num_xfer)
2040 num_xfer = schp->bufflen;
2042 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2044 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2047 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2048 num_xfer, iovec_count, schp->k_use_sg));
2051 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2056 p = page_address(sg_page(sg));
2057 ksglen = sg->length;
2058 for (j = 0, k = 0; j < onum; ++j) {
2059 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2063 for (; p; sg = sg_next(sg), ksglen = sg->length,
2064 p = page_address(sg_page(sg))) {
2067 if (ksglen > usglen) {
2068 if (usglen >= num_xfer) {
2069 if (__copy_to_user(up, p, num_xfer))
2073 if (__copy_to_user(up, p, usglen))
2079 if (ksglen >= num_xfer) {
2080 if (__copy_to_user(up, p, num_xfer))
2084 if (__copy_to_user(up, p, ksglen))
2090 if (k >= schp->k_use_sg)
2099 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2101 Sg_scatter_hold *schp = &srp->data;
2102 struct scatterlist *sg = schp->buffer;
2105 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2107 if ((!outp) || (num_read_xfer <= 0))
2110 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
2112 if (num > num_read_xfer) {
2113 if (__copy_to_user(outp, page_address(sg_page(sg)),
2118 if (__copy_to_user(outp, page_address(sg_page(sg)),
2121 num_read_xfer -= num;
2122 if (num_read_xfer <= 0)
2132 sg_build_reserve(Sg_fd * sfp, int req_size)
2134 Sg_scatter_hold *schp = &sfp->reserve;
2136 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2138 if (req_size < PAGE_SIZE)
2139 req_size = PAGE_SIZE;
2140 if (0 == sg_build_indirect(schp, sfp, req_size))
2143 sg_remove_scat(schp);
2144 req_size >>= 1; /* divide by 2 */
2145 } while (req_size > (PAGE_SIZE / 2));
2149 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2151 Sg_scatter_hold *req_schp = &srp->data;
2152 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2153 struct scatterlist *sg = rsv_schp->buffer;
2157 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2160 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
2163 sfp->save_scat_len = num;
2165 req_schp->k_use_sg = k + 1;
2166 req_schp->sglist_len = rsv_schp->sglist_len;
2167 req_schp->buffer = rsv_schp->buffer;
2169 req_schp->bufflen = size;
2170 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2176 if (k >= rsv_schp->k_use_sg)
2177 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2181 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2183 Sg_scatter_hold *req_schp = &srp->data;
2184 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2186 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2187 (int) req_schp->k_use_sg));
2188 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2189 struct scatterlist *sg = rsv_schp->buffer;
2191 if (sfp->save_scat_len > 0)
2192 (sg + (req_schp->k_use_sg - 1))->length =
2193 (unsigned) sfp->save_scat_len;
2195 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2197 req_schp->k_use_sg = 0;
2198 req_schp->bufflen = 0;
2199 req_schp->buffer = NULL;
2200 req_schp->sglist_len = 0;
2201 sfp->save_scat_len = 0;
2206 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2209 unsigned long iflags;
2211 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2212 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2213 /* look for requests that are ready + not SG_IO owned */
2214 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2215 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2216 resp->done = 2; /* guard against other readers */
2220 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2224 #ifdef CONFIG_SCSI_PROC_FS
2226 sg_get_nth_request(Sg_fd * sfp, int nth)
2229 unsigned long iflags;
2232 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2233 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2234 ++k, resp = resp->nextrp) ;
2235 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2240 /* always adds to end of list */
2242 sg_add_request(Sg_fd * sfp)
2245 unsigned long iflags;
2247 Sg_request *rp = sfp->req_arr;
2249 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2252 memset(rp, 0, sizeof (Sg_request));
2257 if (0 == sfp->cmd_q)
2258 resp = NULL; /* command queuing disallowed */
2260 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2264 if (k < SG_MAX_QUEUE) {
2265 memset(rp, 0, sizeof (Sg_request));
2267 while (resp->nextrp)
2268 resp = resp->nextrp;
2276 resp->nextrp = NULL;
2277 resp->header.duration = jiffies_to_msecs(jiffies);
2279 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2283 /* Return of 1 for found; 0 for not found */
2285 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2287 Sg_request *prev_rp;
2289 unsigned long iflags;
2292 if ((!sfp) || (!srp) || (!sfp->headrp))
2294 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2295 prev_rp = sfp->headrp;
2296 if (srp == prev_rp) {
2297 sfp->headrp = prev_rp->nextrp;
2298 prev_rp->parentfp = NULL;
2301 while ((rp = prev_rp->nextrp)) {
2303 prev_rp->nextrp = rp->nextrp;
2304 rp->parentfp = NULL;
2311 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2315 #ifdef CONFIG_SCSI_PROC_FS
2317 sg_get_nth_sfp(Sg_device * sdp, int nth)
2320 unsigned long iflags;
2323 read_lock_irqsave(&sg_index_lock, iflags);
2324 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2325 ++k, resp = resp->nextfp) ;
2326 read_unlock_irqrestore(&sg_index_lock, iflags);
2332 sg_add_sfp(Sg_device * sdp, int dev)
2335 unsigned long iflags;
2338 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2342 init_waitqueue_head(&sfp->read_wait);
2343 rwlock_init(&sfp->rq_list_lock);
2345 sfp->timeout = SG_DEFAULT_TIMEOUT;
2346 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2347 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2348 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2349 sdp->device->host->unchecked_isa_dma : 1;
2350 sfp->cmd_q = SG_DEF_COMMAND_Q;
2351 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2352 sfp->parentdp = sdp;
2353 write_lock_irqsave(&sg_index_lock, iflags);
2356 else { /* add to tail of existing list */
2357 Sg_fd *pfp = sdp->headfp;
2362 write_unlock_irqrestore(&sg_index_lock, iflags);
2363 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2364 if (unlikely(sg_big_buff != def_reserved_size))
2365 sg_big_buff = def_reserved_size;
2367 bufflen = min_t(int, sg_big_buff,
2368 sdp->device->request_queue->max_sectors * 512);
2369 sg_build_reserve(sfp, bufflen);
2370 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2371 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2376 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2381 prev_fp = sdp->headfp;
2383 sdp->headfp = prev_fp->nextfp;
2385 while ((fp = prev_fp->nextfp)) {
2387 prev_fp->nextfp = fp->nextfp;
2393 if (sfp->reserve.bufflen > 0) {
2395 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2396 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2397 sg_remove_scat(&sfp->reserve);
2399 sfp->parentdp = NULL;
2400 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2404 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2406 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2413 for (srp = sfp->headrp; srp; srp = tsrp) {
2415 if (sg_srp_done(srp, sfp))
2416 sg_finish_rem_req(srp);
2421 unsigned long iflags;
2423 write_lock_irqsave(&sg_index_lock, iflags);
2424 __sg_remove_sfp(sdp, sfp);
2425 if (sdp->detached && (NULL == sdp->headfp)) {
2426 idr_remove(&sg_index_idr, sdp->index);
2430 write_unlock_irqrestore(&sg_index_lock, iflags);
2432 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2433 /* only bump the access_count if we actually succeeded in
2434 * throwing another counter on the host module */
2435 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2436 sfp->closed = 1; /* flag dirty state on this fd */
2437 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2444 sg_res_in_use(Sg_fd * sfp)
2446 const Sg_request *srp;
2447 unsigned long iflags;
2449 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2450 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2453 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2457 /* The size fetched (value output via retSzp) set when non-NULL return */
2458 static struct page *
2459 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2461 struct page *resp = NULL;
2466 if ((rqSz <= 0) || (NULL == retSzp))
2470 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
2472 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
2474 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2475 order++, a_size <<= 1) ;
2476 resSz = a_size; /* rounded up if necessary */
2477 resp = alloc_pages(page_mask, order);
2478 while ((!resp) && order) {
2480 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2481 resp = alloc_pages(page_mask, order); /* try half */
2485 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2486 memset(page_address(resp), 0, resSz);
2493 sg_page_free(struct page *page, int size)
2499 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2500 order++, a_size <<= 1) ;
2501 __free_pages(page, order);
2504 #ifndef MAINTENANCE_IN_CMD
2505 #define MAINTENANCE_IN_CMD 0xa3
2508 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2509 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2510 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2511 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2515 sg_allow_access(unsigned char opcode, char dev_type)
2519 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2521 for (k = 0; k < sizeof (allow_ops); ++k) {
2522 if (opcode == allow_ops[k])
2528 #ifdef CONFIG_SCSI_PROC_FS
2530 sg_idr_max_id(int id, void *p, void *data)
2544 unsigned long iflags;
2546 read_lock_irqsave(&sg_index_lock, iflags);
2547 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2548 read_unlock_irqrestore(&sg_index_lock, iflags);
2549 return k + 1; /* origin 1 */
2557 unsigned long iflags;
2559 read_lock_irqsave(&sg_index_lock, iflags);
2560 sdp = idr_find(&sg_index_idr, dev);
2561 read_unlock_irqrestore(&sg_index_lock, iflags);
2566 #ifdef CONFIG_SCSI_PROC_FS
2568 static struct proc_dir_entry *sg_proc_sgp = NULL;
2570 static char sg_proc_sg_dirname[] = "scsi/sg";
2572 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2574 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2575 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2576 size_t count, loff_t *off);
2577 static struct file_operations adio_fops = {
2578 /* .owner, .read and .llseek added in sg_proc_init() */
2579 .open = sg_proc_single_open_adio,
2580 .write = sg_proc_write_adio,
2581 .release = single_release,
2584 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2585 static ssize_t sg_proc_write_dressz(struct file *filp,
2586 const char __user *buffer, size_t count, loff_t *off);
2587 static struct file_operations dressz_fops = {
2588 .open = sg_proc_single_open_dressz,
2589 .write = sg_proc_write_dressz,
2590 .release = single_release,
2593 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2594 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2595 static struct file_operations version_fops = {
2596 .open = sg_proc_single_open_version,
2597 .release = single_release,
2600 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2601 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2602 static struct file_operations devhdr_fops = {
2603 .open = sg_proc_single_open_devhdr,
2604 .release = single_release,
2607 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2608 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2609 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2610 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2611 static void dev_seq_stop(struct seq_file *s, void *v);
2612 static struct file_operations dev_fops = {
2613 .open = sg_proc_open_dev,
2614 .release = seq_release,
2616 static struct seq_operations dev_seq_ops = {
2617 .start = dev_seq_start,
2618 .next = dev_seq_next,
2619 .stop = dev_seq_stop,
2620 .show = sg_proc_seq_show_dev,
2623 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2624 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2625 static struct file_operations devstrs_fops = {
2626 .open = sg_proc_open_devstrs,
2627 .release = seq_release,
2629 static struct seq_operations devstrs_seq_ops = {
2630 .start = dev_seq_start,
2631 .next = dev_seq_next,
2632 .stop = dev_seq_stop,
2633 .show = sg_proc_seq_show_devstrs,
2636 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2637 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2638 static struct file_operations debug_fops = {
2639 .open = sg_proc_open_debug,
2640 .release = seq_release,
2642 static struct seq_operations debug_seq_ops = {
2643 .start = dev_seq_start,
2644 .next = dev_seq_next,
2645 .stop = dev_seq_stop,
2646 .show = sg_proc_seq_show_debug,
2650 struct sg_proc_leaf {
2652 struct file_operations * fops;
2655 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2656 {"allow_dio", &adio_fops},
2657 {"debug", &debug_fops},
2658 {"def_reserved_size", &dressz_fops},
2659 {"device_hdr", &devhdr_fops},
2660 {"devices", &dev_fops},
2661 {"device_strs", &devstrs_fops},
2662 {"version", &version_fops}
2669 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2670 struct sg_proc_leaf * leaf;
2672 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2675 for (k = 0; k < num_leaves; ++k) {
2676 leaf = &sg_proc_leaf_arr[k];
2677 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2678 leaf->fops->owner = THIS_MODULE;
2679 leaf->fops->read = seq_read;
2680 leaf->fops->llseek = seq_lseek;
2681 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
2687 sg_proc_cleanup(void)
2690 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2694 for (k = 0; k < num_leaves; ++k)
2695 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2696 remove_proc_entry(sg_proc_sg_dirname, NULL);
2700 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2702 seq_printf(s, "%d\n", *((int *)s->private));
2706 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2708 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2712 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2713 size_t count, loff_t *off)
2718 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2720 num = (count < 10) ? count : 10;
2721 if (copy_from_user(buff, buffer, num))
2724 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2728 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2730 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2734 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2735 size_t count, loff_t *off)
2738 unsigned long k = ULONG_MAX;
2741 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2743 num = (count < 10) ? count : 10;
2744 if (copy_from_user(buff, buffer, num))
2747 k = simple_strtoul(buff, NULL, 10);
2748 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2755 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2757 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2762 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2764 return single_open(file, sg_proc_seq_show_version, NULL);
2767 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2769 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2774 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2776 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2779 struct sg_proc_deviter {
2784 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2786 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2793 it->max = sg_last_dev();
2794 if (it->index >= it->max)
2799 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2801 struct sg_proc_deviter * it = s->private;
2804 return (it->index < it->max) ? it : NULL;
2807 static void dev_seq_stop(struct seq_file *s, void *v)
2812 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2814 return seq_open(file, &dev_seq_ops);
2817 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2819 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2821 struct scsi_device *scsidp;
2823 sdp = it ? sg_get_dev(it->index) : NULL;
2824 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2825 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2826 scsidp->host->host_no, scsidp->channel,
2827 scsidp->id, scsidp->lun, (int) scsidp->type,
2829 (int) scsidp->queue_depth,
2830 (int) scsidp->device_busy,
2831 (int) scsi_device_online(scsidp));
2833 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2837 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2839 return seq_open(file, &devstrs_seq_ops);
2842 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2844 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2846 struct scsi_device *scsidp;
2848 sdp = it ? sg_get_dev(it->index) : NULL;
2849 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2850 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2851 scsidp->vendor, scsidp->model, scsidp->rev);
2853 seq_printf(s, "<no active device>\n");
2857 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2859 int k, m, new_interface, blen, usg;
2862 const sg_io_hdr_t *hp;
2866 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2867 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2868 "(res)sgat=%d low_dma=%d\n", k + 1,
2869 jiffies_to_msecs(fp->timeout),
2870 fp->reserve.bufflen,
2871 (int) fp->reserve.k_use_sg,
2873 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2874 (int) fp->cmd_q, (int) fp->force_packid,
2875 (int) fp->keep_orphan, (int) fp->closed);
2876 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2878 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2879 if (srp->res_used) {
2880 if (new_interface &&
2881 (SG_FLAG_MMAP_IO & hp->flags))
2886 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2892 blen = srp->data.bufflen;
2893 usg = srp->data.k_use_sg;
2894 seq_printf(s, srp->done ?
2895 ((1 == srp->done) ? "rcv:" : "fin:")
2897 seq_printf(s, " id=%d blen=%d",
2898 srp->header.pack_id, blen);
2900 seq_printf(s, " dur=%d", hp->duration);
2902 ms = jiffies_to_msecs(jiffies);
2903 seq_printf(s, " t_o/elap=%d/%d",
2904 (new_interface ? hp->timeout :
2905 jiffies_to_msecs(fp->timeout)),
2906 (ms > hp->duration ? ms - hp->duration : 0));
2908 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2909 (int) srp->data.cmd_opcode);
2912 seq_printf(s, " No requests active\n");
2916 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2918 return seq_open(file, &debug_seq_ops);
2921 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2923 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2926 if (it && (0 == it->index)) {
2927 seq_printf(s, "max_active_device=%d(origin 1)\n",
2929 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2931 sdp = it ? sg_get_dev(it->index) : NULL;
2933 struct scsi_device *scsidp = sdp->device;
2935 if (NULL == scsidp) {
2936 seq_printf(s, "device %d detached ??\n",
2941 if (sg_get_nth_sfp(sdp, 0)) {
2942 seq_printf(s, " >>> device=%s ",
2943 sdp->disk->disk_name);
2945 seq_printf(s, "detached pending close ");
2948 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2949 scsidp->host->host_no,
2950 scsidp->channel, scsidp->id,
2952 scsidp->host->hostt->emulated);
2953 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2954 sdp->sg_tablesize, sdp->exclude);
2956 sg_proc_debug_helper(s, sdp);
2961 #endif /* CONFIG_SCSI_PROC_FS */
2963 module_init(init_sg);
2964 module_exit(exit_sg);