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
= 30533; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.33"
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/config.h>
32 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/mtio.h>
41 #include <linux/ioctl.h>
42 #include <linux/fcntl.h>
43 #include <linux/init.h>
44 #include <linux/poll.h>
45 #include <linux/smp_lock.h>
46 #include <linux/moduleparam.h>
47 #include <linux/cdev.h>
48 #include <linux/seq_file.h>
49 #include <linux/blkdev.h>
50 #include <linux/delay.h>
51 #include <linux/scatterlist.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
= "20050908";
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 #define SG_SECTOR_SZ 512
99 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
101 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
103 static int sg_add(struct class_device
*, struct class_interface
*);
104 static void sg_remove(struct class_device
*, struct class_interface
*);
106 static DEFINE_RWLOCK(sg_dev_arr_lock
); /* Also used to lock
107 file descriptor list for device */
109 static struct class_interface sg_interface
= {
114 typedef struct sg_scatter_hold
{ /* holding area for scsi scatter gather info */
115 unsigned short k_use_sg
; /* Count of kernel scatter-gather pieces */
116 unsigned short sglist_len
; /* size of malloc'd scatter-gather list ++ */
117 unsigned bufflen
; /* Size of (aggregate) data buffer */
118 unsigned b_malloc_len
; /* actual len malloc'ed in buffer */
119 struct scatterlist
*buffer
;/* scatter list */
120 char dio_in_use
; /* 0->indirect IO (or mmap), 1->dio */
121 unsigned char cmd_opcode
; /* first byte of command */
124 struct sg_device
; /* forward declarations */
127 typedef struct sg_request
{ /* SG_MAX_QUEUE requests outstanding per file */
128 struct sg_request
*nextrp
; /* NULL -> tail request (slist) */
129 struct sg_fd
*parentfp
; /* NULL -> not in use */
130 Sg_scatter_hold data
; /* hold buffer, perhaps scatter list */
131 sg_io_hdr_t header
; /* scsi command+info, see <scsi/sg.h> */
132 unsigned char sense_b
[SCSI_SENSE_BUFFERSIZE
];
133 char res_used
; /* 1 -> using reserve buffer, 0 -> not ... */
134 char orphan
; /* 1 -> drop on sight, 0 -> normal */
135 char sg_io_owned
; /* 1 -> packet belongs to SG_IO */
136 volatile char done
; /* 0->before bh, 1->before read, 2->read */
139 typedef struct sg_fd
{ /* holds the state of a file descriptor */
140 struct sg_fd
*nextfp
; /* NULL when last opened fd on this device */
141 struct sg_device
*parentdp
; /* owning device */
142 wait_queue_head_t read_wait
; /* queue read until command done */
143 rwlock_t rq_list_lock
; /* protect access to list in req_arr */
144 int timeout
; /* defaults to SG_DEFAULT_TIMEOUT */
145 int timeout_user
; /* defaults to SG_DEFAULT_TIMEOUT_USER */
146 Sg_scatter_hold reserve
; /* buffer held for this file descriptor */
147 unsigned save_scat_len
; /* original length of trunc. scat. element */
148 Sg_request
*headrp
; /* head of request slist, NULL->empty */
149 struct fasync_struct
*async_qp
; /* used by asynchronous notification */
150 Sg_request req_arr
[SG_MAX_QUEUE
]; /* used as singly-linked list */
151 char low_dma
; /* as in parent but possibly overridden to 1 */
152 char force_packid
; /* 1 -> pack_id input to read(), 0 -> ignored */
153 volatile char closed
; /* 1 -> fd closed but request(s) outstanding */
154 char cmd_q
; /* 1 -> allow command queuing, 0 -> don't */
155 char next_cmd_len
; /* 0 -> automatic (def), >0 -> use on next write() */
156 char keep_orphan
; /* 0 -> drop orphan (def), 1 -> keep for read() */
157 char mmap_called
; /* 0 -> mmap() never called on this fd */
160 typedef struct sg_device
{ /* holds the state of each scsi generic device */
161 struct scsi_device
*device
;
162 wait_queue_head_t o_excl_wait
; /* queue open() when O_EXCL in use */
163 int sg_tablesize
; /* adapter's max scatter-gather table size */
164 Sg_fd
*headfp
; /* first open fd belonging to this device */
165 volatile char detached
; /* 0->attached, 1->detached pending removal */
166 volatile char exclude
; /* opened for exclusive access */
167 char sgdebug
; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
168 struct gendisk
*disk
;
169 struct cdev
* cdev
; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
172 static int sg_fasync(int fd
, struct file
*filp
, int mode
);
173 /* tasklet or soft irq callback */
174 static void sg_cmd_done(void *data
, char *sense
, int result
, int resid
);
175 static int sg_start_req(Sg_request
* srp
);
176 static void sg_finish_rem_req(Sg_request
* srp
);
177 static int sg_build_indirect(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
);
178 static int sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
,
180 static ssize_t
sg_new_read(Sg_fd
* sfp
, char __user
*buf
, size_t count
,
182 static ssize_t
sg_new_write(Sg_fd
* sfp
, const char __user
*buf
, size_t count
,
183 int blocking
, int read_only
, Sg_request
** o_srp
);
184 static int sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
185 unsigned char *cmnd
, int timeout
, int blocking
);
186 static int sg_u_iovec(sg_io_hdr_t
* hp
, int sg_num
, int ind
,
187 int wr_xf
, int *countp
, unsigned char __user
**up
);
188 static int sg_write_xfer(Sg_request
* srp
);
189 static int sg_read_xfer(Sg_request
* srp
);
190 static int sg_read_oxfer(Sg_request
* srp
, char __user
*outp
, int num_read_xfer
);
191 static void sg_remove_scat(Sg_scatter_hold
* schp
);
192 static void sg_build_reserve(Sg_fd
* sfp
, int req_size
);
193 static void sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
);
194 static void sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
);
195 static struct page
*sg_page_malloc(int rqSz
, int lowDma
, int *retSzp
);
196 static void sg_page_free(struct page
*page
, int size
);
197 static Sg_fd
*sg_add_sfp(Sg_device
* sdp
, int dev
);
198 static int sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
);
199 static void __sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
);
200 static Sg_request
*sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
);
201 static Sg_request
*sg_add_request(Sg_fd
* sfp
);
202 static int sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
);
203 static int sg_res_in_use(Sg_fd
* sfp
);
204 static int sg_allow_access(unsigned char opcode
, char dev_type
);
205 static int sg_build_direct(Sg_request
* srp
, Sg_fd
* sfp
, int dxfer_len
);
206 static Sg_device
*sg_get_dev(int dev
);
207 #ifdef CONFIG_SCSI_PROC_FS
208 static int sg_last_dev(void);
211 static Sg_device
**sg_dev_arr
= NULL
;
212 static int sg_dev_max
;
213 static int sg_nr_dev
;
215 #define SZ_SG_HEADER sizeof(struct sg_header)
216 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
217 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
218 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
221 sg_open(struct inode
*inode
, struct file
*filp
)
223 int dev
= iminor(inode
);
224 int flags
= filp
->f_flags
;
225 struct request_queue
*q
;
231 nonseekable_open(inode
, filp
);
232 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev
, flags
));
233 sdp
= sg_get_dev(dev
);
234 if ((!sdp
) || (!sdp
->device
))
239 /* This driver's module count bumped by fops_get in <linux/fs.h> */
240 /* Prevent the device driver from vanishing while we sleep */
241 retval
= scsi_device_get(sdp
->device
);
245 if (!((flags
& O_NONBLOCK
) ||
246 scsi_block_when_processing_errors(sdp
->device
))) {
248 /* we are in error recovery for this device */
252 if (flags
& O_EXCL
) {
253 if (O_RDONLY
== (flags
& O_ACCMODE
)) {
254 retval
= -EPERM
; /* Can't lock it with read only access */
257 if (sdp
->headfp
&& (flags
& O_NONBLOCK
)) {
262 __wait_event_interruptible(sdp
->o_excl_wait
,
263 ((sdp
->headfp
|| sdp
->exclude
) ? 0 : (sdp
->exclude
= 1)), res
);
265 retval
= res
; /* -ERESTARTSYS because signal hit process */
268 } else if (sdp
->exclude
) { /* some other fd has an exclusive lock on dev */
269 if (flags
& O_NONBLOCK
) {
274 __wait_event_interruptible(sdp
->o_excl_wait
, (!sdp
->exclude
),
277 retval
= res
; /* -ERESTARTSYS because signal hit process */
285 if (!sdp
->headfp
) { /* no existing opens on this device */
287 q
= sdp
->device
->request_queue
;
288 sdp
->sg_tablesize
= min(q
->max_hw_segments
,
289 q
->max_phys_segments
);
291 if ((sfp
= sg_add_sfp(sdp
, dev
)))
292 filp
->private_data
= sfp
;
295 sdp
->exclude
= 0; /* undo if error */
302 scsi_device_put(sdp
->device
);
306 /* Following function was formerly called 'sg_close' */
308 sg_release(struct inode
*inode
, struct file
*filp
)
313 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
315 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp
->disk
->disk_name
));
316 sg_fasync(-1, filp
, 0); /* remove filp from async notification list */
317 if (0 == sg_remove_sfp(sdp
, sfp
)) { /* Returns 1 when sdp gone */
318 if (!sdp
->detached
) {
319 scsi_device_put(sdp
->device
);
322 wake_up_interruptible(&sdp
->o_excl_wait
);
328 sg_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
* ppos
)
333 int req_pack_id
= -1;
335 struct sg_header
*old_hdr
= NULL
;
338 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
340 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
341 sdp
->disk
->disk_name
, (int) count
));
343 if (!access_ok(VERIFY_WRITE
, buf
, count
))
345 if (sfp
->force_packid
&& (count
>= SZ_SG_HEADER
)) {
346 old_hdr
= kmalloc(SZ_SG_HEADER
, GFP_KERNEL
);
349 if (__copy_from_user(old_hdr
, buf
, SZ_SG_HEADER
)) {
353 if (old_hdr
->reply_len
< 0) {
354 if (count
>= SZ_SG_IO_HDR
) {
355 sg_io_hdr_t
*new_hdr
;
356 new_hdr
= kmalloc(SZ_SG_IO_HDR
, GFP_KERNEL
);
361 retval
=__copy_from_user
362 (new_hdr
, buf
, SZ_SG_IO_HDR
);
363 req_pack_id
= new_hdr
->pack_id
;
371 req_pack_id
= old_hdr
->pack_id
;
373 srp
= sg_get_rq_mark(sfp
, req_pack_id
);
374 if (!srp
) { /* now wait on packet to arrive */
379 if (filp
->f_flags
& O_NONBLOCK
) {
384 retval
= 0; /* following macro beats race condition */
385 __wait_event_interruptible(sfp
->read_wait
,
387 (srp
= sg_get_rq_mark(sfp
, req_pack_id
))),
396 /* -ERESTARTSYS as signal hit process */
400 if (srp
->header
.interface_id
!= '\0') {
401 retval
= sg_new_read(sfp
, buf
, count
, srp
);
406 if (old_hdr
== NULL
) {
407 old_hdr
= kmalloc(SZ_SG_HEADER
, GFP_KERNEL
);
413 memset(old_hdr
, 0, SZ_SG_HEADER
);
414 old_hdr
->reply_len
= (int) hp
->timeout
;
415 old_hdr
->pack_len
= old_hdr
->reply_len
; /* old, strange behaviour */
416 old_hdr
->pack_id
= hp
->pack_id
;
417 old_hdr
->twelve_byte
=
418 ((srp
->data
.cmd_opcode
>= 0xc0) && (12 == hp
->cmd_len
)) ? 1 : 0;
419 old_hdr
->target_status
= hp
->masked_status
;
420 old_hdr
->host_status
= hp
->host_status
;
421 old_hdr
->driver_status
= hp
->driver_status
;
422 if ((CHECK_CONDITION
& hp
->masked_status
) ||
423 (DRIVER_SENSE
& hp
->driver_status
))
424 memcpy(old_hdr
->sense_buffer
, srp
->sense_b
,
425 sizeof (old_hdr
->sense_buffer
));
426 switch (hp
->host_status
) {
427 /* This setup of 'result' is for backward compatibility and is best
428 ignored by the user who should use target, host + driver status */
430 case DID_PASSTHROUGH
:
437 old_hdr
->result
= EBUSY
;
444 old_hdr
->result
= EIO
;
447 old_hdr
->result
= (srp
->sense_b
[0] == 0 &&
448 hp
->masked_status
== GOOD
) ? 0 : EIO
;
451 old_hdr
->result
= EIO
;
455 /* Now copy the result back to the user buffer. */
456 if (count
>= SZ_SG_HEADER
) {
457 if (__copy_to_user(buf
, old_hdr
, SZ_SG_HEADER
)) {
462 if (count
> old_hdr
->reply_len
)
463 count
= old_hdr
->reply_len
;
464 if (count
> SZ_SG_HEADER
) {
465 if (sg_read_oxfer(srp
, buf
, count
- SZ_SG_HEADER
)) {
471 count
= (old_hdr
->result
== 0) ? 0 : -EIO
;
472 sg_finish_rem_req(srp
);
480 sg_new_read(Sg_fd
* sfp
, char __user
*buf
, size_t count
, Sg_request
* srp
)
482 sg_io_hdr_t
*hp
= &srp
->header
;
486 if (count
< SZ_SG_IO_HDR
) {
491 if ((hp
->mx_sb_len
> 0) && hp
->sbp
) {
492 if ((CHECK_CONDITION
& hp
->masked_status
) ||
493 (DRIVER_SENSE
& hp
->driver_status
)) {
494 int sb_len
= SCSI_SENSE_BUFFERSIZE
;
495 sb_len
= (hp
->mx_sb_len
> sb_len
) ? sb_len
: hp
->mx_sb_len
;
496 len
= 8 + (int) srp
->sense_b
[7]; /* Additional sense length field */
497 len
= (len
> sb_len
) ? sb_len
: len
;
498 if (copy_to_user(hp
->sbp
, srp
->sense_b
, len
)) {
505 if (hp
->masked_status
|| hp
->host_status
|| hp
->driver_status
)
506 hp
->info
|= SG_INFO_CHECK
;
507 if (copy_to_user(buf
, hp
, SZ_SG_IO_HDR
)) {
511 err
= sg_read_xfer(srp
);
513 sg_finish_rem_req(srp
);
514 return (0 == err
) ? count
: err
;
518 sg_write(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
* ppos
)
520 int mxsize
, cmd_size
, k
;
521 int input_size
, blocking
;
522 unsigned char opcode
;
526 struct sg_header old_hdr
;
528 unsigned char cmnd
[MAX_COMMAND_SIZE
];
530 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
532 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
533 sdp
->disk
->disk_name
, (int) count
));
536 if (!((filp
->f_flags
& O_NONBLOCK
) ||
537 scsi_block_when_processing_errors(sdp
->device
)))
540 if (!access_ok(VERIFY_READ
, buf
, count
))
541 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
542 if (count
< SZ_SG_HEADER
)
544 if (__copy_from_user(&old_hdr
, buf
, SZ_SG_HEADER
))
546 blocking
= !(filp
->f_flags
& O_NONBLOCK
);
547 if (old_hdr
.reply_len
< 0)
548 return sg_new_write(sfp
, buf
, count
, blocking
, 0, NULL
);
549 if (count
< (SZ_SG_HEADER
+ 6))
550 return -EIO
; /* The minimum scsi command length is 6 bytes. */
552 if (!(srp
= sg_add_request(sfp
))) {
553 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
557 __get_user(opcode
, buf
);
558 if (sfp
->next_cmd_len
> 0) {
559 if (sfp
->next_cmd_len
> MAX_COMMAND_SIZE
) {
560 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
561 sfp
->next_cmd_len
= 0;
562 sg_remove_request(sfp
, srp
);
565 cmd_size
= sfp
->next_cmd_len
;
566 sfp
->next_cmd_len
= 0; /* reset so only this write() effected */
568 cmd_size
= COMMAND_SIZE(opcode
); /* based on SCSI command group */
569 if ((opcode
>= 0xc0) && old_hdr
.twelve_byte
)
572 SCSI_LOG_TIMEOUT(4, printk(
573 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode
, cmd_size
));
574 /* Determine buffer size. */
575 input_size
= count
- cmd_size
;
576 mxsize
= (input_size
> old_hdr
.reply_len
) ? input_size
: old_hdr
.reply_len
;
577 mxsize
-= SZ_SG_HEADER
;
578 input_size
-= SZ_SG_HEADER
;
579 if (input_size
< 0) {
580 sg_remove_request(sfp
, srp
);
581 return -EIO
; /* User did not pass enough bytes for this command. */
584 hp
->interface_id
= '\0'; /* indicator of old interface tunnelled */
585 hp
->cmd_len
= (unsigned char) cmd_size
;
589 hp
->dxfer_direction
= (old_hdr
.reply_len
> SZ_SG_HEADER
) ?
590 SG_DXFER_TO_FROM_DEV
: SG_DXFER_TO_DEV
;
592 hp
->dxfer_direction
= (mxsize
> 0) ? SG_DXFER_FROM_DEV
: SG_DXFER_NONE
;
593 hp
->dxfer_len
= mxsize
;
594 hp
->dxferp
= (char __user
*)buf
+ cmd_size
;
596 hp
->timeout
= old_hdr
.reply_len
; /* structure abuse ... */
597 hp
->flags
= input_size
; /* structure abuse ... */
598 hp
->pack_id
= old_hdr
.pack_id
;
600 if (__copy_from_user(cmnd
, buf
, cmd_size
))
603 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
604 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
605 * is a non-zero input_size, so emit a warning.
607 if (hp
->dxfer_direction
== SG_DXFER_TO_FROM_DEV
)
608 if (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 k
= sg_common_write(sfp
, srp
, cmnd
, sfp
->timeout
, blocking
);
617 return (k
< 0) ? k
: count
;
621 sg_new_write(Sg_fd
* sfp
, const char __user
*buf
, size_t count
,
622 int blocking
, int read_only
, Sg_request
** o_srp
)
627 unsigned char cmnd
[MAX_COMMAND_SIZE
];
629 unsigned long ul_timeout
;
631 if (count
< SZ_SG_IO_HDR
)
633 if (!access_ok(VERIFY_READ
, buf
, count
))
634 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
636 sfp
->cmd_q
= 1; /* when sg_io_hdr seen, set command queuing on */
637 if (!(srp
= sg_add_request(sfp
))) {
638 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
642 if (__copy_from_user(hp
, buf
, SZ_SG_IO_HDR
)) {
643 sg_remove_request(sfp
, srp
);
646 if (hp
->interface_id
!= 'S') {
647 sg_remove_request(sfp
, srp
);
650 if (hp
->flags
& SG_FLAG_MMAP_IO
) {
651 if (hp
->dxfer_len
> sfp
->reserve
.bufflen
) {
652 sg_remove_request(sfp
, srp
);
653 return -ENOMEM
; /* MMAP_IO size must fit in reserve buffer */
655 if (hp
->flags
& SG_FLAG_DIRECT_IO
) {
656 sg_remove_request(sfp
, srp
);
657 return -EINVAL
; /* either MMAP_IO or DIRECT_IO (not both) */
659 if (sg_res_in_use(sfp
)) {
660 sg_remove_request(sfp
, srp
);
661 return -EBUSY
; /* reserve buffer already being used */
664 ul_timeout
= msecs_to_jiffies(srp
->header
.timeout
);
665 timeout
= (ul_timeout
< INT_MAX
) ? ul_timeout
: INT_MAX
;
666 if ((!hp
->cmdp
) || (hp
->cmd_len
< 6) || (hp
->cmd_len
> sizeof (cmnd
))) {
667 sg_remove_request(sfp
, srp
);
670 if (!access_ok(VERIFY_READ
, hp
->cmdp
, hp
->cmd_len
)) {
671 sg_remove_request(sfp
, srp
);
672 return -EFAULT
; /* protects following copy_from_user()s + get_user()s */
674 if (__copy_from_user(cmnd
, hp
->cmdp
, hp
->cmd_len
)) {
675 sg_remove_request(sfp
, srp
);
679 (!sg_allow_access(cmnd
[0], sfp
->parentdp
->device
->type
))) {
680 sg_remove_request(sfp
, srp
);
683 k
= sg_common_write(sfp
, srp
, cmnd
, timeout
, blocking
);
692 sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
693 unsigned char *cmnd
, int timeout
, int blocking
)
696 Sg_device
*sdp
= sfp
->parentdp
;
697 sg_io_hdr_t
*hp
= &srp
->header
;
699 srp
->data
.cmd_opcode
= cmnd
[0]; /* hold opcode of command */
701 hp
->masked_status
= 0;
705 hp
->driver_status
= 0;
707 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
708 (int) cmnd
[0], (int) hp
->cmd_len
));
710 if ((k
= sg_start_req(srp
))) {
711 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k
));
712 sg_finish_rem_req(srp
);
713 return k
; /* probably out of space --> ENOMEM */
715 if ((k
= sg_write_xfer(srp
))) {
716 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
717 sg_finish_rem_req(srp
);
721 sg_finish_rem_req(srp
);
725 switch (hp
->dxfer_direction
) {
726 case SG_DXFER_TO_FROM_DEV
:
727 case SG_DXFER_FROM_DEV
:
728 data_dir
= DMA_FROM_DEVICE
;
730 case SG_DXFER_TO_DEV
:
731 data_dir
= DMA_TO_DEVICE
;
733 case SG_DXFER_UNKNOWN
:
734 data_dir
= DMA_BIDIRECTIONAL
;
740 hp
->duration
= jiffies_to_msecs(jiffies
);
741 /* Now send everything of to mid-level. The next time we hear about this
742 packet is when sg_cmd_done() is called (i.e. a callback). */
743 if (scsi_execute_async(sdp
->device
, cmnd
, hp
->cmd_len
, data_dir
, srp
->data
.buffer
,
744 hp
->dxfer_len
, srp
->data
.k_use_sg
, timeout
,
745 SG_DEFAULT_RETRIES
, srp
, sg_cmd_done
,
747 SCSI_LOG_TIMEOUT(1, printk("sg_write: scsi_execute_async failed\n"));
749 * most likely out of mem, but could also be a bad map
757 sg_srp_done(Sg_request
*srp
, Sg_fd
*sfp
)
759 unsigned long iflags
;
762 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
764 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
769 sg_ioctl(struct inode
*inode
, struct file
*filp
,
770 unsigned int cmd_in
, unsigned long arg
)
772 void __user
*p
= (void __user
*)arg
;
774 int result
, val
, read_only
;
778 unsigned long iflags
;
780 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
782 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
783 sdp
->disk
->disk_name
, (int) cmd_in
));
784 read_only
= (O_RDWR
!= (filp
->f_flags
& O_ACCMODE
));
789 int blocking
= 1; /* ignore O_NONBLOCK flag */
793 if (!scsi_block_when_processing_errors(sdp
->device
))
795 if (!access_ok(VERIFY_WRITE
, p
, SZ_SG_IO_HDR
))
798 sg_new_write(sfp
, p
, SZ_SG_IO_HDR
,
799 blocking
, read_only
, &srp
);
802 srp
->sg_io_owned
= 1;
804 result
= 0; /* following macro to beat race condition */
805 __wait_event_interruptible(sfp
->read_wait
,
806 (sdp
->detached
|| sfp
->closed
|| sg_srp_done(srp
, sfp
)),
811 return 0; /* request packet dropped already */
815 return result
; /* -ERESTARTSYS because signal hit process */
817 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
819 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
820 result
= sg_new_read(sfp
, p
, SZ_SG_IO_HDR
, srp
);
821 return (result
< 0) ? result
: 0;
824 result
= get_user(val
, ip
);
829 if (val
>= MULDIV (INT_MAX
, USER_HZ
, HZ
))
830 val
= MULDIV (INT_MAX
, USER_HZ
, HZ
);
831 sfp
->timeout_user
= val
;
832 sfp
->timeout
= MULDIV (val
, HZ
, USER_HZ
);
835 case SG_GET_TIMEOUT
: /* N.B. User receives timeout as return value */
836 /* strange ..., for backward compatibility */
837 return sfp
->timeout_user
;
838 case SG_SET_FORCE_LOW_DMA
:
839 result
= get_user(val
, ip
);
844 if ((0 == sfp
->low_dma
) && (0 == sg_res_in_use(sfp
))) {
845 val
= (int) sfp
->reserve
.bufflen
;
846 sg_remove_scat(&sfp
->reserve
);
847 sg_build_reserve(sfp
, val
);
852 sfp
->low_dma
= sdp
->device
->host
->unchecked_isa_dma
;
856 return put_user((int) sfp
->low_dma
, ip
);
858 if (!access_ok(VERIFY_WRITE
, p
, sizeof (sg_scsi_id_t
)))
861 sg_scsi_id_t __user
*sg_idp
= p
;
865 __put_user((int) sdp
->device
->host
->host_no
,
867 __put_user((int) sdp
->device
->channel
,
869 __put_user((int) sdp
->device
->id
, &sg_idp
->scsi_id
);
870 __put_user((int) sdp
->device
->lun
, &sg_idp
->lun
);
871 __put_user((int) sdp
->device
->type
, &sg_idp
->scsi_type
);
872 __put_user((short) sdp
->device
->host
->cmd_per_lun
,
873 &sg_idp
->h_cmd_per_lun
);
874 __put_user((short) sdp
->device
->queue_depth
,
875 &sg_idp
->d_queue_depth
);
876 __put_user(0, &sg_idp
->unused
[0]);
877 __put_user(0, &sg_idp
->unused
[1]);
880 case SG_SET_FORCE_PACK_ID
:
881 result
= get_user(val
, ip
);
884 sfp
->force_packid
= val
? 1 : 0;
887 if (!access_ok(VERIFY_WRITE
, ip
, sizeof (int)))
889 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
890 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
891 if ((1 == srp
->done
) && (!srp
->sg_io_owned
)) {
892 read_unlock_irqrestore(&sfp
->rq_list_lock
,
894 __put_user(srp
->header
.pack_id
, ip
);
898 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
901 case SG_GET_NUM_WAITING
:
902 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
903 for (val
= 0, srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
904 if ((1 == srp
->done
) && (!srp
->sg_io_owned
))
907 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
908 return put_user(val
, ip
);
909 case SG_GET_SG_TABLESIZE
:
910 return put_user(sdp
->sg_tablesize
, ip
);
911 case SG_SET_RESERVED_SIZE
:
912 result
= get_user(val
, ip
);
917 if (val
!= sfp
->reserve
.bufflen
) {
918 if (sg_res_in_use(sfp
) || sfp
->mmap_called
)
920 sg_remove_scat(&sfp
->reserve
);
921 sg_build_reserve(sfp
, val
);
924 case SG_GET_RESERVED_SIZE
:
925 val
= (int) sfp
->reserve
.bufflen
;
926 return put_user(val
, ip
);
927 case SG_SET_COMMAND_Q
:
928 result
= get_user(val
, ip
);
931 sfp
->cmd_q
= val
? 1 : 0;
933 case SG_GET_COMMAND_Q
:
934 return put_user((int) sfp
->cmd_q
, ip
);
935 case SG_SET_KEEP_ORPHAN
:
936 result
= get_user(val
, ip
);
939 sfp
->keep_orphan
= val
;
941 case SG_GET_KEEP_ORPHAN
:
942 return put_user((int) sfp
->keep_orphan
, ip
);
943 case SG_NEXT_CMD_LEN
:
944 result
= get_user(val
, ip
);
947 sfp
->next_cmd_len
= (val
> 0) ? val
: 0;
949 case SG_GET_VERSION_NUM
:
950 return put_user(sg_version_num
, ip
);
951 case SG_GET_ACCESS_COUNT
:
952 /* faked - we don't have a real access count anymore */
953 val
= (sdp
->device
? 1 : 0);
954 return put_user(val
, ip
);
955 case SG_GET_REQUEST_TABLE
:
956 if (!access_ok(VERIFY_WRITE
, p
, SZ_SG_REQ_INFO
* SG_MAX_QUEUE
))
959 sg_req_info_t
*rinfo
;
962 rinfo
= kmalloc(SZ_SG_REQ_INFO
* SG_MAX_QUEUE
,
966 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
967 for (srp
= sfp
->headrp
, val
= 0; val
< SG_MAX_QUEUE
;
968 ++val
, srp
= srp
? srp
->nextrp
: srp
) {
969 memset(&rinfo
[val
], 0, SZ_SG_REQ_INFO
);
971 rinfo
[val
].req_state
= srp
->done
+ 1;
973 srp
->header
.masked_status
&
974 srp
->header
.host_status
&
975 srp
->header
.driver_status
;
977 rinfo
[val
].duration
=
978 srp
->header
.duration
;
980 ms
= jiffies_to_msecs(jiffies
);
981 rinfo
[val
].duration
=
982 (ms
> srp
->header
.duration
) ?
983 (ms
- srp
->header
.duration
) : 0;
985 rinfo
[val
].orphan
= srp
->orphan
;
986 rinfo
[val
].sg_io_owned
=
994 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
995 result
= __copy_to_user(p
, rinfo
,
996 SZ_SG_REQ_INFO
* SG_MAX_QUEUE
);
997 result
= result
? -EFAULT
: 0;
1001 case SG_EMULATED_HOST
:
1004 return put_user(sdp
->device
->host
->hostt
->emulated
, ip
);
1008 if (filp
->f_flags
& O_NONBLOCK
) {
1009 if (scsi_host_in_recovery(sdp
->device
->host
))
1011 } else if (!scsi_block_when_processing_errors(sdp
->device
))
1013 result
= get_user(val
, ip
);
1016 if (SG_SCSI_RESET_NOTHING
== val
)
1019 case SG_SCSI_RESET_DEVICE
:
1020 val
= SCSI_TRY_RESET_DEVICE
;
1022 case SG_SCSI_RESET_BUS
:
1023 val
= SCSI_TRY_RESET_BUS
;
1025 case SG_SCSI_RESET_HOST
:
1026 val
= SCSI_TRY_RESET_HOST
;
1031 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
1033 return (scsi_reset_provider(sdp
->device
, val
) ==
1034 SUCCESS
) ? 0 : -EIO
;
1035 case SCSI_IOCTL_SEND_COMMAND
:
1039 unsigned char opcode
= WRITE_6
;
1040 Scsi_Ioctl_Command __user
*siocp
= p
;
1042 if (copy_from_user(&opcode
, siocp
->data
, 1))
1044 if (!sg_allow_access(opcode
, sdp
->device
->type
))
1047 return scsi_ioctl_send_command(sdp
->device
, p
);
1049 result
= get_user(val
, ip
);
1052 sdp
->sgdebug
= (char) val
;
1054 case SCSI_IOCTL_GET_IDLUN
:
1055 case SCSI_IOCTL_GET_BUS_NUMBER
:
1056 case SCSI_IOCTL_PROBE_HOST
:
1057 case SG_GET_TRANSFORM
:
1060 return scsi_ioctl(sdp
->device
, cmd_in
, p
);
1063 return -EPERM
; /* don't know so take safe approach */
1064 return scsi_ioctl(sdp
->device
, cmd_in
, p
);
1068 #ifdef CONFIG_COMPAT
1069 static long sg_compat_ioctl(struct file
*filp
, unsigned int cmd_in
, unsigned long arg
)
1073 struct scsi_device
*sdev
;
1075 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
1079 if (sdev
->host
->hostt
->compat_ioctl
) {
1082 ret
= sdev
->host
->hostt
->compat_ioctl(sdev
, cmd_in
, (void __user
*)arg
);
1087 return -ENOIOCTLCMD
;
1092 sg_poll(struct file
*filp
, poll_table
* wait
)
1094 unsigned int res
= 0;
1099 unsigned long iflags
;
1101 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
))
1104 poll_wait(filp
, &sfp
->read_wait
, wait
);
1105 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1106 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
1107 /* if any read waiting, flag it */
1108 if ((0 == res
) && (1 == srp
->done
) && (!srp
->sg_io_owned
))
1109 res
= POLLIN
| POLLRDNORM
;
1112 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1116 else if (!sfp
->cmd_q
) {
1118 res
|= POLLOUT
| POLLWRNORM
;
1119 } else if (count
< SG_MAX_QUEUE
)
1120 res
|= POLLOUT
| POLLWRNORM
;
1121 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1122 sdp
->disk
->disk_name
, (int) res
));
1127 sg_fasync(int fd
, struct file
*filp
, int mode
)
1133 if ((!(sfp
= (Sg_fd
*) filp
->private_data
)) || (!(sdp
= sfp
->parentdp
)))
1135 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1136 sdp
->disk
->disk_name
, mode
));
1138 retval
= fasync_helper(fd
, filp
, mode
, &sfp
->async_qp
);
1139 return (retval
< 0) ? retval
: 0;
1142 static struct page
*
1143 sg_vma_nopage(struct vm_area_struct
*vma
, unsigned long addr
, int *type
)
1146 struct page
*page
= NOPAGE_SIGBUS
;
1147 unsigned long offset
, len
, sa
;
1148 Sg_scatter_hold
*rsv_schp
;
1149 struct scatterlist
*sg
;
1152 if ((NULL
== vma
) || (!(sfp
= (Sg_fd
*) vma
->vm_private_data
)))
1154 rsv_schp
= &sfp
->reserve
;
1155 offset
= addr
- vma
->vm_start
;
1156 if (offset
>= rsv_schp
->bufflen
)
1158 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1159 offset
, rsv_schp
->k_use_sg
));
1160 sg
= rsv_schp
->buffer
;
1162 for (k
= 0; (k
< rsv_schp
->k_use_sg
) && (sa
< vma
->vm_end
);
1164 len
= vma
->vm_end
- sa
;
1165 len
= (len
< sg
->length
) ? len
: sg
->length
;
1168 get_page(page
); /* increment page count */
1176 *type
= VM_FAULT_MINOR
;
1180 static struct vm_operations_struct sg_mmap_vm_ops
= {
1181 .nopage
= sg_vma_nopage
,
1185 sg_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1188 unsigned long req_sz
, len
, sa
;
1189 Sg_scatter_hold
*rsv_schp
;
1191 struct scatterlist
*sg
;
1193 if ((!filp
) || (!vma
) || (!(sfp
= (Sg_fd
*) filp
->private_data
)))
1195 req_sz
= vma
->vm_end
- vma
->vm_start
;
1196 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1197 (void *) vma
->vm_start
, (int) req_sz
));
1199 return -EINVAL
; /* want no offset */
1200 rsv_schp
= &sfp
->reserve
;
1201 if (req_sz
> rsv_schp
->bufflen
)
1202 return -ENOMEM
; /* cannot map more than reserved buffer */
1205 sg
= rsv_schp
->buffer
;
1206 for (k
= 0; (k
< rsv_schp
->k_use_sg
) && (sa
< vma
->vm_end
);
1208 len
= vma
->vm_end
- sa
;
1209 len
= (len
< sg
->length
) ? len
: sg
->length
;
1213 sfp
->mmap_called
= 1;
1214 vma
->vm_flags
|= VM_RESERVED
;
1215 vma
->vm_private_data
= sfp
;
1216 vma
->vm_ops
= &sg_mmap_vm_ops
;
1220 /* This function is a "bottom half" handler that is called by the
1221 * mid level when a command is completed (or has failed). */
1223 sg_cmd_done(void *data
, char *sense
, int result
, int resid
)
1225 Sg_request
*srp
= data
;
1226 Sg_device
*sdp
= NULL
;
1228 unsigned long iflags
;
1232 printk(KERN_ERR
"sg_cmd_done: NULL request\n");
1235 sfp
= srp
->parentfp
;
1237 sdp
= sfp
->parentdp
;
1238 if ((NULL
== sdp
) || sdp
->detached
) {
1239 printk(KERN_INFO
"sg_cmd_done: device detached\n");
1244 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1245 sdp
->disk
->disk_name
, srp
->header
.pack_id
, result
));
1246 srp
->header
.resid
= resid
;
1247 ms
= jiffies_to_msecs(jiffies
);
1248 srp
->header
.duration
= (ms
> srp
->header
.duration
) ?
1249 (ms
- srp
->header
.duration
) : 0;
1251 struct scsi_sense_hdr sshdr
;
1253 memcpy(srp
->sense_b
, sense
, sizeof (srp
->sense_b
));
1254 srp
->header
.status
= 0xff & result
;
1255 srp
->header
.masked_status
= status_byte(result
);
1256 srp
->header
.msg_status
= msg_byte(result
);
1257 srp
->header
.host_status
= host_byte(result
);
1258 srp
->header
.driver_status
= driver_byte(result
);
1259 if ((sdp
->sgdebug
> 0) &&
1260 ((CHECK_CONDITION
== srp
->header
.masked_status
) ||
1261 (COMMAND_TERMINATED
== srp
->header
.masked_status
)))
1262 __scsi_print_sense("sg_cmd_done", sense
,
1263 SCSI_SENSE_BUFFERSIZE
);
1265 /* Following if statement is a patch supplied by Eric Youngdale */
1266 if (driver_byte(result
) != 0
1267 && scsi_normalize_sense(sense
, SCSI_SENSE_BUFFERSIZE
, &sshdr
)
1268 && !scsi_sense_is_deferred(&sshdr
)
1269 && sshdr
.sense_key
== UNIT_ATTENTION
1270 && sdp
->device
->removable
) {
1271 /* Detected possible disc change. Set the bit - this */
1272 /* may be used if there are filesystems using this device */
1273 sdp
->device
->changed
= 1;
1276 /* Rely on write phase to clean out srp status values, so no "else" */
1278 if (sfp
->closed
) { /* whoops this fd already released, cleanup */
1279 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1280 sg_finish_rem_req(srp
);
1282 if (NULL
== sfp
->headrp
) {
1283 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1284 if (0 == sg_remove_sfp(sdp
, sfp
)) { /* device still present */
1285 scsi_device_put(sdp
->device
);
1289 } else if (srp
&& srp
->orphan
) {
1290 if (sfp
->keep_orphan
)
1291 srp
->sg_io_owned
= 0;
1293 sg_finish_rem_req(srp
);
1298 /* Now wake up any sg_read() that is waiting for this packet. */
1299 kill_fasync(&sfp
->async_qp
, SIGPOLL
, POLL_IN
);
1300 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1302 wake_up_interruptible(&sfp
->read_wait
);
1303 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1307 static struct file_operations sg_fops
= {
1308 .owner
= THIS_MODULE
,
1313 #ifdef CONFIG_COMPAT
1314 .compat_ioctl
= sg_compat_ioctl
,
1318 .release
= sg_release
,
1319 .fasync
= sg_fasync
,
1322 static struct class *sg_sysfs_class
;
1324 static int sg_sysfs_valid
= 0;
1326 static int sg_alloc(struct gendisk
*disk
, struct scsi_device
*scsidp
)
1328 struct request_queue
*q
= scsidp
->request_queue
;
1330 unsigned long iflags
;
1331 void *old_sg_dev_arr
= NULL
;
1334 sdp
= kzalloc(sizeof(Sg_device
), GFP_KERNEL
);
1336 printk(KERN_WARNING
"kmalloc Sg_device failure\n");
1340 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1341 if (unlikely(sg_nr_dev
>= sg_dev_max
)) { /* try to resize */
1343 int tmp_dev_max
= sg_nr_dev
+ SG_DEV_ARR_LUMP
;
1344 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1346 tmp_da
= kzalloc(tmp_dev_max
* sizeof(Sg_device
*), GFP_KERNEL
);
1347 if (unlikely(!tmp_da
))
1350 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1351 memcpy(tmp_da
, sg_dev_arr
, sg_dev_max
* sizeof(Sg_device
*));
1352 old_sg_dev_arr
= sg_dev_arr
;
1353 sg_dev_arr
= tmp_da
;
1354 sg_dev_max
= tmp_dev_max
;
1357 for (k
= 0; k
< sg_dev_max
; k
++)
1360 if (unlikely(k
>= SG_MAX_DEVS
))
1363 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k
));
1364 sprintf(disk
->disk_name
, "sg%d", k
);
1365 disk
->first_minor
= k
;
1367 sdp
->device
= scsidp
;
1368 init_waitqueue_head(&sdp
->o_excl_wait
);
1369 sdp
->sg_tablesize
= min(q
->max_hw_segments
, q
->max_phys_segments
);
1372 sg_dev_arr
[k
] = sdp
;
1373 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1379 kfree(old_sg_dev_arr
);
1383 printk(KERN_WARNING
"sg_alloc: device array cannot be resized\n");
1388 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1389 sdev_printk(KERN_WARNING
, scsidp
,
1390 "Unable to attach sg device type=%d, minor "
1391 "number exceeds %d\n", scsidp
->type
, SG_MAX_DEVS
- 1);
1397 sg_add(struct class_device
*cl_dev
, struct class_interface
*cl_intf
)
1399 struct scsi_device
*scsidp
= to_scsi_device(cl_dev
->dev
);
1400 struct gendisk
*disk
;
1401 Sg_device
*sdp
= NULL
;
1402 struct cdev
* cdev
= NULL
;
1405 disk
= alloc_disk(1);
1407 printk(KERN_WARNING
"alloc_disk failed\n");
1410 disk
->major
= SCSI_GENERIC_MAJOR
;
1413 cdev
= cdev_alloc();
1415 printk(KERN_WARNING
"cdev_alloc failed\n");
1418 cdev
->owner
= THIS_MODULE
;
1419 cdev
->ops
= &sg_fops
;
1421 error
= sg_alloc(disk
, scsidp
);
1423 printk(KERN_WARNING
"sg_alloc failed\n");
1427 sdp
= sg_dev_arr
[k
];
1429 error
= cdev_add(cdev
, MKDEV(SCSI_GENERIC_MAJOR
, k
), 1);
1434 if (sg_sysfs_valid
) {
1435 struct class_device
* sg_class_member
;
1437 sg_class_member
= class_device_create(sg_sysfs_class
, NULL
,
1438 MKDEV(SCSI_GENERIC_MAJOR
, k
),
1441 if (IS_ERR(sg_class_member
))
1442 printk(KERN_WARNING
"sg_add: "
1443 "class_device_create failed\n");
1444 class_set_devdata(sg_class_member
, sdp
);
1445 error
= sysfs_create_link(&scsidp
->sdev_gendev
.kobj
,
1446 &sg_class_member
->kobj
, "generic");
1448 printk(KERN_ERR
"sg_add: unable to make symlink "
1449 "'generic' back to sg%d\n", k
);
1451 printk(KERN_WARNING
"sg_add: sg_sys INvalid\n");
1453 sdev_printk(KERN_NOTICE
, scsidp
,
1454 "Attached scsi generic sg%d type %d\n", k
,scsidp
->type
);
1466 sg_remove(struct class_device
*cl_dev
, struct class_interface
*cl_intf
)
1468 struct scsi_device
*scsidp
= to_scsi_device(cl_dev
->dev
);
1469 Sg_device
*sdp
= NULL
;
1470 unsigned long iflags
;
1477 if (NULL
== sg_dev_arr
)
1480 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1481 for (k
= 0; k
< sg_dev_max
; k
++) {
1482 sdp
= sg_dev_arr
[k
];
1483 if ((NULL
== sdp
) || (sdp
->device
!= scsidp
))
1484 continue; /* dirty but lowers nesting */
1487 for (sfp
= sdp
->headfp
; sfp
; sfp
= tsfp
) {
1489 for (srp
= sfp
->headrp
; srp
; srp
= tsrp
) {
1491 if (sfp
->closed
|| (0 == sg_srp_done(srp
, sfp
)))
1492 sg_finish_rem_req(srp
);
1495 scsi_device_put(sdp
->device
);
1496 __sg_remove_sfp(sdp
, sfp
);
1499 wake_up_interruptible(&sfp
->read_wait
);
1500 kill_fasync(&sfp
->async_qp
, SIGPOLL
,
1504 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k
));
1505 if (NULL
== sdp
->headfp
) {
1506 sg_dev_arr
[k
] = NULL
;
1508 } else { /* nothing active, simple case */
1509 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k
));
1510 sg_dev_arr
[k
] = NULL
;
1515 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1518 sysfs_remove_link(&scsidp
->sdev_gendev
.kobj
, "generic");
1519 class_device_destroy(sg_sysfs_class
, MKDEV(SCSI_GENERIC_MAJOR
, k
));
1520 cdev_del(sdp
->cdev
);
1522 put_disk(sdp
->disk
);
1524 if (NULL
== sdp
->headfp
)
1525 kfree((char *) sdp
);
1529 msleep(10); /* dirty detach so delay device destruction */
1532 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1533 * of sysfs parameters (which module_param doesn't yet support).
1534 * Sysfs parameters defined explicitly below.
1536 module_param_named(def_reserved_size
, def_reserved_size
, int, S_IRUGO
);
1537 module_param_named(allow_dio
, sg_allow_dio
, int, S_IRUGO
| S_IWUSR
);
1539 MODULE_AUTHOR("Douglas Gilbert");
1540 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1541 MODULE_LICENSE("GPL");
1542 MODULE_VERSION(SG_VERSION_STR
);
1543 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR
);
1545 MODULE_PARM_DESC(def_reserved_size
, "size of buffer reserved for each fd");
1546 MODULE_PARM_DESC(allow_dio
, "allow direct I/O (default: 0 (disallow))");
1553 if (def_reserved_size
>= 0)
1554 sg_big_buff
= def_reserved_size
;
1556 rc
= register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0),
1560 sg_sysfs_class
= class_create(THIS_MODULE
, "scsi_generic");
1561 if ( IS_ERR(sg_sysfs_class
) ) {
1562 rc
= PTR_ERR(sg_sysfs_class
);
1566 rc
= scsi_register_interface(&sg_interface
);
1568 #ifdef CONFIG_SCSI_PROC_FS
1570 #endif /* CONFIG_SCSI_PROC_FS */
1573 class_destroy(sg_sysfs_class
);
1575 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0), SG_MAX_DEVS
);
1582 #ifdef CONFIG_SCSI_PROC_FS
1584 #endif /* CONFIG_SCSI_PROC_FS */
1585 scsi_unregister_interface(&sg_interface
);
1586 class_destroy(sg_sysfs_class
);
1588 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR
, 0),
1590 kfree((char *)sg_dev_arr
);
1596 sg_start_req(Sg_request
* srp
)
1599 Sg_fd
*sfp
= srp
->parentfp
;
1600 sg_io_hdr_t
*hp
= &srp
->header
;
1601 int dxfer_len
= (int) hp
->dxfer_len
;
1602 int dxfer_dir
= hp
->dxfer_direction
;
1603 Sg_scatter_hold
*req_schp
= &srp
->data
;
1604 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
1606 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len
));
1607 if ((dxfer_len
<= 0) || (dxfer_dir
== SG_DXFER_NONE
))
1609 if (sg_allow_dio
&& (hp
->flags
& SG_FLAG_DIRECT_IO
) &&
1610 (dxfer_dir
!= SG_DXFER_UNKNOWN
) && (0 == hp
->iovec_count
) &&
1611 (!sfp
->parentdp
->device
->host
->unchecked_isa_dma
)) {
1612 res
= sg_build_direct(srp
, sfp
, dxfer_len
);
1613 if (res
<= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1616 if ((!sg_res_in_use(sfp
)) && (dxfer_len
<= rsv_schp
->bufflen
))
1617 sg_link_reserve(sfp
, srp
, dxfer_len
);
1619 res
= sg_build_indirect(req_schp
, sfp
, dxfer_len
);
1621 sg_remove_scat(req_schp
);
1629 sg_finish_rem_req(Sg_request
* srp
)
1631 Sg_fd
*sfp
= srp
->parentfp
;
1632 Sg_scatter_hold
*req_schp
= &srp
->data
;
1634 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp
->res_used
));
1636 sg_unlink_reserve(sfp
, srp
);
1638 sg_remove_scat(req_schp
);
1639 sg_remove_request(sfp
, srp
);
1643 sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
, int tablesize
)
1645 int sg_bufflen
= tablesize
* sizeof(struct scatterlist
);
1646 gfp_t gfp_flags
= GFP_ATOMIC
| __GFP_NOWARN
;
1649 * TODO: test without low_dma, we should not need it since
1650 * the block layer will bounce the buffer for us
1652 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1655 gfp_flags
|= GFP_DMA
;
1656 schp
->buffer
= kzalloc(sg_bufflen
, gfp_flags
);
1659 schp
->sglist_len
= sg_bufflen
;
1660 return tablesize
; /* number of scat_gath elements allocated */
1663 #ifdef SG_ALLOW_DIO_CODE
1664 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1665 /* TODO: hopefully we can use the generic block layer code */
1667 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1668 - mapping of all pages not successful
1669 (i.e., either completely successful or fails)
1672 st_map_user_pages(struct scatterlist
*sgl
, const unsigned int max_pages
,
1673 unsigned long uaddr
, size_t count
, int rw
)
1675 unsigned long end
= (uaddr
+ count
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1676 unsigned long start
= uaddr
>> PAGE_SHIFT
;
1677 const int nr_pages
= end
- start
;
1679 struct page
**pages
;
1681 /* User attempted Overflow! */
1682 if ((uaddr
+ count
) < uaddr
)
1686 if (nr_pages
> max_pages
)
1693 if ((pages
= kmalloc(max_pages
* sizeof(*pages
), GFP_ATOMIC
)) == NULL
)
1696 /* Try to fault in all of the necessary pages */
1697 down_read(¤t
->mm
->mmap_sem
);
1698 /* rw==READ means read from drive, write into memory area */
1699 res
= get_user_pages(
1705 0, /* don't force */
1708 up_read(¤t
->mm
->mmap_sem
);
1710 /* Errors and no page mapped should return here */
1714 for (i
=0; i
< nr_pages
; i
++) {
1715 /* FIXME: flush superflous for rw==READ,
1716 * probably wrong function for rw==WRITE
1718 flush_dcache_page(pages
[i
]);
1719 /* ?? Is locking needed? I don't think so */
1720 /* if (TestSetPageLocked(pages[i]))
1724 sgl
[0].page
= pages
[0];
1725 sgl
[0].offset
= uaddr
& ~PAGE_MASK
;
1727 sgl
[0].length
= PAGE_SIZE
- sgl
[0].offset
;
1728 count
-= sgl
[0].length
;
1729 for (i
=1; i
< nr_pages
; i
++) {
1730 sgl
[i
].page
= pages
[i
];
1731 sgl
[i
].length
= count
< PAGE_SIZE
? count
: PAGE_SIZE
;
1736 sgl
[0].length
= count
;
1744 for (j
=0; j
< res
; j
++)
1745 page_cache_release(pages
[j
]);
1753 /* And unmap them... */
1755 st_unmap_user_pages(struct scatterlist
*sgl
, const unsigned int nr_pages
,
1760 for (i
=0; i
< nr_pages
; i
++) {
1761 struct page
*page
= sgl
[i
].page
;
1765 /* unlock_page(page); */
1766 /* FIXME: cache flush missing for rw==READ
1767 * FIXME: call the correct reference counting function
1769 page_cache_release(page
);
1775 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1779 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1781 sg_build_direct(Sg_request
* srp
, Sg_fd
* sfp
, int dxfer_len
)
1783 #ifdef SG_ALLOW_DIO_CODE
1784 sg_io_hdr_t
*hp
= &srp
->header
;
1785 Sg_scatter_hold
*schp
= &srp
->data
;
1786 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1787 int mx_sc_elems
, res
;
1788 struct scsi_device
*sdev
= sfp
->parentdp
->device
;
1790 if (((unsigned long)hp
->dxferp
&
1791 queue_dma_alignment(sdev
->request_queue
)) != 0)
1794 mx_sc_elems
= sg_build_sgat(schp
, sfp
, sg_tablesize
);
1795 if (mx_sc_elems
<= 0) {
1798 res
= st_map_user_pages(schp
->buffer
, mx_sc_elems
,
1799 (unsigned long)hp
->dxferp
, dxfer_len
,
1800 (SG_DXFER_TO_DEV
== hp
->dxfer_direction
) ? 1 : 0);
1803 schp
->k_use_sg
= res
;
1804 schp
->dio_in_use
= 1;
1805 hp
->info
|= SG_INFO_DIRECT_IO
;
1813 sg_build_indirect(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
)
1815 struct scatterlist
*sg
;
1816 int ret_sz
= 0, k
, rem_sz
, num
, mx_sc_elems
;
1817 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1818 int blk_size
= buff_size
;
1819 struct page
*p
= NULL
;
1821 if ((blk_size
< 0) || (!sfp
))
1824 ++blk_size
; /* don't know why */
1825 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1826 blk_size
= (blk_size
+ SG_SECTOR_MSK
) & (~SG_SECTOR_MSK
);
1827 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1828 buff_size
, blk_size
));
1830 /* N.B. ret_sz carried into this block ... */
1831 mx_sc_elems
= sg_build_sgat(schp
, sfp
, sg_tablesize
);
1832 if (mx_sc_elems
< 0)
1833 return mx_sc_elems
; /* most likely -ENOMEM */
1835 for (k
= 0, sg
= schp
->buffer
, rem_sz
= blk_size
;
1836 (rem_sz
> 0) && (k
< mx_sc_elems
);
1837 ++k
, rem_sz
-= ret_sz
, ++sg
) {
1839 num
= (rem_sz
> SG_SCATTER_SZ
) ? SG_SCATTER_SZ
: rem_sz
;
1840 p
= sg_page_malloc(num
, sfp
->low_dma
, &ret_sz
);
1845 sg
->length
= ret_sz
;
1847 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1849 } /* end of for loop */
1852 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k
, rem_sz
));
1854 schp
->bufflen
= blk_size
;
1855 if (rem_sz
> 0) /* must have failed */
1862 sg_write_xfer(Sg_request
* srp
)
1864 sg_io_hdr_t
*hp
= &srp
->header
;
1865 Sg_scatter_hold
*schp
= &srp
->data
;
1866 struct scatterlist
*sg
= schp
->buffer
;
1868 int j
, k
, onum
, usglen
, ksglen
, res
;
1869 int iovec_count
= (int) hp
->iovec_count
;
1870 int dxfer_dir
= hp
->dxfer_direction
;
1872 unsigned char __user
*up
;
1873 int new_interface
= ('\0' == hp
->interface_id
) ? 0 : 1;
1875 if ((SG_DXFER_UNKNOWN
== dxfer_dir
) || (SG_DXFER_TO_DEV
== dxfer_dir
) ||
1876 (SG_DXFER_TO_FROM_DEV
== dxfer_dir
)) {
1877 num_xfer
= (int) (new_interface
? hp
->dxfer_len
: hp
->flags
);
1878 if (schp
->bufflen
< num_xfer
)
1879 num_xfer
= schp
->bufflen
;
1881 if ((num_xfer
<= 0) || (schp
->dio_in_use
) ||
1883 && ((SG_FLAG_NO_DXFER
| SG_FLAG_MMAP_IO
) & hp
->flags
)))
1886 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1887 num_xfer
, iovec_count
, schp
->k_use_sg
));
1890 if (!access_ok(VERIFY_READ
, hp
->dxferp
, SZ_SG_IOVEC
* onum
))
1895 ksglen
= sg
->length
;
1896 p
= page_address(sg
->page
);
1897 for (j
= 0, k
= 0; j
< onum
; ++j
) {
1898 res
= sg_u_iovec(hp
, iovec_count
, j
, 1, &usglen
, &up
);
1902 for (; p
; ++sg
, ksglen
= sg
->length
,
1903 p
= page_address(sg
->page
)) {
1906 if (ksglen
> usglen
) {
1907 if (usglen
>= num_xfer
) {
1908 if (__copy_from_user(p
, up
, num_xfer
))
1912 if (__copy_from_user(p
, up
, usglen
))
1918 if (ksglen
>= num_xfer
) {
1919 if (__copy_from_user(p
, up
, num_xfer
))
1923 if (__copy_from_user(p
, up
, ksglen
))
1929 if (k
>= schp
->k_use_sg
)
1938 sg_u_iovec(sg_io_hdr_t
* hp
, int sg_num
, int ind
,
1939 int wr_xf
, int *countp
, unsigned char __user
**up
)
1941 int num_xfer
= (int) hp
->dxfer_len
;
1942 unsigned char __user
*p
= hp
->dxferp
;
1946 if (wr_xf
&& ('\0' == hp
->interface_id
))
1947 count
= (int) hp
->flags
; /* holds "old" input_size */
1952 if (__copy_from_user(&iovec
, p
+ ind
*SZ_SG_IOVEC
, SZ_SG_IOVEC
))
1955 count
= (int) iovec
.iov_len
;
1957 if (!access_ok(wr_xf
? VERIFY_READ
: VERIFY_WRITE
, p
, count
))
1967 sg_remove_scat(Sg_scatter_hold
* schp
)
1969 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp
->k_use_sg
));
1970 if (schp
->buffer
&& (schp
->sglist_len
> 0)) {
1971 struct scatterlist
*sg
= schp
->buffer
;
1973 if (schp
->dio_in_use
) {
1974 #ifdef SG_ALLOW_DIO_CODE
1975 st_unmap_user_pages(sg
, schp
->k_use_sg
, TRUE
);
1980 for (k
= 0; (k
< schp
->k_use_sg
) && sg
->page
;
1982 SCSI_LOG_TIMEOUT(5, printk(
1983 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
1984 k
, sg
->page
, sg
->length
));
1985 sg_page_free(sg
->page
, sg
->length
);
1988 kfree(schp
->buffer
);
1990 memset(schp
, 0, sizeof (*schp
));
1994 sg_read_xfer(Sg_request
* srp
)
1996 sg_io_hdr_t
*hp
= &srp
->header
;
1997 Sg_scatter_hold
*schp
= &srp
->data
;
1998 struct scatterlist
*sg
= schp
->buffer
;
2000 int j
, k
, onum
, usglen
, ksglen
, res
;
2001 int iovec_count
= (int) hp
->iovec_count
;
2002 int dxfer_dir
= hp
->dxfer_direction
;
2004 unsigned char __user
*up
;
2005 int new_interface
= ('\0' == hp
->interface_id
) ? 0 : 1;
2007 if ((SG_DXFER_UNKNOWN
== dxfer_dir
) || (SG_DXFER_FROM_DEV
== dxfer_dir
)
2008 || (SG_DXFER_TO_FROM_DEV
== dxfer_dir
)) {
2009 num_xfer
= hp
->dxfer_len
;
2010 if (schp
->bufflen
< num_xfer
)
2011 num_xfer
= schp
->bufflen
;
2013 if ((num_xfer
<= 0) || (schp
->dio_in_use
) ||
2015 && ((SG_FLAG_NO_DXFER
| SG_FLAG_MMAP_IO
) & hp
->flags
)))
2018 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2019 num_xfer
, iovec_count
, schp
->k_use_sg
));
2022 if (!access_ok(VERIFY_READ
, hp
->dxferp
, SZ_SG_IOVEC
* onum
))
2027 p
= page_address(sg
->page
);
2028 ksglen
= sg
->length
;
2029 for (j
= 0, k
= 0; j
< onum
; ++j
) {
2030 res
= sg_u_iovec(hp
, iovec_count
, j
, 0, &usglen
, &up
);
2034 for (; p
; ++sg
, ksglen
= sg
->length
,
2035 p
= page_address(sg
->page
)) {
2038 if (ksglen
> usglen
) {
2039 if (usglen
>= num_xfer
) {
2040 if (__copy_to_user(up
, p
, num_xfer
))
2044 if (__copy_to_user(up
, p
, usglen
))
2050 if (ksglen
>= num_xfer
) {
2051 if (__copy_to_user(up
, p
, num_xfer
))
2055 if (__copy_to_user(up
, p
, ksglen
))
2061 if (k
>= schp
->k_use_sg
)
2070 sg_read_oxfer(Sg_request
* srp
, char __user
*outp
, int num_read_xfer
)
2072 Sg_scatter_hold
*schp
= &srp
->data
;
2073 struct scatterlist
*sg
= schp
->buffer
;
2076 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2078 if ((!outp
) || (num_read_xfer
<= 0))
2081 for (k
= 0; (k
< schp
->k_use_sg
) && sg
->page
; ++k
, ++sg
) {
2083 if (num
> num_read_xfer
) {
2084 if (__copy_to_user(outp
, page_address(sg
->page
),
2089 if (__copy_to_user(outp
, page_address(sg
->page
),
2092 num_read_xfer
-= num
;
2093 if (num_read_xfer
<= 0)
2103 sg_build_reserve(Sg_fd
* sfp
, int req_size
)
2105 Sg_scatter_hold
*schp
= &sfp
->reserve
;
2107 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size
));
2109 if (req_size
< PAGE_SIZE
)
2110 req_size
= PAGE_SIZE
;
2111 if (0 == sg_build_indirect(schp
, sfp
, req_size
))
2114 sg_remove_scat(schp
);
2115 req_size
>>= 1; /* divide by 2 */
2116 } while (req_size
> (PAGE_SIZE
/ 2));
2120 sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
)
2122 Sg_scatter_hold
*req_schp
= &srp
->data
;
2123 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
2124 struct scatterlist
*sg
= rsv_schp
->buffer
;
2128 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size
));
2131 for (k
= 0; k
< rsv_schp
->k_use_sg
; ++k
, ++sg
) {
2134 sfp
->save_scat_len
= num
;
2136 req_schp
->k_use_sg
= k
+ 1;
2137 req_schp
->sglist_len
= rsv_schp
->sglist_len
;
2138 req_schp
->buffer
= rsv_schp
->buffer
;
2140 req_schp
->bufflen
= size
;
2141 req_schp
->b_malloc_len
= rsv_schp
->b_malloc_len
;
2147 if (k
>= rsv_schp
->k_use_sg
)
2148 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2152 sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
)
2154 Sg_scatter_hold
*req_schp
= &srp
->data
;
2155 Sg_scatter_hold
*rsv_schp
= &sfp
->reserve
;
2157 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2158 (int) req_schp
->k_use_sg
));
2159 if ((rsv_schp
->k_use_sg
> 0) && (req_schp
->k_use_sg
> 0)) {
2160 struct scatterlist
*sg
= rsv_schp
->buffer
;
2162 if (sfp
->save_scat_len
> 0)
2163 (sg
+ (req_schp
->k_use_sg
- 1))->length
=
2164 (unsigned) sfp
->save_scat_len
;
2166 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2168 req_schp
->k_use_sg
= 0;
2169 req_schp
->bufflen
= 0;
2170 req_schp
->buffer
= NULL
;
2171 req_schp
->sglist_len
= 0;
2172 sfp
->save_scat_len
= 0;
2177 sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
)
2180 unsigned long iflags
;
2182 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2183 for (resp
= sfp
->headrp
; resp
; resp
= resp
->nextrp
) {
2184 /* look for requests that are ready + not SG_IO owned */
2185 if ((1 == resp
->done
) && (!resp
->sg_io_owned
) &&
2186 ((-1 == pack_id
) || (resp
->header
.pack_id
== pack_id
))) {
2187 resp
->done
= 2; /* guard against other readers */
2191 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2195 #ifdef CONFIG_SCSI_PROC_FS
2197 sg_get_nth_request(Sg_fd
* sfp
, int nth
)
2200 unsigned long iflags
;
2203 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2204 for (k
= 0, resp
= sfp
->headrp
; resp
&& (k
< nth
);
2205 ++k
, resp
= resp
->nextrp
) ;
2206 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2211 /* always adds to end of list */
2213 sg_add_request(Sg_fd
* sfp
)
2216 unsigned long iflags
;
2218 Sg_request
*rp
= sfp
->req_arr
;
2220 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2223 memset(rp
, 0, sizeof (Sg_request
));
2228 if (0 == sfp
->cmd_q
)
2229 resp
= NULL
; /* command queuing disallowed */
2231 for (k
= 0; k
< SG_MAX_QUEUE
; ++k
, ++rp
) {
2235 if (k
< SG_MAX_QUEUE
) {
2236 memset(rp
, 0, sizeof (Sg_request
));
2238 while (resp
->nextrp
)
2239 resp
= resp
->nextrp
;
2247 resp
->nextrp
= NULL
;
2248 resp
->header
.duration
= jiffies_to_msecs(jiffies
);
2250 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2254 /* Return of 1 for found; 0 for not found */
2256 sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
)
2258 Sg_request
*prev_rp
;
2260 unsigned long iflags
;
2263 if ((!sfp
) || (!srp
) || (!sfp
->headrp
))
2265 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2266 prev_rp
= sfp
->headrp
;
2267 if (srp
== prev_rp
) {
2268 sfp
->headrp
= prev_rp
->nextrp
;
2269 prev_rp
->parentfp
= NULL
;
2272 while ((rp
= prev_rp
->nextrp
)) {
2274 prev_rp
->nextrp
= rp
->nextrp
;
2275 rp
->parentfp
= NULL
;
2282 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2286 #ifdef CONFIG_SCSI_PROC_FS
2288 sg_get_nth_sfp(Sg_device
* sdp
, int nth
)
2291 unsigned long iflags
;
2294 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2295 for (k
= 0, resp
= sdp
->headfp
; resp
&& (k
< nth
);
2296 ++k
, resp
= resp
->nextfp
) ;
2297 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2303 sg_add_sfp(Sg_device
* sdp
, int dev
)
2306 unsigned long iflags
;
2308 sfp
= kzalloc(sizeof(*sfp
), GFP_ATOMIC
| __GFP_NOWARN
);
2312 init_waitqueue_head(&sfp
->read_wait
);
2313 rwlock_init(&sfp
->rq_list_lock
);
2315 sfp
->timeout
= SG_DEFAULT_TIMEOUT
;
2316 sfp
->timeout_user
= SG_DEFAULT_TIMEOUT_USER
;
2317 sfp
->force_packid
= SG_DEF_FORCE_PACK_ID
;
2318 sfp
->low_dma
= (SG_DEF_FORCE_LOW_DMA
== 0) ?
2319 sdp
->device
->host
->unchecked_isa_dma
: 1;
2320 sfp
->cmd_q
= SG_DEF_COMMAND_Q
;
2321 sfp
->keep_orphan
= SG_DEF_KEEP_ORPHAN
;
2322 sfp
->parentdp
= sdp
;
2323 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2326 else { /* add to tail of existing list */
2327 Sg_fd
*pfp
= sdp
->headfp
;
2332 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2333 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp
));
2334 sg_build_reserve(sfp
, sg_big_buff
);
2335 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2336 sfp
->reserve
.bufflen
, sfp
->reserve
.k_use_sg
));
2341 __sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
)
2346 prev_fp
= sdp
->headfp
;
2348 sdp
->headfp
= prev_fp
->nextfp
;
2350 while ((fp
= prev_fp
->nextfp
)) {
2352 prev_fp
->nextfp
= fp
->nextfp
;
2358 if (sfp
->reserve
.bufflen
> 0) {
2360 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2361 (int) sfp
->reserve
.bufflen
, (int) sfp
->reserve
.k_use_sg
));
2362 sg_remove_scat(&sfp
->reserve
);
2364 sfp
->parentdp
= NULL
;
2365 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp
));
2369 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2371 sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
)
2378 for (srp
= sfp
->headrp
; srp
; srp
= tsrp
) {
2380 if (sg_srp_done(srp
, sfp
))
2381 sg_finish_rem_req(srp
);
2386 unsigned long iflags
;
2388 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2389 __sg_remove_sfp(sdp
, sfp
);
2390 if (sdp
->detached
&& (NULL
== sdp
->headfp
)) {
2394 for (k
= 0; k
< maxd
; ++k
) {
2395 if (sdp
== sg_dev_arr
[k
])
2399 sg_dev_arr
[k
] = NULL
;
2400 kfree((char *) sdp
);
2403 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2405 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2406 /* only bump the access_count if we actually succeeded in
2407 * throwing another counter on the host module */
2408 scsi_device_get(sdp
->device
); /* XXX: retval ignored? */
2409 sfp
->closed
= 1; /* flag dirty state on this fd */
2410 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2417 sg_res_in_use(Sg_fd
* sfp
)
2419 const Sg_request
*srp
;
2420 unsigned long iflags
;
2422 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2423 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
)
2426 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2430 /* If retSzp==NULL want exact size or fail */
2431 static struct page
*
2432 sg_page_malloc(int rqSz
, int lowDma
, int *retSzp
)
2434 struct page
*resp
= NULL
;
2443 page_mask
= GFP_ATOMIC
| GFP_DMA
| __GFP_COMP
| __GFP_NOWARN
;
2445 page_mask
= GFP_ATOMIC
| __GFP_COMP
| __GFP_NOWARN
;
2447 for (order
= 0, a_size
= PAGE_SIZE
; a_size
< rqSz
;
2448 order
++, a_size
<<= 1) ;
2449 resp
= alloc_pages(page_mask
, order
);
2450 while ((!resp
) && order
&& retSzp
) {
2452 a_size
>>= 1; /* divide by 2, until PAGE_SIZE */
2453 resp
= alloc_pages(page_mask
, order
); /* try half */
2457 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2458 memset(page_address(resp
), 0, resSz
);
2466 sg_page_free(struct page
*page
, int size
)
2472 for (order
= 0, a_size
= PAGE_SIZE
; a_size
< size
;
2473 order
++, a_size
<<= 1) ;
2474 __free_pages(page
, order
);
2477 #ifndef MAINTENANCE_IN_CMD
2478 #define MAINTENANCE_IN_CMD 0xa3
2481 static unsigned char allow_ops
[] = { TEST_UNIT_READY
, REQUEST_SENSE
,
2482 INQUIRY
, READ_CAPACITY
, READ_BUFFER
, READ_6
, READ_10
, READ_12
,
2483 READ_16
, MODE_SENSE
, MODE_SENSE_10
, LOG_SENSE
, REPORT_LUNS
,
2484 SERVICE_ACTION_IN
, RECEIVE_DIAGNOSTIC
, READ_LONG
, MAINTENANCE_IN_CMD
2488 sg_allow_access(unsigned char opcode
, char dev_type
)
2492 if (TYPE_SCANNER
== dev_type
) /* TYPE_ROM maybe burner */
2494 for (k
= 0; k
< sizeof (allow_ops
); ++k
) {
2495 if (opcode
== allow_ops
[k
])
2501 #ifdef CONFIG_SCSI_PROC_FS
2506 unsigned long iflags
;
2508 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2509 for (k
= sg_dev_max
- 1; k
>= 0; --k
)
2510 if (sg_dev_arr
[k
] && sg_dev_arr
[k
]->device
)
2512 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2513 return k
+ 1; /* origin 1 */
2520 Sg_device
*sdp
= NULL
;
2521 unsigned long iflags
;
2523 if (sg_dev_arr
&& (dev
>= 0)) {
2524 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2525 if (dev
< sg_dev_max
)
2526 sdp
= sg_dev_arr
[dev
];
2527 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2532 #ifdef CONFIG_SCSI_PROC_FS
2534 static struct proc_dir_entry
*sg_proc_sgp
= NULL
;
2536 static char sg_proc_sg_dirname
[] = "scsi/sg";
2538 static int sg_proc_seq_show_int(struct seq_file
*s
, void *v
);
2540 static int sg_proc_single_open_adio(struct inode
*inode
, struct file
*file
);
2541 static ssize_t
sg_proc_write_adio(struct file
*filp
, const char __user
*buffer
,
2542 size_t count
, loff_t
*off
);
2543 static struct file_operations adio_fops
= {
2544 /* .owner, .read and .llseek added in sg_proc_init() */
2545 .open
= sg_proc_single_open_adio
,
2546 .write
= sg_proc_write_adio
,
2547 .release
= single_release
,
2550 static int sg_proc_single_open_dressz(struct inode
*inode
, struct file
*file
);
2551 static ssize_t
sg_proc_write_dressz(struct file
*filp
,
2552 const char __user
*buffer
, size_t count
, loff_t
*off
);
2553 static struct file_operations dressz_fops
= {
2554 .open
= sg_proc_single_open_dressz
,
2555 .write
= sg_proc_write_dressz
,
2556 .release
= single_release
,
2559 static int sg_proc_seq_show_version(struct seq_file
*s
, void *v
);
2560 static int sg_proc_single_open_version(struct inode
*inode
, struct file
*file
);
2561 static struct file_operations version_fops
= {
2562 .open
= sg_proc_single_open_version
,
2563 .release
= single_release
,
2566 static int sg_proc_seq_show_devhdr(struct seq_file
*s
, void *v
);
2567 static int sg_proc_single_open_devhdr(struct inode
*inode
, struct file
*file
);
2568 static struct file_operations devhdr_fops
= {
2569 .open
= sg_proc_single_open_devhdr
,
2570 .release
= single_release
,
2573 static int sg_proc_seq_show_dev(struct seq_file
*s
, void *v
);
2574 static int sg_proc_open_dev(struct inode
*inode
, struct file
*file
);
2575 static void * dev_seq_start(struct seq_file
*s
, loff_t
*pos
);
2576 static void * dev_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
);
2577 static void dev_seq_stop(struct seq_file
*s
, void *v
);
2578 static struct file_operations dev_fops
= {
2579 .open
= sg_proc_open_dev
,
2580 .release
= seq_release
,
2582 static struct seq_operations dev_seq_ops
= {
2583 .start
= dev_seq_start
,
2584 .next
= dev_seq_next
,
2585 .stop
= dev_seq_stop
,
2586 .show
= sg_proc_seq_show_dev
,
2589 static int sg_proc_seq_show_devstrs(struct seq_file
*s
, void *v
);
2590 static int sg_proc_open_devstrs(struct inode
*inode
, struct file
*file
);
2591 static struct file_operations devstrs_fops
= {
2592 .open
= sg_proc_open_devstrs
,
2593 .release
= seq_release
,
2595 static struct seq_operations devstrs_seq_ops
= {
2596 .start
= dev_seq_start
,
2597 .next
= dev_seq_next
,
2598 .stop
= dev_seq_stop
,
2599 .show
= sg_proc_seq_show_devstrs
,
2602 static int sg_proc_seq_show_debug(struct seq_file
*s
, void *v
);
2603 static int sg_proc_open_debug(struct inode
*inode
, struct file
*file
);
2604 static struct file_operations debug_fops
= {
2605 .open
= sg_proc_open_debug
,
2606 .release
= seq_release
,
2608 static struct seq_operations debug_seq_ops
= {
2609 .start
= dev_seq_start
,
2610 .next
= dev_seq_next
,
2611 .stop
= dev_seq_stop
,
2612 .show
= sg_proc_seq_show_debug
,
2616 struct sg_proc_leaf
{
2618 struct file_operations
* fops
;
2621 static struct sg_proc_leaf sg_proc_leaf_arr
[] = {
2622 {"allow_dio", &adio_fops
},
2623 {"debug", &debug_fops
},
2624 {"def_reserved_size", &dressz_fops
},
2625 {"device_hdr", &devhdr_fops
},
2626 {"devices", &dev_fops
},
2627 {"device_strs", &devstrs_fops
},
2628 {"version", &version_fops
}
2636 sizeof (sg_proc_leaf_arr
) / sizeof (sg_proc_leaf_arr
[0]);
2637 struct proc_dir_entry
*pdep
;
2638 struct sg_proc_leaf
* leaf
;
2640 sg_proc_sgp
= proc_mkdir(sg_proc_sg_dirname
, NULL
);
2643 for (k
= 0; k
< num_leaves
; ++k
) {
2644 leaf
= &sg_proc_leaf_arr
[k
];
2645 mask
= leaf
->fops
->write
? S_IRUGO
| S_IWUSR
: S_IRUGO
;
2646 pdep
= create_proc_entry(leaf
->name
, mask
, sg_proc_sgp
);
2648 leaf
->fops
->owner
= THIS_MODULE
,
2649 leaf
->fops
->read
= seq_read
,
2650 leaf
->fops
->llseek
= seq_lseek
,
2651 pdep
->proc_fops
= leaf
->fops
;
2658 sg_proc_cleanup(void)
2662 sizeof (sg_proc_leaf_arr
) / sizeof (sg_proc_leaf_arr
[0]);
2666 for (k
= 0; k
< num_leaves
; ++k
)
2667 remove_proc_entry(sg_proc_leaf_arr
[k
].name
, sg_proc_sgp
);
2668 remove_proc_entry(sg_proc_sg_dirname
, NULL
);
2672 static int sg_proc_seq_show_int(struct seq_file
*s
, void *v
)
2674 seq_printf(s
, "%d\n", *((int *)s
->private));
2678 static int sg_proc_single_open_adio(struct inode
*inode
, struct file
*file
)
2680 return single_open(file
, sg_proc_seq_show_int
, &sg_allow_dio
);
2684 sg_proc_write_adio(struct file
*filp
, const char __user
*buffer
,
2685 size_t count
, loff_t
*off
)
2690 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2692 num
= (count
< 10) ? count
: 10;
2693 if (copy_from_user(buff
, buffer
, num
))
2696 sg_allow_dio
= simple_strtoul(buff
, NULL
, 10) ? 1 : 0;
2700 static int sg_proc_single_open_dressz(struct inode
*inode
, struct file
*file
)
2702 return single_open(file
, sg_proc_seq_show_int
, &sg_big_buff
);
2706 sg_proc_write_dressz(struct file
*filp
, const char __user
*buffer
,
2707 size_t count
, loff_t
*off
)
2710 unsigned long k
= ULONG_MAX
;
2713 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2715 num
= (count
< 10) ? count
: 10;
2716 if (copy_from_user(buff
, buffer
, num
))
2719 k
= simple_strtoul(buff
, NULL
, 10);
2720 if (k
<= 1048576) { /* limit "big buff" to 1 MB */
2727 static int sg_proc_seq_show_version(struct seq_file
*s
, void *v
)
2729 seq_printf(s
, "%d\t%s [%s]\n", sg_version_num
, SG_VERSION_STR
,
2734 static int sg_proc_single_open_version(struct inode
*inode
, struct file
*file
)
2736 return single_open(file
, sg_proc_seq_show_version
, NULL
);
2739 static int sg_proc_seq_show_devhdr(struct seq_file
*s
, void *v
)
2741 seq_printf(s
, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2746 static int sg_proc_single_open_devhdr(struct inode
*inode
, struct file
*file
)
2748 return single_open(file
, sg_proc_seq_show_devhdr
, NULL
);
2751 struct sg_proc_deviter
{
2756 static void * dev_seq_start(struct seq_file
*s
, loff_t
*pos
)
2758 struct sg_proc_deviter
* it
= kmalloc(sizeof(*it
), GFP_KERNEL
);
2764 if (NULL
== sg_dev_arr
)
2767 it
->max
= sg_last_dev();
2768 if (it
->index
>= it
->max
)
2773 static void * dev_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2775 struct sg_proc_deviter
* it
= s
->private;
2778 return (it
->index
< it
->max
) ? it
: NULL
;
2781 static void dev_seq_stop(struct seq_file
*s
, void *v
)
2786 static int sg_proc_open_dev(struct inode
*inode
, struct file
*file
)
2788 return seq_open(file
, &dev_seq_ops
);
2791 static int sg_proc_seq_show_dev(struct seq_file
*s
, void *v
)
2793 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2795 struct scsi_device
*scsidp
;
2797 sdp
= it
? sg_get_dev(it
->index
) : NULL
;
2798 if (sdp
&& (scsidp
= sdp
->device
) && (!sdp
->detached
))
2799 seq_printf(s
, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2800 scsidp
->host
->host_no
, scsidp
->channel
,
2801 scsidp
->id
, scsidp
->lun
, (int) scsidp
->type
,
2803 (int) scsidp
->queue_depth
,
2804 (int) scsidp
->device_busy
,
2805 (int) scsi_device_online(scsidp
));
2807 seq_printf(s
, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2811 static int sg_proc_open_devstrs(struct inode
*inode
, struct file
*file
)
2813 return seq_open(file
, &devstrs_seq_ops
);
2816 static int sg_proc_seq_show_devstrs(struct seq_file
*s
, void *v
)
2818 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2820 struct scsi_device
*scsidp
;
2822 sdp
= it
? sg_get_dev(it
->index
) : NULL
;
2823 if (sdp
&& (scsidp
= sdp
->device
) && (!sdp
->detached
))
2824 seq_printf(s
, "%8.8s\t%16.16s\t%4.4s\n",
2825 scsidp
->vendor
, scsidp
->model
, scsidp
->rev
);
2827 seq_printf(s
, "<no active device>\n");
2831 static void sg_proc_debug_helper(struct seq_file
*s
, Sg_device
* sdp
)
2833 int k
, m
, new_interface
, blen
, usg
;
2836 const sg_io_hdr_t
*hp
;
2840 for (k
= 0; (fp
= sg_get_nth_sfp(sdp
, k
)); ++k
) {
2841 seq_printf(s
, " FD(%d): timeout=%dms bufflen=%d "
2842 "(res)sgat=%d low_dma=%d\n", k
+ 1,
2843 jiffies_to_msecs(fp
->timeout
),
2844 fp
->reserve
.bufflen
,
2845 (int) fp
->reserve
.k_use_sg
,
2847 seq_printf(s
, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2848 (int) fp
->cmd_q
, (int) fp
->force_packid
,
2849 (int) fp
->keep_orphan
, (int) fp
->closed
);
2850 for (m
= 0; (srp
= sg_get_nth_request(fp
, m
)); ++m
) {
2852 new_interface
= (hp
->interface_id
== '\0') ? 0 : 1;
2853 if (srp
->res_used
) {
2854 if (new_interface
&&
2855 (SG_FLAG_MMAP_IO
& hp
->flags
))
2860 if (SG_INFO_DIRECT_IO_MASK
& hp
->info
)
2866 blen
= srp
->data
.bufflen
;
2867 usg
= srp
->data
.k_use_sg
;
2868 seq_printf(s
, srp
->done
?
2869 ((1 == srp
->done
) ? "rcv:" : "fin:")
2871 seq_printf(s
, " id=%d blen=%d",
2872 srp
->header
.pack_id
, blen
);
2874 seq_printf(s
, " dur=%d", hp
->duration
);
2876 ms
= jiffies_to_msecs(jiffies
);
2877 seq_printf(s
, " t_o/elap=%d/%d",
2878 (new_interface
? hp
->timeout
:
2879 jiffies_to_msecs(fp
->timeout
)),
2880 (ms
> hp
->duration
? ms
- hp
->duration
: 0));
2882 seq_printf(s
, "ms sgat=%d op=0x%02x\n", usg
,
2883 (int) srp
->data
.cmd_opcode
);
2886 seq_printf(s
, " No requests active\n");
2890 static int sg_proc_open_debug(struct inode
*inode
, struct file
*file
)
2892 return seq_open(file
, &debug_seq_ops
);
2895 static int sg_proc_seq_show_debug(struct seq_file
*s
, void *v
)
2897 struct sg_proc_deviter
* it
= (struct sg_proc_deviter
*) v
;
2900 if (it
&& (0 == it
->index
)) {
2901 seq_printf(s
, "dev_max(currently)=%d max_active_device=%d "
2902 "(origin 1)\n", sg_dev_max
, (int)it
->max
);
2903 seq_printf(s
, " def_reserved_size=%d\n", sg_big_buff
);
2905 sdp
= it
? sg_get_dev(it
->index
) : NULL
;
2907 struct scsi_device
*scsidp
= sdp
->device
;
2909 if (NULL
== scsidp
) {
2910 seq_printf(s
, "device %d detached ??\n",
2915 if (sg_get_nth_sfp(sdp
, 0)) {
2916 seq_printf(s
, " >>> device=%s ",
2917 sdp
->disk
->disk_name
);
2919 seq_printf(s
, "detached pending close ");
2922 (s
, "scsi%d chan=%d id=%d lun=%d em=%d",
2923 scsidp
->host
->host_no
,
2924 scsidp
->channel
, scsidp
->id
,
2926 scsidp
->host
->hostt
->emulated
);
2927 seq_printf(s
, " sg_tablesize=%d excl=%d\n",
2928 sdp
->sg_tablesize
, sdp
->exclude
);
2930 sg_proc_debug_helper(s
, sdp
);
2935 #endif /* CONFIG_SCSI_PROC_FS */
2937 module_init(init_sg
);
2938 module_exit(exit_sg
);