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 - 2004 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 = 30531; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.31"
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/devfs_fs_kernel.h>
48 #include <linux/cdev.h>
49 #include <linux/seq_file.h>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_driver.h>
56 #include <scsi/scsi_ioctl.h>
59 #include "scsi_logging.h"
61 #ifdef CONFIG_SCSI_PROC_FS
62 #include <linux/proc_fs.h>
63 static char *sg_version_date = "20040516";
65 static int sg_proc_init(void);
66 static void sg_proc_cleanup(void);
69 #ifndef LINUX_VERSION_CODE
70 #include <linux/version.h>
71 #endif /* LINUX_VERSION_CODE */
73 #define SG_ALLOW_DIO_DEF 0
74 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
76 #define SG_MAX_DEVS 32768
79 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
80 * Then when using 32 bit integers x * m may overflow during the calculation.
81 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
82 * calculates the same, but prevents the overflow when both m and d
83 * are "small" numbers (like HZ and USER_HZ).
84 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
87 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
89 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
91 int sg_big_buff = SG_DEF_RESERVED_SIZE;
92 /* N.B. This variable is readable and writeable via
93 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
94 of this size (or less if there is not enough memory) will be reserved
95 for use by this file descriptor. [Deprecated usage: this variable is also
96 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
97 the kernel (i.e. it is not a module).] */
98 static int def_reserved_size = -1; /* picks up init parameter */
99 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
101 #define SG_SECTOR_SZ 512
102 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
104 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
106 static int sg_add(struct class_device *);
107 static void sg_remove(struct class_device *);
109 static Scsi_Request *dummy_cmdp; /* only used for sizeof */
111 static rwlock_t sg_dev_arr_lock = RW_LOCK_UNLOCKED; /* Also used to lock
112 file descriptor list for device */
114 static struct class_interface sg_interface = {
119 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
120 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
121 unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
122 unsigned bufflen; /* Size of (aggregate) data buffer */
123 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
124 void *buffer; /* Data buffer or scatter list (k_use_sg>0) */
125 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
126 unsigned char cmd_opcode; /* first byte of command */
129 struct sg_device; /* forward declarations */
132 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
133 Scsi_Request *my_cmdp; /* != 0 when request with lower levels */
134 struct sg_request *nextrp; /* NULL -> tail request (slist) */
135 struct sg_fd *parentfp; /* NULL -> not in use */
136 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
137 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
138 unsigned char sense_b[sizeof (dummy_cmdp->sr_sense_buffer)];
139 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
140 char orphan; /* 1 -> drop on sight, 0 -> normal */
141 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
142 volatile char done; /* 0->before bh, 1->before read, 2->read */
145 typedef struct sg_fd { /* holds the state of a file descriptor */
146 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
147 struct sg_device *parentdp; /* owning device */
148 wait_queue_head_t read_wait; /* queue read until command done */
149 rwlock_t rq_list_lock; /* protect access to list in req_arr */
150 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
151 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
152 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
153 unsigned save_scat_len; /* original length of trunc. scat. element */
154 Sg_request *headrp; /* head of request slist, NULL->empty */
155 struct fasync_struct *async_qp; /* used by asynchronous notification */
156 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
157 char low_dma; /* as in parent but possibly overridden to 1 */
158 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
159 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
160 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
161 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
162 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
163 char mmap_called; /* 0 -> mmap() never called on this fd */
166 typedef struct sg_device { /* holds the state of each scsi generic device */
167 struct scsi_device *device;
168 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
169 int sg_tablesize; /* adapter's max scatter-gather table size */
170 Sg_fd *headfp; /* first open fd belonging to this device */
171 volatile char detached; /* 0->attached, 1->detached pending removal */
172 volatile char exclude; /* opened for exclusive access */
173 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
174 struct gendisk *disk;
175 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
178 static int sg_fasync(int fd, struct file *filp, int mode);
179 static void sg_cmd_done(Scsi_Cmnd * SCpnt); /* tasklet or soft irq callback */
180 static int sg_start_req(Sg_request * srp);
181 static void sg_finish_rem_req(Sg_request * srp);
182 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
183 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
185 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
187 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
188 int blocking, int read_only, Sg_request ** o_srp);
189 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
191 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
192 int wr_xf, int *countp, unsigned char __user **up);
193 static int sg_write_xfer(Sg_request * srp);
194 static int sg_read_xfer(Sg_request * srp);
195 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196 static void sg_remove_scat(Sg_scatter_hold * schp);
197 static void sg_build_reserve(Sg_fd * sfp, int req_size);
198 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
200 static char *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
201 static void sg_page_free(char *buff, int size);
202 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
203 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
205 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
206 static Sg_request *sg_add_request(Sg_fd * sfp);
207 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
208 static int sg_res_in_use(Sg_fd * sfp);
209 static int sg_allow_access(unsigned char opcode, char dev_type);
210 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
211 static Sg_device *sg_get_dev(int dev);
212 static inline unsigned char *sg_scatg2virt(const struct scatterlist *sclp);
213 #ifdef CONFIG_SCSI_PROC_FS
214 static int sg_last_dev(void);
217 static Sg_device **sg_dev_arr = NULL;
218 static int sg_dev_max;
219 static int sg_nr_dev;
221 #define SZ_SG_HEADER sizeof(struct sg_header)
222 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
223 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
224 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
227 sg_open(struct inode *inode, struct file *filp)
229 int dev = iminor(inode);
230 int flags = filp->f_flags;
236 nonseekable_open(inode, filp);
237 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
238 sdp = sg_get_dev(dev);
239 if ((!sdp) || (!sdp->device))
244 /* This driver's module count bumped by fops_get in <linux/fs.h> */
245 /* Prevent the device driver from vanishing while we sleep */
246 retval = scsi_device_get(sdp->device);
250 if (!((flags & O_NONBLOCK) ||
251 scsi_block_when_processing_errors(sdp->device))) {
253 /* we are in error recovery for this device */
257 if (flags & O_EXCL) {
258 if (O_RDONLY == (flags & O_ACCMODE)) {
259 retval = -EPERM; /* Can't lock it with read only access */
262 if (sdp->headfp && (flags & O_NONBLOCK)) {
267 __wait_event_interruptible(sdp->o_excl_wait,
268 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
270 retval = res; /* -ERESTARTSYS because signal hit process */
273 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
274 if (flags & O_NONBLOCK) {
279 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
282 retval = res; /* -ERESTARTSYS because signal hit process */
290 if (!sdp->headfp) { /* no existing opens on this device */
292 sdp->sg_tablesize = sdp->device->host->sg_tablesize;
294 if ((sfp = sg_add_sfp(sdp, dev)))
295 filp->private_data = sfp;
298 sdp->exclude = 0; /* undo if error */
305 scsi_device_put(sdp->device);
309 /* Following function was formerly called 'sg_close' */
311 sg_release(struct inode *inode, struct file *filp)
316 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
318 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
319 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
320 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
321 if (!sdp->detached) {
322 scsi_device_put(sdp->device);
325 wake_up_interruptible(&sdp->o_excl_wait);
331 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
337 int req_pack_id = -1;
338 struct sg_header old_hdr;
342 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
344 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
345 sdp->disk->disk_name, (int) count));
346 if ((k = verify_area(VERIFY_WRITE, buf, count)))
348 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
349 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
351 if (old_hdr.reply_len < 0) {
352 if (count >= SZ_SG_IO_HDR) {
354 (&new_hdr, buf, SZ_SG_IO_HDR))
356 req_pack_id = new_hdr.pack_id;
359 req_pack_id = old_hdr.pack_id;
361 srp = sg_get_rq_mark(sfp, req_pack_id);
362 if (!srp) { /* now wait on packet to arrive */
365 if (filp->f_flags & O_NONBLOCK)
368 res = 0; /* following is a macro that beats race condition */
369 __wait_event_interruptible(sfp->read_wait,
370 (sdp->detached || (srp = sg_get_rq_mark(sfp, req_pack_id))),
376 return res; /* -ERESTARTSYS because signal hit process */
379 if (srp->header.interface_id != '\0')
380 return sg_new_read(sfp, buf, count, srp);
383 memset(&old_hdr, 0, SZ_SG_HEADER);
384 old_hdr.reply_len = (int) hp->timeout;
385 old_hdr.pack_len = old_hdr.reply_len; /* very old, strange behaviour */
386 old_hdr.pack_id = hp->pack_id;
387 old_hdr.twelve_byte =
388 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
389 old_hdr.target_status = hp->masked_status;
390 old_hdr.host_status = hp->host_status;
391 old_hdr.driver_status = hp->driver_status;
392 if ((CHECK_CONDITION & hp->masked_status) ||
393 (DRIVER_SENSE & hp->driver_status))
394 memcpy(old_hdr.sense_buffer, srp->sense_b,
395 sizeof (old_hdr.sense_buffer));
396 switch (hp->host_status) {
397 /* This setup of 'result' is for backward compatibility and is best
398 ignored by the user who should use target, host + driver status */
400 case DID_PASSTHROUGH:
407 old_hdr.result = EBUSY;
414 old_hdr.result = EIO;
417 old_hdr.result = (srp->sense_b[0] == 0 &&
418 hp->masked_status == GOOD) ? 0 : EIO;
421 old_hdr.result = EIO;
425 /* Now copy the result back to the user buffer. */
426 if (count >= SZ_SG_HEADER) {
427 if (__copy_to_user(buf, &old_hdr, SZ_SG_HEADER))
430 if (count > old_hdr.reply_len)
431 count = old_hdr.reply_len;
432 if (count > SZ_SG_HEADER) {
434 sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)))
438 count = (old_hdr.result == 0) ? 0 : -EIO;
439 sg_finish_rem_req(srp);
444 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
446 sg_io_hdr_t *hp = &srp->header;
450 if (count < SZ_SG_IO_HDR) {
455 if ((hp->mx_sb_len > 0) && hp->sbp) {
456 if ((CHECK_CONDITION & hp->masked_status) ||
457 (DRIVER_SENSE & hp->driver_status)) {
458 int sb_len = sizeof (dummy_cmdp->sr_sense_buffer);
459 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
460 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
461 len = (len > sb_len) ? sb_len : len;
462 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
469 if (hp->masked_status || hp->host_status || hp->driver_status)
470 hp->info |= SG_INFO_CHECK;
471 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
475 err = sg_read_xfer(srp);
477 sg_finish_rem_req(srp);
478 return (0 == err) ? count : err;
482 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
484 int mxsize, cmd_size, k;
485 int input_size, blocking;
486 unsigned char opcode;
490 struct sg_header old_hdr;
492 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
494 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
496 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
497 sdp->disk->disk_name, (int) count));
500 if (!((filp->f_flags & O_NONBLOCK) ||
501 scsi_block_when_processing_errors(sdp->device)))
504 if ((k = verify_area(VERIFY_READ, buf, count)))
505 return k; /* protects following copy_from_user()s + get_user()s */
506 if (count < SZ_SG_HEADER)
508 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
510 blocking = !(filp->f_flags & O_NONBLOCK);
511 if (old_hdr.reply_len < 0)
512 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
513 if (count < (SZ_SG_HEADER + 6))
514 return -EIO; /* The minimum scsi command length is 6 bytes. */
516 if (!(srp = sg_add_request(sfp))) {
517 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
521 __get_user(opcode, buf);
522 if (sfp->next_cmd_len > 0) {
523 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
524 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
525 sfp->next_cmd_len = 0;
526 sg_remove_request(sfp, srp);
529 cmd_size = sfp->next_cmd_len;
530 sfp->next_cmd_len = 0; /* reset so only this write() effected */
532 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
533 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
536 SCSI_LOG_TIMEOUT(4, printk(
537 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
538 /* Determine buffer size. */
539 input_size = count - cmd_size;
540 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
541 mxsize -= SZ_SG_HEADER;
542 input_size -= SZ_SG_HEADER;
543 if (input_size < 0) {
544 sg_remove_request(sfp, srp);
545 return -EIO; /* User did not pass enough bytes for this command. */
548 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
549 hp->cmd_len = (unsigned char) cmd_size;
553 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
554 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
556 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
557 hp->dxfer_len = mxsize;
558 hp->dxferp = (char __user *)buf + cmd_size;
560 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
561 hp->flags = input_size; /* structure abuse ... */
562 hp->pack_id = old_hdr.pack_id;
564 if (__copy_from_user(cmnd, buf, cmd_size))
566 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
567 return (k < 0) ? k : count;
571 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
572 int blocking, int read_only, Sg_request ** o_srp)
577 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
580 if (count < SZ_SG_IO_HDR)
582 if ((k = verify_area(VERIFY_READ, buf, count)))
583 return k; /* protects following copy_from_user()s + get_user()s */
585 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
586 if (!(srp = sg_add_request(sfp))) {
587 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
591 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
592 sg_remove_request(sfp, srp);
595 if (hp->interface_id != 'S') {
596 sg_remove_request(sfp, srp);
599 if (hp->flags & SG_FLAG_MMAP_IO) {
600 if (hp->dxfer_len > sfp->reserve.bufflen) {
601 sg_remove_request(sfp, srp);
602 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
604 if (hp->flags & SG_FLAG_DIRECT_IO) {
605 sg_remove_request(sfp, srp);
606 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
608 if (sg_res_in_use(sfp)) {
609 sg_remove_request(sfp, srp);
610 return -EBUSY; /* reserve buffer already being used */
613 timeout = msecs_to_jiffies(srp->header.timeout);
614 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
615 sg_remove_request(sfp, srp);
618 if ((k = verify_area(VERIFY_READ, hp->cmdp, hp->cmd_len))) {
619 sg_remove_request(sfp, srp);
620 return k; /* protects following copy_from_user()s + get_user()s */
622 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
623 sg_remove_request(sfp, srp);
627 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
628 sg_remove_request(sfp, srp);
631 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
640 sg_common_write(Sg_fd * sfp, Sg_request * srp,
641 unsigned char *cmnd, int timeout, int blocking)
645 Sg_device *sdp = sfp->parentdp;
646 sg_io_hdr_t *hp = &srp->header;
649 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
651 hp->masked_status = 0;
655 hp->driver_status = 0;
657 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
658 (int) cmnd[0], (int) hp->cmd_len));
660 if ((k = sg_start_req(srp))) {
661 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
662 sg_finish_rem_req(srp);
663 return k; /* probably out of space --> ENOMEM */
665 if ((k = sg_write_xfer(srp))) {
666 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
667 sg_finish_rem_req(srp);
671 sg_finish_rem_req(srp);
674 SRpnt = scsi_allocate_request(sdp->device, GFP_ATOMIC);
676 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
677 sg_finish_rem_req(srp);
681 srp->my_cmdp = SRpnt;
682 q = SRpnt->sr_device->request_queue;
683 SRpnt->sr_request->rq_disk = sdp->disk;
684 SRpnt->sr_sense_buffer[0] = 0;
685 SRpnt->sr_cmd_len = hp->cmd_len;
686 SRpnt->sr_use_sg = srp->data.k_use_sg;
687 SRpnt->sr_sglist_len = srp->data.sglist_len;
688 SRpnt->sr_bufflen = srp->data.bufflen;
689 SRpnt->sr_underflow = 0;
690 SRpnt->sr_buffer = srp->data.buffer;
691 switch (hp->dxfer_direction) {
692 case SG_DXFER_TO_FROM_DEV:
693 case SG_DXFER_FROM_DEV:
694 SRpnt->sr_data_direction = SCSI_DATA_READ;
696 case SG_DXFER_TO_DEV:
697 SRpnt->sr_data_direction = SCSI_DATA_WRITE;
699 case SG_DXFER_UNKNOWN:
700 SRpnt->sr_data_direction = SCSI_DATA_UNKNOWN;
703 SRpnt->sr_data_direction = SCSI_DATA_NONE;
706 SRpnt->upper_private_data = srp;
707 srp->data.k_use_sg = 0;
708 srp->data.sglist_len = 0;
709 srp->data.bufflen = 0;
710 srp->data.buffer = NULL;
711 hp->duration = jiffies; /* unit jiffies now, millisecs after done */
712 /* Now send everything of to mid-level. The next time we hear about this
713 packet is when sg_cmd_done() is called (i.e. a callback). */
714 scsi_do_req(SRpnt, (void *) cmnd,
715 (void *) SRpnt->sr_buffer, hp->dxfer_len,
716 sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
717 /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
722 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
724 unsigned long iflags;
727 read_lock_irqsave(&sfp->rq_list_lock, iflags);
729 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
734 sg_ioctl(struct inode *inode, struct file *filp,
735 unsigned int cmd_in, unsigned long arg)
737 void __user *p = (void __user *)arg;
739 int result, val, read_only;
743 unsigned long iflags;
745 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
747 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
748 sdp->disk->disk_name, (int) cmd_in));
749 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
754 int blocking = 1; /* ignore O_NONBLOCK flag */
758 if (!scsi_block_when_processing_errors(sdp->device))
760 result = verify_area(VERIFY_WRITE, p, SZ_SG_IO_HDR);
764 sg_new_write(sfp, p, SZ_SG_IO_HDR,
765 blocking, read_only, &srp);
768 srp->sg_io_owned = 1;
770 result = 0; /* following macro to beat race condition */
771 __wait_event_interruptible(sfp->read_wait,
772 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
777 return 0; /* request packet dropped already */
781 return result; /* -ERESTARTSYS because signal hit process */
783 write_lock_irqsave(&sfp->rq_list_lock, iflags);
785 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
786 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
787 return (result < 0) ? result : 0;
790 result = get_user(val, ip);
795 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
796 val = MULDIV (INT_MAX, USER_HZ, HZ);
797 sfp->timeout_user = val;
798 sfp->timeout = MULDIV (val, HZ, USER_HZ);
801 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
802 /* strange ..., for backward compatibility */
803 return sfp->timeout_user;
804 case SG_SET_FORCE_LOW_DMA:
805 result = get_user(val, ip);
810 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
811 val = (int) sfp->reserve.bufflen;
812 sg_remove_scat(&sfp->reserve);
813 sg_build_reserve(sfp, val);
818 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
822 return put_user((int) sfp->low_dma, ip);
825 verify_area(VERIFY_WRITE, p, sizeof (sg_scsi_id_t));
829 sg_scsi_id_t __user *sg_idp = p;
833 __put_user((int) sdp->device->host->host_no,
835 __put_user((int) sdp->device->channel,
837 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
838 __put_user((int) sdp->device->lun, &sg_idp->lun);
839 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
840 __put_user((short) sdp->device->host->cmd_per_lun,
841 &sg_idp->h_cmd_per_lun);
842 __put_user((short) sdp->device->queue_depth,
843 &sg_idp->d_queue_depth);
844 __put_user(0, &sg_idp->unused[0]);
845 __put_user(0, &sg_idp->unused[1]);
848 case SG_SET_FORCE_PACK_ID:
849 result = get_user(val, ip);
852 sfp->force_packid = val ? 1 : 0;
855 result = verify_area(VERIFY_WRITE, ip, sizeof (int));
858 read_lock_irqsave(&sfp->rq_list_lock, iflags);
859 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
860 if ((1 == srp->done) && (!srp->sg_io_owned)) {
861 read_unlock_irqrestore(&sfp->rq_list_lock,
863 __put_user(srp->header.pack_id, ip);
867 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
870 case SG_GET_NUM_WAITING:
871 read_lock_irqsave(&sfp->rq_list_lock, iflags);
872 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
873 if ((1 == srp->done) && (!srp->sg_io_owned))
876 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
877 return put_user(val, ip);
878 case SG_GET_SG_TABLESIZE:
879 return put_user(sdp->sg_tablesize, ip);
880 case SG_SET_RESERVED_SIZE:
881 result = get_user(val, ip);
886 if (val != sfp->reserve.bufflen) {
887 if (sg_res_in_use(sfp) || sfp->mmap_called)
889 sg_remove_scat(&sfp->reserve);
890 sg_build_reserve(sfp, val);
893 case SG_GET_RESERVED_SIZE:
894 val = (int) sfp->reserve.bufflen;
895 return put_user(val, ip);
896 case SG_SET_COMMAND_Q:
897 result = get_user(val, ip);
900 sfp->cmd_q = val ? 1 : 0;
902 case SG_GET_COMMAND_Q:
903 return put_user((int) sfp->cmd_q, ip);
904 case SG_SET_KEEP_ORPHAN:
905 result = get_user(val, ip);
908 sfp->keep_orphan = val;
910 case SG_GET_KEEP_ORPHAN:
911 return put_user((int) sfp->keep_orphan, ip);
912 case SG_NEXT_CMD_LEN:
913 result = get_user(val, ip);
916 sfp->next_cmd_len = (val > 0) ? val : 0;
918 case SG_GET_VERSION_NUM:
919 return put_user(sg_version_num, ip);
920 case SG_GET_ACCESS_COUNT:
921 /* faked - we don't have a real access count anymore */
922 val = (sdp->device ? 1 : 0);
923 return put_user(val, ip);
924 case SG_GET_REQUEST_TABLE:
925 result = verify_area(VERIFY_WRITE, p,
926 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
930 sg_req_info_t rinfo[SG_MAX_QUEUE];
932 read_lock_irqsave(&sfp->rq_list_lock, iflags);
933 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
934 ++val, srp = srp ? srp->nextrp : srp) {
935 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
937 rinfo[val].req_state = srp->done + 1;
939 srp->header.masked_status &
940 srp->header.host_status &
941 srp->header.driver_status;
942 rinfo[val].duration =
943 srp->done ? srp->header.duration :
945 jiffies - srp->header.duration);
946 rinfo[val].orphan = srp->orphan;
947 rinfo[val].sg_io_owned = srp->sg_io_owned;
948 rinfo[val].pack_id = srp->header.pack_id;
949 rinfo[val].usr_ptr = srp->header.usr_ptr;
952 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
953 return (__copy_to_user(p, rinfo,
954 SZ_SG_REQ_INFO * SG_MAX_QUEUE) ? -EFAULT : 0);
956 case SG_EMULATED_HOST:
959 return put_user(sdp->device->host->hostt->emulated, ip);
963 if (filp->f_flags & O_NONBLOCK) {
964 if (test_bit(SHOST_RECOVERY,
965 &sdp->device->host->shost_state))
967 } else if (!scsi_block_when_processing_errors(sdp->device))
969 result = get_user(val, ip);
972 if (SG_SCSI_RESET_NOTHING == val)
975 case SG_SCSI_RESET_DEVICE:
976 val = SCSI_TRY_RESET_DEVICE;
978 case SG_SCSI_RESET_BUS:
979 val = SCSI_TRY_RESET_BUS;
981 case SG_SCSI_RESET_HOST:
982 val = SCSI_TRY_RESET_HOST;
987 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
989 return (scsi_reset_provider(sdp->device, val) ==
991 case SCSI_IOCTL_SEND_COMMAND:
995 unsigned char opcode = WRITE_6;
996 Scsi_Ioctl_Command __user *siocp = p;
998 if (copy_from_user(&opcode, siocp->data, 1))
1000 if (!sg_allow_access(opcode, sdp->device->type))
1003 return scsi_ioctl_send_command(sdp->device, p);
1005 result = get_user(val, ip);
1008 sdp->sgdebug = (char) val;
1010 case SCSI_IOCTL_GET_IDLUN:
1011 case SCSI_IOCTL_GET_BUS_NUMBER:
1012 case SCSI_IOCTL_PROBE_HOST:
1013 case SG_GET_TRANSFORM:
1016 return scsi_ioctl(sdp->device, cmd_in, p);
1019 return -EPERM; /* don't know so take safe approach */
1020 return scsi_ioctl(sdp->device, cmd_in, p);
1025 sg_poll(struct file *filp, poll_table * wait)
1027 unsigned int res = 0;
1032 unsigned long iflags;
1034 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1037 poll_wait(filp, &sfp->read_wait, wait);
1038 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1039 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1040 /* if any read waiting, flag it */
1041 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1042 res = POLLIN | POLLRDNORM;
1045 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1049 else if (!sfp->cmd_q) {
1051 res |= POLLOUT | POLLWRNORM;
1052 } else if (count < SG_MAX_QUEUE)
1053 res |= POLLOUT | POLLWRNORM;
1054 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1055 sdp->disk->disk_name, (int) res));
1060 sg_fasync(int fd, struct file *filp, int mode)
1066 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1068 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1069 sdp->disk->disk_name, mode));
1071 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1072 return (retval < 0) ? retval : 0;
1075 static inline unsigned char *
1076 sg_scatg2virt(const struct scatterlist *sclp)
1078 return (sclp && sclp->page) ?
1079 (unsigned char *) page_address(sclp->page) + sclp->offset : NULL;
1082 /* When startFinish==1 increments page counts for pages other than the
1083 first of scatter gather elements obtained from __get_free_pages().
1084 When startFinish==0 decrements ... */
1086 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1092 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1093 startFinish, rsv_schp->k_use_sg));
1094 /* N.B. correction _not_ applied to base page of each allocation */
1095 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1096 struct scatterlist *sclp = rsv_schp->buffer;
1098 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
1099 for (m = PAGE_SIZE; m < sclp->length; m += PAGE_SIZE) {
1100 page_ptr = sg_scatg2virt(sclp) + m;
1101 page = virt_to_page(page_ptr);
1105 if (page_count(page) > 0)
1110 } else { /* reserve buffer is just a single allocation */
1111 for (m = PAGE_SIZE; m < rsv_schp->bufflen; m += PAGE_SIZE) {
1112 page_ptr = (unsigned char *) rsv_schp->buffer + m;
1113 page = virt_to_page(page_ptr);
1117 if (page_count(page) > 0)
1124 static struct page *
1125 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1128 struct page *page = NOPAGE_SIGBUS;
1129 void *page_ptr = NULL;
1130 unsigned long offset;
1131 Sg_scatter_hold *rsv_schp;
1133 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1135 rsv_schp = &sfp->reserve;
1136 offset = addr - vma->vm_start;
1137 if (offset >= rsv_schp->bufflen)
1139 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1140 offset, rsv_schp->k_use_sg));
1141 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1143 unsigned long sa = vma->vm_start;
1145 struct scatterlist *sclp = rsv_schp->buffer;
1147 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1149 len = vma->vm_end - sa;
1150 len = (len < sclp->length) ? len : sclp->length;
1152 page_ptr = sg_scatg2virt(sclp) + offset;
1153 page = virt_to_page(page_ptr);
1154 get_page(page); /* increment page count */
1160 } else { /* reserve buffer is just a single allocation */
1161 page_ptr = (unsigned char *) rsv_schp->buffer + offset;
1162 page = virt_to_page(page_ptr);
1163 get_page(page); /* increment page count */
1166 *type = VM_FAULT_MINOR;
1170 static struct vm_operations_struct sg_mmap_vm_ops = {
1171 .nopage = sg_vma_nopage,
1175 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1178 unsigned long req_sz = vma->vm_end - vma->vm_start;
1179 Sg_scatter_hold *rsv_schp;
1181 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1183 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1184 (void *) vma->vm_start, (int) req_sz));
1186 return -EINVAL; /* want no offset */
1187 rsv_schp = &sfp->reserve;
1188 if (req_sz > rsv_schp->bufflen)
1189 return -ENOMEM; /* cannot map more than reserved buffer */
1191 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1193 unsigned long sa = vma->vm_start;
1195 struct scatterlist *sclp = rsv_schp->buffer;
1197 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1199 if (0 != sclp->offset)
1200 return -EFAULT; /* non page aligned memory ?? */
1201 len = vma->vm_end - sa;
1202 len = (len < sclp->length) ? len : sclp->length;
1205 } else { /* reserve buffer is just a single allocation */
1206 if ((unsigned long) rsv_schp->buffer & (PAGE_SIZE - 1))
1207 return -EFAULT; /* non page aligned memory ?? */
1209 if (0 == sfp->mmap_called) {
1210 sg_rb_correct4mmap(rsv_schp, 1); /* do only once per fd lifetime */
1211 sfp->mmap_called = 1;
1213 vma->vm_flags |= (VM_RESERVED | VM_IO);
1214 vma->vm_private_data = sfp;
1215 vma->vm_ops = &sg_mmap_vm_ops;
1219 /* This function is a "bottom half" handler that is called by the
1220 * mid level when a command is completed (or has failed). */
1222 sg_cmd_done(Scsi_Cmnd * SCpnt)
1224 Scsi_Request *SRpnt = NULL;
1225 Sg_device *sdp = NULL;
1227 Sg_request *srp = NULL;
1228 unsigned long iflags;
1230 if (SCpnt && (SRpnt = SCpnt->sc_request))
1231 srp = (Sg_request *) SRpnt->upper_private_data;
1233 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1235 scsi_release_request(SRpnt);
1238 sfp = srp->parentfp;
1240 sdp = sfp->parentdp;
1241 if ((NULL == sdp) || sdp->detached) {
1242 printk(KERN_INFO "sg_cmd_done: device detached\n");
1243 scsi_release_request(SRpnt);
1247 /* First transfer ownership of data buffers to sg_device object. */
1248 srp->data.k_use_sg = SRpnt->sr_use_sg;
1249 srp->data.sglist_len = SRpnt->sr_sglist_len;
1250 srp->data.bufflen = SRpnt->sr_bufflen;
1251 srp->data.buffer = SRpnt->sr_buffer;
1252 /* now clear out request structure */
1253 SRpnt->sr_use_sg = 0;
1254 SRpnt->sr_sglist_len = 0;
1255 SRpnt->sr_bufflen = 0;
1256 SRpnt->sr_buffer = NULL;
1257 SRpnt->sr_underflow = 0;
1258 SRpnt->sr_request->rq_disk = NULL; /* "sg" _disowns_ request blk */
1260 srp->my_cmdp = NULL;
1262 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1263 sdp->disk->disk_name, srp->header.pack_id, (int) SRpnt->sr_result));
1264 srp->header.resid = SCpnt->resid;
1265 /* N.B. unit of duration changes here from jiffies to millisecs */
1266 srp->header.duration =
1267 jiffies_to_msecs(jiffies - srp->header.duration);
1268 if (0 != SRpnt->sr_result) {
1269 memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
1270 sizeof (srp->sense_b));
1271 srp->header.status = 0xff & SRpnt->sr_result;
1272 srp->header.masked_status = status_byte(SRpnt->sr_result);
1273 srp->header.msg_status = msg_byte(SRpnt->sr_result);
1274 srp->header.host_status = host_byte(SRpnt->sr_result);
1275 srp->header.driver_status = driver_byte(SRpnt->sr_result);
1276 if ((sdp->sgdebug > 0) &&
1277 ((CHECK_CONDITION == srp->header.masked_status) ||
1278 (COMMAND_TERMINATED == srp->header.masked_status)))
1279 print_req_sense("sg_cmd_done", SRpnt);
1281 /* Following if statement is a patch supplied by Eric Youngdale */
1282 if (driver_byte(SRpnt->sr_result) != 0
1283 && (SRpnt->sr_sense_buffer[0] & 0x7f) == 0x70
1284 && (SRpnt->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION
1285 && sdp->device->removable) {
1286 /* Detected disc change. Set the bit - this may be used if */
1287 /* there are filesystems using this device. */
1288 sdp->device->changed = 1;
1291 /* Rely on write phase to clean out srp status values, so no "else" */
1293 scsi_release_request(SRpnt);
1295 if (sfp->closed) { /* whoops this fd already released, cleanup */
1296 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1297 sg_finish_rem_req(srp);
1299 if (NULL == sfp->headrp) {
1300 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1301 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1302 scsi_device_put(sdp->device);
1306 } else if (srp && srp->orphan) {
1307 if (sfp->keep_orphan)
1308 srp->sg_io_owned = 0;
1310 sg_finish_rem_req(srp);
1315 /* Now wake up any sg_read() that is waiting for this packet. */
1316 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1317 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1319 wake_up_interruptible(&sfp->read_wait);
1320 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1324 static struct file_operations sg_fops = {
1325 .owner = THIS_MODULE,
1332 .release = sg_release,
1333 .fasync = sg_fasync,
1336 static struct class_simple * sg_sysfs_class;
1338 static int sg_sysfs_valid = 0;
1340 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1343 unsigned long iflags;
1344 void *old_sg_dev_arr = NULL;
1347 sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
1349 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1353 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1354 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1356 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1357 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1359 tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1360 if (unlikely(!tmp_da))
1363 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1364 memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
1365 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1366 old_sg_dev_arr = sg_dev_arr;
1367 sg_dev_arr = tmp_da;
1368 sg_dev_max = tmp_dev_max;
1371 for (k = 0; k < sg_dev_max; k++)
1374 if (unlikely(k >= SG_MAX_DEVS))
1377 memset(sdp, 0, sizeof(*sdp));
1378 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1379 sprintf(disk->disk_name, "sg%d", k);
1380 disk->first_minor = k;
1382 sdp->device = scsidp;
1383 init_waitqueue_head(&sdp->o_excl_wait);
1384 sdp->sg_tablesize = scsidp->host ? scsidp->host->sg_tablesize : 0;
1387 sg_dev_arr[k] = sdp;
1388 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1394 kfree(old_sg_dev_arr);
1398 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1403 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1405 "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1406 "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
1407 scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
1413 sg_add(struct class_device *cl_dev)
1415 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1416 struct gendisk *disk;
1417 Sg_device *sdp = NULL;
1418 struct cdev * cdev = NULL;
1421 disk = alloc_disk(1);
1423 printk(KERN_WARNING "alloc_disk failed\n");
1426 disk->major = SCSI_GENERIC_MAJOR;
1429 cdev = cdev_alloc();
1431 printk(KERN_WARNING "cdev_alloc failed\n");
1434 cdev->owner = THIS_MODULE;
1435 cdev->ops = &sg_fops;
1437 error = sg_alloc(disk, scsidp);
1439 printk(KERN_WARNING "sg_alloc failed\n");
1443 sdp = sg_dev_arr[k];
1445 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1446 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1447 "%s/generic", scsidp->devfs_name);
1448 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1450 devfs_remove("%s/generic", scsidp->devfs_name);
1454 if (sg_sysfs_valid) {
1455 struct class_device * sg_class_member;
1457 sg_class_member = class_simple_device_add(sg_sysfs_class,
1458 MKDEV(SCSI_GENERIC_MAJOR, k),
1461 if (IS_ERR(sg_class_member))
1462 printk(KERN_WARNING "sg_add: "
1463 "class_simple_device_add failed\n");
1464 class_set_devdata(sg_class_member, sdp);
1465 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1466 &sg_class_member->kobj, "generic");
1468 printk(KERN_ERR "sg_add: unable to make symlink "
1469 "'generic' back to sg%d\n", k);
1471 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1474 "Attached scsi generic sg%d at scsi%d, channel"
1475 " %d, id %d, lun %d, type %d\n", k,
1476 scsidp->host->host_no, scsidp->channel, scsidp->id,
1477 scsidp->lun, scsidp->type);
1489 sg_remove(struct class_device *cl_dev)
1491 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1492 Sg_device *sdp = NULL;
1493 unsigned long iflags;
1500 if (NULL == sg_dev_arr)
1503 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1504 for (k = 0; k < sg_dev_max; k++) {
1505 sdp = sg_dev_arr[k];
1506 if ((NULL == sdp) || (sdp->device != scsidp))
1507 continue; /* dirty but lowers nesting */
1510 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1512 for (srp = sfp->headrp; srp; srp = tsrp) {
1514 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1515 sg_finish_rem_req(srp);
1518 scsi_device_put(sdp->device);
1519 __sg_remove_sfp(sdp, sfp);
1522 wake_up_interruptible(&sfp->read_wait);
1523 kill_fasync(&sfp->async_qp, SIGPOLL,
1527 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1528 if (NULL == sdp->headfp) {
1529 sg_dev_arr[k] = NULL;
1531 } else { /* nothing active, simple case */
1532 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1533 sg_dev_arr[k] = NULL;
1538 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1541 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1542 class_simple_device_remove(MKDEV(SCSI_GENERIC_MAJOR, k));
1543 cdev_del(sdp->cdev);
1545 devfs_remove("%s/generic", scsidp->devfs_name);
1546 put_disk(sdp->disk);
1548 if (NULL == sdp->headfp)
1549 kfree((char *) sdp);
1553 msleep(10); /* dirty detach so delay device destruction */
1556 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1557 * of sysfs parameters (which module_param doesn't yet support).
1558 * Sysfs parameters defined explicitly below.
1560 module_param_named(def_reserved_size, def_reserved_size, int, 0);
1561 module_param_named(allow_dio, sg_allow_dio, int, 0);
1563 MODULE_AUTHOR("Douglas Gilbert");
1564 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1565 MODULE_LICENSE("GPL");
1566 MODULE_VERSION(SG_VERSION_STR);
1568 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1569 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1576 if (def_reserved_size >= 0)
1577 sg_big_buff = def_reserved_size;
1579 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1583 sg_sysfs_class = class_simple_create(THIS_MODULE, "scsi_generic");
1584 if ( IS_ERR(sg_sysfs_class) ) {
1585 rc = PTR_ERR(sg_sysfs_class);
1589 rc = scsi_register_interface(&sg_interface);
1591 #ifdef CONFIG_SCSI_PROC_FS
1593 #endif /* CONFIG_SCSI_PROC_FS */
1596 class_simple_destroy(sg_sysfs_class);
1598 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1605 #ifdef CONFIG_SCSI_PROC_FS
1607 #endif /* CONFIG_SCSI_PROC_FS */
1608 scsi_unregister_interface(&sg_interface);
1609 class_simple_destroy(sg_sysfs_class);
1611 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1613 if (sg_dev_arr != NULL) {
1614 kfree((char *) sg_dev_arr);
1621 sg_start_req(Sg_request * srp)
1624 Sg_fd *sfp = srp->parentfp;
1625 sg_io_hdr_t *hp = &srp->header;
1626 int dxfer_len = (int) hp->dxfer_len;
1627 int dxfer_dir = hp->dxfer_direction;
1628 Sg_scatter_hold *req_schp = &srp->data;
1629 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1631 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1632 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1634 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1635 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1636 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1637 res = sg_build_direct(srp, sfp, dxfer_len);
1638 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1641 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1642 sg_link_reserve(sfp, srp, dxfer_len);
1644 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1646 sg_remove_scat(req_schp);
1654 sg_finish_rem_req(Sg_request * srp)
1656 Sg_fd *sfp = srp->parentfp;
1657 Sg_scatter_hold *req_schp = &srp->data;
1659 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1661 sg_unlink_reserve(sfp, srp);
1663 sg_remove_scat(req_schp);
1664 sg_remove_request(sfp, srp);
1668 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1671 int elem_sz = sizeof (struct scatterlist);
1672 int sg_bufflen = tablesize * elem_sz;
1673 int mx_sc_elems = tablesize;
1675 schp->buffer = sg_page_malloc(sg_bufflen, sfp->low_dma, &ret_sz);
1678 else if (ret_sz != sg_bufflen) {
1679 sg_bufflen = ret_sz;
1680 mx_sc_elems = sg_bufflen / elem_sz;
1682 schp->sglist_len = sg_bufflen;
1683 memset(schp->buffer, 0, sg_bufflen);
1684 return mx_sc_elems; /* number of scat_gath elements allocated */
1687 #ifdef SG_ALLOW_DIO_CODE
1688 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1689 /* hopefully this generic code will moved to a library */
1691 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1692 - mapping of all pages not successful
1693 - any page is above max_pfn
1694 (i.e., either completely successful or fails)
1697 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1698 unsigned long uaddr, size_t count, int rw,
1699 unsigned long max_pfn)
1702 unsigned int nr_pages;
1703 struct page **pages;
1705 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
1707 /* User attempted Overflow! */
1708 if ((uaddr + count) < uaddr)
1712 if (nr_pages > max_pages)
1719 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1722 /* Try to fault in all of the necessary pages */
1723 down_read(¤t->mm->mmap_sem);
1724 /* rw==READ means read from drive, write into memory area */
1725 res = get_user_pages(
1731 0, /* don't force */
1734 up_read(¤t->mm->mmap_sem);
1736 /* Errors and no page mapped should return here */
1740 for (i=0; i < nr_pages; i++) {
1741 /* FIXME: flush superflous for rw==READ,
1742 * probably wrong function for rw==WRITE
1744 flush_dcache_page(pages[i]);
1745 if (page_to_pfn(pages[i]) > max_pfn)
1747 /* ?? Is locking needed? I don't think so */
1748 /* if (TestSetPageLocked(pages[i]))
1752 /* Populate the scatter/gather list */
1753 sgl[0].page = pages[0];
1754 sgl[0].offset = uaddr & ~PAGE_MASK;
1756 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1757 count -= sgl[0].length;
1758 for (i=1; i < nr_pages ; i++) {
1760 sgl[i].page = pages[i];
1761 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1766 sgl[0].length = count;
1773 /* for (j=0; j < i; j++)
1774 unlock_page(pages[j]); */
1778 for (j=0; j < res; j++)
1779 page_cache_release(pages[j]);
1785 /* And unmap them... */
1787 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1792 for (i=0; i < nr_pages; i++) {
1793 if (dirtied && !PageReserved(sgl[i].page))
1794 SetPageDirty(sgl[i].page);
1795 /* unlock_page(sgl[i].page); */
1796 /* FIXME: cache flush missing for rw==READ
1797 * FIXME: call the correct reference counting function
1799 page_cache_release(sgl[i].page);
1805 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1809 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1811 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1813 #ifdef SG_ALLOW_DIO_CODE
1814 sg_io_hdr_t *hp = &srp->header;
1815 Sg_scatter_hold *schp = &srp->data;
1816 int sg_tablesize = sfp->parentdp->sg_tablesize;
1817 struct scatterlist *sgl;
1818 int mx_sc_elems, res;
1819 struct scsi_device *sdev = sfp->parentdp->device;
1821 if (((unsigned long)hp->dxferp &
1822 queue_dma_alignment(sdev->request_queue)) != 0)
1824 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1825 if (mx_sc_elems <= 0) {
1828 sgl = (struct scatterlist *)schp->buffer;
1829 res = st_map_user_pages(sgl, mx_sc_elems, (unsigned long)hp->dxferp, dxfer_len,
1830 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0, ULONG_MAX);
1833 schp->k_use_sg = res;
1834 schp->dio_in_use = 1;
1835 hp->info |= SG_INFO_DIRECT_IO;
1843 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1846 int blk_size = buff_size;
1847 unsigned char *p = NULL;
1849 if ((blk_size < 0) || (!sfp))
1852 ++blk_size; /* don't know why */
1853 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1854 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1855 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1856 buff_size, blk_size));
1857 if (blk_size <= SG_SCATTER_SZ) {
1858 p = sg_page_malloc(blk_size, sfp->low_dma, &ret_sz);
1861 if (blk_size == ret_sz) { /* got it on the first attempt */
1864 schp->bufflen = blk_size;
1865 schp->b_malloc_len = blk_size;
1869 p = sg_page_malloc(SG_SCATTER_SZ, sfp->low_dma, &ret_sz);
1873 /* Want some local declarations, so start new block ... */
1874 { /* lets try and build a scatter gather list */
1875 struct scatterlist *sclp;
1878 int sg_tablesize = sfp->parentdp->sg_tablesize;
1881 /* N.B. ret_sz carried into this block ... */
1882 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1883 if (mx_sc_elems < 0)
1884 return mx_sc_elems; /* most likely -ENOMEM */
1886 for (k = 0, sclp = schp->buffer, rem_sz = blk_size;
1887 (rem_sz > 0) && (k < mx_sc_elems);
1888 ++k, rem_sz -= ret_sz, ++sclp) {
1894 SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1895 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1899 sclp->page = virt_to_page(p);
1900 sclp->offset = offset_in_page(p);
1901 sclp->length = ret_sz;
1903 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1904 k, sg_scatg2virt(sclp), ret_sz));
1905 } /* end of for loop */
1907 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1908 schp->bufflen = blk_size;
1909 if (rem_sz > 0) /* must have failed */
1916 sg_write_xfer(Sg_request * srp)
1918 sg_io_hdr_t *hp = &srp->header;
1919 Sg_scatter_hold *schp = &srp->data;
1921 int j, k, onum, usglen, ksglen, res;
1922 int iovec_count = (int) hp->iovec_count;
1923 int dxfer_dir = hp->dxfer_direction;
1925 unsigned char __user *up;
1926 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1928 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1929 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1930 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1931 if (schp->bufflen < num_xfer)
1932 num_xfer = schp->bufflen;
1934 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1936 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1939 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1940 num_xfer, iovec_count, schp->k_use_sg));
1943 if ((k = verify_area(VERIFY_READ, hp->dxferp,
1944 SZ_SG_IOVEC * onum)))
1949 if (0 == schp->k_use_sg) { /* kernel has single buffer */
1950 for (j = 0, p = schp->buffer; j < onum; ++j) {
1951 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1954 usglen = (num_xfer > usglen) ? usglen : num_xfer;
1955 if (__copy_from_user(p, up, usglen))
1962 } else { /* kernel using scatter gather list */
1963 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
1965 ksglen = (int) sclp->length;
1966 p = sg_scatg2virt(sclp);
1967 for (j = 0, k = 0; j < onum; ++j) {
1968 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1972 for (; p; ++sclp, ksglen = (int) sclp->length,
1973 p = sg_scatg2virt(sclp)) {
1976 if (ksglen > usglen) {
1977 if (usglen >= num_xfer) {
1978 if (__copy_from_user
1983 if (__copy_from_user(p, up, usglen))
1989 if (ksglen >= num_xfer) {
1990 if (__copy_from_user
1995 if (__copy_from_user(p, up, ksglen))
2001 if (k >= schp->k_use_sg)
2010 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
2011 int wr_xf, int *countp, unsigned char __user **up)
2013 int num_xfer = (int) hp->dxfer_len;
2014 unsigned char __user *p = hp->dxferp;
2018 if (wr_xf && ('\0' == hp->interface_id))
2019 count = (int) hp->flags; /* holds "old" input_size */
2024 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2027 count = (int) iovec.iov_len;
2029 if ((k = verify_area(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count)))
2039 sg_remove_scat(Sg_scatter_hold * schp)
2041 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2042 if (schp->buffer && (schp->sglist_len > 0)) {
2043 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2045 if (schp->dio_in_use) {
2046 #ifdef SG_ALLOW_DIO_CODE
2047 st_unmap_user_pages(sclp, schp->k_use_sg, TRUE);
2052 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2054 SCSI_LOG_TIMEOUT(5, printk(
2055 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2056 k, sg_scatg2virt(sclp), sclp->length));
2057 sg_page_free(sg_scatg2virt(sclp), sclp->length);
2063 sg_page_free(schp->buffer, schp->sglist_len);
2064 } else if (schp->buffer)
2065 sg_page_free(schp->buffer, schp->b_malloc_len);
2066 memset(schp, 0, sizeof (*schp));
2070 sg_read_xfer(Sg_request * srp)
2072 sg_io_hdr_t *hp = &srp->header;
2073 Sg_scatter_hold *schp = &srp->data;
2075 int j, k, onum, usglen, ksglen, res;
2076 int iovec_count = (int) hp->iovec_count;
2077 int dxfer_dir = hp->dxfer_direction;
2079 unsigned char __user *up;
2080 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2082 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2083 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2084 num_xfer = hp->dxfer_len;
2085 if (schp->bufflen < num_xfer)
2086 num_xfer = schp->bufflen;
2088 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2090 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2093 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2094 num_xfer, iovec_count, schp->k_use_sg));
2097 if ((k = verify_area(VERIFY_READ, hp->dxferp,
2098 SZ_SG_IOVEC * onum)))
2103 if (0 == schp->k_use_sg) { /* kernel has single buffer */
2104 for (j = 0, p = schp->buffer; j < onum; ++j) {
2105 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2108 usglen = (num_xfer > usglen) ? usglen : num_xfer;
2109 if (__copy_to_user(up, p, usglen))
2116 } else { /* kernel using scatter gather list */
2117 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2119 ksglen = (int) sclp->length;
2120 p = sg_scatg2virt(sclp);
2121 for (j = 0, k = 0; j < onum; ++j) {
2122 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2126 for (; p; ++sclp, ksglen = (int) sclp->length,
2127 p = sg_scatg2virt(sclp)) {
2130 if (ksglen > usglen) {
2131 if (usglen >= num_xfer) {
2137 if (__copy_to_user(up, p, usglen))
2143 if (ksglen >= num_xfer) {
2149 if (__copy_to_user(up, p, ksglen))
2155 if (k >= schp->k_use_sg)
2164 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2166 Sg_scatter_hold *schp = &srp->data;
2168 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2170 if ((!outp) || (num_read_xfer <= 0))
2172 if (schp->k_use_sg > 0) {
2174 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2176 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2178 num = (int) sclp->length;
2179 if (num > num_read_xfer) {
2181 (outp, sg_scatg2virt(sclp), num_read_xfer))
2186 (outp, sg_scatg2virt(sclp), num))
2188 num_read_xfer -= num;
2189 if (num_read_xfer <= 0)
2195 if (__copy_to_user(outp, schp->buffer, num_read_xfer))
2202 sg_build_reserve(Sg_fd * sfp, int req_size)
2204 Sg_scatter_hold *schp = &sfp->reserve;
2206 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2208 if (req_size < PAGE_SIZE)
2209 req_size = PAGE_SIZE;
2210 if (0 == sg_build_indirect(schp, sfp, req_size))
2213 sg_remove_scat(schp);
2214 req_size >>= 1; /* divide by 2 */
2215 } while (req_size > (PAGE_SIZE / 2));
2219 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2221 Sg_scatter_hold *req_schp = &srp->data;
2222 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2225 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2226 size = (size + 1) & (~1); /* round to even for aha1542 */
2227 if (rsv_schp->k_use_sg > 0) {
2230 struct scatterlist *sclp =
2231 (struct scatterlist *) rsv_schp->buffer;
2233 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
2234 num = (int) sclp->length;
2237 req_schp->k_use_sg = 0;
2238 req_schp->buffer = sg_scatg2virt(sclp);
2240 sfp->save_scat_len = num;
2241 sclp->length = (unsigned) rem;
2242 req_schp->k_use_sg = k + 1;
2243 req_schp->sglist_len =
2244 rsv_schp->sglist_len;
2245 req_schp->buffer = rsv_schp->buffer;
2247 req_schp->bufflen = size;
2248 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2253 if (k >= rsv_schp->k_use_sg)
2254 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2256 req_schp->k_use_sg = 0;
2257 req_schp->bufflen = size;
2258 req_schp->buffer = rsv_schp->buffer;
2259 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2264 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2266 Sg_scatter_hold *req_schp = &srp->data;
2267 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2269 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2270 (int) req_schp->k_use_sg));
2271 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2272 struct scatterlist *sclp =
2273 (struct scatterlist *) rsv_schp->buffer;
2275 if (sfp->save_scat_len > 0)
2276 (sclp + (req_schp->k_use_sg - 1))->length =
2277 (unsigned) sfp->save_scat_len;
2279 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2281 req_schp->k_use_sg = 0;
2282 req_schp->bufflen = 0;
2283 req_schp->buffer = NULL;
2284 req_schp->sglist_len = 0;
2285 sfp->save_scat_len = 0;
2290 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2293 unsigned long iflags;
2295 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2296 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2297 /* look for requests that are ready + not SG_IO owned */
2298 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2299 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2300 resp->done = 2; /* guard against other readers */
2304 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2308 #ifdef CONFIG_SCSI_PROC_FS
2310 sg_get_nth_request(Sg_fd * sfp, int nth)
2313 unsigned long iflags;
2316 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2317 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2318 ++k, resp = resp->nextrp) ;
2319 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2324 /* always adds to end of list */
2326 sg_add_request(Sg_fd * sfp)
2329 unsigned long iflags;
2331 Sg_request *rp = sfp->req_arr;
2333 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2336 memset(rp, 0, sizeof (Sg_request));
2341 if (0 == sfp->cmd_q)
2342 resp = NULL; /* command queuing disallowed */
2344 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2348 if (k < SG_MAX_QUEUE) {
2349 memset(rp, 0, sizeof (Sg_request));
2351 while (resp->nextrp)
2352 resp = resp->nextrp;
2360 resp->nextrp = NULL;
2361 resp->header.duration = jiffies;
2362 resp->my_cmdp = NULL;
2364 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2368 /* Return of 1 for found; 0 for not found */
2370 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2372 Sg_request *prev_rp;
2374 unsigned long iflags;
2377 if ((!sfp) || (!srp) || (!sfp->headrp))
2379 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2380 prev_rp = sfp->headrp;
2381 if (srp == prev_rp) {
2382 sfp->headrp = prev_rp->nextrp;
2383 prev_rp->parentfp = NULL;
2386 while ((rp = prev_rp->nextrp)) {
2388 prev_rp->nextrp = rp->nextrp;
2389 rp->parentfp = NULL;
2396 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2400 #ifdef CONFIG_SCSI_PROC_FS
2402 sg_get_nth_sfp(Sg_device * sdp, int nth)
2405 unsigned long iflags;
2408 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2409 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2410 ++k, resp = resp->nextfp) ;
2411 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2417 sg_add_sfp(Sg_device * sdp, int dev)
2420 unsigned long iflags;
2422 sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
2425 memset(sfp, 0, sizeof (Sg_fd));
2426 init_waitqueue_head(&sfp->read_wait);
2427 sfp->rq_list_lock = RW_LOCK_UNLOCKED;
2429 sfp->timeout = SG_DEFAULT_TIMEOUT;
2430 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2431 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2432 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2433 sdp->device->host->unchecked_isa_dma : 1;
2434 sfp->cmd_q = SG_DEF_COMMAND_Q;
2435 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2436 sfp->parentdp = sdp;
2437 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2440 else { /* add to tail of existing list */
2441 Sg_fd *pfp = sdp->headfp;
2446 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2447 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2448 sg_build_reserve(sfp, sg_big_buff);
2449 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2450 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2455 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2460 prev_fp = sdp->headfp;
2462 sdp->headfp = prev_fp->nextfp;
2464 while ((fp = prev_fp->nextfp)) {
2466 prev_fp->nextfp = fp->nextfp;
2472 if (sfp->reserve.bufflen > 0) {
2474 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2475 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2476 if (sfp->mmap_called)
2477 sg_rb_correct4mmap(&sfp->reserve, 0); /* undo correction */
2478 sg_remove_scat(&sfp->reserve);
2480 sfp->parentdp = NULL;
2481 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2482 sg_page_free((char *) sfp, sizeof (Sg_fd));
2485 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2487 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2494 for (srp = sfp->headrp; srp; srp = tsrp) {
2496 if (sg_srp_done(srp, sfp))
2497 sg_finish_rem_req(srp);
2502 unsigned long iflags;
2504 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2505 __sg_remove_sfp(sdp, sfp);
2506 if (sdp->detached && (NULL == sdp->headfp)) {
2510 for (k = 0; k < maxd; ++k) {
2511 if (sdp == sg_dev_arr[k])
2515 sg_dev_arr[k] = NULL;
2516 kfree((char *) sdp);
2519 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2521 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2522 /* only bump the access_count if we actually succeeded in
2523 * throwing another counter on the host module */
2524 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2525 sfp->closed = 1; /* flag dirty state on this fd */
2526 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2533 sg_res_in_use(Sg_fd * sfp)
2535 const Sg_request *srp;
2536 unsigned long iflags;
2538 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2539 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2542 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2546 /* If retSzp==NULL want exact size or fail */
2548 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2559 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2561 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2563 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2564 order++, a_size <<= 1) ;
2565 resp = (char *) __get_free_pages(page_mask, order);
2566 while ((!resp) && order && retSzp) {
2568 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2569 resp = (char *) __get_free_pages(page_mask, order); /* try half */
2573 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2574 memset(resp, 0, resSz);
2582 sg_page_free(char *buff, int size)
2588 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2589 order++, a_size <<= 1) ;
2590 free_pages((unsigned long) buff, order);
2593 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2594 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2595 MODE_SENSE, MODE_SENSE_10, LOG_SENSE
2599 sg_allow_access(unsigned char opcode, char dev_type)
2603 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2605 for (k = 0; k < sizeof (allow_ops); ++k) {
2606 if (opcode == allow_ops[k])
2612 #ifdef CONFIG_SCSI_PROC_FS
2617 unsigned long iflags;
2619 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2620 for (k = sg_dev_max - 1; k >= 0; --k)
2621 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2623 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2624 return k + 1; /* origin 1 */
2631 Sg_device *sdp = NULL;
2632 unsigned long iflags;
2634 if (sg_dev_arr && (dev >= 0)) {
2635 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2636 if (dev < sg_dev_max)
2637 sdp = sg_dev_arr[dev];
2638 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2643 #ifdef CONFIG_SCSI_PROC_FS
2645 static struct proc_dir_entry *sg_proc_sgp = NULL;
2647 static char sg_proc_sg_dirname[] = "scsi/sg";
2649 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2651 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2652 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2653 size_t count, loff_t *off);
2654 static struct file_operations adio_fops = {
2655 /* .owner, .read and .llseek added in sg_proc_init() */
2656 .open = sg_proc_single_open_adio,
2657 .write = sg_proc_write_adio,
2658 .release = single_release,
2661 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2662 static ssize_t sg_proc_write_dressz(struct file *filp,
2663 const char __user *buffer, size_t count, loff_t *off);
2664 static struct file_operations dressz_fops = {
2665 .open = sg_proc_single_open_dressz,
2666 .write = sg_proc_write_dressz,
2667 .release = single_release,
2670 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2671 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2672 static struct file_operations version_fops = {
2673 .open = sg_proc_single_open_version,
2674 .release = single_release,
2677 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2678 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2679 static struct file_operations devhdr_fops = {
2680 .open = sg_proc_single_open_devhdr,
2681 .release = single_release,
2684 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2685 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2686 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2687 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2688 static void dev_seq_stop(struct seq_file *s, void *v);
2689 static struct file_operations dev_fops = {
2690 .open = sg_proc_open_dev,
2691 .release = seq_release,
2693 static struct seq_operations dev_seq_ops = {
2694 .start = dev_seq_start,
2695 .next = dev_seq_next,
2696 .stop = dev_seq_stop,
2697 .show = sg_proc_seq_show_dev,
2700 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2701 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2702 static struct file_operations devstrs_fops = {
2703 .open = sg_proc_open_devstrs,
2704 .release = seq_release,
2706 static struct seq_operations devstrs_seq_ops = {
2707 .start = dev_seq_start,
2708 .next = dev_seq_next,
2709 .stop = dev_seq_stop,
2710 .show = sg_proc_seq_show_devstrs,
2713 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2714 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2715 static struct file_operations debug_fops = {
2716 .open = sg_proc_open_debug,
2717 .release = seq_release,
2719 static struct seq_operations debug_seq_ops = {
2720 .start = dev_seq_start,
2721 .next = dev_seq_next,
2722 .stop = dev_seq_stop,
2723 .show = sg_proc_seq_show_debug,
2727 struct sg_proc_leaf {
2729 struct file_operations * fops;
2732 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2733 {"allow_dio", &adio_fops},
2734 {"debug", &debug_fops},
2735 {"def_reserved_size", &dressz_fops},
2736 {"device_hdr", &devhdr_fops},
2737 {"devices", &dev_fops},
2738 {"device_strs", &devstrs_fops},
2739 {"version", &version_fops}
2747 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2748 struct proc_dir_entry *pdep;
2749 struct sg_proc_leaf * leaf;
2751 sg_proc_sgp = create_proc_entry(sg_proc_sg_dirname,
2752 S_IFDIR | S_IRUGO | S_IXUGO, NULL);
2755 for (k = 0; k < num_leaves; ++k) {
2756 leaf = &sg_proc_leaf_arr[k];
2757 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2758 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2760 leaf->fops->owner = THIS_MODULE,
2761 leaf->fops->read = seq_read,
2762 leaf->fops->llseek = seq_lseek,
2763 pdep->proc_fops = leaf->fops;
2770 sg_proc_cleanup(void)
2774 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2778 for (k = 0; k < num_leaves; ++k)
2779 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2780 remove_proc_entry(sg_proc_sg_dirname, NULL);
2784 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2786 seq_printf(s, "%d\n", *((int *)s->private));
2790 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2792 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2796 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2797 size_t count, loff_t *off)
2802 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2804 num = (count < 10) ? count : 10;
2805 if (copy_from_user(buff, buffer, num))
2808 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2812 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2814 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2818 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2819 size_t count, loff_t *off)
2822 unsigned long k = ULONG_MAX;
2825 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2827 num = (count < 10) ? count : 10;
2828 if (copy_from_user(buff, buffer, num))
2831 k = simple_strtoul(buff, NULL, 10);
2832 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2839 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2841 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2846 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2848 return single_open(file, sg_proc_seq_show_version, NULL);
2851 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2853 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2858 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2860 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2863 struct sg_proc_deviter {
2868 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2870 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2874 if (NULL == sg_dev_arr)
2877 it->max = sg_last_dev();
2878 if (it->index >= it->max)
2886 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2888 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2891 return (it->index < it->max) ? it : NULL;
2894 static void dev_seq_stop(struct seq_file *s, void *v)
2899 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2901 return seq_open(file, &dev_seq_ops);
2904 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2906 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2908 struct scsi_device *scsidp;
2910 sdp = it ? sg_get_dev(it->index) : NULL;
2911 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2912 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2913 scsidp->host->host_no, scsidp->channel,
2914 scsidp->id, scsidp->lun, (int) scsidp->type,
2916 (int) scsidp->queue_depth,
2917 (int) scsidp->device_busy,
2918 (int) scsi_device_online(scsidp));
2920 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2924 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2926 return seq_open(file, &devstrs_seq_ops);
2929 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2931 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2933 struct scsi_device *scsidp;
2935 sdp = it ? sg_get_dev(it->index) : NULL;
2936 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2937 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2938 scsidp->vendor, scsidp->model, scsidp->rev);
2940 seq_printf(s, "<no active device>\n");
2944 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2946 int k, m, new_interface, blen, usg;
2949 const sg_io_hdr_t *hp;
2952 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2953 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2954 "(res)sgat=%d low_dma=%d\n", k + 1,
2955 jiffies_to_msecs(fp->timeout),
2956 fp->reserve.bufflen,
2957 (int) fp->reserve.k_use_sg,
2959 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2960 (int) fp->cmd_q, (int) fp->force_packid,
2961 (int) fp->keep_orphan, (int) fp->closed);
2962 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2964 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2965 if (srp->res_used) {
2966 if (new_interface &&
2967 (SG_FLAG_MMAP_IO & hp->flags))
2972 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2978 blen = srp->my_cmdp ?
2979 srp->my_cmdp->sr_bufflen : srp->data.bufflen;
2980 usg = srp->my_cmdp ?
2981 srp->my_cmdp->sr_use_sg : srp->data.k_use_sg;
2982 seq_printf(s, srp->done ?
2983 ((1 == srp->done) ? "rcv:" : "fin:")
2984 : (srp->my_cmdp ? "act:" : "prior:"));
2985 seq_printf(s, " id=%d blen=%d",
2986 srp->header.pack_id, blen);
2988 seq_printf(s, " dur=%d", hp->duration);
2990 seq_printf(s, " t_o/elap=%d/%d",
2991 new_interface ? hp->timeout : jiffies_to_msecs(fp->timeout),
2992 jiffies_to_msecs(hp->duration ? (jiffies - hp->duration) : 0));
2993 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2994 (int) srp->data.cmd_opcode);
2997 seq_printf(s, " No requests active\n");
3001 static int sg_proc_open_debug(struct inode *inode, struct file *file)
3003 return seq_open(file, &debug_seq_ops);
3006 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
3008 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3011 if (it && (0 == it->index)) {
3012 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
3013 "(origin 1)\n", sg_dev_max, (int)it->max);
3014 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
3016 sdp = it ? sg_get_dev(it->index) : NULL;
3018 struct scsi_device *scsidp = sdp->device;
3020 if (NULL == scsidp) {
3021 seq_printf(s, "device %d detached ??\n",
3026 if (sg_get_nth_sfp(sdp, 0)) {
3027 seq_printf(s, " >>> device=%s ",
3028 sdp->disk->disk_name);
3030 seq_printf(s, "detached pending close ");
3033 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
3034 scsidp->host->host_no,
3035 scsidp->channel, scsidp->id,
3037 scsidp->host->hostt->emulated);
3038 seq_printf(s, " sg_tablesize=%d excl=%d\n",
3039 sdp->sg_tablesize, sdp->exclude);
3041 sg_proc_debug_helper(s, sdp);
3046 #endif /* CONFIG_SCSI_PROC_FS */
3048 module_init(init_sg);
3049 module_exit(exit_sg);
3050 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);