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/smp_lock.h>
45 #include <linux/moduleparam.h>
46 #include <linux/cdev.h>
47 #include <linux/seq_file.h>
48 #include <linux/blkdev.h>
49 #include <linux/delay.h>
50 #include <linux/scatterlist.h>
53 #include <scsi/scsi_dbg.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_driver.h>
56 #include <scsi/scsi_ioctl.h>
59 #include "scsi_logging.h"
61 #ifdef CONFIG_SCSI_PROC_FS
62 #include <linux/proc_fs.h>
63 static char *sg_version_date = "20061027";
65 static int sg_proc_init(void);
66 static void sg_proc_cleanup(void);
69 #define SG_ALLOW_DIO_DEF 0
70 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
72 #define SG_MAX_DEVS 32768
75 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
76 * Then when using 32 bit integers x * m may overflow during the calculation.
77 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
78 * calculates the same, but prevents the overflow when both m and d
79 * are "small" numbers (like HZ and USER_HZ).
80 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
83 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
85 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
87 int sg_big_buff = SG_DEF_RESERVED_SIZE;
88 /* N.B. This variable is readable and writeable via
89 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
90 of this size (or less if there is not enough memory) will be reserved
91 for use by this file descriptor. [Deprecated usage: this variable is also
92 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
93 the kernel (i.e. it is not a module).] */
94 static int def_reserved_size = -1; /* picks up init parameter */
95 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
97 static int scatter_elem_sz = SG_SCATTER_SZ;
98 static int scatter_elem_sz_prev = SG_SCATTER_SZ;
100 #define SG_SECTOR_SZ 512
101 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
103 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
105 static int sg_add(struct class_device *, struct class_interface *);
106 static void sg_remove(struct class_device *, struct class_interface *);
108 static DEFINE_RWLOCK(sg_dev_arr_lock); /* Also used to lock
109 file descriptor list for device */
111 static struct class_interface sg_interface = {
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 short sglist_len; /* size of malloc'd scatter-gather list ++ */
119 unsigned bufflen; /* Size of (aggregate) data buffer */
120 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
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 Sg_fd *headfp; /* first open fd belonging to this device */
167 volatile char detached; /* 0->attached, 1->detached pending removal */
168 volatile char exclude; /* opened for exclusive access */
169 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
170 struct gendisk *disk;
171 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
174 static int sg_fasync(int fd, struct file *filp, int mode);
175 /* tasklet or soft irq callback */
176 static void sg_cmd_done(void *data, char *sense, int result, int resid);
177 static int sg_start_req(Sg_request * srp);
178 static void sg_finish_rem_req(Sg_request * srp);
179 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
180 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
182 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
184 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
185 int blocking, int read_only, Sg_request ** o_srp);
186 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
187 unsigned char *cmnd, int timeout, int blocking);
188 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
189 int wr_xf, int *countp, unsigned char __user **up);
190 static int sg_write_xfer(Sg_request * srp);
191 static int sg_read_xfer(Sg_request * srp);
192 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
193 static void sg_remove_scat(Sg_scatter_hold * schp);
194 static void sg_build_reserve(Sg_fd * sfp, int req_size);
195 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
196 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
197 static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
198 static void sg_page_free(struct page *page, int size);
199 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
200 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
201 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
203 static Sg_request *sg_add_request(Sg_fd * sfp);
204 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
205 static int sg_res_in_use(Sg_fd * sfp);
206 static int sg_allow_access(unsigned char opcode, char dev_type);
207 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
208 static Sg_device *sg_get_dev(int dev);
209 #ifdef CONFIG_SCSI_PROC_FS
210 static int sg_last_dev(void);
213 static Sg_device **sg_dev_arr = NULL;
214 static int sg_dev_max;
215 static int sg_nr_dev;
217 #define SZ_SG_HEADER sizeof(struct sg_header)
218 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
219 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
220 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
223 sg_open(struct inode *inode, struct file *filp)
225 int dev = iminor(inode);
226 int flags = filp->f_flags;
227 struct request_queue *q;
233 nonseekable_open(inode, filp);
234 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
235 sdp = sg_get_dev(dev);
236 if ((!sdp) || (!sdp->device))
241 /* This driver's module count bumped by fops_get in <linux/fs.h> */
242 /* Prevent the device driver from vanishing while we sleep */
243 retval = scsi_device_get(sdp->device);
247 if (!((flags & O_NONBLOCK) ||
248 scsi_block_when_processing_errors(sdp->device))) {
250 /* we are in error recovery for this device */
254 if (flags & O_EXCL) {
255 if (O_RDONLY == (flags & O_ACCMODE)) {
256 retval = -EPERM; /* Can't lock it with read only access */
259 if (sdp->headfp && (flags & O_NONBLOCK)) {
264 __wait_event_interruptible(sdp->o_excl_wait,
265 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
267 retval = res; /* -ERESTARTSYS because signal hit process */
270 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
271 if (flags & O_NONBLOCK) {
276 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
279 retval = res; /* -ERESTARTSYS because signal hit process */
287 if (!sdp->headfp) { /* no existing opens on this device */
289 q = sdp->device->request_queue;
290 sdp->sg_tablesize = min(q->max_hw_segments,
291 q->max_phys_segments);
293 if ((sfp = sg_add_sfp(sdp, dev)))
294 filp->private_data = sfp;
297 sdp->exclude = 0; /* undo if error */
304 scsi_device_put(sdp->device);
308 /* Following function was formerly called 'sg_close' */
310 sg_release(struct inode *inode, struct file *filp)
315 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
317 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
318 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
319 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
320 if (!sdp->detached) {
321 scsi_device_put(sdp->device);
324 wake_up_interruptible(&sdp->o_excl_wait);
330 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
335 int req_pack_id = -1;
337 struct sg_header *old_hdr = NULL;
340 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
342 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
343 sdp->disk->disk_name, (int) count));
345 if (!access_ok(VERIFY_WRITE, buf, count))
347 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
348 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
351 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
355 if (old_hdr->reply_len < 0) {
356 if (count >= SZ_SG_IO_HDR) {
357 sg_io_hdr_t *new_hdr;
358 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
363 retval =__copy_from_user
364 (new_hdr, buf, SZ_SG_IO_HDR);
365 req_pack_id = new_hdr->pack_id;
373 req_pack_id = old_hdr->pack_id;
375 srp = sg_get_rq_mark(sfp, req_pack_id);
376 if (!srp) { /* now wait on packet to arrive */
381 if (filp->f_flags & O_NONBLOCK) {
386 retval = 0; /* following macro beats race condition */
387 __wait_event_interruptible(sfp->read_wait,
389 (srp = sg_get_rq_mark(sfp, req_pack_id))),
398 /* -ERESTARTSYS as signal hit process */
402 if (srp->header.interface_id != '\0') {
403 retval = sg_new_read(sfp, buf, count, srp);
408 if (old_hdr == NULL) {
409 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
415 memset(old_hdr, 0, SZ_SG_HEADER);
416 old_hdr->reply_len = (int) hp->timeout;
417 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
418 old_hdr->pack_id = hp->pack_id;
419 old_hdr->twelve_byte =
420 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
421 old_hdr->target_status = hp->masked_status;
422 old_hdr->host_status = hp->host_status;
423 old_hdr->driver_status = hp->driver_status;
424 if ((CHECK_CONDITION & hp->masked_status) ||
425 (DRIVER_SENSE & hp->driver_status))
426 memcpy(old_hdr->sense_buffer, srp->sense_b,
427 sizeof (old_hdr->sense_buffer));
428 switch (hp->host_status) {
429 /* This setup of 'result' is for backward compatibility and is best
430 ignored by the user who should use target, host + driver status */
432 case DID_PASSTHROUGH:
439 old_hdr->result = EBUSY;
446 old_hdr->result = EIO;
449 old_hdr->result = (srp->sense_b[0] == 0 &&
450 hp->masked_status == GOOD) ? 0 : EIO;
453 old_hdr->result = EIO;
457 /* Now copy the result back to the user buffer. */
458 if (count >= SZ_SG_HEADER) {
459 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
464 if (count > old_hdr->reply_len)
465 count = old_hdr->reply_len;
466 if (count > SZ_SG_HEADER) {
467 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
473 count = (old_hdr->result == 0) ? 0 : -EIO;
474 sg_finish_rem_req(srp);
482 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
484 sg_io_hdr_t *hp = &srp->header;
488 if (count < SZ_SG_IO_HDR) {
493 if ((hp->mx_sb_len > 0) && hp->sbp) {
494 if ((CHECK_CONDITION & hp->masked_status) ||
495 (DRIVER_SENSE & hp->driver_status)) {
496 int sb_len = SCSI_SENSE_BUFFERSIZE;
497 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
498 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
499 len = (len > sb_len) ? sb_len : len;
500 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
507 if (hp->masked_status || hp->host_status || hp->driver_status)
508 hp->info |= SG_INFO_CHECK;
509 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
513 err = sg_read_xfer(srp);
515 sg_finish_rem_req(srp);
516 return (0 == err) ? count : err;
520 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
522 int mxsize, cmd_size, k;
523 int input_size, blocking;
524 unsigned char opcode;
528 struct sg_header old_hdr;
530 unsigned char cmnd[MAX_COMMAND_SIZE];
532 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
534 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
535 sdp->disk->disk_name, (int) count));
538 if (!((filp->f_flags & O_NONBLOCK) ||
539 scsi_block_when_processing_errors(sdp->device)))
542 if (!access_ok(VERIFY_READ, buf, count))
543 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
544 if (count < SZ_SG_HEADER)
546 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
548 blocking = !(filp->f_flags & O_NONBLOCK);
549 if (old_hdr.reply_len < 0)
550 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
551 if (count < (SZ_SG_HEADER + 6))
552 return -EIO; /* The minimum scsi command length is 6 bytes. */
554 if (!(srp = sg_add_request(sfp))) {
555 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
559 __get_user(opcode, buf);
560 if (sfp->next_cmd_len > 0) {
561 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
562 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
563 sfp->next_cmd_len = 0;
564 sg_remove_request(sfp, srp);
567 cmd_size = sfp->next_cmd_len;
568 sfp->next_cmd_len = 0; /* reset so only this write() effected */
570 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
571 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
574 SCSI_LOG_TIMEOUT(4, printk(
575 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
576 /* Determine buffer size. */
577 input_size = count - cmd_size;
578 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
579 mxsize -= SZ_SG_HEADER;
580 input_size -= SZ_SG_HEADER;
581 if (input_size < 0) {
582 sg_remove_request(sfp, srp);
583 return -EIO; /* User did not pass enough bytes for this command. */
586 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
587 hp->cmd_len = (unsigned char) cmd_size;
591 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
592 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
594 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
595 hp->dxfer_len = mxsize;
596 hp->dxferp = (char __user *)buf + cmd_size;
598 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
599 hp->flags = input_size; /* structure abuse ... */
600 hp->pack_id = old_hdr.pack_id;
602 if (__copy_from_user(cmnd, buf, cmd_size))
605 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
606 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
607 * is a non-zero input_size, so emit a warning.
609 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
610 if (printk_ratelimit())
612 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
613 "guessing data in;\n" KERN_WARNING " "
614 "program %s not setting count and/or reply_len properly\n",
615 old_hdr.reply_len - (int)SZ_SG_HEADER,
616 input_size, (unsigned int) cmnd[0],
618 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
619 return (k < 0) ? k : count;
623 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
624 int blocking, int read_only, Sg_request ** o_srp)
629 unsigned char cmnd[MAX_COMMAND_SIZE];
631 unsigned long ul_timeout;
633 if (count < SZ_SG_IO_HDR)
635 if (!access_ok(VERIFY_READ, buf, count))
636 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
638 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
639 if (!(srp = sg_add_request(sfp))) {
640 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
644 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
645 sg_remove_request(sfp, srp);
648 if (hp->interface_id != 'S') {
649 sg_remove_request(sfp, srp);
652 if (hp->flags & SG_FLAG_MMAP_IO) {
653 if (hp->dxfer_len > sfp->reserve.bufflen) {
654 sg_remove_request(sfp, srp);
655 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
657 if (hp->flags & SG_FLAG_DIRECT_IO) {
658 sg_remove_request(sfp, srp);
659 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
661 if (sg_res_in_use(sfp)) {
662 sg_remove_request(sfp, srp);
663 return -EBUSY; /* reserve buffer already being used */
666 ul_timeout = msecs_to_jiffies(srp->header.timeout);
667 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
668 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
669 sg_remove_request(sfp, srp);
672 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
673 sg_remove_request(sfp, srp);
674 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
676 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
677 sg_remove_request(sfp, srp);
681 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
682 sg_remove_request(sfp, srp);
685 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
694 sg_common_write(Sg_fd * sfp, Sg_request * srp,
695 unsigned char *cmnd, int timeout, int blocking)
698 Sg_device *sdp = sfp->parentdp;
699 sg_io_hdr_t *hp = &srp->header;
701 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
703 hp->masked_status = 0;
707 hp->driver_status = 0;
709 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
710 (int) cmnd[0], (int) hp->cmd_len));
712 if ((k = sg_start_req(srp))) {
713 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
714 sg_finish_rem_req(srp);
715 return k; /* probably out of space --> ENOMEM */
717 if ((k = sg_write_xfer(srp))) {
718 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
719 sg_finish_rem_req(srp);
723 sg_finish_rem_req(srp);
727 switch (hp->dxfer_direction) {
728 case SG_DXFER_TO_FROM_DEV:
729 case SG_DXFER_FROM_DEV:
730 data_dir = DMA_FROM_DEVICE;
732 case SG_DXFER_TO_DEV:
733 data_dir = DMA_TO_DEVICE;
735 case SG_DXFER_UNKNOWN:
736 data_dir = DMA_BIDIRECTIONAL;
742 hp->duration = jiffies_to_msecs(jiffies);
743 /* Now send everything of to mid-level. The next time we hear about this
744 packet is when sg_cmd_done() is called (i.e. a callback). */
745 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
746 hp->dxfer_len, srp->data.k_use_sg, timeout,
747 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
749 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
751 * most likely out of mem, but could also be a bad map
753 sg_finish_rem_req(srp);
760 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
762 unsigned long iflags;
765 read_lock_irqsave(&sfp->rq_list_lock, iflags);
767 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
772 sg_ioctl(struct inode *inode, struct file *filp,
773 unsigned int cmd_in, unsigned long arg)
775 void __user *p = (void __user *)arg;
777 int result, val, read_only;
781 unsigned long iflags;
783 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
785 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
786 sdp->disk->disk_name, (int) cmd_in));
787 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
792 int blocking = 1; /* ignore O_NONBLOCK flag */
796 if (!scsi_block_when_processing_errors(sdp->device))
798 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
801 sg_new_write(sfp, p, SZ_SG_IO_HDR,
802 blocking, read_only, &srp);
805 srp->sg_io_owned = 1;
807 result = 0; /* following macro to beat race condition */
808 __wait_event_interruptible(sfp->read_wait,
809 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
814 return 0; /* request packet dropped already */
818 return result; /* -ERESTARTSYS because signal hit process */
820 write_lock_irqsave(&sfp->rq_list_lock, iflags);
822 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
823 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
824 return (result < 0) ? result : 0;
827 result = get_user(val, ip);
832 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
833 val = MULDIV (INT_MAX, USER_HZ, HZ);
834 sfp->timeout_user = val;
835 sfp->timeout = MULDIV (val, HZ, USER_HZ);
838 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
839 /* strange ..., for backward compatibility */
840 return sfp->timeout_user;
841 case SG_SET_FORCE_LOW_DMA:
842 result = get_user(val, ip);
847 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
848 val = (int) sfp->reserve.bufflen;
849 sg_remove_scat(&sfp->reserve);
850 sg_build_reserve(sfp, val);
855 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
859 return put_user((int) sfp->low_dma, ip);
861 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
864 sg_scsi_id_t __user *sg_idp = p;
868 __put_user((int) sdp->device->host->host_no,
870 __put_user((int) sdp->device->channel,
872 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
873 __put_user((int) sdp->device->lun, &sg_idp->lun);
874 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
875 __put_user((short) sdp->device->host->cmd_per_lun,
876 &sg_idp->h_cmd_per_lun);
877 __put_user((short) sdp->device->queue_depth,
878 &sg_idp->d_queue_depth);
879 __put_user(0, &sg_idp->unused[0]);
880 __put_user(0, &sg_idp->unused[1]);
883 case SG_SET_FORCE_PACK_ID:
884 result = get_user(val, ip);
887 sfp->force_packid = val ? 1 : 0;
890 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
892 read_lock_irqsave(&sfp->rq_list_lock, iflags);
893 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
894 if ((1 == srp->done) && (!srp->sg_io_owned)) {
895 read_unlock_irqrestore(&sfp->rq_list_lock,
897 __put_user(srp->header.pack_id, ip);
901 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
904 case SG_GET_NUM_WAITING:
905 read_lock_irqsave(&sfp->rq_list_lock, iflags);
906 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
907 if ((1 == srp->done) && (!srp->sg_io_owned))
910 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
911 return put_user(val, ip);
912 case SG_GET_SG_TABLESIZE:
913 return put_user(sdp->sg_tablesize, ip);
914 case SG_SET_RESERVED_SIZE:
915 result = get_user(val, ip);
920 if (val != sfp->reserve.bufflen) {
921 if (sg_res_in_use(sfp) || sfp->mmap_called)
923 sg_remove_scat(&sfp->reserve);
924 sg_build_reserve(sfp, val);
927 case SG_GET_RESERVED_SIZE:
928 val = (int) sfp->reserve.bufflen;
929 return put_user(val, ip);
930 case SG_SET_COMMAND_Q:
931 result = get_user(val, ip);
934 sfp->cmd_q = val ? 1 : 0;
936 case SG_GET_COMMAND_Q:
937 return put_user((int) sfp->cmd_q, ip);
938 case SG_SET_KEEP_ORPHAN:
939 result = get_user(val, ip);
942 sfp->keep_orphan = val;
944 case SG_GET_KEEP_ORPHAN:
945 return put_user((int) sfp->keep_orphan, ip);
946 case SG_NEXT_CMD_LEN:
947 result = get_user(val, ip);
950 sfp->next_cmd_len = (val > 0) ? val : 0;
952 case SG_GET_VERSION_NUM:
953 return put_user(sg_version_num, ip);
954 case SG_GET_ACCESS_COUNT:
955 /* faked - we don't have a real access count anymore */
956 val = (sdp->device ? 1 : 0);
957 return put_user(val, ip);
958 case SG_GET_REQUEST_TABLE:
959 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
962 sg_req_info_t *rinfo;
965 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
969 read_lock_irqsave(&sfp->rq_list_lock, iflags);
970 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
971 ++val, srp = srp ? srp->nextrp : srp) {
972 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
974 rinfo[val].req_state = srp->done + 1;
976 srp->header.masked_status &
977 srp->header.host_status &
978 srp->header.driver_status;
980 rinfo[val].duration =
981 srp->header.duration;
983 ms = jiffies_to_msecs(jiffies);
984 rinfo[val].duration =
985 (ms > srp->header.duration) ?
986 (ms - srp->header.duration) : 0;
988 rinfo[val].orphan = srp->orphan;
989 rinfo[val].sg_io_owned =
997 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
998 result = __copy_to_user(p, rinfo,
999 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1000 result = result ? -EFAULT : 0;
1004 case SG_EMULATED_HOST:
1007 return put_user(sdp->device->host->hostt->emulated, ip);
1011 if (filp->f_flags & O_NONBLOCK) {
1012 if (scsi_host_in_recovery(sdp->device->host))
1014 } else if (!scsi_block_when_processing_errors(sdp->device))
1016 result = get_user(val, ip);
1019 if (SG_SCSI_RESET_NOTHING == val)
1022 case SG_SCSI_RESET_DEVICE:
1023 val = SCSI_TRY_RESET_DEVICE;
1025 case SG_SCSI_RESET_BUS:
1026 val = SCSI_TRY_RESET_BUS;
1028 case SG_SCSI_RESET_HOST:
1029 val = SCSI_TRY_RESET_HOST;
1034 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1036 return (scsi_reset_provider(sdp->device, val) ==
1037 SUCCESS) ? 0 : -EIO;
1038 case SCSI_IOCTL_SEND_COMMAND:
1042 unsigned char opcode = WRITE_6;
1043 Scsi_Ioctl_Command __user *siocp = p;
1045 if (copy_from_user(&opcode, siocp->data, 1))
1047 if (!sg_allow_access(opcode, sdp->device->type))
1050 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
1052 result = get_user(val, ip);
1055 sdp->sgdebug = (char) val;
1057 case SCSI_IOCTL_GET_IDLUN:
1058 case SCSI_IOCTL_GET_BUS_NUMBER:
1059 case SCSI_IOCTL_PROBE_HOST:
1060 case SG_GET_TRANSFORM:
1063 return scsi_ioctl(sdp->device, cmd_in, p);
1066 return -EPERM; /* don't know so take safe approach */
1067 return scsi_ioctl(sdp->device, cmd_in, p);
1071 #ifdef CONFIG_COMPAT
1072 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1076 struct scsi_device *sdev;
1078 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1082 if (sdev->host->hostt->compat_ioctl) {
1085 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1090 return -ENOIOCTLCMD;
1095 sg_poll(struct file *filp, poll_table * wait)
1097 unsigned int res = 0;
1102 unsigned long iflags;
1104 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1107 poll_wait(filp, &sfp->read_wait, wait);
1108 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1109 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1110 /* if any read waiting, flag it */
1111 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1112 res = POLLIN | POLLRDNORM;
1115 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1119 else if (!sfp->cmd_q) {
1121 res |= POLLOUT | POLLWRNORM;
1122 } else if (count < SG_MAX_QUEUE)
1123 res |= POLLOUT | POLLWRNORM;
1124 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1125 sdp->disk->disk_name, (int) res));
1130 sg_fasync(int fd, struct file *filp, int mode)
1136 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1138 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1139 sdp->disk->disk_name, mode));
1141 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1142 return (retval < 0) ? retval : 0;
1145 static struct page *
1146 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1149 struct page *page = NOPAGE_SIGBUS;
1150 unsigned long offset, len, sa;
1151 Sg_scatter_hold *rsv_schp;
1152 struct scatterlist *sg;
1155 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1157 rsv_schp = &sfp->reserve;
1158 offset = addr - vma->vm_start;
1159 if (offset >= rsv_schp->bufflen)
1161 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1162 offset, rsv_schp->k_use_sg));
1163 sg = rsv_schp->buffer;
1165 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1167 len = vma->vm_end - sa;
1168 len = (len < sg->length) ? len : sg->length;
1170 page = virt_to_page(page_address(sg->page) + offset);
1171 get_page(page); /* increment page count */
1179 *type = VM_FAULT_MINOR;
1183 static struct vm_operations_struct sg_mmap_vm_ops = {
1184 .nopage = sg_vma_nopage,
1188 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1191 unsigned long req_sz, len, sa;
1192 Sg_scatter_hold *rsv_schp;
1194 struct scatterlist *sg;
1196 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1198 req_sz = vma->vm_end - vma->vm_start;
1199 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1200 (void *) vma->vm_start, (int) req_sz));
1202 return -EINVAL; /* want no offset */
1203 rsv_schp = &sfp->reserve;
1204 if (req_sz > rsv_schp->bufflen)
1205 return -ENOMEM; /* cannot map more than reserved buffer */
1208 sg = rsv_schp->buffer;
1209 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1211 len = vma->vm_end - sa;
1212 len = (len < sg->length) ? len : sg->length;
1216 sfp->mmap_called = 1;
1217 vma->vm_flags |= VM_RESERVED;
1218 vma->vm_private_data = sfp;
1219 vma->vm_ops = &sg_mmap_vm_ops;
1223 /* This function is a "bottom half" handler that is called by the
1224 * mid level when a command is completed (or has failed). */
1226 sg_cmd_done(void *data, char *sense, int result, int resid)
1228 Sg_request *srp = data;
1229 Sg_device *sdp = NULL;
1231 unsigned long iflags;
1235 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1238 sfp = srp->parentfp;
1240 sdp = sfp->parentdp;
1241 if ((NULL == sdp) || sdp->detached) {
1242 printk(KERN_INFO "sg_cmd_done: device detached\n");
1247 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1248 sdp->disk->disk_name, srp->header.pack_id, result));
1249 srp->header.resid = resid;
1250 ms = jiffies_to_msecs(jiffies);
1251 srp->header.duration = (ms > srp->header.duration) ?
1252 (ms - srp->header.duration) : 0;
1254 struct scsi_sense_hdr sshdr;
1256 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1257 srp->header.status = 0xff & result;
1258 srp->header.masked_status = status_byte(result);
1259 srp->header.msg_status = msg_byte(result);
1260 srp->header.host_status = host_byte(result);
1261 srp->header.driver_status = driver_byte(result);
1262 if ((sdp->sgdebug > 0) &&
1263 ((CHECK_CONDITION == srp->header.masked_status) ||
1264 (COMMAND_TERMINATED == srp->header.masked_status)))
1265 __scsi_print_sense("sg_cmd_done", sense,
1266 SCSI_SENSE_BUFFERSIZE);
1268 /* Following if statement is a patch supplied by Eric Youngdale */
1269 if (driver_byte(result) != 0
1270 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1271 && !scsi_sense_is_deferred(&sshdr)
1272 && sshdr.sense_key == UNIT_ATTENTION
1273 && sdp->device->removable) {
1274 /* Detected possible disc change. Set the bit - this */
1275 /* may be used if there are filesystems using this device */
1276 sdp->device->changed = 1;
1279 /* Rely on write phase to clean out srp status values, so no "else" */
1281 if (sfp->closed) { /* whoops this fd already released, cleanup */
1282 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1283 sg_finish_rem_req(srp);
1285 if (NULL == sfp->headrp) {
1286 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1287 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1288 scsi_device_put(sdp->device);
1292 } else if (srp && srp->orphan) {
1293 if (sfp->keep_orphan)
1294 srp->sg_io_owned = 0;
1296 sg_finish_rem_req(srp);
1301 /* Now wake up any sg_read() that is waiting for this packet. */
1302 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1303 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1305 wake_up_interruptible(&sfp->read_wait);
1306 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1310 static struct file_operations sg_fops = {
1311 .owner = THIS_MODULE,
1316 #ifdef CONFIG_COMPAT
1317 .compat_ioctl = sg_compat_ioctl,
1321 .release = sg_release,
1322 .fasync = sg_fasync,
1325 static struct class *sg_sysfs_class;
1327 static int sg_sysfs_valid = 0;
1329 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1331 struct request_queue *q = scsidp->request_queue;
1333 unsigned long iflags;
1334 void *old_sg_dev_arr = NULL;
1337 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1339 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1343 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1344 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1346 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1347 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1349 tmp_da = kzalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1350 if (unlikely(!tmp_da))
1353 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1354 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1355 old_sg_dev_arr = sg_dev_arr;
1356 sg_dev_arr = tmp_da;
1357 sg_dev_max = tmp_dev_max;
1360 for (k = 0; k < sg_dev_max; k++)
1363 if (unlikely(k >= SG_MAX_DEVS))
1366 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1367 sprintf(disk->disk_name, "sg%d", k);
1368 disk->first_minor = k;
1370 sdp->device = scsidp;
1371 init_waitqueue_head(&sdp->o_excl_wait);
1372 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1375 sg_dev_arr[k] = sdp;
1376 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1382 kfree(old_sg_dev_arr);
1386 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1391 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1392 sdev_printk(KERN_WARNING, scsidp,
1393 "Unable to attach sg device type=%d, minor "
1394 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1400 sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
1402 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1403 struct gendisk *disk;
1404 Sg_device *sdp = NULL;
1405 struct cdev * cdev = NULL;
1407 unsigned long iflags;
1409 disk = alloc_disk(1);
1411 printk(KERN_WARNING "alloc_disk failed\n");
1414 disk->major = SCSI_GENERIC_MAJOR;
1417 cdev = cdev_alloc();
1419 printk(KERN_WARNING "cdev_alloc failed\n");
1422 cdev->owner = THIS_MODULE;
1423 cdev->ops = &sg_fops;
1425 error = sg_alloc(disk, scsidp);
1427 printk(KERN_WARNING "sg_alloc failed\n");
1431 sdp = sg_dev_arr[k];
1433 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1438 if (sg_sysfs_valid) {
1439 struct class_device * sg_class_member;
1441 sg_class_member = class_device_create(sg_sysfs_class, NULL,
1442 MKDEV(SCSI_GENERIC_MAJOR, k),
1445 if (IS_ERR(sg_class_member))
1446 printk(KERN_WARNING "sg_add: "
1447 "class_device_create failed\n");
1448 class_set_devdata(sg_class_member, sdp);
1449 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1450 &sg_class_member->kobj, "generic");
1452 printk(KERN_ERR "sg_add: unable to make symlink "
1453 "'generic' back to sg%d\n", k);
1455 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1457 sdev_printk(KERN_NOTICE, scsidp,
1458 "Attached scsi generic sg%d type %d\n", k,scsidp->type);
1463 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1464 kfree(sg_dev_arr[k]);
1465 sg_dev_arr[k] = NULL;
1467 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1477 sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
1479 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1480 Sg_device *sdp = NULL;
1481 unsigned long iflags;
1488 if (NULL == sg_dev_arr)
1491 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1492 for (k = 0; k < sg_dev_max; k++) {
1493 sdp = sg_dev_arr[k];
1494 if ((NULL == sdp) || (sdp->device != scsidp))
1495 continue; /* dirty but lowers nesting */
1498 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1500 for (srp = sfp->headrp; srp; srp = tsrp) {
1502 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1503 sg_finish_rem_req(srp);
1506 scsi_device_put(sdp->device);
1507 __sg_remove_sfp(sdp, sfp);
1510 wake_up_interruptible(&sfp->read_wait);
1511 kill_fasync(&sfp->async_qp, SIGPOLL,
1515 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", k));
1516 if (NULL == sdp->headfp) {
1517 sg_dev_arr[k] = NULL;
1519 } else { /* nothing active, simple case */
1520 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", k));
1521 sg_dev_arr[k] = NULL;
1526 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1529 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1530 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, k));
1531 cdev_del(sdp->cdev);
1533 put_disk(sdp->disk);
1535 if (NULL == sdp->headfp)
1536 kfree((char *) sdp);
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 kfree((char *)sg_dev_arr);
1613 sg_start_req(Sg_request * srp)
1616 Sg_fd *sfp = srp->parentfp;
1617 sg_io_hdr_t *hp = &srp->header;
1618 int dxfer_len = (int) hp->dxfer_len;
1619 int dxfer_dir = hp->dxfer_direction;
1620 Sg_scatter_hold *req_schp = &srp->data;
1621 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1623 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1624 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1626 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1627 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1628 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1629 res = sg_build_direct(srp, sfp, dxfer_len);
1630 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1633 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1634 sg_link_reserve(sfp, srp, dxfer_len);
1636 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1638 sg_remove_scat(req_schp);
1646 sg_finish_rem_req(Sg_request * srp)
1648 Sg_fd *sfp = srp->parentfp;
1649 Sg_scatter_hold *req_schp = &srp->data;
1651 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1653 sg_unlink_reserve(sfp, srp);
1655 sg_remove_scat(req_schp);
1656 sg_remove_request(sfp, srp);
1660 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1662 int sg_bufflen = tablesize * sizeof(struct scatterlist);
1663 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1666 * TODO: test without low_dma, we should not need it since
1667 * the block layer will bounce the buffer for us
1669 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1672 gfp_flags |= GFP_DMA;
1673 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
1676 schp->sglist_len = sg_bufflen;
1677 return tablesize; /* number of scat_gath elements allocated */
1680 #ifdef SG_ALLOW_DIO_CODE
1681 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1682 /* TODO: hopefully we can use the generic block layer code */
1684 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1685 - mapping of all pages not successful
1686 (i.e., either completely successful or fails)
1689 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1690 unsigned long uaddr, size_t count, int rw)
1692 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1693 unsigned long start = uaddr >> PAGE_SHIFT;
1694 const int nr_pages = end - start;
1696 struct page **pages;
1698 /* User attempted Overflow! */
1699 if ((uaddr + count) < uaddr)
1703 if (nr_pages > max_pages)
1710 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1713 /* Try to fault in all of the necessary pages */
1714 down_read(¤t->mm->mmap_sem);
1715 /* rw==READ means read from drive, write into memory area */
1716 res = get_user_pages(
1722 0, /* don't force */
1725 up_read(¤t->mm->mmap_sem);
1727 /* Errors and no page mapped should return here */
1731 for (i=0; i < nr_pages; i++) {
1732 /* FIXME: flush superflous for rw==READ,
1733 * probably wrong function for rw==WRITE
1735 flush_dcache_page(pages[i]);
1736 /* ?? Is locking needed? I don't think so */
1737 /* if (TestSetPageLocked(pages[i]))
1741 sgl[0].page = pages[0];
1742 sgl[0].offset = uaddr & ~PAGE_MASK;
1744 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1745 count -= sgl[0].length;
1746 for (i=1; i < nr_pages ; i++) {
1747 sgl[i].page = pages[i];
1748 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1753 sgl[0].length = count;
1761 for (j=0; j < res; j++)
1762 page_cache_release(pages[j]);
1770 /* And unmap them... */
1772 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1777 for (i=0; i < nr_pages; i++) {
1778 struct page *page = sgl[i].page;
1782 /* unlock_page(page); */
1783 /* FIXME: cache flush missing for rw==READ
1784 * FIXME: call the correct reference counting function
1786 page_cache_release(page);
1792 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1796 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1798 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1800 #ifdef SG_ALLOW_DIO_CODE
1801 sg_io_hdr_t *hp = &srp->header;
1802 Sg_scatter_hold *schp = &srp->data;
1803 int sg_tablesize = sfp->parentdp->sg_tablesize;
1804 int mx_sc_elems, res;
1805 struct scsi_device *sdev = sfp->parentdp->device;
1807 if (((unsigned long)hp->dxferp &
1808 queue_dma_alignment(sdev->request_queue)) != 0)
1811 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1812 if (mx_sc_elems <= 0) {
1815 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1816 (unsigned long)hp->dxferp, dxfer_len,
1817 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
1819 sg_remove_scat(schp);
1822 schp->k_use_sg = res;
1823 schp->dio_in_use = 1;
1824 hp->info |= SG_INFO_DIRECT_IO;
1832 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1834 struct scatterlist *sg;
1835 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1836 int sg_tablesize = sfp->parentdp->sg_tablesize;
1837 int blk_size = buff_size;
1838 struct page *p = NULL;
1840 if ((blk_size < 0) || (!sfp))
1843 ++blk_size; /* don't know why */
1844 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1845 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1846 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1847 buff_size, blk_size));
1849 /* N.B. ret_sz carried into this block ... */
1850 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1851 if (mx_sc_elems < 0)
1852 return mx_sc_elems; /* most likely -ENOMEM */
1854 num = scatter_elem_sz;
1855 if (unlikely(num != scatter_elem_sz_prev)) {
1856 if (num < PAGE_SIZE) {
1857 scatter_elem_sz = PAGE_SIZE;
1858 scatter_elem_sz_prev = PAGE_SIZE;
1860 scatter_elem_sz_prev = num;
1862 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1863 (rem_sz > 0) && (k < mx_sc_elems);
1864 ++k, rem_sz -= ret_sz, ++sg) {
1866 num = (rem_sz > scatter_elem_sz_prev) ?
1867 scatter_elem_sz_prev : rem_sz;
1868 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1872 if (num == scatter_elem_sz_prev) {
1873 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1874 scatter_elem_sz = ret_sz;
1875 scatter_elem_sz_prev = ret_sz;
1879 sg->length = (ret_sz > num) ? num : ret_sz;
1881 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1882 "ret_sz=%d\n", k, num, ret_sz));
1883 } /* end of for loop */
1886 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1887 "rem_sz=%d\n", k, rem_sz));
1889 schp->bufflen = blk_size;
1890 if (rem_sz > 0) /* must have failed */
1897 sg_write_xfer(Sg_request * srp)
1899 sg_io_hdr_t *hp = &srp->header;
1900 Sg_scatter_hold *schp = &srp->data;
1901 struct scatterlist *sg = schp->buffer;
1903 int j, k, onum, usglen, ksglen, res;
1904 int iovec_count = (int) hp->iovec_count;
1905 int dxfer_dir = hp->dxfer_direction;
1907 unsigned char __user *up;
1908 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1910 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1911 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1912 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1913 if (schp->bufflen < num_xfer)
1914 num_xfer = schp->bufflen;
1916 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1918 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1921 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1922 num_xfer, iovec_count, schp->k_use_sg));
1925 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1930 ksglen = sg->length;
1931 p = page_address(sg->page);
1932 for (j = 0, k = 0; j < onum; ++j) {
1933 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1937 for (; p; ++sg, ksglen = sg->length,
1938 p = page_address(sg->page)) {
1941 if (ksglen > usglen) {
1942 if (usglen >= num_xfer) {
1943 if (__copy_from_user(p, up, num_xfer))
1947 if (__copy_from_user(p, up, usglen))
1953 if (ksglen >= num_xfer) {
1954 if (__copy_from_user(p, up, num_xfer))
1958 if (__copy_from_user(p, up, ksglen))
1964 if (k >= schp->k_use_sg)
1973 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1974 int wr_xf, int *countp, unsigned char __user **up)
1976 int num_xfer = (int) hp->dxfer_len;
1977 unsigned char __user *p = hp->dxferp;
1981 if (wr_xf && ('\0' == hp->interface_id))
1982 count = (int) hp->flags; /* holds "old" input_size */
1987 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1990 count = (int) iovec.iov_len;
1992 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2002 sg_remove_scat(Sg_scatter_hold * schp)
2004 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2005 if (schp->buffer && (schp->sglist_len > 0)) {
2006 struct scatterlist *sg = schp->buffer;
2008 if (schp->dio_in_use) {
2009 #ifdef SG_ALLOW_DIO_CODE
2010 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
2015 for (k = 0; (k < schp->k_use_sg) && sg->page;
2017 SCSI_LOG_TIMEOUT(5, printk(
2018 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
2019 k, sg->page, sg->length));
2020 sg_page_free(sg->page, sg->length);
2023 kfree(schp->buffer);
2025 memset(schp, 0, sizeof (*schp));
2029 sg_read_xfer(Sg_request * srp)
2031 sg_io_hdr_t *hp = &srp->header;
2032 Sg_scatter_hold *schp = &srp->data;
2033 struct scatterlist *sg = schp->buffer;
2035 int j, k, onum, usglen, ksglen, res;
2036 int iovec_count = (int) hp->iovec_count;
2037 int dxfer_dir = hp->dxfer_direction;
2039 unsigned char __user *up;
2040 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2042 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2043 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2044 num_xfer = hp->dxfer_len;
2045 if (schp->bufflen < num_xfer)
2046 num_xfer = schp->bufflen;
2048 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2050 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2053 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2054 num_xfer, iovec_count, schp->k_use_sg));
2057 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2062 p = page_address(sg->page);
2063 ksglen = sg->length;
2064 for (j = 0, k = 0; j < onum; ++j) {
2065 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2069 for (; p; ++sg, ksglen = sg->length,
2070 p = page_address(sg->page)) {
2073 if (ksglen > usglen) {
2074 if (usglen >= num_xfer) {
2075 if (__copy_to_user(up, p, num_xfer))
2079 if (__copy_to_user(up, p, usglen))
2085 if (ksglen >= num_xfer) {
2086 if (__copy_to_user(up, p, num_xfer))
2090 if (__copy_to_user(up, p, ksglen))
2096 if (k >= schp->k_use_sg)
2105 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2107 Sg_scatter_hold *schp = &srp->data;
2108 struct scatterlist *sg = schp->buffer;
2111 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2113 if ((!outp) || (num_read_xfer <= 0))
2116 for (k = 0; (k < schp->k_use_sg) && sg->page; ++k, ++sg) {
2118 if (num > num_read_xfer) {
2119 if (__copy_to_user(outp, page_address(sg->page),
2124 if (__copy_to_user(outp, page_address(sg->page),
2127 num_read_xfer -= num;
2128 if (num_read_xfer <= 0)
2138 sg_build_reserve(Sg_fd * sfp, int req_size)
2140 Sg_scatter_hold *schp = &sfp->reserve;
2142 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2144 if (req_size < PAGE_SIZE)
2145 req_size = PAGE_SIZE;
2146 if (0 == sg_build_indirect(schp, sfp, req_size))
2149 sg_remove_scat(schp);
2150 req_size >>= 1; /* divide by 2 */
2151 } while (req_size > (PAGE_SIZE / 2));
2155 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2157 Sg_scatter_hold *req_schp = &srp->data;
2158 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2159 struct scatterlist *sg = rsv_schp->buffer;
2163 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2166 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sg) {
2169 sfp->save_scat_len = num;
2171 req_schp->k_use_sg = k + 1;
2172 req_schp->sglist_len = rsv_schp->sglist_len;
2173 req_schp->buffer = rsv_schp->buffer;
2175 req_schp->bufflen = size;
2176 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2182 if (k >= rsv_schp->k_use_sg)
2183 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2187 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2189 Sg_scatter_hold *req_schp = &srp->data;
2190 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2192 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2193 (int) req_schp->k_use_sg));
2194 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2195 struct scatterlist *sg = rsv_schp->buffer;
2197 if (sfp->save_scat_len > 0)
2198 (sg + (req_schp->k_use_sg - 1))->length =
2199 (unsigned) sfp->save_scat_len;
2201 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2203 req_schp->k_use_sg = 0;
2204 req_schp->bufflen = 0;
2205 req_schp->buffer = NULL;
2206 req_schp->sglist_len = 0;
2207 sfp->save_scat_len = 0;
2212 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2215 unsigned long iflags;
2217 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2218 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2219 /* look for requests that are ready + not SG_IO owned */
2220 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2221 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2222 resp->done = 2; /* guard against other readers */
2226 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2230 #ifdef CONFIG_SCSI_PROC_FS
2232 sg_get_nth_request(Sg_fd * sfp, int nth)
2235 unsigned long iflags;
2238 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2239 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2240 ++k, resp = resp->nextrp) ;
2241 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2246 /* always adds to end of list */
2248 sg_add_request(Sg_fd * sfp)
2251 unsigned long iflags;
2253 Sg_request *rp = sfp->req_arr;
2255 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2258 memset(rp, 0, sizeof (Sg_request));
2263 if (0 == sfp->cmd_q)
2264 resp = NULL; /* command queuing disallowed */
2266 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2270 if (k < SG_MAX_QUEUE) {
2271 memset(rp, 0, sizeof (Sg_request));
2273 while (resp->nextrp)
2274 resp = resp->nextrp;
2282 resp->nextrp = NULL;
2283 resp->header.duration = jiffies_to_msecs(jiffies);
2285 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2289 /* Return of 1 for found; 0 for not found */
2291 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2293 Sg_request *prev_rp;
2295 unsigned long iflags;
2298 if ((!sfp) || (!srp) || (!sfp->headrp))
2300 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2301 prev_rp = sfp->headrp;
2302 if (srp == prev_rp) {
2303 sfp->headrp = prev_rp->nextrp;
2304 prev_rp->parentfp = NULL;
2307 while ((rp = prev_rp->nextrp)) {
2309 prev_rp->nextrp = rp->nextrp;
2310 rp->parentfp = NULL;
2317 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2321 #ifdef CONFIG_SCSI_PROC_FS
2323 sg_get_nth_sfp(Sg_device * sdp, int nth)
2326 unsigned long iflags;
2329 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2330 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2331 ++k, resp = resp->nextfp) ;
2332 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2338 sg_add_sfp(Sg_device * sdp, int dev)
2341 unsigned long iflags;
2343 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2347 init_waitqueue_head(&sfp->read_wait);
2348 rwlock_init(&sfp->rq_list_lock);
2350 sfp->timeout = SG_DEFAULT_TIMEOUT;
2351 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2352 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2353 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2354 sdp->device->host->unchecked_isa_dma : 1;
2355 sfp->cmd_q = SG_DEF_COMMAND_Q;
2356 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2357 sfp->parentdp = sdp;
2358 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2361 else { /* add to tail of existing list */
2362 Sg_fd *pfp = sdp->headfp;
2367 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2368 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2369 if (unlikely(sg_big_buff != def_reserved_size))
2370 sg_big_buff = def_reserved_size;
2372 sg_build_reserve(sfp, sg_big_buff);
2373 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2374 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2379 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2384 prev_fp = sdp->headfp;
2386 sdp->headfp = prev_fp->nextfp;
2388 while ((fp = prev_fp->nextfp)) {
2390 prev_fp->nextfp = fp->nextfp;
2396 if (sfp->reserve.bufflen > 0) {
2398 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2399 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2400 sg_remove_scat(&sfp->reserve);
2402 sfp->parentdp = NULL;
2403 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2407 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2409 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2416 for (srp = sfp->headrp; srp; srp = tsrp) {
2418 if (sg_srp_done(srp, sfp))
2419 sg_finish_rem_req(srp);
2424 unsigned long iflags;
2426 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2427 __sg_remove_sfp(sdp, sfp);
2428 if (sdp->detached && (NULL == sdp->headfp)) {
2432 for (k = 0; k < maxd; ++k) {
2433 if (sdp == sg_dev_arr[k])
2437 sg_dev_arr[k] = NULL;
2438 kfree((char *) sdp);
2441 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2443 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2444 /* only bump the access_count if we actually succeeded in
2445 * throwing another counter on the host module */
2446 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2447 sfp->closed = 1; /* flag dirty state on this fd */
2448 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2455 sg_res_in_use(Sg_fd * sfp)
2457 const Sg_request *srp;
2458 unsigned long iflags;
2460 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2461 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2464 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2468 /* The size fetched (value output via retSzp) set when non-NULL return */
2469 static struct page *
2470 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2472 struct page *resp = NULL;
2477 if ((rqSz <= 0) || (NULL == retSzp))
2481 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
2483 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
2485 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2486 order++, a_size <<= 1) ;
2487 resSz = a_size; /* rounded up if necessary */
2488 resp = alloc_pages(page_mask, order);
2489 while ((!resp) && order) {
2491 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2492 resp = alloc_pages(page_mask, order); /* try half */
2496 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2497 memset(page_address(resp), 0, resSz);
2504 sg_page_free(struct page *page, int size)
2510 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2511 order++, a_size <<= 1) ;
2512 __free_pages(page, order);
2515 #ifndef MAINTENANCE_IN_CMD
2516 #define MAINTENANCE_IN_CMD 0xa3
2519 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2520 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2521 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2522 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2526 sg_allow_access(unsigned char opcode, char dev_type)
2530 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2532 for (k = 0; k < sizeof (allow_ops); ++k) {
2533 if (opcode == allow_ops[k])
2539 #ifdef CONFIG_SCSI_PROC_FS
2544 unsigned long iflags;
2546 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2547 for (k = sg_dev_max - 1; k >= 0; --k)
2548 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2550 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2551 return k + 1; /* origin 1 */
2558 Sg_device *sdp = NULL;
2559 unsigned long iflags;
2561 if (sg_dev_arr && (dev >= 0)) {
2562 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2563 if (dev < sg_dev_max)
2564 sdp = sg_dev_arr[dev];
2565 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2570 #ifdef CONFIG_SCSI_PROC_FS
2572 static struct proc_dir_entry *sg_proc_sgp = NULL;
2574 static char sg_proc_sg_dirname[] = "scsi/sg";
2576 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2578 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2579 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2580 size_t count, loff_t *off);
2581 static struct file_operations adio_fops = {
2582 /* .owner, .read and .llseek added in sg_proc_init() */
2583 .open = sg_proc_single_open_adio,
2584 .write = sg_proc_write_adio,
2585 .release = single_release,
2588 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2589 static ssize_t sg_proc_write_dressz(struct file *filp,
2590 const char __user *buffer, size_t count, loff_t *off);
2591 static struct file_operations dressz_fops = {
2592 .open = sg_proc_single_open_dressz,
2593 .write = sg_proc_write_dressz,
2594 .release = single_release,
2597 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2598 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2599 static struct file_operations version_fops = {
2600 .open = sg_proc_single_open_version,
2601 .release = single_release,
2604 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2605 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2606 static struct file_operations devhdr_fops = {
2607 .open = sg_proc_single_open_devhdr,
2608 .release = single_release,
2611 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2612 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2613 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2614 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2615 static void dev_seq_stop(struct seq_file *s, void *v);
2616 static struct file_operations dev_fops = {
2617 .open = sg_proc_open_dev,
2618 .release = seq_release,
2620 static struct seq_operations dev_seq_ops = {
2621 .start = dev_seq_start,
2622 .next = dev_seq_next,
2623 .stop = dev_seq_stop,
2624 .show = sg_proc_seq_show_dev,
2627 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2628 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2629 static struct file_operations devstrs_fops = {
2630 .open = sg_proc_open_devstrs,
2631 .release = seq_release,
2633 static struct seq_operations devstrs_seq_ops = {
2634 .start = dev_seq_start,
2635 .next = dev_seq_next,
2636 .stop = dev_seq_stop,
2637 .show = sg_proc_seq_show_devstrs,
2640 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2641 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2642 static struct file_operations debug_fops = {
2643 .open = sg_proc_open_debug,
2644 .release = seq_release,
2646 static struct seq_operations debug_seq_ops = {
2647 .start = dev_seq_start,
2648 .next = dev_seq_next,
2649 .stop = dev_seq_stop,
2650 .show = sg_proc_seq_show_debug,
2654 struct sg_proc_leaf {
2656 struct file_operations * fops;
2659 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2660 {"allow_dio", &adio_fops},
2661 {"debug", &debug_fops},
2662 {"def_reserved_size", &dressz_fops},
2663 {"device_hdr", &devhdr_fops},
2664 {"devices", &dev_fops},
2665 {"device_strs", &devstrs_fops},
2666 {"version", &version_fops}
2673 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2674 struct proc_dir_entry *pdep;
2675 struct sg_proc_leaf * leaf;
2677 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2680 for (k = 0; k < num_leaves; ++k) {
2681 leaf = &sg_proc_leaf_arr[k];
2682 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2683 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2685 leaf->fops->owner = THIS_MODULE,
2686 leaf->fops->read = seq_read,
2687 leaf->fops->llseek = seq_lseek,
2688 pdep->proc_fops = leaf->fops;
2695 sg_proc_cleanup(void)
2698 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2702 for (k = 0; k < num_leaves; ++k)
2703 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2704 remove_proc_entry(sg_proc_sg_dirname, NULL);
2708 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2710 seq_printf(s, "%d\n", *((int *)s->private));
2714 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2716 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2720 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2721 size_t count, loff_t *off)
2726 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2728 num = (count < 10) ? count : 10;
2729 if (copy_from_user(buff, buffer, num))
2732 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2736 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2738 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2742 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2743 size_t count, loff_t *off)
2746 unsigned long k = ULONG_MAX;
2749 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2751 num = (count < 10) ? count : 10;
2752 if (copy_from_user(buff, buffer, num))
2755 k = simple_strtoul(buff, NULL, 10);
2756 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2763 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2765 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2770 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2772 return single_open(file, sg_proc_seq_show_version, NULL);
2775 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2777 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2782 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2784 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2787 struct sg_proc_deviter {
2792 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2794 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2800 if (NULL == sg_dev_arr)
2803 it->max = sg_last_dev();
2804 if (it->index >= it->max)
2809 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2811 struct sg_proc_deviter * it = s->private;
2814 return (it->index < it->max) ? it : NULL;
2817 static void dev_seq_stop(struct seq_file *s, void *v)
2822 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2824 return seq_open(file, &dev_seq_ops);
2827 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2829 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2831 struct scsi_device *scsidp;
2833 sdp = it ? sg_get_dev(it->index) : NULL;
2834 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2835 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2836 scsidp->host->host_no, scsidp->channel,
2837 scsidp->id, scsidp->lun, (int) scsidp->type,
2839 (int) scsidp->queue_depth,
2840 (int) scsidp->device_busy,
2841 (int) scsi_device_online(scsidp));
2843 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2847 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2849 return seq_open(file, &devstrs_seq_ops);
2852 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2854 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2856 struct scsi_device *scsidp;
2858 sdp = it ? sg_get_dev(it->index) : NULL;
2859 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2860 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2861 scsidp->vendor, scsidp->model, scsidp->rev);
2863 seq_printf(s, "<no active device>\n");
2867 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2869 int k, m, new_interface, blen, usg;
2872 const sg_io_hdr_t *hp;
2876 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2877 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2878 "(res)sgat=%d low_dma=%d\n", k + 1,
2879 jiffies_to_msecs(fp->timeout),
2880 fp->reserve.bufflen,
2881 (int) fp->reserve.k_use_sg,
2883 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2884 (int) fp->cmd_q, (int) fp->force_packid,
2885 (int) fp->keep_orphan, (int) fp->closed);
2886 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2888 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2889 if (srp->res_used) {
2890 if (new_interface &&
2891 (SG_FLAG_MMAP_IO & hp->flags))
2896 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2902 blen = srp->data.bufflen;
2903 usg = srp->data.k_use_sg;
2904 seq_printf(s, srp->done ?
2905 ((1 == srp->done) ? "rcv:" : "fin:")
2907 seq_printf(s, " id=%d blen=%d",
2908 srp->header.pack_id, blen);
2910 seq_printf(s, " dur=%d", hp->duration);
2912 ms = jiffies_to_msecs(jiffies);
2913 seq_printf(s, " t_o/elap=%d/%d",
2914 (new_interface ? hp->timeout :
2915 jiffies_to_msecs(fp->timeout)),
2916 (ms > hp->duration ? ms - hp->duration : 0));
2918 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2919 (int) srp->data.cmd_opcode);
2922 seq_printf(s, " No requests active\n");
2926 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2928 return seq_open(file, &debug_seq_ops);
2931 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2933 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2936 if (it && (0 == it->index)) {
2937 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
2938 "(origin 1)\n", sg_dev_max, (int)it->max);
2939 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2941 sdp = it ? sg_get_dev(it->index) : NULL;
2943 struct scsi_device *scsidp = sdp->device;
2945 if (NULL == scsidp) {
2946 seq_printf(s, "device %d detached ??\n",
2951 if (sg_get_nth_sfp(sdp, 0)) {
2952 seq_printf(s, " >>> device=%s ",
2953 sdp->disk->disk_name);
2955 seq_printf(s, "detached pending close ");
2958 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2959 scsidp->host->host_no,
2960 scsidp->channel, scsidp->id,
2962 scsidp->host->hostt->emulated);
2963 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2964 sdp->sg_tablesize, sdp->exclude);
2966 sg_proc_debug_helper(s, sdp);
2971 #endif /* CONFIG_SCSI_PROC_FS */
2973 module_init(init_sg);
2974 module_exit(exit_sg);