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 - 2000 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)
20 static char * sg_version_str
= "Version: 3.1.16 (20000716)";
21 static int sg_version_num
= 30116; /* 2 digits for each component */
23 * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
24 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
25 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
26 * (otherwise the macros compile to empty statements).
27 * Then before running the program to be debugged enter:
28 * # echo "scsi log timeout 7" > /proc/scsi/scsi
29 * This will send copious output to the console and the log which
30 * is usually /var/log/messages. To turn off debugging enter:
31 * # echo "scsi log timeout 0" > /proc/scsi/scsi
32 * The 'timeout' token was chosen because it is relatively unused.
33 * The token 'hlcomplete' should be used but that triggers too
34 * much output from the sd device driver. To dump the current
35 * state of the SCSI mid level data structures enter:
36 * # echo "scsi dump 1" > /proc/scsi/scsi
37 * To dump the state of sg's data structures use:
38 * # cat /proc/scsi/sg/debug
41 #include <linux/config.h>
42 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
47 #include <linux/string.h>
49 #include <linux/errno.h>
50 #include <linux/mtio.h>
51 #include <linux/ioctl.h>
52 #include <linux/fcntl.h>
53 #include <linux/init.h>
54 #include <linux/poll.h>
55 #include <linux/smp_lock.h>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
61 #include <linux/blk.h>
64 #include <scsi/scsi_ioctl.h>
68 #include <linux/proc_fs.h>
69 static int sg_proc_init(void);
71 static void sg_proc_cleanup(void);
75 #ifndef LINUX_VERSION_CODE
76 #include <linux/version.h>
77 #endif /* LINUX_VERSION_CODE */
79 /* #define SG_ALLOW_DIO */
81 #include <linux/iobuf.h>
84 int sg_big_buff
= SG_DEF_RESERVED_SIZE
;
85 /* N.B. This variable is readable and writeable via
86 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
87 of this size (or less if there is not enough memory) will be reserved
88 for use by this file descriptor. [Deprecated usage: this variable is also
89 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
90 the kernel (i.e. it is not a module).] */
91 static int def_reserved_size
= -1; /* picks up init parameter */
93 #define SG_SECTOR_SZ 512
94 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
96 #define SG_LOW_POOL_THRESHHOLD 30
97 #define SG_MAX_POOL_SECTORS 320 /* Max. number of pool sectors to take */
99 static int sg_pool_secs_avail
= SG_MAX_POOL_SECTORS
;
101 #define SG_HEAP_PAGE 1 /* heap from kernel via get_free_pages() */
102 #define SG_HEAP_KMAL 2 /* heap from kernel via kmalloc() */
103 #define SG_HEAP_POOL 3 /* heap from scsi dma pool (mid-level) */
104 #define SG_USER_MEM 4 /* memory belongs to user space */
106 #define SG_DEV_ARR_LUMP 6 /* amount to over allocate sg_dev_arr by */
109 static int sg_init(void);
110 static int sg_attach(Scsi_Device
*);
111 static void sg_finish(void);
112 static int sg_detect(Scsi_Device
*);
113 static void sg_detach(Scsi_Device
*);
115 static Scsi_Cmnd
* dummy_cmdp
= 0; /* only used for sizeof */
117 static rwlock_t sg_dev_arr_lock
= RW_LOCK_UNLOCKED
; /* Also used to lock
118 file descriptor list for device */
120 struct Scsi_Device_Template sg_template
=
124 major
:SCSI_GENERIC_MAJOR
,
133 typedef struct sg_scatter_hold
/* holding area for scsi scatter gather info */
135 unsigned short k_use_sg
; /* Count of kernel scatter-gather pieces */
136 unsigned short sglist_len
; /* size of malloc'd scatter-gather list */
137 unsigned bufflen
; /* Size of (aggregate) data buffer */
138 unsigned b_malloc_len
; /* actual len malloc'ed in buffer */
139 void * buffer
; /* Data buffer or scatter list,12 bytes each*/
140 struct kiobuf
* kiobp
; /* for direct IO information */
141 char mapped
; /* indicates kiobp has locked pages */
142 char buffer_mem_src
; /* heap whereabouts of 'buffer' */
143 unsigned char cmd_opcode
; /* first byte of command */
144 } Sg_scatter_hold
; /* 24 bytes long on i386 */
146 struct sg_device
; /* forward declarations */
149 typedef struct sg_request
/* SG_MAX_QUEUE requests outstanding per file */
151 Scsi_Cmnd
* my_cmdp
; /* != 0 when request with lower levels */
152 struct sg_request
* nextrp
; /* NULL -> tail request (slist) */
153 struct sg_fd
* parentfp
; /* NULL -> not in use */
154 Sg_scatter_hold data
; /* hold buffer, perhaps scatter list */
155 sg_io_hdr_t header
; /* scsi command+info, see <scsi/sg.h> */
156 unsigned char sense_b
[sizeof(dummy_cmdp
->sense_buffer
)];
157 char res_used
; /* 1 -> using reserve buffer, 0 -> not ... */
158 char orphan
; /* 1 -> drop on sight, 0 -> normal */
159 char sg_io_owned
; /* 1 -> packet belongs to SG_IO */
160 char done
; /* 0->before bh, 1->before read, 2->read */
161 } Sg_request
; /* 168 bytes long on i386 */
163 typedef struct sg_fd
/* holds the state of a file descriptor */
165 struct sg_fd
* nextfp
; /* NULL when last opened fd on this device */
166 struct sg_device
* parentdp
; /* owning device */
167 wait_queue_head_t read_wait
; /* queue read until command done */
168 rwlock_t rq_list_lock
; /* protect access to list in req_arr */
169 int timeout
; /* defaults to SG_DEFAULT_TIMEOUT */
170 Sg_scatter_hold reserve
; /* buffer held for this file descriptor */
171 unsigned save_scat_len
; /* original length of trunc. scat. element */
172 Sg_request
* headrp
; /* head of request slist, NULL->empty */
173 struct fasync_struct
* async_qp
; /* used by asynchronous notification */
174 Sg_request req_arr
[SG_MAX_QUEUE
]; /* used as singly-linked list */
175 char low_dma
; /* as in parent but possibly overridden to 1 */
176 char force_packid
; /* 1 -> pack_id input to read(), 0 -> ignored */
177 char closed
; /* 1 -> fd closed but request(s) outstanding */
178 char fd_mem_src
; /* heap whereabouts of this Sg_fd object */
179 char cmd_q
; /* 1 -> allow command queuing, 0 -> don't */
180 char next_cmd_len
; /* 0 -> automatic (def), >0 -> use on next write() */
181 char keep_orphan
; /* 0 -> drop orphan (def), 1 -> keep for read() */
182 } Sg_fd
; /* 2768 bytes long on i386 */
184 typedef struct sg_device
/* holds the state of each scsi generic device */
186 Scsi_Device
* device
;
187 wait_queue_head_t o_excl_wait
; /* queue open() when O_EXCL in use */
188 int sg_tablesize
; /* adapter's max scatter-gather table size */
189 Sg_fd
* headfp
; /* first open fd belonging to this device */
191 kdev_t i_rdev
; /* holds device major+minor number */
192 char exclude
; /* opened for exclusive access */
193 char sgdebug
; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
194 char detached
; /* 0->attached, 1->detached pending removal */
195 } Sg_device
; /* 44 bytes long on i386 */
198 static int sg_fasync(int fd
, struct file
* filp
, int mode
);
199 static void sg_cmd_done_bh(Scsi_Cmnd
* SCpnt
);
200 static int sg_start_req(Sg_request
* srp
);
201 static void sg_finish_rem_req(Sg_request
* srp
);
202 static int sg_build_indi(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
);
203 static int sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
);
204 static ssize_t
sg_new_read(Sg_fd
* sfp
, char * buf
, size_t count
,
206 static ssize_t
sg_new_write(Sg_fd
* sfp
, const char * buf
, size_t count
,
207 int blocking
, int read_only
, Sg_request
** o_srp
);
208 static int sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
209 unsigned char * cmnd
, int timeout
, int blocking
);
210 static int sg_u_iovec(sg_io_hdr_t
* hp
, int sg_num
, int ind
,
211 int wr_xf
, int * countp
, unsigned char ** up
);
212 static int sg_write_xfer(Sg_request
* srp
);
213 static int sg_read_xfer(Sg_request
* srp
);
214 static void sg_read_oxfer(Sg_request
* srp
, char * outp
, int num_read_xfer
);
215 static void sg_remove_scat(Sg_scatter_hold
* schp
);
216 static char * sg_get_sgat_msa(Sg_scatter_hold
* schp
);
217 static void sg_build_reserve(Sg_fd
* sfp
, int req_size
);
218 static void sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
);
219 static void sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
);
220 static char * sg_malloc(const Sg_fd
* sfp
, int size
, int * retSzp
,
222 static void sg_free(char * buff
, int size
, int mem_src
);
223 static char * sg_low_malloc(int rqSz
, int lowDma
, int mem_src
,
225 static void sg_low_free(char * buff
, int size
, int mem_src
);
226 static Sg_fd
* sg_add_sfp(Sg_device
* sdp
, int dev
);
227 static int sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
);
228 static Sg_request
* sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
);
229 static Sg_request
* sg_add_request(Sg_fd
* sfp
);
230 static int sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
);
231 static int sg_res_in_use(Sg_fd
* sfp
);
232 static int sg_dio_in_use(Sg_fd
* sfp
);
233 static void sg_clr_scpnt(Scsi_Cmnd
* SCpnt
);
234 static void sg_shorten_timeout(Scsi_Cmnd
* scpnt
);
235 static int sg_ms_to_jif(unsigned int msecs
);
236 static unsigned sg_jif_to_ms(int jifs
);
237 static int sg_allow_access(unsigned char opcode
, char dev_type
);
238 static int sg_last_dev(void);
239 static int sg_build_dir(Sg_request
* srp
, Sg_fd
* sfp
, int dxfer_len
);
240 static void sg_unmap_and(Sg_scatter_hold
* schp
, int free_also
);
241 static Sg_device
* sg_get_dev(int dev
);
243 static Sg_device
** sg_dev_arr
= NULL
;
245 static const int size_sg_header
= sizeof(struct sg_header
);
246 static const int size_sg_io_hdr
= sizeof(sg_io_hdr_t
);
247 static const int size_sg_iovec
= sizeof(sg_iovec_t
);
248 static const int size_sg_req_info
= sizeof(sg_req_info_t
);
251 static int sg_open(struct inode
* inode
, struct file
* filp
)
253 int dev
= MINOR(inode
->i_rdev
);
254 int flags
= filp
->f_flags
;
259 sdp
= sg_get_dev(dev
);
260 if ((! sdp
) || (! sdp
->device
) || (! sdp
->device
->host
))
262 if (sdp
->i_rdev
!= inode
->i_rdev
)
263 printk("sg_open: inode maj=%d, min=%d sdp maj=%d, min=%d\n",
264 MAJOR(inode
->i_rdev
), MINOR(inode
->i_rdev
),
265 MAJOR(sdp
->i_rdev
), MINOR(sdp
->i_rdev
));
266 /* If we are in the middle of error recovery, don't let anyone
267 * else try and use this device. Also, if error recovery fails, it
268 * may try and take the device offline, in which case all further
269 * access to the device is prohibited. */
270 if(! scsi_block_when_processing_errors(sdp
->device
))
273 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev
, flags
));
275 if (flags
& O_EXCL
) {
276 if (O_RDONLY
== (flags
& O_ACCMODE
))
277 return -EACCES
; /* Can't lock it with read only access */
278 if (sdp
->headfp
&& (filp
->f_flags
& O_NONBLOCK
))
280 res
= 0; /* following is a macro that beats race condition */
281 __wait_event_interruptible(sdp
->o_excl_wait
,
282 ((sdp
->headfp
|| sdp
->exclude
) ? 0 : (sdp
->exclude
= 1)),
285 return res
; /* -ERESTARTSYS because signal hit process */
287 else if (sdp
->exclude
) { /* some other fd has an exclusive lock on dev */
288 if (filp
->f_flags
& O_NONBLOCK
)
290 res
= 0; /* following is a macro that beats race condition */
291 __wait_event_interruptible(sdp
->o_excl_wait
, (! sdp
->exclude
), res
);
293 return res
; /* -ERESTARTSYS because signal hit process */
295 if (! sdp
->headfp
) { /* no existing opens on this device */
297 sdp
->sg_tablesize
= sdp
->device
->host
->sg_tablesize
;
299 if ((sfp
= sg_add_sfp(sdp
, dev
)))
300 filp
->private_data
= sfp
;
302 if (flags
& O_EXCL
) sdp
->exclude
= 0; /* undo if error */
306 if (sdp
->device
->host
->hostt
->module
)
307 __MOD_INC_USE_COUNT(sdp
->device
->host
->hostt
->module
);
311 /* Following function was formerly called 'sg_close' */
312 static int sg_release(struct inode
* inode
, struct file
* filp
)
318 if ((! (sfp
= (Sg_fd
*)filp
->private_data
)) || (! (sdp
= sfp
->parentdp
))) {
322 SCSI_LOG_TIMEOUT(3, printk("sg_release: dev=%d\n", MINOR(sdp
->i_rdev
)));
323 sg_fasync(-1, filp
, 0); /* remove filp from async notification list */
324 sg_remove_sfp(sdp
, sfp
);
326 filp
->private_data
= NULL
;
328 if (sdp
->device
->host
->hostt
->module
)
329 __MOD_DEC_USE_COUNT(sdp
->device
->host
->hostt
->module
);
331 wake_up_interruptible(&sdp
->o_excl_wait
);
336 static ssize_t
sg_read(struct file
* filp
, char * buf
,
337 size_t count
, loff_t
*ppos
)
343 int req_pack_id
= -1;
344 struct sg_header old_hdr
;
348 if ((! (sfp
= (Sg_fd
*)filp
->private_data
)) || (! (sdp
= sfp
->parentdp
)))
350 SCSI_LOG_TIMEOUT(3, printk("sg_read: dev=%d, count=%d\n",
351 MINOR(sdp
->i_rdev
), (int)count
));
353 if(! scsi_block_when_processing_errors(sdp
->device
))
355 if (ppos
!= &filp
->f_pos
)
356 ; /* FIXME: Hmm. Seek to the right place, or fail? */
357 if ((k
= verify_area(VERIFY_WRITE
, buf
, count
)))
359 if (sfp
->force_packid
&& (count
>= size_sg_header
)) {
360 __copy_from_user(&old_hdr
, buf
, size_sg_header
);
361 if (old_hdr
.reply_len
< 0) {
362 if (count
>= size_sg_io_hdr
) {
363 __copy_from_user(&new_hdr
, buf
, size_sg_io_hdr
);
364 req_pack_id
= new_hdr
.pack_id
;
368 req_pack_id
= old_hdr
.pack_id
;
370 srp
= sg_get_rq_mark(sfp
, req_pack_id
);
371 if (! srp
) { /* now wait on packet to arrive */
372 if (filp
->f_flags
& O_NONBLOCK
)
375 int dio
= sg_dio_in_use(sfp
);
376 res
= 0; /* following is a macro that beats race condition */
377 __wait_event_interruptible(sfp
->read_wait
,
378 (srp
= sg_get_rq_mark(sfp
, req_pack_id
)),
382 else if (! dio
) /* only let signal out if no dio */
383 return res
; /* -ERESTARTSYS because signal hit process */
386 if (srp
->header
.interface_id
!= '\0')
387 return sg_new_read(sfp
, buf
, count
, srp
);
390 memset(&old_hdr
, 0, size_sg_header
);
391 old_hdr
.reply_len
= (int)hp
->timeout
;
392 old_hdr
.pack_len
= old_hdr
.reply_len
; /* very old, strange behaviour */
393 old_hdr
.pack_id
= hp
->pack_id
;
394 old_hdr
.twelve_byte
=
395 ((srp
->data
.cmd_opcode
>= 0xc0) && (12 == hp
->cmd_len
)) ? 1 : 0;
396 old_hdr
.target_status
= hp
->masked_status
;
397 old_hdr
.host_status
= hp
->host_status
;
398 old_hdr
.driver_status
= hp
->driver_status
;
399 if ((CHECK_CONDITION
& hp
->masked_status
) ||
400 (DRIVER_SENSE
& hp
->driver_status
))
401 memcpy(old_hdr
.sense_buffer
, srp
->sense_b
,
402 sizeof(old_hdr
.sense_buffer
));
403 switch (hp
->host_status
)
404 { /* This setup of 'result' is for backward compatibility and is best
405 ignored by the user who should use target, host + driver status */
407 case DID_PASSTHROUGH
:
414 old_hdr
.result
= EBUSY
;
421 old_hdr
.result
= EIO
;
425 (srp
->sense_b
[0] == 0 && hp
->masked_status
== GOOD
) ? 0 : EIO
;
428 old_hdr
.result
= EIO
;
432 /* Now copy the result back to the user buffer. */
433 if (count
>= size_sg_header
) {
434 __copy_to_user(buf
, &old_hdr
, size_sg_header
);
435 buf
+= size_sg_header
;
436 if (count
> old_hdr
.reply_len
)
437 count
= old_hdr
.reply_len
;
438 if (count
> size_sg_header
)
439 sg_read_oxfer(srp
, buf
, count
- size_sg_header
);
442 count
= (old_hdr
.result
== 0) ? 0 : -EIO
;
443 sg_finish_rem_req(srp
);
447 static ssize_t
sg_new_read(Sg_fd
* sfp
, char * buf
, size_t count
,
450 Sg_device
* sdp
= sfp
->parentdp
;
451 sg_io_hdr_t
* hp
= &srp
->header
;
454 if(! scsi_block_when_processing_errors(sdp
->device
) )
456 if (count
< size_sg_io_hdr
)
460 if ((hp
->mx_sb_len
> 0) && hp
->sbp
) {
461 if ((CHECK_CONDITION
& hp
->masked_status
) ||
462 (DRIVER_SENSE
& hp
->driver_status
)) {
463 int sb_len
= sizeof(dummy_cmdp
->sense_buffer
);
464 sb_len
= (hp
->mx_sb_len
> sb_len
) ? sb_len
: hp
->mx_sb_len
;
465 len
= 8 + (int)srp
->sense_b
[7]; /* Additional sense length field */
466 len
= (len
> sb_len
) ? sb_len
: len
;
467 if ((k
= verify_area(VERIFY_WRITE
, hp
->sbp
, len
)))
469 __copy_to_user(hp
->sbp
, srp
->sense_b
, len
);
473 if (hp
->masked_status
|| hp
->host_status
|| hp
->driver_status
)
474 hp
->info
|= SG_INFO_CHECK
;
475 copy_to_user(buf
, hp
, size_sg_io_hdr
);
477 k
= sg_read_xfer(srp
);
478 if (k
) return k
; /* probably -EFAULT, bad addr in dxferp or iovec list */
479 sg_finish_rem_req(srp
);
484 static ssize_t
sg_write(struct file
* filp
, const char * buf
,
485 size_t count
, loff_t
*ppos
)
487 int mxsize
, cmd_size
, k
;
488 int input_size
, blocking
;
489 unsigned char opcode
;
493 struct sg_header old_hdr
;
495 unsigned char cmnd
[sizeof(dummy_cmdp
->cmnd
)];
497 if ((! (sfp
= (Sg_fd
*)filp
->private_data
)) || (! (sdp
= sfp
->parentdp
)))
499 SCSI_LOG_TIMEOUT(3, printk("sg_write: dev=%d, count=%d\n",
500 MINOR(sdp
->i_rdev
), (int)count
));
502 if(! scsi_block_when_processing_errors(sdp
->device
) )
504 if (ppos
!= &filp
->f_pos
)
505 ; /* FIXME: Hmm. Seek to the right place, or fail? */
507 if ((k
= verify_area(VERIFY_READ
, buf
, count
)))
508 return k
; /* protects following copy_from_user()s + get_user()s */
509 if (count
< size_sg_header
)
511 __copy_from_user(&old_hdr
, buf
, size_sg_header
);
512 blocking
= !(filp
->f_flags
& O_NONBLOCK
);
513 if (old_hdr
.reply_len
< 0)
514 return sg_new_write(sfp
, buf
, count
, blocking
, 0, NULL
);
515 if (count
< (size_sg_header
+ 6))
516 return -EIO
; /* The minimum scsi command length is 6 bytes. */
518 if (! (srp
= sg_add_request(sfp
))) {
519 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
522 buf
+= size_sg_header
;
523 __get_user(opcode
, buf
);
524 if (sfp
->next_cmd_len
> 0) {
525 if (sfp
->next_cmd_len
> MAX_COMMAND_SIZE
) {
526 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
527 sfp
->next_cmd_len
= 0;
528 sg_remove_request(sfp
, srp
);
531 cmd_size
= sfp
->next_cmd_len
;
532 sfp
->next_cmd_len
= 0; /* reset so only this write() effected */
535 cmd_size
= COMMAND_SIZE(opcode
); /* based on SCSI command group */
536 if ((opcode
>= 0xc0) && old_hdr
.twelve_byte
)
539 SCSI_LOG_TIMEOUT(4, printk("sg_write: scsi opcode=0x%02x, cmd_size=%d\n",
540 (int)opcode
, cmd_size
));
541 /* Determine buffer size. */
542 input_size
= count
- cmd_size
;
543 mxsize
= (input_size
> old_hdr
.reply_len
) ? input_size
:
545 mxsize
-= size_sg_header
;
546 input_size
-= size_sg_header
;
547 if (input_size
< 0) {
548 sg_remove_request(sfp
, srp
);
549 return -EIO
; /* User did not pass enough bytes for this command. */
552 hp
->interface_id
= '\0'; /* indicator of old interface tunnelled */
553 hp
->cmd_len
= (unsigned char)cmd_size
;
557 hp
->dxfer_direction
= SG_DXFER_UNKNOWN
;
560 hp
->dxfer_direction
= ((old_hdr
.reply_len
- size_sg_header
) > 0) ?
561 SG_DXFER_TO_FROM_DEV
: SG_DXFER_TO_DEV
;
563 hp
->dxfer_direction
= (mxsize
> 0) ? SG_DXFER_FROM_DEV
:
566 hp
->dxfer_len
= mxsize
;
567 hp
->dxferp
= (unsigned char *)buf
+ cmd_size
;
569 hp
->timeout
= old_hdr
.reply_len
; /* structure abuse ... */
570 hp
->flags
= input_size
; /* structure abuse ... */
571 hp
->pack_id
= old_hdr
.pack_id
;
573 __copy_from_user(cmnd
, buf
, cmd_size
);
574 k
= sg_common_write(sfp
, srp
, cmnd
, sfp
->timeout
, blocking
);
575 return (k
< 0) ? k
: count
;
578 static ssize_t
sg_new_write(Sg_fd
* sfp
, const char * buf
, size_t count
,
579 int blocking
, int read_only
, Sg_request
** o_srp
)
584 unsigned char cmnd
[sizeof(dummy_cmdp
->cmnd
)];
587 if (count
< size_sg_io_hdr
)
589 if ((k
= verify_area(VERIFY_READ
, buf
, count
)))
590 return k
; /* protects following copy_from_user()s + get_user()s */
592 sfp
->cmd_q
= 1; /* when sg_io_hdr seen, set command queuing on */
593 if (! (srp
= sg_add_request(sfp
))) {
594 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
598 __copy_from_user(hp
, buf
, size_sg_io_hdr
);
599 if (hp
->interface_id
!= 'S') {
600 sg_remove_request(sfp
, srp
);
603 timeout
= sg_ms_to_jif(srp
->header
.timeout
);
604 if ((! hp
->cmdp
) || (hp
->cmd_len
< 6) || (hp
->cmd_len
> sizeof(cmnd
))) {
605 sg_remove_request(sfp
, srp
);
608 if ((k
= verify_area(VERIFY_READ
, hp
->cmdp
, hp
->cmd_len
))) {
609 sg_remove_request(sfp
, srp
);
610 return k
; /* protects following copy_from_user()s + get_user()s */
612 __copy_from_user(cmnd
, hp
->cmdp
, hp
->cmd_len
);
614 (! sg_allow_access(cmnd
[0], sfp
->parentdp
->device
->type
))) {
615 sg_remove_request(sfp
, srp
);
618 k
= sg_common_write(sfp
, srp
, cmnd
, timeout
, blocking
);
620 if (o_srp
) *o_srp
= srp
;
624 static int sg_common_write(Sg_fd
* sfp
, Sg_request
* srp
,
625 unsigned char * cmnd
, int timeout
, int blocking
)
629 Sg_device
* sdp
= sfp
->parentdp
;
630 sg_io_hdr_t
* hp
= &srp
->header
;
632 srp
->data
.cmd_opcode
= cmnd
[0]; /* hold opcode of command */
634 hp
->masked_status
= 0;
638 hp
->driver_status
= 0;
641 printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
642 (int)cmnd
[0], (int)hp
->cmd_len
));
644 if ((k
= sg_start_req(srp
))) {
645 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k
));
646 sg_finish_rem_req(srp
);
647 return k
; /* probably out of space --> ENOMEM */
649 if ((k
= sg_write_xfer(srp
))) {
650 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
651 sg_finish_rem_req(srp
);
654 /* SCSI_LOG_TIMEOUT(7, printk("sg_write: allocating device\n")); */
655 SCpnt
= scsi_allocate_device(sdp
->device
, blocking
, TRUE
);
657 sg_finish_rem_req(srp
);
658 return (signal_pending(current
)) ? -EINTR
: -EAGAIN
;
659 /* No available command blocks, or, interrupted while waiting */
661 /* SCSI_LOG_TIMEOUT(7, printk("sg_write: device allocated\n")); */
662 srp
->my_cmdp
= SCpnt
;
663 SCpnt
->request
.rq_dev
= sdp
->i_rdev
;
664 SCpnt
->request
.rq_status
= RQ_ACTIVE
;
665 SCpnt
->sense_buffer
[0] = 0;
666 SCpnt
->cmd_len
= hp
->cmd_len
;
667 /* Set the LUN field in the command structure, overriding user input */
668 if (! (hp
->flags
& SG_FLAG_LUN_INHIBIT
))
669 cmnd
[1] = (cmnd
[1] & 0x1f) | (sdp
->device
->lun
<< 5);
671 /* SCSI_LOG_TIMEOUT(7, printk("sg_write: do cmd\n")); */
672 SCpnt
->use_sg
= srp
->data
.k_use_sg
;
673 SCpnt
->sglist_len
= srp
->data
.sglist_len
;
674 SCpnt
->bufflen
= srp
->data
.bufflen
;
675 SCpnt
->underflow
= 0;
676 SCpnt
->buffer
= srp
->data
.buffer
;
677 switch (hp
->dxfer_direction
) {
678 case SG_DXFER_TO_FROM_DEV
:
679 case SG_DXFER_FROM_DEV
:
680 SCpnt
->sc_data_direction
= SCSI_DATA_READ
; break;
681 case SG_DXFER_TO_DEV
:
682 SCpnt
->sc_data_direction
= SCSI_DATA_WRITE
; break;
683 case SG_DXFER_UNKNOWN
:
684 SCpnt
->sc_data_direction
= SCSI_DATA_UNKNOWN
; break;
686 SCpnt
->sc_data_direction
= SCSI_DATA_NONE
; break;
688 srp
->data
.k_use_sg
= 0;
689 srp
->data
.sglist_len
= 0;
690 srp
->data
.bufflen
= 0;
691 srp
->data
.buffer
= NULL
;
692 hp
->duration
= jiffies
; /* unit jiffies now, millisecs after done */
693 /* Now send everything of to mid-level. The next time we hear about this
694 packet is when sg_cmd_done_bh() is called (i.e. a callback). */
695 scsi_do_cmd(SCpnt
, (void *)cmnd
,
696 (void *)SCpnt
->buffer
, hp
->dxfer_len
,
697 sg_cmd_done_bh
, timeout
, SG_DEFAULT_RETRIES
);
698 /* dxfer_len overwrites SCpnt->bufflen, hence need for b_malloc_len */
702 static int sg_ioctl(struct inode
* inode
, struct file
* filp
,
703 unsigned int cmd_in
, unsigned long arg
)
705 int result
, val
, read_only
;
709 unsigned long iflags
;
711 if ((! (sfp
= (Sg_fd
*)filp
->private_data
)) || (! (sdp
= sfp
->parentdp
)))
713 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: dev=%d, cmd=0x%x\n",
714 MINOR(sdp
->i_rdev
), (int)cmd_in
));
715 if(! scsi_block_when_processing_errors(sdp
->device
) )
717 read_only
= (O_RDWR
!= (filp
->f_flags
& O_ACCMODE
));
723 int blocking
= 1; /* ignore O_NONBLOCK flag */
725 if(! scsi_block_when_processing_errors(sdp
->device
) )
727 result
= verify_area(VERIFY_WRITE
, (void *)arg
, size_sg_io_hdr
);
728 if (result
) return result
;
729 result
= sg_new_write(sfp
, (const char *)arg
, size_sg_io_hdr
,
730 blocking
, read_only
, &srp
);
731 if (result
< 0) return result
;
732 srp
->sg_io_owned
= 1;
734 int dio
= sg_dio_in_use(sfp
);
735 result
= 0; /* following macro to beat race condition */
736 __wait_event_interruptible(sfp
->read_wait
,
737 (sfp
->closed
|| srp
->done
), result
);
739 return 0; /* request packet dropped already */
742 else if (! dio
) { /* only let signal out if no dio */
744 return result
; /* -ERESTARTSYS because signal hit process */
748 result
= sg_new_read(sfp
, (char *)arg
, size_sg_io_hdr
, srp
);
749 return (result
< 0) ? result
: 0;
752 result
= get_user(val
, (int *)arg
);
753 if (result
) return result
;
758 case SG_GET_TIMEOUT
: /* N.B. User receives timeout as return value */
759 return sfp
->timeout
; /* strange ..., for backward compatibility */
760 case SG_SET_FORCE_LOW_DMA
:
761 result
= get_user(val
, (int *)arg
);
762 if (result
) return result
;
765 if ((0 == sfp
->low_dma
) && (0 == sg_res_in_use(sfp
))) {
766 val
= (int)sfp
->reserve
.bufflen
;
767 sg_remove_scat(&sfp
->reserve
);
768 sg_build_reserve(sfp
, val
);
772 sfp
->low_dma
= sdp
->device
->host
->unchecked_isa_dma
;
775 return put_user((int)sfp
->low_dma
, (int *)arg
);
777 result
= verify_area(VERIFY_WRITE
, (void *)arg
, sizeof(sg_scsi_id_t
));
778 if (result
) return result
;
780 sg_scsi_id_t
* sg_idp
= (sg_scsi_id_t
*)arg
;
781 __put_user((int)sdp
->device
->host
->host_no
, &sg_idp
->host_no
);
782 __put_user((int)sdp
->device
->channel
, &sg_idp
->channel
);
783 __put_user((int)sdp
->device
->id
, &sg_idp
->scsi_id
);
784 __put_user((int)sdp
->device
->lun
, &sg_idp
->lun
);
785 __put_user((int)sdp
->device
->type
, &sg_idp
->scsi_type
);
786 __put_user((short)sdp
->device
->host
->cmd_per_lun
,
787 &sg_idp
->h_cmd_per_lun
);
788 __put_user((short)sdp
->device
->queue_depth
,
789 &sg_idp
->d_queue_depth
);
790 __put_user(0, &sg_idp
->unused
[0]);
791 __put_user(0, &sg_idp
->unused
[1]);
794 case SG_SET_FORCE_PACK_ID
:
795 result
= get_user(val
, (int *)arg
);
796 if (result
) return result
;
797 sfp
->force_packid
= val
? 1 : 0;
800 result
= verify_area(VERIFY_WRITE
, (void *) arg
, sizeof(int));
801 if (result
) return result
;
802 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
803 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
804 if ((1 == srp
->done
) && (! srp
->sg_io_owned
)) {
805 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
806 __put_user(srp
->header
.pack_id
, (int *)arg
);
810 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
811 __put_user(-1, (int *)arg
);
813 case SG_GET_NUM_WAITING
:
814 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
815 for (val
= 0, srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
816 if ((1 == srp
->done
) && (! srp
->sg_io_owned
))
819 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
820 return put_user(val
, (int *)arg
);
821 case SG_GET_SG_TABLESIZE
:
822 return put_user(sdp
->sg_tablesize
, (int *)arg
);
823 case SG_SET_RESERVED_SIZE
:
824 result
= get_user(val
, (int *)arg
);
825 if (result
) return result
;
826 if (val
!= sfp
->reserve
.bufflen
) {
827 if (sg_res_in_use(sfp
))
829 sg_remove_scat(&sfp
->reserve
);
830 sg_build_reserve(sfp
, val
);
833 case SG_GET_RESERVED_SIZE
:
834 val
= (int)sfp
->reserve
.bufflen
;
835 return put_user(val
, (int *)arg
);
836 case SG_SET_COMMAND_Q
:
837 result
= get_user(val
, (int *)arg
);
838 if (result
) return result
;
839 sfp
->cmd_q
= val
? 1 : 0;
841 case SG_GET_COMMAND_Q
:
842 return put_user((int)sfp
->cmd_q
, (int *)arg
);
843 case SG_SET_KEEP_ORPHAN
:
844 result
= get_user(val
, (int *)arg
);
845 if (result
) return result
;
846 sfp
->keep_orphan
= val
;
848 case SG_GET_KEEP_ORPHAN
:
849 return put_user((int)sfp
->keep_orphan
, (int *)arg
);
850 case SG_NEXT_CMD_LEN
:
851 result
= get_user(val
, (int *)arg
);
852 if (result
) return result
;
853 sfp
->next_cmd_len
= (val
> 0) ? val
: 0;
855 case SG_GET_VERSION_NUM
:
856 return put_user(sg_version_num
, (int *)arg
);
857 case SG_GET_REQUEST_TABLE
:
858 result
= verify_area(VERIFY_WRITE
, (void *) arg
,
859 size_sg_req_info
* SG_MAX_QUEUE
);
860 if (result
) return result
;
862 sg_req_info_t rinfo
[SG_MAX_QUEUE
];
864 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
865 for (srp
= sfp
->headrp
, val
= 0; val
< SG_MAX_QUEUE
;
866 ++val
, srp
= srp
? srp
->nextrp
: srp
) {
867 memset(&rinfo
[val
], 0, size_sg_req_info
);
869 rinfo
[val
].req_state
= srp
->done
+ 1;
870 rinfo
[val
].problem
= srp
->header
.masked_status
&
871 srp
->header
.host_status
& srp
->header
.driver_status
;
872 rinfo
[val
].duration
= srp
->done
?
873 srp
->header
.duration
:
874 sg_jif_to_ms(jiffies
- srp
->header
.duration
);
875 rinfo
[val
].orphan
= srp
->orphan
;
876 rinfo
[val
].sg_io_owned
= srp
->sg_io_owned
;
877 rinfo
[val
].pack_id
= srp
->header
.pack_id
;
878 rinfo
[val
].usr_ptr
= srp
->header
.usr_ptr
;
881 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
882 __copy_to_user((void *)arg
, rinfo
, size_sg_req_info
* SG_MAX_QUEUE
);
885 case SG_EMULATED_HOST
:
886 return put_user(sdp
->device
->host
->hostt
->emulated
, (int *)arg
);
888 if (! scsi_block_when_processing_errors(sdp
->device
))
890 result
= get_user(val
, (int *)arg
);
891 if (result
) return result
;
892 if (SG_SCSI_RESET_NOTHING
== val
)
894 #ifdef SCSI_TRY_RESET_DEVICE
897 case SG_SCSI_RESET_DEVICE
:
898 val
= SCSI_TRY_RESET_DEVICE
;
900 case SG_SCSI_RESET_BUS
:
901 val
= SCSI_TRY_RESET_BUS
;
903 case SG_SCSI_RESET_HOST
:
904 val
= SCSI_TRY_RESET_HOST
;
909 if(! capable(CAP_SYS_ADMIN
)) return -EACCES
;
910 return (scsi_reset_provider(sdp
->device
, val
) == SUCCESS
) ? 0 : -EIO
;
912 SCSI_LOG_TIMEOUT(1, printk("sg_ioctl: SG_RESET_SCSI not supported\n"));
915 case SCSI_IOCTL_SEND_COMMAND
:
917 unsigned char opcode
= WRITE_6
;
918 Scsi_Ioctl_Command
* siocp
= (void *)arg
;
920 copy_from_user(&opcode
, siocp
->data
, 1);
921 if (! sg_allow_access(opcode
, sdp
->device
->type
))
924 return scsi_ioctl_send_command(sdp
->device
, (void *)arg
);
926 result
= get_user(val
, (int *)arg
);
927 if (result
) return result
;
928 sdp
->sgdebug
= (char)val
;
930 case SCSI_IOCTL_GET_IDLUN
:
931 case SCSI_IOCTL_GET_BUS_NUMBER
:
932 case SCSI_IOCTL_PROBE_HOST
:
933 case SG_GET_TRANSFORM
:
934 return scsi_ioctl(sdp
->device
, cmd_in
, (void *)arg
);
937 return -EACCES
; /* don't know so take safe approach */
938 return scsi_ioctl(sdp
->device
, cmd_in
, (void *)arg
);
942 static unsigned int sg_poll(struct file
* filp
, poll_table
* wait
)
944 unsigned int res
= 0;
949 unsigned long iflags
;
951 if ((! (sfp
= (Sg_fd
*)filp
->private_data
)) || (! (sdp
= sfp
->parentdp
)))
953 poll_wait(filp
, &sfp
->read_wait
, wait
);
954 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
955 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
956 /* if any read waiting, flag it */
957 if ((0 == res
) && (1 == srp
->done
) && (! srp
->sg_io_owned
))
958 res
= POLLIN
| POLLRDNORM
;
961 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
964 res
|= POLLOUT
| POLLWRNORM
;
966 else if (count
< SG_MAX_QUEUE
)
967 res
|= POLLOUT
| POLLWRNORM
;
968 SCSI_LOG_TIMEOUT(3, printk("sg_poll: dev=%d, res=0x%x\n",
969 MINOR(sdp
->i_rdev
), (int)res
));
973 static int sg_fasync(int fd
, struct file
* filp
, int mode
)
979 if ((! (sfp
= (Sg_fd
*)filp
->private_data
)) || (! (sdp
= sfp
->parentdp
)))
981 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: dev=%d, mode=%d\n",
982 MINOR(sdp
->i_rdev
), mode
));
984 retval
= fasync_helper(fd
, filp
, mode
, &sfp
->async_qp
);
985 return (retval
< 0) ? retval
: 0;
988 /* This function is a "bottom half" handler that is called by the
989 * mid level when a command is completed (or has failed). */
990 static void sg_cmd_done_bh(Scsi_Cmnd
* SCpnt
)
992 int dev
= MINOR(SCpnt
->request
.rq_dev
);
993 Sg_device
* sdp
= NULL
;
995 Sg_request
* srp
= NULL
;
997 read_lock(&sg_dev_arr_lock
);
998 if (sg_dev_arr
&& (dev
>= 0)) {
999 if (dev
< sg_template
.dev_max
)
1000 sdp
= sg_dev_arr
[dev
];
1003 read_unlock(&sg_dev_arr_lock
);
1004 SCSI_LOG_TIMEOUT(1, printk("sg...bh: bad args dev=%d\n", dev
));
1005 scsi_release_command(SCpnt
);
1011 read_lock(&sfp
->rq_list_lock
);
1012 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
1013 if (SCpnt
== srp
->my_cmdp
)
1016 read_unlock(&sfp
->rq_list_lock
);
1021 read_unlock(&sg_dev_arr_lock
);
1023 SCSI_LOG_TIMEOUT(1, printk("sg...bh: req missing, dev=%d\n", dev
));
1024 scsi_release_command(SCpnt
);
1028 /* First transfer ownership of data buffers to sg_device object. */
1029 srp
->data
.k_use_sg
= SCpnt
->use_sg
;
1030 srp
->data
.sglist_len
= SCpnt
->sglist_len
;
1031 srp
->data
.bufflen
= SCpnt
->bufflen
;
1032 srp
->data
.buffer
= SCpnt
->buffer
;
1033 sg_clr_scpnt(SCpnt
);
1034 srp
->my_cmdp
= NULL
;
1037 SCSI_LOG_TIMEOUT(4, printk("sg...bh: dev=%d, pack_id=%d, res=0x%x\n",
1038 dev
, srp
->header
.pack_id
, (int)SCpnt
->result
));
1039 srp
->header
.resid
= SCpnt
->resid
;
1040 /* sg_unmap_and(&srp->data, 0); */ /* unmap locked pages a.s.a.p. */
1041 /* N.B. unit of duration changes here from jiffies to millisecs */
1042 srp
->header
.duration
= sg_jif_to_ms(jiffies
- (int)srp
->header
.duration
);
1043 if (0 != SCpnt
->result
) {
1044 memcpy(srp
->sense_b
, SCpnt
->sense_buffer
, sizeof(srp
->sense_b
));
1045 srp
->header
.status
= 0xff & SCpnt
->result
;
1046 srp
->header
.masked_status
= status_byte(SCpnt
->result
);
1047 srp
->header
.msg_status
= msg_byte(SCpnt
->result
);
1048 srp
->header
.host_status
= host_byte(SCpnt
->result
);
1049 srp
->header
.driver_status
= driver_byte(SCpnt
->result
);
1050 if ((sdp
->sgdebug
> 0) &&
1051 ((CHECK_CONDITION
== srp
->header
.masked_status
) ||
1052 (COMMAND_TERMINATED
== srp
->header
.masked_status
)))
1053 print_sense("sg_cmd_done_bh", SCpnt
);
1055 /* Following if statement is a patch supplied by Eric Youngdale */
1056 if (driver_byte(SCpnt
->result
) != 0
1057 && (SCpnt
->sense_buffer
[0] & 0x7f) == 0x70
1058 && (SCpnt
->sense_buffer
[2] & 0xf) == UNIT_ATTENTION
1059 && sdp
->device
->removable
) {
1060 /* Detected disc change. Set the bit - this may be used if */
1061 /* there are filesystems using this device. */
1062 sdp
->device
->changed
= 1;
1065 /* Rely on write phase to clean out srp status values, so no "else" */
1067 scsi_release_command(SCpnt
);
1069 if (sfp
->closed
) { /* whoops this fd already released, cleanup */
1071 printk("sg...bh: already closed, freeing ...\n"));
1072 /* should check if module is unloaded <<<<<<< */
1073 sg_finish_rem_req(srp
);
1075 if (NULL
== sfp
->headrp
) {
1077 printk("sg...bh: already closed, final cleanup\n"));
1078 sg_remove_sfp(sdp
, sfp
);
1082 else if (srp
&& srp
->orphan
) {
1083 if (sfp
->keep_orphan
)
1084 srp
->sg_io_owned
= 0;
1086 sg_finish_rem_req(srp
);
1091 /* Now wake up any sg_read() that is waiting for this packet. */
1092 wake_up_interruptible(&sfp
->read_wait
);
1093 kill_fasync(&sfp
->async_qp
, SIGPOLL
, POLL_IN
);
1097 static struct file_operations sg_fops
= {
1104 release
: sg_release
,
1109 static int sg_detect(Scsi_Device
* scsidp
)
1111 switch (scsidp
->type
) {
1116 case TYPE_TAPE
: break;
1118 printk("Detected scsi generic sg%d at scsi%d,"
1119 " channel %d, id %d, lun %d, type %d\n",
1120 sg_template
.dev_noticed
,
1121 scsidp
->host
->host_no
, scsidp
->channel
,
1122 scsidp
->id
, scsidp
->lun
, scsidp
->type
);
1124 sg_template
.dev_noticed
++;
1128 /* Driver initialization */
1129 static int sg_init()
1131 static int sg_registered
= 0;
1132 unsigned long iflags
;
1134 if ((sg_template
.dev_noticed
== 0) || sg_dev_arr
)
1137 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1138 if(!sg_registered
) {
1139 if (devfs_register_chrdev(SCSI_GENERIC_MAJOR
,"sg",&sg_fops
))
1141 printk("Unable to get major %d for generic SCSI device\n",
1142 SCSI_GENERIC_MAJOR
);
1143 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1149 SCSI_LOG_TIMEOUT(3, printk("sg_init\n"));
1150 sg_template
.dev_max
= sg_template
.dev_noticed
+ SG_DEV_ARR_LUMP
;
1151 sg_dev_arr
= (Sg_device
**)kmalloc(sg_template
.dev_max
*
1152 sizeof(Sg_device
*), GFP_ATOMIC
);
1153 if (NULL
== sg_dev_arr
) {
1154 printk("sg_init: no space for sg_dev_arr\n");
1155 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1158 memset(sg_dev_arr
, 0, sg_template
.dev_max
* sizeof(Sg_device
*));
1159 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1160 #ifdef CONFIG_PROC_FS
1162 #endif /* CONFIG_PROC_FS */
1167 static int __init
sg_def_reserved_size_setup(char *str
)
1171 if (get_option(&str
, &tmp
) == 1) {
1172 def_reserved_size
= tmp
;
1177 printk("sg_def_reserved_size : usage sg_def_reserved_size=n "
1178 "(n could be 65536, 131072 or 262144)\n");
1183 __setup("sg_def_reserved_size=", sg_def_reserved_size_setup
);
1187 static int sg_attach(Scsi_Device
* scsidp
)
1190 unsigned long iflags
;
1193 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1194 if (sg_template
.nr_dev
>= sg_template
.dev_max
) { /* try to resize */
1195 Sg_device
** tmp_da
;
1196 int tmp_dev_max
= sg_template
.nr_dev
+ SG_DEV_ARR_LUMP
;
1198 tmp_da
= (Sg_device
**)kmalloc(tmp_dev_max
*
1199 sizeof(Sg_device
*), GFP_ATOMIC
);
1200 if (NULL
== tmp_da
) {
1202 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1203 printk("sg_attach: device array cannot be resized\n");
1206 memset(tmp_da
, 0, tmp_dev_max
* sizeof(Sg_device
*));
1207 memcpy(tmp_da
, sg_dev_arr
, sg_template
.dev_max
* sizeof(Sg_device
*));
1208 kfree((char *)sg_dev_arr
);
1209 sg_dev_arr
= tmp_da
;
1210 sg_template
.dev_max
= tmp_dev_max
;
1213 for(k
= 0; k
< sg_template
.dev_max
; k
++)
1214 if(! sg_dev_arr
[k
]) break;
1215 if(k
< sg_template
.dev_max
)
1216 sdp
= (Sg_device
*)kmalloc(sizeof(Sg_device
), GFP_ATOMIC
);
1221 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1222 printk("sg_attach: Sg_device cannot be allocated\n");
1226 SCSI_LOG_TIMEOUT(3, printk("sg_attach: dev=%d \n", k
));
1227 sdp
->device
= scsidp
;
1228 init_waitqueue_head(&sdp
->o_excl_wait
);
1233 sdp
->sg_tablesize
= scsidp
->host
? scsidp
->host
->sg_tablesize
: 0;
1234 sdp
->i_rdev
= MKDEV(SCSI_GENERIC_MAJOR
, k
);
1235 sdp
->de
= devfs_register (scsidp
->de
, "generic", DEVFS_FL_DEFAULT
,
1236 SCSI_GENERIC_MAJOR
, k
,
1237 S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
,
1239 sg_template
.nr_dev
++;
1240 sg_dev_arr
[k
] = sdp
;
1241 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1245 /* Called at 'finish' of init process, after all attaches */
1246 static void sg_finish(void)
1248 SCSI_LOG_TIMEOUT(3, printk("sg_finish: dma_free_sectors=%u\n",
1249 scsi_dma_free_sectors
));
1252 static void sg_detach(Scsi_Device
* scsidp
)
1255 unsigned long iflags
;
1260 if (NULL
== sg_dev_arr
)
1262 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1263 for (k
= 0; k
< sg_template
.dev_max
; k
++) {
1264 sdp
= sg_dev_arr
[k
];
1265 if ((NULL
== sdp
) || (sdp
->device
!= scsidp
))
1266 continue; /* dirty but lowers nesting */
1268 for (sfp
= sdp
->headfp
; sfp
; sfp
= sfp
->nextfp
) {
1269 /* no lock on request list here */
1270 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
) {
1272 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1273 sg_shorten_timeout(srp
->my_cmdp
);
1274 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1278 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1279 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty, sleep(3)\n", k
));
1280 scsi_sleep(3); /* sleep 3 jiffies, hoping for timeout to go off */
1281 devfs_unregister (sdp
->de
);
1284 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
1287 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k
));
1288 devfs_unregister (sdp
->de
);
1290 sg_dev_arr
[k
] = NULL
;
1293 sg_template
.nr_dev
--;
1294 /* avoid associated device /dev/sg? being incremented
1295 * each time module is inserted/removed , <dan@lectra.fr> */
1296 sg_template
.dev_noticed
--;
1299 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
1305 MODULE_PARM(def_reserved_size
, "i");
1306 MODULE_PARM_DESC(def_reserved_size
, "size of buffer reserved for each fd");
1308 int init_module(void) {
1309 if (def_reserved_size
>= 0)
1310 sg_big_buff
= def_reserved_size
;
1311 sg_template
.module
= &__this_module
;
1312 return scsi_register_module(MODULE_SCSI_DEV
, &sg_template
);
1315 void cleanup_module( void)
1317 #ifdef CONFIG_PROC_FS
1319 #endif /* CONFIG_PROC_FS */
1320 scsi_unregister_module(MODULE_SCSI_DEV
, &sg_template
);
1321 devfs_unregister_chrdev(SCSI_GENERIC_MAJOR
, "sg");
1322 if(sg_dev_arr
!= NULL
) {
1323 /* Really worrying situation of writes still pending and get here */
1324 /* Strategy: shorten timeout on release + wait on detach ... */
1325 kfree((char *)sg_dev_arr
);
1328 sg_template
.dev_max
= 0;
1334 extern void scsi_times_out (Scsi_Cmnd
* SCpnt
);
1335 extern void scsi_old_times_out (Scsi_Cmnd
* SCpnt
);
1338 /* Can't see clean way to abort a command so shorten timeout to 1 jiffy */
1339 static void sg_shorten_timeout(Scsi_Cmnd
* scpnt
)
1341 #if 0 /* scsi_syms.c is very miserly about exported functions */
1342 scsi_delete_timer(scpnt
);
1345 scpnt
->timeout_per_command
= 1; /* try 1 jiffy (perhaps 0 jiffies) */
1346 if (scpnt
->host
->hostt
->use_new_eh_code
)
1347 scsi_add_timer(scpnt
, scpnt
->timeout_per_command
, scsi_times_out
);
1349 scsi_add_timer(scpnt
, scpnt
->timeout_per_command
,
1350 scsi_old_times_out
);
1352 scsi_sleep(HZ
); /* just sleep 1 second and hope ... */
1356 static int sg_start_req(Sg_request
* srp
)
1359 Sg_fd
* sfp
= srp
->parentfp
;
1360 sg_io_hdr_t
* hp
= &srp
->header
;
1361 int dxfer_len
= (int)hp
->dxfer_len
;
1362 int dxfer_dir
= hp
->dxfer_direction
;
1363 Sg_scatter_hold
* req_schp
= &srp
->data
;
1364 Sg_scatter_hold
* rsv_schp
= &sfp
->reserve
;
1366 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len
));
1367 if ((dxfer_len
<= 0) || (dxfer_dir
== SG_DXFER_NONE
))
1369 if ((hp
->flags
& SG_FLAG_DIRECT_IO
) &&
1370 (dxfer_dir
!= SG_DXFER_UNKNOWN
) &&
1371 (0 == hp
->iovec_count
) &&
1372 (! sfp
->parentdp
->device
->host
->unchecked_isa_dma
)) {
1373 res
= sg_build_dir(srp
, sfp
, dxfer_len
);
1374 if (res
<= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1377 if ((! sg_res_in_use(sfp
)) && (dxfer_len
<= rsv_schp
->bufflen
)) {
1378 sg_link_reserve(sfp
, srp
, dxfer_len
);
1381 res
= sg_build_indi(req_schp
, sfp
, dxfer_len
);
1383 sg_remove_scat(req_schp
);
1390 static void sg_finish_rem_req(Sg_request
* srp
)
1392 Sg_fd
* sfp
= srp
->parentfp
;
1393 Sg_scatter_hold
* req_schp
= &srp
->data
;
1395 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n",
1396 (int)srp
->res_used
));
1397 sg_unmap_and(&srp
->data
, 1);
1399 sg_unlink_reserve(sfp
, srp
);
1401 sg_remove_scat(req_schp
);
1402 sg_remove_request(sfp
, srp
);
1405 static int sg_build_sgat(Sg_scatter_hold
* schp
, const Sg_fd
* sfp
)
1407 int mem_src
, ret_sz
;
1408 int sg_bufflen
= PAGE_SIZE
;
1409 int elem_sz
= sizeof(struct scatterlist
) + sizeof(char);
1410 int mx_sc_elems
= (sg_bufflen
/ elem_sz
) - 1;
1412 mem_src
= SG_HEAP_KMAL
;
1413 schp
->buffer
= (struct scatterlist
*)sg_malloc(sfp
, sg_bufflen
,
1415 schp
->buffer_mem_src
= (char)mem_src
;
1418 else if (ret_sz
!= sg_bufflen
) {
1419 sg_bufflen
= ret_sz
;
1420 mx_sc_elems
= (sg_bufflen
/ elem_sz
) - 1;
1422 schp
->sglist_len
= sg_bufflen
;
1423 memset(schp
->buffer
, 0, sg_bufflen
);
1424 return mx_sc_elems
; /* number of scat_gath elements allocated */
1427 static void sg_unmap_and(Sg_scatter_hold
* schp
, int free_also
)
1430 if (schp
&& schp
->kiobp
) {
1432 unmap_kiobuf(schp
->kiobp
);
1436 free_kiovec(1, &schp
->kiobp
);
1443 static int sg_build_dir(Sg_request
* srp
, Sg_fd
* sfp
, int dxfer_len
)
1446 int res
, k
, split
, offset
, num
, mx_sc_elems
, rem_sz
;
1449 struct scatterlist
* sclp
;
1450 unsigned long addr
, prev_addr
;
1451 sg_io_hdr_t
* hp
= &srp
->header
;
1452 Sg_scatter_hold
* schp
= &srp
->data
;
1453 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1455 res
= alloc_kiovec(1, &schp
->kiobp
);
1457 SCSI_LOG_TIMEOUT(5, printk("sg_build_dir: alloc_kiovec res=%d\n", res
));
1460 res
= map_user_kiobuf((SG_DXFER_TO_DEV
== hp
->dxfer_direction
) ? 1 : 0,
1461 schp
->kiobp
, (unsigned long)hp
->dxferp
, dxfer_len
);
1464 printk("sg_build_dir: map_user_kiobuf res=%d\n", res
));
1465 sg_unmap_and(schp
, 1);
1470 prev_addr
= (unsigned long) page_address(kp
->maplist
[0]);
1471 for (k
= 1, split
= 0; k
< kp
->nr_pages
; ++k
, prev_addr
= addr
) {
1472 addr
= (unsigned long) page_address(kp
->maplist
[k
]);
1473 if ((prev_addr
+ PAGE_SIZE
) != addr
) {
1480 schp
->buffer
= page_address(kp
->maplist
[0]) + kp
->offset
;
1481 schp
->bufflen
= dxfer_len
;
1482 schp
->buffer_mem_src
= SG_USER_MEM
;
1483 schp
->b_malloc_len
= dxfer_len
;
1484 hp
->info
|= SG_INFO_DIRECT_IO
;
1487 mx_sc_elems
= sg_build_sgat(schp
, sfp
);
1488 if (mx_sc_elems
<= 1) {
1489 sg_unmap_and(schp
, 1);
1490 sg_remove_scat(schp
);
1493 mem_src_arr
= schp
->buffer
+ (mx_sc_elems
* sizeof(struct scatterlist
));
1494 for (k
= 0, sclp
= schp
->buffer
, rem_sz
= dxfer_len
;
1495 (k
< sg_tablesize
) && (rem_sz
> 0) && (k
< mx_sc_elems
);
1497 offset
= (0 == k
) ? kp
->offset
: 0;
1498 num
= (rem_sz
> (PAGE_SIZE
- offset
)) ? (PAGE_SIZE
- offset
) :
1500 sclp
->address
= page_address(kp
->maplist
[k
]) + offset
;
1502 mem_src_arr
[k
] = SG_USER_MEM
;
1505 printk("sg_build_dir: k=%d, a=0x%p, len=%d, ms=%d\n",
1506 k
, sclp
->address
, num
, mem_src_arr
[k
]));
1510 printk("sg_build_dir: k_use_sg=%d, rem_sz=%d\n", k
, rem_sz
));
1511 schp
->bufflen
= dxfer_len
;
1512 if (rem_sz
> 0) { /* must have failed */
1513 sg_unmap_and(schp
, 1);
1514 sg_remove_scat(schp
);
1515 return 1; /* out of scatter gather elements, try indirect */
1517 hp
->info
|= SG_INFO_DIRECT_IO
;
1521 #endif /* SG_ALLOW_DIO */
1524 static int sg_build_indi(Sg_scatter_hold
* schp
, Sg_fd
* sfp
, int buff_size
)
1526 int ret_sz
, mem_src
;
1527 int blk_size
= buff_size
;
1530 if ((blk_size
< 0) || (! sfp
))
1533 ++blk_size
; /* don't know why */
1534 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1535 blk_size
= (blk_size
+ SG_SECTOR_MSK
) & (~SG_SECTOR_MSK
);
1536 SCSI_LOG_TIMEOUT(4, printk("sg_build_indi: buff_size=%d, blk_size=%d\n",
1537 buff_size
, blk_size
));
1538 if (blk_size
<= SG_SCATTER_SZ
) {
1539 mem_src
= SG_HEAP_PAGE
;
1540 p
= sg_malloc(sfp
, blk_size
, &ret_sz
, &mem_src
);
1543 if (blk_size
== ret_sz
) { /* got it on the first attempt */
1546 schp
->bufflen
= blk_size
;
1547 schp
->buffer_mem_src
= (char)mem_src
;
1548 schp
->b_malloc_len
= blk_size
;
1553 mem_src
= SG_HEAP_PAGE
;
1554 p
= sg_malloc(sfp
, SG_SCATTER_SZ
, &ret_sz
, &mem_src
);
1558 /* Want some local declarations, so start new block ... */
1559 { /* lets try and build a scatter gather list */
1560 struct scatterlist
* sclp
;
1563 int sg_tablesize
= sfp
->parentdp
->sg_tablesize
;
1567 /* N.B. ret_sz and mem_src carried into this block ... */
1568 mx_sc_elems
= sg_build_sgat(schp
, sfp
);
1569 if (mx_sc_elems
< 0)
1570 return mx_sc_elems
; /* most likely -ENOMEM */
1571 mem_src_arr
= schp
->buffer
+
1572 (mx_sc_elems
* sizeof(struct scatterlist
));
1574 for (k
= 0, sclp
= schp
->buffer
, rem_sz
= blk_size
;
1575 (k
< sg_tablesize
) && (rem_sz
> 0) && (k
< mx_sc_elems
);
1576 ++k
, rem_sz
-= ret_sz
, ++sclp
) {
1580 num
= (rem_sz
> SG_SCATTER_SZ
) ? SG_SCATTER_SZ
: rem_sz
;
1581 mem_src
= SG_HEAP_PAGE
;
1582 p
= sg_malloc(sfp
, num
, &ret_sz
, &mem_src
);
1587 sclp
->length
= ret_sz
;
1588 mem_src_arr
[k
] = mem_src
;
1591 printk("sg_build_build: k=%d, a=0x%p, len=%d, ms=%d\n",
1592 k
, sclp
->address
, ret_sz
, mem_src
));
1593 } /* end of for loop */
1596 printk("sg_build_indi: k_use_sg=%d, rem_sz=%d\n", k
, rem_sz
));
1597 schp
->bufflen
= blk_size
;
1598 if (rem_sz
> 0) /* must have failed */
1604 static int sg_write_xfer(Sg_request
* srp
)
1606 sg_io_hdr_t
* hp
= &srp
->header
;
1607 Sg_scatter_hold
* schp
= &srp
->data
;
1609 int j
, k
, onum
, usglen
, ksglen
, res
, ok
;
1610 int iovec_count
= (int)hp
->iovec_count
;
1611 int dxfer_dir
= hp
->dxfer_direction
;
1614 int new_interface
= ('\0' == hp
->interface_id
) ? 0 : 1;
1616 if ((SG_DXFER_UNKNOWN
== dxfer_dir
) || (SG_DXFER_TO_DEV
== dxfer_dir
) ||
1617 (SG_DXFER_TO_FROM_DEV
== dxfer_dir
)) {
1618 num_xfer
= (int)(new_interface
? hp
->dxfer_len
: hp
->flags
);
1619 if (schp
->bufflen
< num_xfer
)
1620 num_xfer
= schp
->bufflen
;
1622 if ((num_xfer
<= 0) || (new_interface
&& (SG_FLAG_NO_DXFER
& hp
->flags
)))
1626 printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1627 num_xfer
, iovec_count
, schp
->k_use_sg
));
1630 if ((k
= verify_area(VERIFY_READ
, hp
->dxferp
,
1631 size_sg_iovec
* onum
)))
1637 if (0 == schp
->k_use_sg
) { /* kernel has single buffer */
1638 if (SG_USER_MEM
!= schp
->buffer_mem_src
) { /* else nothing to do */
1640 for (j
= 0, p
= schp
->buffer
; j
< onum
; ++j
) {
1641 res
= sg_u_iovec(hp
, iovec_count
, j
, 1, &usglen
, &up
);
1642 if (res
) return res
;
1643 usglen
= (num_xfer
> usglen
) ? usglen
: num_xfer
;
1644 __copy_from_user(p
, up
, usglen
);
1652 else { /* kernel using scatter gather list */
1653 struct scatterlist
* sclp
= (struct scatterlist
*)schp
->buffer
;
1654 char * mem_src_arr
= sg_get_sgat_msa(schp
);
1655 ksglen
= (int)sclp
->length
;
1658 for (j
= 0, k
= 0; j
< onum
; ++j
) {
1659 res
= sg_u_iovec(hp
, iovec_count
, j
, 1, &usglen
, &up
);
1660 if (res
) return res
;
1662 for (; (k
< schp
->k_use_sg
) && p
;
1663 ++k
, ++sclp
, ksglen
= (int)sclp
->length
, p
= sclp
->address
) {
1664 ok
= (SG_USER_MEM
!= mem_src_arr
[k
]);
1667 if (ksglen
> usglen
) {
1668 if (usglen
>= num_xfer
) {
1669 if (ok
) __copy_from_user(p
, up
, num_xfer
);
1672 if (ok
) __copy_from_user(p
, up
, usglen
);
1678 if (ksglen
>= num_xfer
) {
1679 if (ok
) __copy_from_user(p
, up
, num_xfer
);
1682 if (ok
) __copy_from_user(p
, up
, ksglen
);
1692 static int sg_u_iovec(sg_io_hdr_t
* hp
, int sg_num
, int ind
,
1693 int wr_xf
, int * countp
, unsigned char ** up
)
1695 int num_xfer
= (int)hp
->dxfer_len
;
1701 p
= (unsigned char *)hp
->dxferp
;
1702 if (wr_xf
&& ('\0' == hp
->interface_id
))
1703 count
= (int)hp
->flags
; /* holds "old" input_size */
1708 __copy_from_user(&u_iovec
,
1709 (unsigned char *)hp
->dxferp
+ (ind
* size_sg_iovec
),
1711 p
= (unsigned char *)u_iovec
.iov_base
;
1712 count
= (int)u_iovec
.iov_len
;
1714 if ((k
= verify_area(wr_xf
? VERIFY_READ
: VERIFY_WRITE
, p
, count
)))
1717 if (countp
) *countp
= count
;
1721 static char * sg_get_sgat_msa(Sg_scatter_hold
* schp
)
1723 int elem_sz
= sizeof(struct scatterlist
) + sizeof(char);
1724 int mx_sc_elems
= (schp
->sglist_len
/ elem_sz
) - 1;
1725 return schp
->buffer
+ (sizeof(struct scatterlist
) * mx_sc_elems
);
1728 static void sg_remove_scat(Sg_scatter_hold
* schp
)
1730 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n",
1732 if (schp
->buffer
&& schp
->sglist_len
) {
1734 struct scatterlist
* sclp
= (struct scatterlist
*)schp
->buffer
;
1735 char * mem_src_arr
= sg_get_sgat_msa(schp
);
1737 for (k
= 0; (k
< schp
->k_use_sg
) && sclp
->address
; ++k
, ++sclp
) {
1738 mem_src
= mem_src_arr
[k
];
1740 printk("sg_remove_scat: k=%d, a=0x%p, len=%d, ms=%d\n",
1741 k
, sclp
->address
, sclp
->length
, mem_src
));
1742 sg_free(sclp
->address
, sclp
->length
, mem_src
);
1743 sclp
->address
= NULL
;
1746 sg_free(schp
->buffer
, schp
->sglist_len
, schp
->buffer_mem_src
);
1748 else if (schp
->buffer
)
1749 sg_free(schp
->buffer
, schp
->b_malloc_len
, schp
->buffer_mem_src
);
1750 memset(schp
, 0, sizeof(*schp
));
1753 static int sg_read_xfer(Sg_request
* srp
)
1755 sg_io_hdr_t
* hp
= &srp
->header
;
1756 Sg_scatter_hold
* schp
= &srp
->data
;
1758 int j
, k
, onum
, usglen
, ksglen
, res
, ok
;
1759 int iovec_count
= (int)hp
->iovec_count
;
1760 int dxfer_dir
= hp
->dxfer_direction
;
1763 int new_interface
= ('\0' == hp
->interface_id
) ? 0 : 1;
1765 if ((SG_DXFER_UNKNOWN
== dxfer_dir
) || (SG_DXFER_FROM_DEV
== dxfer_dir
) ||
1766 (SG_DXFER_TO_FROM_DEV
== dxfer_dir
)) {
1767 num_xfer
= hp
->dxfer_len
;
1768 if (schp
->bufflen
< num_xfer
)
1769 num_xfer
= schp
->bufflen
;
1771 if ((num_xfer
<= 0) || (new_interface
&& (SG_FLAG_NO_DXFER
& hp
->flags
)))
1775 printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1776 num_xfer
, iovec_count
, schp
->k_use_sg
));
1779 if ((k
= verify_area(VERIFY_READ
, hp
->dxferp
,
1780 size_sg_iovec
* onum
)))
1786 if (0 == schp
->k_use_sg
) { /* kernel has single buffer */
1787 if (SG_USER_MEM
!= schp
->buffer_mem_src
) { /* else nothing to do */
1789 for (j
= 0, p
= schp
->buffer
; j
< onum
; ++j
) {
1790 res
= sg_u_iovec(hp
, iovec_count
, j
, 0, &usglen
, &up
);
1791 if (res
) return res
;
1792 usglen
= (num_xfer
> usglen
) ? usglen
: num_xfer
;
1793 __copy_to_user(up
, p
, usglen
);
1801 else { /* kernel using scatter gather list */
1802 struct scatterlist
* sclp
= (struct scatterlist
*)schp
->buffer
;
1803 char * mem_src_arr
= sg_get_sgat_msa(schp
);
1804 ksglen
= (int)sclp
->length
;
1807 for (j
= 0, k
= 0; j
< onum
; ++j
) {
1808 res
= sg_u_iovec(hp
, iovec_count
, j
, 0, &usglen
, &up
);
1809 if (res
) return res
;
1811 for (; (k
< schp
->k_use_sg
) && p
;
1812 ++k
, ++sclp
, ksglen
= (int)sclp
->length
, p
= sclp
->address
) {
1813 ok
= (SG_USER_MEM
!= mem_src_arr
[k
]);
1816 if (ksglen
> usglen
) {
1817 if (usglen
>= num_xfer
) {
1818 if (ok
) __copy_to_user(up
, p
, num_xfer
);
1821 if (ok
) __copy_to_user(up
, p
, usglen
);
1827 if (ksglen
>= num_xfer
) {
1828 if (ok
) __copy_to_user(up
, p
, num_xfer
);
1831 if (ok
) __copy_to_user(up
, p
, ksglen
);
1841 static void sg_read_oxfer(Sg_request
* srp
, char * outp
, int num_read_xfer
)
1843 Sg_scatter_hold
* schp
= &srp
->data
;
1845 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1847 if ((! outp
) || (num_read_xfer
<= 0))
1849 if(schp
->k_use_sg
> 0) {
1851 struct scatterlist
* sclp
= (struct scatterlist
*)schp
->buffer
;
1853 for (k
= 0; (k
< schp
->k_use_sg
) && sclp
->address
; ++k
, ++sclp
) {
1854 num
= (int)sclp
->length
;
1855 if (num
> num_read_xfer
) {
1856 __copy_to_user(outp
, sclp
->address
, num_read_xfer
);
1860 __copy_to_user(outp
, sclp
->address
, num
);
1861 num_read_xfer
-= num
;
1862 if (num_read_xfer
<= 0)
1869 __copy_to_user(outp
, schp
->buffer
, num_read_xfer
);
1872 static void sg_build_reserve(Sg_fd
* sfp
, int req_size
)
1874 Sg_scatter_hold
* schp
= &sfp
->reserve
;
1876 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size
));
1878 if (req_size
< PAGE_SIZE
)
1879 req_size
= PAGE_SIZE
;
1880 if (0 == sg_build_indi(schp
, sfp
, req_size
))
1883 sg_remove_scat(schp
);
1884 req_size
>>= 1; /* divide by 2 */
1885 } while (req_size
> (PAGE_SIZE
/ 2));
1888 static void sg_link_reserve(Sg_fd
* sfp
, Sg_request
* srp
, int size
)
1890 Sg_scatter_hold
* req_schp
= &srp
->data
;
1891 Sg_scatter_hold
* rsv_schp
= &sfp
->reserve
;
1894 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size
));
1895 size
= (size
+ 1) & (~1); /* round to even for aha1542 */
1896 if (rsv_schp
->k_use_sg
> 0) {
1899 struct scatterlist
* sclp
= (struct scatterlist
*)rsv_schp
->buffer
;
1901 for (k
= 0; k
< rsv_schp
->k_use_sg
; ++k
, ++sclp
) {
1902 num
= (int)sclp
->length
;
1905 req_schp
->k_use_sg
= 0;
1906 req_schp
->buffer
= sclp
->address
;
1909 sfp
->save_scat_len
= num
;
1910 sclp
->length
= (unsigned)rem
;
1911 req_schp
->k_use_sg
= k
+ 1;
1912 req_schp
->sglist_len
= rsv_schp
->sglist_len
;
1913 req_schp
->buffer
= rsv_schp
->buffer
;
1915 req_schp
->bufflen
= size
;
1916 req_schp
->buffer_mem_src
= rsv_schp
->buffer_mem_src
;
1917 req_schp
->b_malloc_len
= rsv_schp
->b_malloc_len
;
1923 if (k
>= rsv_schp
->k_use_sg
)
1924 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
1927 req_schp
->k_use_sg
= 0;
1928 req_schp
->bufflen
= size
;
1929 req_schp
->buffer
= rsv_schp
->buffer
;
1930 req_schp
->buffer_mem_src
= rsv_schp
->buffer_mem_src
;
1931 req_schp
->b_malloc_len
= rsv_schp
->b_malloc_len
;
1935 static void sg_unlink_reserve(Sg_fd
* sfp
, Sg_request
* srp
)
1937 Sg_scatter_hold
* req_schp
= &srp
->data
;
1938 Sg_scatter_hold
* rsv_schp
= &sfp
->reserve
;
1940 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1941 (int)req_schp
->k_use_sg
));
1942 if ((rsv_schp
->k_use_sg
> 0) && (req_schp
->k_use_sg
> 0)) {
1943 struct scatterlist
* sclp
= (struct scatterlist
*)rsv_schp
->buffer
;
1945 if (sfp
->save_scat_len
> 0)
1946 (sclp
+ (req_schp
->k_use_sg
- 1))->length
=
1947 (unsigned)sfp
->save_scat_len
;
1949 SCSI_LOG_TIMEOUT(1, printk(
1950 "sg_unlink_reserve: BAD save_scat_len\n"));
1952 req_schp
->k_use_sg
= 0;
1953 req_schp
->bufflen
= 0;
1954 req_schp
->buffer
= NULL
;
1955 req_schp
->sglist_len
= 0;
1956 sfp
->save_scat_len
= 0;
1960 static Sg_request
* sg_get_rq_mark(Sg_fd
* sfp
, int pack_id
)
1963 unsigned long iflags
;
1965 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1966 for (resp
= sfp
->headrp
; resp
; resp
= resp
->nextrp
) {
1967 /* look for requests that are ready + not SG_IO owned */
1968 if ((1 == resp
->done
) && (! resp
->sg_io_owned
) &&
1969 ((-1 == pack_id
) || (resp
->header
.pack_id
== pack_id
))) {
1970 resp
->done
= 2; /* guard against other readers */
1974 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1978 static Sg_request
* sg_get_nth_request(Sg_fd
* sfp
, int nth
)
1981 unsigned long iflags
;
1984 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
1985 for (k
= 0, resp
= sfp
->headrp
; resp
&& (k
< nth
);
1986 ++k
, resp
= resp
->nextrp
)
1988 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
1992 /* always adds to end of list */
1993 static Sg_request
* sg_add_request(Sg_fd
* sfp
)
1996 unsigned long iflags
;
1998 Sg_request
* rp
= sfp
->req_arr
;
2000 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2003 memset(rp
, 0, sizeof(Sg_request
));
2009 if (0 == sfp
->cmd_q
)
2010 resp
= NULL
; /* command queuing disallowed */
2012 for (k
= 0; k
< SG_MAX_QUEUE
; ++k
, ++rp
) {
2016 if (k
< SG_MAX_QUEUE
) {
2017 memset(rp
, 0, sizeof(Sg_request
));
2019 while (resp
->nextrp
)
2020 resp
= resp
->nextrp
;
2029 resp
->nextrp
= NULL
;
2030 resp
->header
.duration
= jiffies
;
2031 resp
->my_cmdp
= NULL
;
2032 resp
->data
.kiobp
= NULL
;
2034 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2038 /* Return of 1 for found; 0 for not found */
2039 static int sg_remove_request(Sg_fd
* sfp
, Sg_request
* srp
)
2041 Sg_request
* prev_rp
;
2043 unsigned long iflags
;
2046 if ((! sfp
) || (! srp
) || (! sfp
->headrp
))
2048 write_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2049 prev_rp
= sfp
->headrp
;
2050 if (srp
== prev_rp
) {
2051 sfp
->headrp
= prev_rp
->nextrp
;
2052 prev_rp
->parentfp
= NULL
;
2056 while ((rp
= prev_rp
->nextrp
)) {
2058 prev_rp
->nextrp
= rp
->nextrp
;
2059 rp
->parentfp
= NULL
;
2066 write_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2070 static Sg_fd
* sg_get_nth_sfp(Sg_device
* sdp
, int nth
)
2073 unsigned long iflags
;
2076 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2077 for (k
= 0, resp
= sdp
->headfp
; resp
&& (k
< nth
);
2078 ++k
, resp
= resp
->nextfp
)
2080 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2084 static Sg_fd
* sg_add_sfp(Sg_device
* sdp
, int dev
)
2087 unsigned long iflags
;
2089 sfp
= (Sg_fd
*)sg_low_malloc(sizeof(Sg_fd
), 0, SG_HEAP_KMAL
, 0);
2092 memset(sfp
, 0, sizeof(Sg_fd
));
2093 sfp
->fd_mem_src
= SG_HEAP_KMAL
;
2094 init_waitqueue_head(&sfp
->read_wait
);
2095 sfp
->rq_list_lock
= RW_LOCK_UNLOCKED
;
2097 sfp
->timeout
= SG_DEFAULT_TIMEOUT
;
2098 sfp
->force_packid
= SG_DEF_FORCE_PACK_ID
;
2099 sfp
->low_dma
= (SG_DEF_FORCE_LOW_DMA
== 0) ?
2100 sdp
->device
->host
->unchecked_isa_dma
: 1;
2101 sfp
->cmd_q
= SG_DEF_COMMAND_Q
;
2102 sfp
->keep_orphan
= SG_DEF_KEEP_ORPHAN
;
2103 sfp
->parentdp
= sdp
;
2104 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2107 else { /* add to tail of existing list */
2108 Sg_fd
* pfp
= sdp
->headfp
;
2113 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2114 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p, m_s=%d\n",
2115 sfp
, (int)sfp
->fd_mem_src
));
2116 sg_build_reserve(sfp
, sg_big_buff
);
2117 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2118 sfp
->reserve
.bufflen
, sfp
->reserve
.k_use_sg
));
2122 static int sg_remove_sfp(Sg_device
* sdp
, Sg_fd
* sfp
)
2134 sg_finish_rem_req(srp
);
2143 unsigned long iflags
;
2145 write_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2146 prev_fp
= sdp
->headfp
;
2148 sdp
->headfp
= prev_fp
->nextfp
;
2150 while ((fp
= prev_fp
->nextfp
)) {
2152 prev_fp
->nextfp
= fp
->nextfp
;
2158 write_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2159 if (sfp
->reserve
.bufflen
> 0) {
2160 SCSI_LOG_TIMEOUT(6, printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2161 (int)sfp
->reserve
.bufflen
, (int)sfp
->reserve
.k_use_sg
));
2162 sg_remove_scat(&sfp
->reserve
);
2164 sfp
->parentdp
= NULL
;
2165 SCSI_LOG_TIMEOUT(6, printk("sg_remove_sfp: sfp=0x%p\n", sfp
));
2166 sg_low_free((char *)sfp
, sizeof(Sg_fd
), sfp
->fd_mem_src
);
2167 if (sdp
->detached
&& (NULL
== sdp
->headfp
)) {
2170 maxd
= sg_template
.dev_max
;
2171 for (k
= 0; k
< maxd
; ++k
) {
2172 if (sdp
== sg_dev_arr
[k
])
2176 sg_dev_arr
[k
] = NULL
;
2182 sfp
->closed
= 1; /* flag dirty state on this fd */
2183 SCSI_LOG_TIMEOUT(1, printk(
2184 "sg_remove_sfp: worrisome, %d writes pending\n", dirty
));
2189 static int sg_res_in_use(Sg_fd
* sfp
)
2191 const Sg_request
* srp
;
2192 unsigned long iflags
;
2194 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2195 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
)
2196 if (srp
->res_used
) break;
2197 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2201 static int sg_dio_in_use(Sg_fd
* sfp
)
2203 const Sg_request
* srp
;
2204 unsigned long iflags
;
2206 read_lock_irqsave(&sfp
->rq_list_lock
, iflags
);
2207 for (srp
= sfp
->headrp
; srp
; srp
= srp
->nextrp
)
2208 if ((! srp
->done
) && srp
->data
.kiobp
) break;
2209 read_unlock_irqrestore(&sfp
->rq_list_lock
, iflags
);
2213 /* If retSzp==NULL want exact size or fail */
2214 /* sg_low_malloc() should always be called from a process context allowing
2215 GFP_KERNEL to be used instead of GFP_ATOMIC */
2216 static char * sg_low_malloc(int rqSz
, int lowDma
, int mem_src
, int * retSzp
)
2219 int page_mask
= lowDma
? (GFP_KERNEL
| GFP_DMA
) : GFP_KERNEL
;
2223 if (SG_HEAP_KMAL
== mem_src
) {
2224 page_mask
= lowDma
? (GFP_ATOMIC
| GFP_DMA
) : GFP_ATOMIC
;
2225 /* Seen kmalloc(..,GFP_KERNEL) hang for 40 secs! */
2226 resp
= kmalloc(rqSz
, page_mask
);
2227 if (resp
&& retSzp
) *retSzp
= rqSz
;
2230 if (SG_HEAP_POOL
== mem_src
) {
2231 int num_sect
= rqSz
/ SG_SECTOR_SZ
;
2233 if (0 != (rqSz
& SG_SECTOR_MSK
)) {
2237 rqSz
= num_sect
* SG_SECTOR_SZ
;
2239 while (num_sect
> 0) {
2240 if ((num_sect
<= sg_pool_secs_avail
) &&
2241 (scsi_dma_free_sectors
> (SG_LOW_POOL_THRESHHOLD
+ num_sect
))) {
2242 resp
= scsi_malloc(rqSz
);
2244 if (retSzp
) *retSzp
= rqSz
;
2245 sg_pool_secs_avail
-= num_sect
;
2251 num_sect
/= 2; /* try half as many */
2252 rqSz
= num_sect
* SG_SECTOR_SZ
;
2255 else if (SG_HEAP_PAGE
== mem_src
) {
2259 for (order
= 0, a_size
= PAGE_SIZE
;
2260 a_size
< rqSz
; order
++, a_size
<<= 1)
2262 resp
= (char *)__get_free_pages(page_mask
, order
);
2263 while ((! resp
) && order
&& retSzp
) {
2265 a_size
>>= 1; /* divide by 2, until PAGE_SIZE */
2266 resp
= (char *)__get_free_pages(page_mask
, order
); /* try half */
2269 if (retSzp
) *retSzp
= resSz
;
2272 printk("sg_low_malloc: bad mem_src=%d, rqSz=%df\n", mem_src
, rqSz
);
2276 static char * sg_malloc(const Sg_fd
* sfp
, int size
, int * retSzp
,
2281 if (retSzp
) *retSzp
= size
;
2285 int low_dma
= sfp
->low_dma
;
2286 int l_ms
= -1; /* invalid value */
2291 l_ms
= (size
< PAGE_SIZE
) ? SG_HEAP_POOL
: SG_HEAP_PAGE
;
2292 resp
= sg_low_malloc(size
, low_dma
, l_ms
, 0);
2295 resp
= sg_low_malloc(size
, low_dma
, l_ms
, &size
);
2297 l_ms
= (SG_HEAP_POOL
== l_ms
) ? SG_HEAP_PAGE
: SG_HEAP_POOL
;
2298 resp
= sg_low_malloc(size
, low_dma
, l_ms
, &size
);
2300 l_ms
= SG_HEAP_KMAL
;
2301 resp
= sg_low_malloc(size
, low_dma
, l_ms
, &size
);
2304 if (resp
&& retSzp
) *retSzp
= size
;
2307 l_ms
= SG_HEAP_PAGE
;
2308 resp
= sg_low_malloc(size
, low_dma
, l_ms
, 0);
2311 l_ms
= SG_HEAP_POOL
;
2312 resp
= sg_low_malloc(size
, low_dma
, l_ms
, &size
);
2313 if (resp
&& retSzp
) *retSzp
= size
;
2316 SCSI_LOG_TIMEOUT(1, printk("sg_malloc: bad ms=%d\n", *mem_srcp
));
2319 if (resp
) *mem_srcp
= l_ms
;
2321 SCSI_LOG_TIMEOUT(6, printk("sg_malloc: size=%d, ms=%d, ret=0x%p\n",
2322 size
, *mem_srcp
, resp
));
2326 static void sg_low_free(char * buff
, int size
, int mem_src
)
2332 int num_sect
= size
/ SG_SECTOR_SZ
;
2334 scsi_free(buff
, size
);
2335 sg_pool_secs_avail
+= num_sect
;
2339 kfree(buff
); /* size not used */
2344 for (order
= 0, a_size
= PAGE_SIZE
;
2345 a_size
< size
; order
++, a_size
<<= 1)
2347 free_pages((unsigned long)buff
, order
);
2351 break; /* nothing to do */
2353 printk("sg_low_free: bad mem_src=%d, buff=0x%p, rqSz=%df\n",
2354 mem_src
, buff
, size
);
2359 static void sg_free(char * buff
, int size
, int mem_src
)
2362 printk("sg_free: buff=0x%p, size=%d\n", buff
, size
));
2363 if ((! buff
) || (size
<= 0))
2366 sg_low_free(buff
, size
, mem_src
);
2369 static void sg_clr_scpnt(Scsi_Cmnd
* SCpnt
)
2372 SCpnt
->sglist_len
= 0;
2374 SCpnt
->buffer
= NULL
;
2375 SCpnt
->underflow
= 0;
2376 SCpnt
->request
.rq_dev
= MKDEV(0, 0); /* "sg" _disowns_ command blk */
2379 static int sg_ms_to_jif(unsigned int msecs
)
2381 if ((UINT_MAX
/ 2U) < msecs
)
2382 return INT_MAX
; /* special case, set largest possible */
2384 return ((int)msecs
< (INT_MAX
/ 1000)) ? (((int)msecs
* HZ
) / 1000)
2385 : (((int)msecs
/ 1000) * HZ
);
2388 static unsigned sg_jif_to_ms(int jifs
)
2393 unsigned int j
= (unsigned int)jifs
;
2394 return (j
< (UINT_MAX
/ 1000)) ? ((j
* 1000) / HZ
) : ((j
/ HZ
) * 1000);
2398 static unsigned char allow_ops
[] = {TEST_UNIT_READY
, INQUIRY
,
2399 READ_CAPACITY
, READ_BUFFER
, READ_6
, READ_10
, READ_12
,
2400 MODE_SENSE
, MODE_SENSE_10
};
2402 static int sg_allow_access(unsigned char opcode
, char dev_type
)
2406 if (TYPE_SCANNER
== dev_type
) /* TYPE_ROM maybe burner */
2408 for (k
= 0; k
< sizeof(allow_ops
); ++k
) {
2409 if (opcode
== allow_ops
[k
])
2416 static int sg_last_dev()
2419 unsigned long iflags
;
2421 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2422 for (k
= sg_template
.dev_max
- 1; k
>= 0; --k
)
2423 if (sg_dev_arr
[k
] && sg_dev_arr
[k
]->device
) break;
2424 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2425 return k
+ 1; /* origin 1 */
2428 static Sg_device
* sg_get_dev(int dev
)
2430 Sg_device
* sdp
= NULL
;
2431 unsigned long iflags
;
2433 if (sg_dev_arr
&& (dev
>= 0))
2435 read_lock_irqsave(&sg_dev_arr_lock
, iflags
);
2436 if (dev
< sg_template
.dev_max
)
2437 sdp
= sg_dev_arr
[dev
];
2438 read_unlock_irqrestore(&sg_dev_arr_lock
, iflags
);
2443 #ifdef CONFIG_PROC_FS
2445 static struct proc_dir_entry
* sg_proc_sgp
= NULL
;
2447 static const char * sg_proc_sg_dirname
= "sg";
2448 static const char * sg_proc_leaf_names
[] = {"def_reserved_size", "debug",
2449 "devices", "device_hdr", "device_strs",
2450 "hosts", "host_hdr", "host_strs", "version"};
2452 static int sg_proc_dressz_read(char * buffer
, char ** start
, off_t offset
,
2453 int size
, int * eof
, void * data
);
2454 static int sg_proc_dressz_info(char * buffer
, int * len
, off_t
* begin
,
2455 off_t offset
, int size
);
2456 static int sg_proc_dressz_write(struct file
* filp
, const char * buffer
,
2457 unsigned long count
, void * data
);
2458 static int sg_proc_debug_read(char * buffer
, char ** start
, off_t offset
,
2459 int size
, int * eof
, void * data
);
2460 static int sg_proc_debug_info(char * buffer
, int * len
, off_t
* begin
,
2461 off_t offset
, int size
);
2462 static int sg_proc_dev_read(char * buffer
, char ** start
, off_t offset
,
2463 int size
, int * eof
, void * data
);
2464 static int sg_proc_dev_info(char * buffer
, int * len
, off_t
* begin
,
2465 off_t offset
, int size
);
2466 static int sg_proc_devhdr_read(char * buffer
, char ** start
, off_t offset
,
2467 int size
, int * eof
, void * data
);
2468 static int sg_proc_devhdr_info(char * buffer
, int * len
, off_t
* begin
,
2469 off_t offset
, int size
);
2470 static int sg_proc_devstrs_read(char * buffer
, char ** start
, off_t offset
,
2471 int size
, int * eof
, void * data
);
2472 static int sg_proc_devstrs_info(char * buffer
, int * len
, off_t
* begin
,
2473 off_t offset
, int size
);
2474 static int sg_proc_host_read(char * buffer
, char ** start
, off_t offset
,
2475 int size
, int * eof
, void * data
);
2476 static int sg_proc_host_info(char * buffer
, int * len
, off_t
* begin
,
2477 off_t offset
, int size
);
2478 static int sg_proc_hosthdr_read(char * buffer
, char ** start
, off_t offset
,
2479 int size
, int * eof
, void * data
);
2480 static int sg_proc_hosthdr_info(char * buffer
, int * len
, off_t
* begin
,
2481 off_t offset
, int size
);
2482 static int sg_proc_hoststrs_read(char * buffer
, char ** start
, off_t offset
,
2483 int size
, int * eof
, void * data
);
2484 static int sg_proc_hoststrs_info(char * buffer
, int * len
, off_t
* begin
,
2485 off_t offset
, int size
);
2486 static int sg_proc_version_read(char * buffer
, char ** start
, off_t offset
,
2487 int size
, int * eof
, void * data
);
2488 static int sg_proc_version_info(char * buffer
, int * len
, off_t
* begin
,
2489 off_t offset
, int size
);
2490 static read_proc_t
* sg_proc_leaf_reads
[] = {
2491 sg_proc_dressz_read
, sg_proc_debug_read
,
2492 sg_proc_dev_read
, sg_proc_devhdr_read
, sg_proc_devstrs_read
,
2493 sg_proc_host_read
, sg_proc_hosthdr_read
, sg_proc_hoststrs_read
,
2494 sg_proc_version_read
};
2495 static write_proc_t
* sg_proc_leaf_writes
[] = {
2496 sg_proc_dressz_write
, 0, 0, 0, 0, 0, 0, 0, 0};
2498 #define PRINT_PROC(fmt,args...) \
2500 *len += sprintf(buffer + *len, fmt, ##args); \
2501 if (*begin + *len > offset + size) \
2503 if (*begin + *len < offset) { \
2509 #define SG_PROC_READ_FN(infofp) \
2513 *eof = infofp(buffer, &len, &begin, offset, size); \
2514 if (offset >= (begin + len)) \
2516 *start = buffer + offset - begin; \
2517 return (size < (begin + len - offset)) ? \
2518 size : begin + len - offset; \
2522 static int sg_proc_init()
2525 int leaves
= sizeof(sg_proc_leaf_names
) / sizeof(sg_proc_leaf_names
[0]);
2526 struct proc_dir_entry
* pdep
;
2530 sg_proc_sgp
= create_proc_entry(sg_proc_sg_dirname
,
2531 S_IFDIR
| S_IRUGO
| S_IXUGO
, proc_scsi
);
2534 for (k
= 0; k
< leaves
; ++k
) {
2535 mask
= sg_proc_leaf_writes
[k
] ? S_IRUGO
| S_IWUSR
: S_IRUGO
;
2536 pdep
= create_proc_entry(sg_proc_leaf_names
[k
], mask
, sg_proc_sgp
);
2538 pdep
->read_proc
= sg_proc_leaf_reads
[k
];
2539 if (sg_proc_leaf_writes
[k
])
2540 pdep
->write_proc
= sg_proc_leaf_writes
[k
];
2547 static void sg_proc_cleanup()
2550 int leaves
= sizeof(sg_proc_leaf_names
) / sizeof(sg_proc_leaf_names
[0]);
2552 if ((! proc_scsi
) || (! sg_proc_sgp
))
2554 for (k
= 0; k
< leaves
; ++k
)
2555 remove_proc_entry(sg_proc_leaf_names
[k
], sg_proc_sgp
);
2556 remove_proc_entry(sg_proc_sg_dirname
, proc_scsi
);
2560 static int sg_proc_dressz_read(char * buffer
, char ** start
, off_t offset
,
2561 int size
, int * eof
, void * data
)
2562 { SG_PROC_READ_FN(sg_proc_dressz_info
); }
2564 static int sg_proc_dressz_info(char * buffer
, int * len
, off_t
* begin
,
2565 off_t offset
, int size
)
2567 PRINT_PROC("%d\n", sg_big_buff
);
2571 static int sg_proc_dressz_write(struct file
* filp
, const char * buffer
,
2572 unsigned long count
, void * data
)
2575 unsigned long k
= ULONG_MAX
;
2578 if (! capable(CAP_SYS_ADMIN
))
2580 num
= (count
< 10) ? count
: 10;
2581 copy_from_user(buff
, buffer
, num
);
2583 k
= simple_strtoul(buff
, 0, 10);
2591 static int sg_proc_debug_read(char * buffer
, char ** start
, off_t offset
,
2592 int size
, int * eof
, void * data
)
2593 { SG_PROC_READ_FN(sg_proc_debug_info
); }
2595 static int sg_proc_debug_info(char * buffer
, int * len
, off_t
* begin
,
2596 off_t offset
, int size
)
2599 const sg_io_hdr_t
* hp
;
2602 if (NULL
== sg_dev_arr
) {
2603 PRINT_PROC("sg_dev_arr NULL, driver not initialized\n");
2606 max_dev
= sg_last_dev();
2607 PRINT_PROC("dev_max(currently)=%d max_active_device=%d (origin 1)\n",
2608 sg_template
.dev_max
, max_dev
);
2609 PRINT_PROC(" scsi_dma_free_sectors=%u sg_pool_secs_aval=%d "
2610 "def_reserved_size=%d\n",
2611 scsi_dma_free_sectors
, sg_pool_secs_avail
, sg_big_buff
);
2612 for (j
= 0; j
< max_dev
; ++j
) {
2613 if ((sdp
= sg_get_dev(j
))) {
2616 struct scsi_device
* scsidp
;
2617 int dev
, k
, m
, blen
, usg
;
2619 if (! (scsidp
= sdp
->device
)) {
2620 PRINT_PROC("device %d detached ??\n", j
);
2623 dev
= MINOR(sdp
->i_rdev
);
2625 if (sg_get_nth_sfp(sdp
, 0)) {
2626 PRINT_PROC(" >>> device=sg%d ", dev
);
2627 PRINT_PROC("scsi%d chan=%d id=%d lun=%d em=%d sg_tablesize=%d"
2628 " excl=%d\n", scsidp
->host
->host_no
, scsidp
->channel
,
2629 scsidp
->id
, scsidp
->lun
, scsidp
->host
->hostt
->emulated
,
2630 sdp
->sg_tablesize
, sdp
->exclude
);
2632 for (k
= 0; (fp
= sg_get_nth_sfp(sdp
, k
)); ++k
) {
2633 PRINT_PROC(" FD(%d): timeout=%dms bufflen=%d "
2634 "(res)sgat=%d low_dma=%d\n", k
+ 1,
2635 sg_jif_to_ms(fp
->timeout
), fp
->reserve
.bufflen
,
2636 (int)fp
->reserve
.k_use_sg
, (int)fp
->low_dma
);
2637 PRINT_PROC(" cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2638 (int)fp
->cmd_q
, (int)fp
->force_packid
,
2639 (int)fp
->keep_orphan
, (int)fp
->closed
);
2640 for (m
= 0; (srp
= sg_get_nth_request(fp
, m
)); ++m
) {
2642 /* stop indenting so far ... */
2643 PRINT_PROC(srp
->res_used
? " rb>> " :
2644 ((SG_INFO_DIRECT_IO_MASK
& hp
->info
) ? " dio>> " : " "));
2645 blen
= srp
->my_cmdp
? srp
->my_cmdp
->bufflen
: srp
->data
.bufflen
;
2646 usg
= srp
->my_cmdp
? srp
->my_cmdp
->use_sg
: srp
->data
.k_use_sg
;
2647 PRINT_PROC(srp
->done
? ((1 == srp
->done
) ? "rcv:" : "fin:")
2648 : (srp
->my_cmdp
? "act:" : "prior:"));
2649 PRINT_PROC(" id=%d blen=%d", srp
->header
.pack_id
, blen
);
2651 PRINT_PROC(" dur=%d", hp
->duration
);
2653 PRINT_PROC(" t_o/elap=%d/%d", ((hp
->interface_id
== '\0') ?
2654 sg_jif_to_ms(fp
->timeout
) : hp
->timeout
),
2655 sg_jif_to_ms(hp
->duration
? (jiffies
- hp
->duration
) : 0));
2656 PRINT_PROC("ms sgat=%d op=0x%02x\n", usg
, (int)srp
->data
.cmd_opcode
);
2657 /* reset indenting */
2660 PRINT_PROC(" No requests active\n");
2667 static int sg_proc_dev_read(char * buffer
, char ** start
, off_t offset
,
2668 int size
, int * eof
, void * data
)
2669 { SG_PROC_READ_FN(sg_proc_dev_info
); }
2671 static int sg_proc_dev_info(char * buffer
, int * len
, off_t
* begin
,
2672 off_t offset
, int size
)
2676 struct scsi_device
* scsidp
;
2678 max_dev
= sg_last_dev();
2679 for (j
= 0; j
< max_dev
; ++j
) {
2680 sdp
= sg_get_dev(j
);
2681 if (sdp
&& (scsidp
= sdp
->device
))
2682 PRINT_PROC("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2683 scsidp
->host
->host_no
, scsidp
->channel
, scsidp
->id
,
2684 scsidp
->lun
, (int)scsidp
->type
, (int)scsidp
->access_count
,
2685 (int)scsidp
->queue_depth
, (int)scsidp
->device_busy
);
2687 PRINT_PROC("-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2692 static int sg_proc_devhdr_read(char * buffer
, char ** start
, off_t offset
,
2693 int size
, int * eof
, void * data
)
2694 { SG_PROC_READ_FN(sg_proc_devhdr_info
); }
2696 static int sg_proc_devhdr_info(char * buffer
, int * len
, off_t
* begin
,
2697 off_t offset
, int size
)
2699 PRINT_PROC("host\tchan\tid\tlun\ttype\tbopens\tqdepth\tbusy\n");
2703 static int sg_proc_devstrs_read(char * buffer
, char ** start
, off_t offset
,
2704 int size
, int * eof
, void * data
)
2705 { SG_PROC_READ_FN(sg_proc_devstrs_info
); }
2707 static int sg_proc_devstrs_info(char * buffer
, int * len
, off_t
* begin
,
2708 off_t offset
, int size
)
2712 struct scsi_device
* scsidp
;
2714 max_dev
= sg_last_dev();
2715 for (j
= 0; j
< max_dev
; ++j
) {
2716 sdp
= sg_get_dev(j
);
2717 if (sdp
&& (scsidp
= sdp
->device
))
2718 PRINT_PROC("%8.8s\t%16.16s\t%4.4s\n",
2719 scsidp
->vendor
, scsidp
->model
, scsidp
->rev
);
2721 PRINT_PROC("<no active device>\n");
2726 static int sg_proc_host_read(char * buffer
, char ** start
, off_t offset
,
2727 int size
, int * eof
, void * data
)
2728 { SG_PROC_READ_FN(sg_proc_host_info
); }
2730 static int sg_proc_host_info(char * buffer
, int * len
, off_t
* begin
,
2731 off_t offset
, int size
)
2733 struct Scsi_Host
* shp
;
2736 for (k
= 0, shp
= scsi_hostlist
; shp
; shp
= shp
->next
, ++k
) {
2737 for ( ; k
< shp
->host_no
; ++k
)
2738 PRINT_PROC("-1\t-1\t-1\t-1\t-1\t-1\n");
2739 PRINT_PROC("%u\t%hu\t%hd\t%hu\t%d\t%d\n",
2740 shp
->unique_id
, shp
->host_busy
, shp
->cmd_per_lun
,
2741 shp
->sg_tablesize
, (int)shp
->unchecked_isa_dma
,
2742 (int)shp
->hostt
->emulated
);
2747 static int sg_proc_hosthdr_read(char * buffer
, char ** start
, off_t offset
,
2748 int size
, int * eof
, void * data
)
2749 { SG_PROC_READ_FN(sg_proc_hosthdr_info
); }
2751 static int sg_proc_hosthdr_info(char * buffer
, int * len
, off_t
* begin
,
2752 off_t offset
, int size
)
2754 PRINT_PROC("uid\tbusy\tcpl\tscatg\tisa\temul\n");
2758 static int sg_proc_hoststrs_read(char * buffer
, char ** start
, off_t offset
,
2759 int size
, int * eof
, void * data
)
2760 { SG_PROC_READ_FN(sg_proc_hoststrs_info
); }
2762 static int sg_proc_hoststrs_info(char * buffer
, int * len
, off_t
* begin
,
2763 off_t offset
, int size
)
2765 struct Scsi_Host
* shp
;
2768 for (k
= 0, shp
= scsi_hostlist
; shp
; shp
= shp
->next
, ++k
) {
2769 for ( ; k
< shp
->host_no
; ++k
)
2770 PRINT_PROC("<no active host>\n");
2771 PRINT_PROC("%s\n", shp
->hostt
->info
? shp
->hostt
->info(shp
) :
2772 (shp
->hostt
->name
? shp
->hostt
->name
: "<no name>"));
2777 static int sg_proc_version_read(char * buffer
, char ** start
, off_t offset
,
2778 int size
, int * eof
, void * data
)
2779 { SG_PROC_READ_FN(sg_proc_version_info
); }
2781 static int sg_proc_version_info(char * buffer
, int * len
, off_t
* begin
,
2782 off_t offset
, int size
)
2784 PRINT_PROC("%d\t%s\n", sg_version_num
, sg_version_str
);
2787 #endif /* CONFIG_PROC_FS */