Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6] / drivers / scsi / sg.c
1 /*
2  *  History:
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
6  *
7  * Original driver (sg.c):
8  *        Copyright (C) 1992 Lawrence Foard
9  * Version 2 and 3 extensions to driver:
10  *        Copyright (C) 1998 - 2005 Douglas Gilbert
11  *
12  *  Modified  19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Devfs support
13  *
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)
17  * any later version.
18  *
19  */
20
21 static int sg_version_num = 30533;      /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.33"
23
24 /*
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).
29  *
30  */
31 #include <linux/config.h>
32 #include <linux/module.h>
33
34 #include <linux/fs.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/string.h>
38 #include <linux/mm.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>
52 #include <linux/scatterlist.h>
53
54 #include "scsi.h"
55 #include <scsi/scsi_dbg.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_driver.h>
58 #include <scsi/scsi_ioctl.h>
59 #include <scsi/sg.h>
60
61 #include "scsi_logging.h"
62
63 #ifdef CONFIG_SCSI_PROC_FS
64 #include <linux/proc_fs.h>
65 static char *sg_version_date = "20050908";
66
67 static int sg_proc_init(void);
68 static void sg_proc_cleanup(void);
69 #endif
70
71 #ifndef LINUX_VERSION_CODE
72 #include <linux/version.h>
73 #endif                          /* LINUX_VERSION_CODE */
74
75 #define SG_ALLOW_DIO_DEF 0
76 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
77
78 #define SG_MAX_DEVS 32768
79
80 /*
81  * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
82  * Then when using 32 bit integers x * m may overflow during the calculation.
83  * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
84  * calculates the same, but prevents the overflow when both m and d
85  * are "small" numbers (like HZ and USER_HZ).
86  * Of course an overflow is inavoidable if the result of muldiv doesn't fit
87  * in 32 bits.
88  */
89 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
90
91 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
92
93 int sg_big_buff = SG_DEF_RESERVED_SIZE;
94 /* N.B. This variable is readable and writeable via
95    /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
96    of this size (or less if there is not enough memory) will be reserved
97    for use by this file descriptor. [Deprecated usage: this variable is also
98    readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
99    the kernel (i.e. it is not a module).] */
100 static int def_reserved_size = -1;      /* picks up init parameter */
101 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
102
103 #define SG_SECTOR_SZ 512
104 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
105
106 #define SG_DEV_ARR_LUMP 32      /* amount to over allocate sg_dev_arr by */
107
108 static int sg_add(struct class_device *, struct class_interface *);
109 static void sg_remove(struct class_device *, struct class_interface *);
110
111 static Scsi_Request *dummy_cmdp;        /* only used for sizeof */
112
113 static DEFINE_RWLOCK(sg_dev_arr_lock);  /* Also used to lock
114                                                            file descriptor list for device */
115
116 static struct class_interface sg_interface = {
117         .add            = sg_add,
118         .remove         = sg_remove,
119 };
120
121 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
122         unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
123         unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
124         unsigned bufflen;       /* Size of (aggregate) data buffer */
125         unsigned b_malloc_len;  /* actual len malloc'ed in buffer */
126         void *buffer;           /* Data buffer or scatter list (k_use_sg>0) */
127         char dio_in_use;        /* 0->indirect IO (or mmap), 1->dio */
128         unsigned char cmd_opcode; /* first byte of command */
129 } Sg_scatter_hold;
130
131 struct sg_device;               /* forward declarations */
132 struct sg_fd;
133
134 typedef struct sg_request {     /* SG_MAX_QUEUE requests outstanding per file */
135         Scsi_Request *my_cmdp;  /* != 0  when request with lower levels */
136         struct sg_request *nextrp;      /* NULL -> tail request (slist) */
137         struct sg_fd *parentfp; /* NULL -> not in use */
138         Sg_scatter_hold data;   /* hold buffer, perhaps scatter list */
139         sg_io_hdr_t header;     /* scsi command+info, see <scsi/sg.h> */
140         unsigned char sense_b[sizeof (dummy_cmdp->sr_sense_buffer)];
141         char res_used;          /* 1 -> using reserve buffer, 0 -> not ... */
142         char orphan;            /* 1 -> drop on sight, 0 -> normal */
143         char sg_io_owned;       /* 1 -> packet belongs to SG_IO */
144         volatile char done;     /* 0->before bh, 1->before read, 2->read */
145 } Sg_request;
146
147 typedef struct sg_fd {          /* holds the state of a file descriptor */
148         struct sg_fd *nextfp;   /* NULL when last opened fd on this device */
149         struct sg_device *parentdp;     /* owning device */
150         wait_queue_head_t read_wait;    /* queue read until command done */
151         rwlock_t rq_list_lock;  /* protect access to list in req_arr */
152         int timeout;            /* defaults to SG_DEFAULT_TIMEOUT      */
153         int timeout_user;       /* defaults to SG_DEFAULT_TIMEOUT_USER */
154         Sg_scatter_hold reserve;        /* buffer held for this file descriptor */
155         unsigned save_scat_len; /* original length of trunc. scat. element */
156         Sg_request *headrp;     /* head of request slist, NULL->empty */
157         struct fasync_struct *async_qp; /* used by asynchronous notification */
158         Sg_request req_arr[SG_MAX_QUEUE];       /* used as singly-linked list */
159         char low_dma;           /* as in parent but possibly overridden to 1 */
160         char force_packid;      /* 1 -> pack_id input to read(), 0 -> ignored */
161         volatile char closed;   /* 1 -> fd closed but request(s) outstanding */
162         char cmd_q;             /* 1 -> allow command queuing, 0 -> don't */
163         char next_cmd_len;      /* 0 -> automatic (def), >0 -> use on next write() */
164         char keep_orphan;       /* 0 -> drop orphan (def), 1 -> keep for read() */
165         char mmap_called;       /* 0 -> mmap() never called on this fd */
166 } Sg_fd;
167
168 typedef struct sg_device { /* holds the state of each scsi generic device */
169         struct scsi_device *device;
170         wait_queue_head_t o_excl_wait;  /* queue open() when O_EXCL in use */
171         int sg_tablesize;       /* adapter's max scatter-gather table size */
172         Sg_fd *headfp;          /* first open fd belonging to this device */
173         volatile char detached; /* 0->attached, 1->detached pending removal */
174         volatile char exclude;  /* opened for exclusive access */
175         char sgdebug;           /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
176         struct gendisk *disk;
177         struct cdev * cdev;     /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
178 } Sg_device;
179
180 static int sg_fasync(int fd, struct file *filp, int mode);
181 static void sg_cmd_done(Scsi_Cmnd * SCpnt);     /* tasklet or soft irq callback */
182 static int sg_start_req(Sg_request * srp);
183 static void sg_finish_rem_req(Sg_request * srp);
184 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
185 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
186                          int tablesize);
187 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
188                            Sg_request * srp);
189 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
190                             int blocking, int read_only, Sg_request ** o_srp);
191 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
192                            unsigned char *cmnd, int timeout, int blocking);
193 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
194                       int wr_xf, int *countp, unsigned char __user **up);
195 static int sg_write_xfer(Sg_request * srp);
196 static int sg_read_xfer(Sg_request * srp);
197 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
198 static void sg_remove_scat(Sg_scatter_hold * schp);
199 static void sg_build_reserve(Sg_fd * sfp, int req_size);
200 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
201 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
202 static char *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
203 static void sg_page_free(char *buff, int size);
204 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
205 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
206 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
207 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
208 static Sg_request *sg_add_request(Sg_fd * sfp);
209 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
210 static int sg_res_in_use(Sg_fd * sfp);
211 static int sg_allow_access(unsigned char opcode, char dev_type);
212 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
213 static Sg_device *sg_get_dev(int dev);
214 static inline unsigned char *sg_scatg2virt(const struct scatterlist *sclp);
215 #ifdef CONFIG_SCSI_PROC_FS
216 static int sg_last_dev(void);
217 #endif
218
219 static Sg_device **sg_dev_arr = NULL;
220 static int sg_dev_max;
221 static int sg_nr_dev;
222
223 #define SZ_SG_HEADER sizeof(struct sg_header)
224 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
225 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
226 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
227
228 static int
229 sg_open(struct inode *inode, struct file *filp)
230 {
231         int dev = iminor(inode);
232         int flags = filp->f_flags;
233         Sg_device *sdp;
234         Sg_fd *sfp;
235         int res;
236         int retval;
237
238         nonseekable_open(inode, filp);
239         SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
240         sdp = sg_get_dev(dev);
241         if ((!sdp) || (!sdp->device))
242                 return -ENXIO;
243         if (sdp->detached)
244                 return -ENODEV;
245
246         /* This driver's module count bumped by fops_get in <linux/fs.h> */
247         /* Prevent the device driver from vanishing while we sleep */
248         retval = scsi_device_get(sdp->device);
249         if (retval)
250                 return retval;
251
252         if (!((flags & O_NONBLOCK) ||
253               scsi_block_when_processing_errors(sdp->device))) {
254                 retval = -ENXIO;
255                 /* we are in error recovery for this device */
256                 goto error_out;
257         }
258
259         if (flags & O_EXCL) {
260                 if (O_RDONLY == (flags & O_ACCMODE)) {
261                         retval = -EPERM; /* Can't lock it with read only access */
262                         goto error_out;
263                 }
264                 if (sdp->headfp && (flags & O_NONBLOCK)) {
265                         retval = -EBUSY;
266                         goto error_out;
267                 }
268                 res = 0;
269                 __wait_event_interruptible(sdp->o_excl_wait,
270                         ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
271                 if (res) {
272                         retval = res;   /* -ERESTARTSYS because signal hit process */
273                         goto error_out;
274                 }
275         } else if (sdp->exclude) {      /* some other fd has an exclusive lock on dev */
276                 if (flags & O_NONBLOCK) {
277                         retval = -EBUSY;
278                         goto error_out;
279                 }
280                 res = 0;
281                 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
282                                            res);
283                 if (res) {
284                         retval = res;   /* -ERESTARTSYS because signal hit process */
285                         goto error_out;
286                 }
287         }
288         if (sdp->detached) {
289                 retval = -ENODEV;
290                 goto error_out;
291         }
292         if (!sdp->headfp) {     /* no existing opens on this device */
293                 sdp->sgdebug = 0;
294                 sdp->sg_tablesize = sdp->device->host->sg_tablesize;
295         }
296         if ((sfp = sg_add_sfp(sdp, dev)))
297                 filp->private_data = sfp;
298         else {
299                 if (flags & O_EXCL)
300                         sdp->exclude = 0;       /* undo if error */
301                 retval = -ENOMEM;
302                 goto error_out;
303         }
304         return 0;
305
306       error_out:
307         scsi_device_put(sdp->device);
308         return retval;
309 }
310
311 /* Following function was formerly called 'sg_close' */
312 static int
313 sg_release(struct inode *inode, struct file *filp)
314 {
315         Sg_device *sdp;
316         Sg_fd *sfp;
317
318         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
319                 return -ENXIO;
320         SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
321         sg_fasync(-1, filp, 0); /* remove filp from async notification list */
322         if (0 == sg_remove_sfp(sdp, sfp)) {     /* Returns 1 when sdp gone */
323                 if (!sdp->detached) {
324                         scsi_device_put(sdp->device);
325                 }
326                 sdp->exclude = 0;
327                 wake_up_interruptible(&sdp->o_excl_wait);
328         }
329         return 0;
330 }
331
332 static ssize_t
333 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
334 {
335         Sg_device *sdp;
336         Sg_fd *sfp;
337         Sg_request *srp;
338         int req_pack_id = -1;
339         sg_io_hdr_t *hp;
340         struct sg_header *old_hdr = NULL;
341         int retval = 0;
342
343         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
344                 return -ENXIO;
345         SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
346                                    sdp->disk->disk_name, (int) count));
347         if (!access_ok(VERIFY_WRITE, buf, count))
348                 return -EFAULT;
349         if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
350                 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
351                 if (!old_hdr)
352                         return -ENOMEM;
353                 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
354                         retval = -EFAULT;
355                         goto free_old_hdr;
356                 }
357                 if (old_hdr->reply_len < 0) {
358                         if (count >= SZ_SG_IO_HDR) {
359                                 sg_io_hdr_t *new_hdr;
360                                 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
361                                 if (!new_hdr) {
362                                         retval = -ENOMEM;
363                                         goto free_old_hdr;
364                                 }
365                                 retval =__copy_from_user
366                                     (new_hdr, buf, SZ_SG_IO_HDR);
367                                 req_pack_id = new_hdr->pack_id;
368                                 kfree(new_hdr);
369                                 if (retval) {
370                                         retval = -EFAULT;
371                                         goto free_old_hdr;
372                                 }
373                         }
374                 } else
375                         req_pack_id = old_hdr->pack_id;
376         }
377         srp = sg_get_rq_mark(sfp, req_pack_id);
378         if (!srp) {             /* now wait on packet to arrive */
379                 if (sdp->detached) {
380                         retval = -ENODEV;
381                         goto free_old_hdr;
382                 }
383                 if (filp->f_flags & O_NONBLOCK) {
384                         retval = -EAGAIN;
385                         goto free_old_hdr;
386                 }
387                 while (1) {
388                         retval = 0; /* following macro beats race condition */
389                         __wait_event_interruptible(sfp->read_wait,
390                                 (sdp->detached ||
391                                 (srp = sg_get_rq_mark(sfp, req_pack_id))), 
392                                 retval);
393                         if (sdp->detached) {
394                                 retval = -ENODEV;
395                                 goto free_old_hdr;
396                         }
397                         if (0 == retval)
398                                 break;
399
400                         /* -ERESTARTSYS as signal hit process */
401                         goto free_old_hdr;
402                 }
403         }
404         if (srp->header.interface_id != '\0') {
405                 retval = sg_new_read(sfp, buf, count, srp);
406                 goto free_old_hdr;
407         }
408
409         hp = &srp->header;
410         if (old_hdr == NULL) {
411                 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
412                 if (! old_hdr) {
413                         retval = -ENOMEM;
414                         goto free_old_hdr;
415                 }
416         }
417         memset(old_hdr, 0, SZ_SG_HEADER);
418         old_hdr->reply_len = (int) hp->timeout;
419         old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
420         old_hdr->pack_id = hp->pack_id;
421         old_hdr->twelve_byte =
422             ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
423         old_hdr->target_status = hp->masked_status;
424         old_hdr->host_status = hp->host_status;
425         old_hdr->driver_status = hp->driver_status;
426         if ((CHECK_CONDITION & hp->masked_status) ||
427             (DRIVER_SENSE & hp->driver_status))
428                 memcpy(old_hdr->sense_buffer, srp->sense_b,
429                        sizeof (old_hdr->sense_buffer));
430         switch (hp->host_status) {
431         /* This setup of 'result' is for backward compatibility and is best
432            ignored by the user who should use target, host + driver status */
433         case DID_OK:
434         case DID_PASSTHROUGH:
435         case DID_SOFT_ERROR:
436                 old_hdr->result = 0;
437                 break;
438         case DID_NO_CONNECT:
439         case DID_BUS_BUSY:
440         case DID_TIME_OUT:
441                 old_hdr->result = EBUSY;
442                 break;
443         case DID_BAD_TARGET:
444         case DID_ABORT:
445         case DID_PARITY:
446         case DID_RESET:
447         case DID_BAD_INTR:
448                 old_hdr->result = EIO;
449                 break;
450         case DID_ERROR:
451                 old_hdr->result = (srp->sense_b[0] == 0 && 
452                                   hp->masked_status == GOOD) ? 0 : EIO;
453                 break;
454         default:
455                 old_hdr->result = EIO;
456                 break;
457         }
458
459         /* Now copy the result back to the user buffer.  */
460         if (count >= SZ_SG_HEADER) {
461                 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
462                         retval = -EFAULT;
463                         goto free_old_hdr;
464                 }
465                 buf += SZ_SG_HEADER;
466                 if (count > old_hdr->reply_len)
467                         count = old_hdr->reply_len;
468                 if (count > SZ_SG_HEADER) {
469                         if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
470                                 retval = -EFAULT;
471                                 goto free_old_hdr;
472                         }
473                 }
474         } else
475                 count = (old_hdr->result == 0) ? 0 : -EIO;
476         sg_finish_rem_req(srp);
477         retval = count;
478 free_old_hdr:
479         if (old_hdr)
480                 kfree(old_hdr);
481         return retval;
482 }
483
484 static ssize_t
485 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
486 {
487         sg_io_hdr_t *hp = &srp->header;
488         int err = 0;
489         int len;
490
491         if (count < SZ_SG_IO_HDR) {
492                 err = -EINVAL;
493                 goto err_out;
494         }
495         hp->sb_len_wr = 0;
496         if ((hp->mx_sb_len > 0) && hp->sbp) {
497                 if ((CHECK_CONDITION & hp->masked_status) ||
498                     (DRIVER_SENSE & hp->driver_status)) {
499                         int sb_len = sizeof (dummy_cmdp->sr_sense_buffer);
500                         sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
501                         len = 8 + (int) srp->sense_b[7];        /* Additional sense length field */
502                         len = (len > sb_len) ? sb_len : len;
503                         if (copy_to_user(hp->sbp, srp->sense_b, len)) {
504                                 err = -EFAULT;
505                                 goto err_out;
506                         }
507                         hp->sb_len_wr = len;
508                 }
509         }
510         if (hp->masked_status || hp->host_status || hp->driver_status)
511                 hp->info |= SG_INFO_CHECK;
512         if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
513                 err = -EFAULT;
514                 goto err_out;
515         }
516         err = sg_read_xfer(srp);
517       err_out:
518         sg_finish_rem_req(srp);
519         return (0 == err) ? count : err;
520 }
521
522 static ssize_t
523 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
524 {
525         int mxsize, cmd_size, k;
526         int input_size, blocking;
527         unsigned char opcode;
528         Sg_device *sdp;
529         Sg_fd *sfp;
530         Sg_request *srp;
531         struct sg_header old_hdr;
532         sg_io_hdr_t *hp;
533         unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
534
535         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
536                 return -ENXIO;
537         SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
538                                    sdp->disk->disk_name, (int) count));
539         if (sdp->detached)
540                 return -ENODEV;
541         if (!((filp->f_flags & O_NONBLOCK) ||
542               scsi_block_when_processing_errors(sdp->device)))
543                 return -ENXIO;
544
545         if (!access_ok(VERIFY_READ, buf, count))
546                 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
547         if (count < SZ_SG_HEADER)
548                 return -EIO;
549         if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
550                 return -EFAULT;
551         blocking = !(filp->f_flags & O_NONBLOCK);
552         if (old_hdr.reply_len < 0)
553                 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
554         if (count < (SZ_SG_HEADER + 6))
555                 return -EIO;    /* The minimum scsi command length is 6 bytes. */
556
557         if (!(srp = sg_add_request(sfp))) {
558                 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
559                 return -EDOM;
560         }
561         buf += SZ_SG_HEADER;
562         __get_user(opcode, buf);
563         if (sfp->next_cmd_len > 0) {
564                 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
565                         SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
566                         sfp->next_cmd_len = 0;
567                         sg_remove_request(sfp, srp);
568                         return -EIO;
569                 }
570                 cmd_size = sfp->next_cmd_len;
571                 sfp->next_cmd_len = 0;  /* reset so only this write() effected */
572         } else {
573                 cmd_size = COMMAND_SIZE(opcode);        /* based on SCSI command group */
574                 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
575                         cmd_size = 12;
576         }
577         SCSI_LOG_TIMEOUT(4, printk(
578                 "sg_write:   scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
579 /* Determine buffer size.  */
580         input_size = count - cmd_size;
581         mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
582         mxsize -= SZ_SG_HEADER;
583         input_size -= SZ_SG_HEADER;
584         if (input_size < 0) {
585                 sg_remove_request(sfp, srp);
586                 return -EIO;    /* User did not pass enough bytes for this command. */
587         }
588         hp = &srp->header;
589         hp->interface_id = '\0';        /* indicator of old interface tunnelled */
590         hp->cmd_len = (unsigned char) cmd_size;
591         hp->iovec_count = 0;
592         hp->mx_sb_len = 0;
593         if (input_size > 0)
594                 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
595                     SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
596         else
597                 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
598         hp->dxfer_len = mxsize;
599         hp->dxferp = (char __user *)buf + cmd_size;
600         hp->sbp = NULL;
601         hp->timeout = old_hdr.reply_len;        /* structure abuse ... */
602         hp->flags = input_size; /* structure abuse ... */
603         hp->pack_id = old_hdr.pack_id;
604         hp->usr_ptr = NULL;
605         if (__copy_from_user(cmnd, buf, cmd_size))
606                 return -EFAULT;
607         /*
608          * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
609          * but is is possible that the app intended SG_DXFER_TO_DEV, because there
610          * is a non-zero input_size, so emit a warning.
611          */
612         if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
613                 if (printk_ratelimit())
614                         printk(KERN_WARNING
615                                "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
616                                "guessing data in;\n" KERN_WARNING "   "
617                                "program %s not setting count and/or reply_len properly\n",
618                                old_hdr.reply_len - (int)SZ_SG_HEADER,
619                                input_size, (unsigned int) cmnd[0],
620                                current->comm);
621         k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
622         return (k < 0) ? k : count;
623 }
624
625 static ssize_t
626 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
627              int blocking, int read_only, Sg_request ** o_srp)
628 {
629         int k;
630         Sg_request *srp;
631         sg_io_hdr_t *hp;
632         unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
633         int timeout;
634         unsigned long ul_timeout;
635
636         if (count < SZ_SG_IO_HDR)
637                 return -EINVAL;
638         if (!access_ok(VERIFY_READ, buf, count))
639                 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
640
641         sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
642         if (!(srp = sg_add_request(sfp))) {
643                 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
644                 return -EDOM;
645         }
646         hp = &srp->header;
647         if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
648                 sg_remove_request(sfp, srp);
649                 return -EFAULT;
650         }
651         if (hp->interface_id != 'S') {
652                 sg_remove_request(sfp, srp);
653                 return -ENOSYS;
654         }
655         if (hp->flags & SG_FLAG_MMAP_IO) {
656                 if (hp->dxfer_len > sfp->reserve.bufflen) {
657                         sg_remove_request(sfp, srp);
658                         return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
659                 }
660                 if (hp->flags & SG_FLAG_DIRECT_IO) {
661                         sg_remove_request(sfp, srp);
662                         return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
663                 }
664                 if (sg_res_in_use(sfp)) {
665                         sg_remove_request(sfp, srp);
666                         return -EBUSY;  /* reserve buffer already being used */
667                 }
668         }
669         ul_timeout = msecs_to_jiffies(srp->header.timeout);
670         timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
671         if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
672                 sg_remove_request(sfp, srp);
673                 return -EMSGSIZE;
674         }
675         if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
676                 sg_remove_request(sfp, srp);
677                 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
678         }
679         if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
680                 sg_remove_request(sfp, srp);
681                 return -EFAULT;
682         }
683         if (read_only &&
684             (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
685                 sg_remove_request(sfp, srp);
686                 return -EPERM;
687         }
688         k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
689         if (k < 0)
690                 return k;
691         if (o_srp)
692                 *o_srp = srp;
693         return count;
694 }
695
696 static int
697 sg_common_write(Sg_fd * sfp, Sg_request * srp,
698                 unsigned char *cmnd, int timeout, int blocking)
699 {
700         int k;
701         Scsi_Request *SRpnt;
702         Sg_device *sdp = sfp->parentdp;
703         sg_io_hdr_t *hp = &srp->header;
704         request_queue_t *q;
705
706         srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
707         hp->status = 0;
708         hp->masked_status = 0;
709         hp->msg_status = 0;
710         hp->info = 0;
711         hp->host_status = 0;
712         hp->driver_status = 0;
713         hp->resid = 0;
714         SCSI_LOG_TIMEOUT(4, printk("sg_common_write:  scsi opcode=0x%02x, cmd_size=%d\n",
715                           (int) cmnd[0], (int) hp->cmd_len));
716
717         if ((k = sg_start_req(srp))) {
718                 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
719                 sg_finish_rem_req(srp);
720                 return k;       /* probably out of space --> ENOMEM */
721         }
722         if ((k = sg_write_xfer(srp))) {
723                 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
724                 sg_finish_rem_req(srp);
725                 return k;
726         }
727         if (sdp->detached) {
728                 sg_finish_rem_req(srp);
729                 return -ENODEV;
730         }
731         SRpnt = scsi_allocate_request(sdp->device, GFP_ATOMIC);
732         if (SRpnt == NULL) {
733                 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
734                 sg_finish_rem_req(srp);
735                 return -ENOMEM;
736         }
737
738         srp->my_cmdp = SRpnt;
739         q = SRpnt->sr_device->request_queue;
740         SRpnt->sr_request->rq_disk = sdp->disk;
741         SRpnt->sr_sense_buffer[0] = 0;
742         SRpnt->sr_cmd_len = hp->cmd_len;
743         SRpnt->sr_use_sg = srp->data.k_use_sg;
744         SRpnt->sr_sglist_len = srp->data.sglist_len;
745         SRpnt->sr_bufflen = srp->data.bufflen;
746         SRpnt->sr_underflow = 0;
747         SRpnt->sr_buffer = srp->data.buffer;
748         switch (hp->dxfer_direction) {
749         case SG_DXFER_TO_FROM_DEV:
750         case SG_DXFER_FROM_DEV:
751                 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
752                 break;
753         case SG_DXFER_TO_DEV:
754                 SRpnt->sr_data_direction = DMA_TO_DEVICE;
755                 break;
756         case SG_DXFER_UNKNOWN:
757                 SRpnt->sr_data_direction = DMA_BIDIRECTIONAL;
758                 break;
759         default:
760                 SRpnt->sr_data_direction = DMA_NONE;
761                 break;
762         }
763         SRpnt->upper_private_data = srp;
764         srp->data.k_use_sg = 0;
765         srp->data.sglist_len = 0;
766         srp->data.bufflen = 0;
767         srp->data.buffer = NULL;
768         hp->duration = jiffies_to_msecs(jiffies);
769 /* Now send everything of to mid-level. The next time we hear about this
770    packet is when sg_cmd_done() is called (i.e. a callback). */
771         scsi_do_req(SRpnt, (void *) cmnd,
772                     (void *) SRpnt->sr_buffer, hp->dxfer_len,
773                     sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
774         /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
775         return 0;
776 }
777
778 static int
779 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
780 {
781         unsigned long iflags;
782         int done;
783
784         read_lock_irqsave(&sfp->rq_list_lock, iflags);
785         done = srp->done;
786         read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
787         return done;
788 }
789
790 static int
791 sg_ioctl(struct inode *inode, struct file *filp,
792          unsigned int cmd_in, unsigned long arg)
793 {
794         void __user *p = (void __user *)arg;
795         int __user *ip = p;
796         int result, val, read_only;
797         Sg_device *sdp;
798         Sg_fd *sfp;
799         Sg_request *srp;
800         unsigned long iflags;
801
802         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
803                 return -ENXIO;
804         SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
805                                    sdp->disk->disk_name, (int) cmd_in));
806         read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
807
808         switch (cmd_in) {
809         case SG_IO:
810                 {
811                         int blocking = 1;       /* ignore O_NONBLOCK flag */
812
813                         if (sdp->detached)
814                                 return -ENODEV;
815                         if (!scsi_block_when_processing_errors(sdp->device))
816                                 return -ENXIO;
817                         if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
818                                 return -EFAULT;
819                         result =
820                             sg_new_write(sfp, p, SZ_SG_IO_HDR,
821                                          blocking, read_only, &srp);
822                         if (result < 0)
823                                 return result;
824                         srp->sg_io_owned = 1;
825                         while (1) {
826                                 result = 0;     /* following macro to beat race condition */
827                                 __wait_event_interruptible(sfp->read_wait,
828                                         (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
829                                                            result);
830                                 if (sdp->detached)
831                                         return -ENODEV;
832                                 if (sfp->closed)
833                                         return 0;       /* request packet dropped already */
834                                 if (0 == result)
835                                         break;
836                                 srp->orphan = 1;
837                                 return result;  /* -ERESTARTSYS because signal hit process */
838                         }
839                         write_lock_irqsave(&sfp->rq_list_lock, iflags);
840                         srp->done = 2;
841                         write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
842                         result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
843                         return (result < 0) ? result : 0;
844                 }
845         case SG_SET_TIMEOUT:
846                 result = get_user(val, ip);
847                 if (result)
848                         return result;
849                 if (val < 0)
850                         return -EIO;
851                 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
852                     val = MULDIV (INT_MAX, USER_HZ, HZ);
853                 sfp->timeout_user = val;
854                 sfp->timeout = MULDIV (val, HZ, USER_HZ);
855
856                 return 0;
857         case SG_GET_TIMEOUT:    /* N.B. User receives timeout as return value */
858                                 /* strange ..., for backward compatibility */
859                 return sfp->timeout_user;
860         case SG_SET_FORCE_LOW_DMA:
861                 result = get_user(val, ip);
862                 if (result)
863                         return result;
864                 if (val) {
865                         sfp->low_dma = 1;
866                         if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
867                                 val = (int) sfp->reserve.bufflen;
868                                 sg_remove_scat(&sfp->reserve);
869                                 sg_build_reserve(sfp, val);
870                         }
871                 } else {
872                         if (sdp->detached)
873                                 return -ENODEV;
874                         sfp->low_dma = sdp->device->host->unchecked_isa_dma;
875                 }
876                 return 0;
877         case SG_GET_LOW_DMA:
878                 return put_user((int) sfp->low_dma, ip);
879         case SG_GET_SCSI_ID:
880                 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
881                         return -EFAULT;
882                 else {
883                         sg_scsi_id_t __user *sg_idp = p;
884
885                         if (sdp->detached)
886                                 return -ENODEV;
887                         __put_user((int) sdp->device->host->host_no,
888                                    &sg_idp->host_no);
889                         __put_user((int) sdp->device->channel,
890                                    &sg_idp->channel);
891                         __put_user((int) sdp->device->id, &sg_idp->scsi_id);
892                         __put_user((int) sdp->device->lun, &sg_idp->lun);
893                         __put_user((int) sdp->device->type, &sg_idp->scsi_type);
894                         __put_user((short) sdp->device->host->cmd_per_lun,
895                                    &sg_idp->h_cmd_per_lun);
896                         __put_user((short) sdp->device->queue_depth,
897                                    &sg_idp->d_queue_depth);
898                         __put_user(0, &sg_idp->unused[0]);
899                         __put_user(0, &sg_idp->unused[1]);
900                         return 0;
901                 }
902         case SG_SET_FORCE_PACK_ID:
903                 result = get_user(val, ip);
904                 if (result)
905                         return result;
906                 sfp->force_packid = val ? 1 : 0;
907                 return 0;
908         case SG_GET_PACK_ID:
909                 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
910                         return -EFAULT;
911                 read_lock_irqsave(&sfp->rq_list_lock, iflags);
912                 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
913                         if ((1 == srp->done) && (!srp->sg_io_owned)) {
914                                 read_unlock_irqrestore(&sfp->rq_list_lock,
915                                                        iflags);
916                                 __put_user(srp->header.pack_id, ip);
917                                 return 0;
918                         }
919                 }
920                 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
921                 __put_user(-1, ip);
922                 return 0;
923         case SG_GET_NUM_WAITING:
924                 read_lock_irqsave(&sfp->rq_list_lock, iflags);
925                 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
926                         if ((1 == srp->done) && (!srp->sg_io_owned))
927                                 ++val;
928                 }
929                 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
930                 return put_user(val, ip);
931         case SG_GET_SG_TABLESIZE:
932                 return put_user(sdp->sg_tablesize, ip);
933         case SG_SET_RESERVED_SIZE:
934                 result = get_user(val, ip);
935                 if (result)
936                         return result;
937                 if (val < 0)
938                         return -EINVAL;
939                 if (val != sfp->reserve.bufflen) {
940                         if (sg_res_in_use(sfp) || sfp->mmap_called)
941                                 return -EBUSY;
942                         sg_remove_scat(&sfp->reserve);
943                         sg_build_reserve(sfp, val);
944                 }
945                 return 0;
946         case SG_GET_RESERVED_SIZE:
947                 val = (int) sfp->reserve.bufflen;
948                 return put_user(val, ip);
949         case SG_SET_COMMAND_Q:
950                 result = get_user(val, ip);
951                 if (result)
952                         return result;
953                 sfp->cmd_q = val ? 1 : 0;
954                 return 0;
955         case SG_GET_COMMAND_Q:
956                 return put_user((int) sfp->cmd_q, ip);
957         case SG_SET_KEEP_ORPHAN:
958                 result = get_user(val, ip);
959                 if (result)
960                         return result;
961                 sfp->keep_orphan = val;
962                 return 0;
963         case SG_GET_KEEP_ORPHAN:
964                 return put_user((int) sfp->keep_orphan, ip);
965         case SG_NEXT_CMD_LEN:
966                 result = get_user(val, ip);
967                 if (result)
968                         return result;
969                 sfp->next_cmd_len = (val > 0) ? val : 0;
970                 return 0;
971         case SG_GET_VERSION_NUM:
972                 return put_user(sg_version_num, ip);
973         case SG_GET_ACCESS_COUNT:
974                 /* faked - we don't have a real access count anymore */
975                 val = (sdp->device ? 1 : 0);
976                 return put_user(val, ip);
977         case SG_GET_REQUEST_TABLE:
978                 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
979                         return -EFAULT;
980                 else {
981                         sg_req_info_t *rinfo;
982                         unsigned int ms;
983
984                         rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
985                                                                 GFP_KERNEL);
986                         if (!rinfo)
987                                 return -ENOMEM;
988                         read_lock_irqsave(&sfp->rq_list_lock, iflags);
989                         for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
990                              ++val, srp = srp ? srp->nextrp : srp) {
991                                 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
992                                 if (srp) {
993                                         rinfo[val].req_state = srp->done + 1;
994                                         rinfo[val].problem =
995                                             srp->header.masked_status & 
996                                             srp->header.host_status & 
997                                             srp->header.driver_status;
998                                         if (srp->done)
999                                                 rinfo[val].duration =
1000                                                         srp->header.duration;
1001                                         else {
1002                                                 ms = jiffies_to_msecs(jiffies);
1003                                                 rinfo[val].duration =
1004                                                     (ms > srp->header.duration) ?
1005                                                     (ms - srp->header.duration) : 0;
1006                                         }
1007                                         rinfo[val].orphan = srp->orphan;
1008                                         rinfo[val].sg_io_owned =
1009                                                         srp->sg_io_owned;
1010                                         rinfo[val].pack_id =
1011                                                         srp->header.pack_id;
1012                                         rinfo[val].usr_ptr =
1013                                                         srp->header.usr_ptr;
1014                                 }
1015                         }
1016                         read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1017                         result = __copy_to_user(p, rinfo, 
1018                                                 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1019                         result = result ? -EFAULT : 0;
1020                         kfree(rinfo);
1021                         return result;
1022                 }
1023         case SG_EMULATED_HOST:
1024                 if (sdp->detached)
1025                         return -ENODEV;
1026                 return put_user(sdp->device->host->hostt->emulated, ip);
1027         case SG_SCSI_RESET:
1028                 if (sdp->detached)
1029                         return -ENODEV;
1030                 if (filp->f_flags & O_NONBLOCK) {
1031                         if (scsi_host_in_recovery(sdp->device->host))
1032                                 return -EBUSY;
1033                 } else if (!scsi_block_when_processing_errors(sdp->device))
1034                         return -EBUSY;
1035                 result = get_user(val, ip);
1036                 if (result)
1037                         return result;
1038                 if (SG_SCSI_RESET_NOTHING == val)
1039                         return 0;
1040                 switch (val) {
1041                 case SG_SCSI_RESET_DEVICE:
1042                         val = SCSI_TRY_RESET_DEVICE;
1043                         break;
1044                 case SG_SCSI_RESET_BUS:
1045                         val = SCSI_TRY_RESET_BUS;
1046                         break;
1047                 case SG_SCSI_RESET_HOST:
1048                         val = SCSI_TRY_RESET_HOST;
1049                         break;
1050                 default:
1051                         return -EINVAL;
1052                 }
1053                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1054                         return -EACCES;
1055                 return (scsi_reset_provider(sdp->device, val) ==
1056                         SUCCESS) ? 0 : -EIO;
1057         case SCSI_IOCTL_SEND_COMMAND:
1058                 if (sdp->detached)
1059                         return -ENODEV;
1060                 if (read_only) {
1061                         unsigned char opcode = WRITE_6;
1062                         Scsi_Ioctl_Command __user *siocp = p;
1063
1064                         if (copy_from_user(&opcode, siocp->data, 1))
1065                                 return -EFAULT;
1066                         if (!sg_allow_access(opcode, sdp->device->type))
1067                                 return -EPERM;
1068                 }
1069                 return scsi_ioctl_send_command(sdp->device, p);
1070         case SG_SET_DEBUG:
1071                 result = get_user(val, ip);
1072                 if (result)
1073                         return result;
1074                 sdp->sgdebug = (char) val;
1075                 return 0;
1076         case SCSI_IOCTL_GET_IDLUN:
1077         case SCSI_IOCTL_GET_BUS_NUMBER:
1078         case SCSI_IOCTL_PROBE_HOST:
1079         case SG_GET_TRANSFORM:
1080                 if (sdp->detached)
1081                         return -ENODEV;
1082                 return scsi_ioctl(sdp->device, cmd_in, p);
1083         default:
1084                 if (read_only)
1085                         return -EPERM;  /* don't know so take safe approach */
1086                 return scsi_ioctl(sdp->device, cmd_in, p);
1087         }
1088 }
1089
1090 #ifdef CONFIG_COMPAT
1091 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1092 {
1093         Sg_device *sdp;
1094         Sg_fd *sfp;
1095         struct scsi_device *sdev;
1096
1097         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1098                 return -ENXIO;
1099
1100         sdev = sdp->device;
1101         if (sdev->host->hostt->compat_ioctl) { 
1102                 int ret;
1103
1104                 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1105
1106                 return ret;
1107         }
1108         
1109         return -ENOIOCTLCMD;
1110 }
1111 #endif
1112
1113 static unsigned int
1114 sg_poll(struct file *filp, poll_table * wait)
1115 {
1116         unsigned int res = 0;
1117         Sg_device *sdp;
1118         Sg_fd *sfp;
1119         Sg_request *srp;
1120         int count = 0;
1121         unsigned long iflags;
1122
1123         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1124             || sfp->closed)
1125                 return POLLERR;
1126         poll_wait(filp, &sfp->read_wait, wait);
1127         read_lock_irqsave(&sfp->rq_list_lock, iflags);
1128         for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1129                 /* if any read waiting, flag it */
1130                 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1131                         res = POLLIN | POLLRDNORM;
1132                 ++count;
1133         }
1134         read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1135
1136         if (sdp->detached)
1137                 res |= POLLHUP;
1138         else if (!sfp->cmd_q) {
1139                 if (0 == count)
1140                         res |= POLLOUT | POLLWRNORM;
1141         } else if (count < SG_MAX_QUEUE)
1142                 res |= POLLOUT | POLLWRNORM;
1143         SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1144                                    sdp->disk->disk_name, (int) res));
1145         return res;
1146 }
1147
1148 static int
1149 sg_fasync(int fd, struct file *filp, int mode)
1150 {
1151         int retval;
1152         Sg_device *sdp;
1153         Sg_fd *sfp;
1154
1155         if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1156                 return -ENXIO;
1157         SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1158                                    sdp->disk->disk_name, mode));
1159
1160         retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1161         return (retval < 0) ? retval : 0;
1162 }
1163
1164 static inline unsigned char *
1165 sg_scatg2virt(const struct scatterlist *sclp)
1166 {
1167         return (sclp && sclp->page) ?
1168             (unsigned char *) page_address(sclp->page) + sclp->offset : NULL;
1169 }
1170
1171 /* When startFinish==1 increments page counts for pages other than the 
1172    first of scatter gather elements obtained from __get_free_pages().
1173    When startFinish==0 decrements ... */
1174 static void
1175 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1176 {
1177         void *page_ptr;
1178         struct page *page;
1179         int k, m;
1180
1181         SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n", 
1182                                    startFinish, rsv_schp->k_use_sg));
1183         /* N.B. correction _not_ applied to base page of each allocation */
1184         if (rsv_schp->k_use_sg) {       /* reserve buffer is a scatter gather list */
1185                 struct scatterlist *sclp = rsv_schp->buffer;
1186
1187                 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
1188                         for (m = PAGE_SIZE; m < sclp->length; m += PAGE_SIZE) {
1189                                 page_ptr = sg_scatg2virt(sclp) + m;
1190                                 page = virt_to_page(page_ptr);
1191                                 if (startFinish)
1192                                         get_page(page);
1193                                 else {
1194                                         if (page_count(page) > 0)
1195                                                 __put_page(page);
1196                                 }
1197                         }
1198                 }
1199         } else {                /* reserve buffer is just a single allocation */
1200                 for (m = PAGE_SIZE; m < rsv_schp->bufflen; m += PAGE_SIZE) {
1201                         page_ptr = (unsigned char *) rsv_schp->buffer + m;
1202                         page = virt_to_page(page_ptr);
1203                         if (startFinish)
1204                                 get_page(page);
1205                         else {
1206                                 if (page_count(page) > 0)
1207                                         __put_page(page);
1208                         }
1209                 }
1210         }
1211 }
1212
1213 static struct page *
1214 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1215 {
1216         Sg_fd *sfp;
1217         struct page *page = NOPAGE_SIGBUS;
1218         void *page_ptr = NULL;
1219         unsigned long offset;
1220         Sg_scatter_hold *rsv_schp;
1221
1222         if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1223                 return page;
1224         rsv_schp = &sfp->reserve;
1225         offset = addr - vma->vm_start;
1226         if (offset >= rsv_schp->bufflen)
1227                 return page;
1228         SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1229                                    offset, rsv_schp->k_use_sg));
1230         if (rsv_schp->k_use_sg) {       /* reserve buffer is a scatter gather list */
1231                 int k;
1232                 unsigned long sa = vma->vm_start;
1233                 unsigned long len;
1234                 struct scatterlist *sclp = rsv_schp->buffer;
1235
1236                 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1237                      ++k, ++sclp) {
1238                         len = vma->vm_end - sa;
1239                         len = (len < sclp->length) ? len : sclp->length;
1240                         if (offset < len) {
1241                                 page_ptr = sg_scatg2virt(sclp) + offset;
1242                                 page = virt_to_page(page_ptr);
1243                                 get_page(page); /* increment page count */
1244                                 break;
1245                         }
1246                         sa += len;
1247                         offset -= len;
1248                 }
1249         } else {                /* reserve buffer is just a single allocation */
1250                 page_ptr = (unsigned char *) rsv_schp->buffer + offset;
1251                 page = virt_to_page(page_ptr);
1252                 get_page(page); /* increment page count */
1253         }
1254         if (type)
1255                 *type = VM_FAULT_MINOR;
1256         return page;
1257 }
1258
1259 static struct vm_operations_struct sg_mmap_vm_ops = {
1260         .nopage = sg_vma_nopage,
1261 };
1262
1263 static int
1264 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1265 {
1266         Sg_fd *sfp;
1267         unsigned long req_sz;
1268         Sg_scatter_hold *rsv_schp;
1269
1270         if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1271                 return -ENXIO;
1272         req_sz = vma->vm_end - vma->vm_start;
1273         SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1274                                    (void *) vma->vm_start, (int) req_sz));
1275         if (vma->vm_pgoff)
1276                 return -EINVAL; /* want no offset */
1277         rsv_schp = &sfp->reserve;
1278         if (req_sz > rsv_schp->bufflen)
1279                 return -ENOMEM; /* cannot map more than reserved buffer */
1280
1281         if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1282                 int k;
1283                 unsigned long sa = vma->vm_start;
1284                 unsigned long len;
1285                 struct scatterlist *sclp = rsv_schp->buffer;
1286
1287                 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1288                      ++k, ++sclp) {
1289                         if (0 != sclp->offset)
1290                                 return -EFAULT; /* non page aligned memory ?? */
1291                         len = vma->vm_end - sa;
1292                         len = (len < sclp->length) ? len : sclp->length;
1293                         sa += len;
1294                 }
1295         } else {        /* reserve buffer is just a single allocation */
1296                 if ((unsigned long) rsv_schp->buffer & (PAGE_SIZE - 1))
1297                         return -EFAULT; /* non page aligned memory ?? */
1298         }
1299         if (0 == sfp->mmap_called) {
1300                 sg_rb_correct4mmap(rsv_schp, 1);        /* do only once per fd lifetime */
1301                 sfp->mmap_called = 1;
1302         }
1303         vma->vm_flags |= VM_RESERVED;
1304         vma->vm_private_data = sfp;
1305         vma->vm_ops = &sg_mmap_vm_ops;
1306         return 0;
1307 }
1308
1309 /* This function is a "bottom half" handler that is called by the
1310  * mid level when a command is completed (or has failed). */
1311 static void
1312 sg_cmd_done(Scsi_Cmnd * SCpnt)
1313 {
1314         Scsi_Request *SRpnt = NULL;
1315         Sg_device *sdp = NULL;
1316         Sg_fd *sfp;
1317         Sg_request *srp = NULL;
1318         unsigned long iflags;
1319         unsigned int ms;
1320
1321         if (SCpnt && (SRpnt = SCpnt->sc_request))
1322                 srp = (Sg_request *) SRpnt->upper_private_data;
1323         if (NULL == srp) {
1324                 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1325                 if (SRpnt)
1326                         scsi_release_request(SRpnt);
1327                 return;
1328         }
1329         sfp = srp->parentfp;
1330         if (sfp)
1331                 sdp = sfp->parentdp;
1332         if ((NULL == sdp) || sdp->detached) {
1333                 printk(KERN_INFO "sg_cmd_done: device detached\n");
1334                 scsi_release_request(SRpnt);
1335                 return;
1336         }
1337
1338         /* First transfer ownership of data buffers to sg_device object. */
1339         srp->data.k_use_sg = SRpnt->sr_use_sg;
1340         srp->data.sglist_len = SRpnt->sr_sglist_len;
1341         srp->data.bufflen = SRpnt->sr_bufflen;
1342         srp->data.buffer = SRpnt->sr_buffer;
1343         /* now clear out request structure */
1344         SRpnt->sr_use_sg = 0;
1345         SRpnt->sr_sglist_len = 0;
1346         SRpnt->sr_bufflen = 0;
1347         SRpnt->sr_buffer = NULL;
1348         SRpnt->sr_underflow = 0;
1349         SRpnt->sr_request->rq_disk = NULL; /* "sg" _disowns_ request blk */
1350
1351         srp->my_cmdp = NULL;
1352
1353         SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1354                 sdp->disk->disk_name, srp->header.pack_id, (int) SRpnt->sr_result));
1355         srp->header.resid = SCpnt->resid;
1356         ms = jiffies_to_msecs(jiffies);
1357         srp->header.duration = (ms > srp->header.duration) ?
1358                                 (ms - srp->header.duration) : 0;
1359         if (0 != SRpnt->sr_result) {
1360                 struct scsi_sense_hdr sshdr;
1361
1362                 memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
1363                        sizeof (srp->sense_b));
1364                 srp->header.status = 0xff & SRpnt->sr_result;
1365                 srp->header.masked_status = status_byte(SRpnt->sr_result);
1366                 srp->header.msg_status = msg_byte(SRpnt->sr_result);
1367                 srp->header.host_status = host_byte(SRpnt->sr_result);
1368                 srp->header.driver_status = driver_byte(SRpnt->sr_result);
1369                 if ((sdp->sgdebug > 0) &&
1370                     ((CHECK_CONDITION == srp->header.masked_status) ||
1371                      (COMMAND_TERMINATED == srp->header.masked_status)))
1372                         scsi_print_req_sense("sg_cmd_done", SRpnt);
1373
1374                 /* Following if statement is a patch supplied by Eric Youngdale */
1375                 if (driver_byte(SRpnt->sr_result) != 0
1376                     && scsi_command_normalize_sense(SCpnt, &sshdr)
1377                     && !scsi_sense_is_deferred(&sshdr)
1378                     && sshdr.sense_key == UNIT_ATTENTION
1379                     && sdp->device->removable) {
1380                         /* Detected possible disc change. Set the bit - this */
1381                         /* may be used if there are filesystems using this device */
1382                         sdp->device->changed = 1;
1383                 }
1384         }
1385         /* Rely on write phase to clean out srp status values, so no "else" */
1386
1387         scsi_release_request(SRpnt);
1388         SRpnt = NULL;
1389         if (sfp->closed) {      /* whoops this fd already released, cleanup */
1390                 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1391                 sg_finish_rem_req(srp);
1392                 srp = NULL;
1393                 if (NULL == sfp->headrp) {
1394                         SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1395                         if (0 == sg_remove_sfp(sdp, sfp)) {     /* device still present */
1396                                 scsi_device_put(sdp->device);
1397                         }
1398                         sfp = NULL;
1399                 }
1400         } else if (srp && srp->orphan) {
1401                 if (sfp->keep_orphan)
1402                         srp->sg_io_owned = 0;
1403                 else {
1404                         sg_finish_rem_req(srp);
1405                         srp = NULL;
1406                 }
1407         }
1408         if (sfp && srp) {
1409                 /* Now wake up any sg_read() that is waiting for this packet. */
1410                 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1411                 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1412                 srp->done = 1;
1413                 wake_up_interruptible(&sfp->read_wait);
1414                 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1415         }
1416 }
1417
1418 static struct file_operations sg_fops = {
1419         .owner = THIS_MODULE,
1420         .read = sg_read,
1421         .write = sg_write,
1422         .poll = sg_poll,
1423         .ioctl = sg_ioctl,
1424 #ifdef CONFIG_COMPAT
1425         .compat_ioctl = sg_compat_ioctl,
1426 #endif
1427         .open = sg_open,
1428         .mmap = sg_mmap,
1429         .release = sg_release,
1430         .fasync = sg_fasync,
1431 };
1432
1433 static struct class *sg_sysfs_class;
1434
1435 static int sg_sysfs_valid = 0;
1436
1437 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1438 {
1439         Sg_device *sdp;
1440         unsigned long iflags;
1441         void *old_sg_dev_arr = NULL;
1442         int k, error;
1443
1444         sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
1445         if (!sdp) {
1446                 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1447                 return -ENOMEM;
1448         }
1449
1450         write_lock_irqsave(&sg_dev_arr_lock, iflags);
1451         if (unlikely(sg_nr_dev >= sg_dev_max)) {        /* try to resize */
1452                 Sg_device **tmp_da;
1453                 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1454                 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1455
1456                 tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1457                 if (unlikely(!tmp_da))
1458                         goto expand_failed;
1459
1460                 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1461                 memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
1462                 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1463                 old_sg_dev_arr = sg_dev_arr;
1464                 sg_dev_arr = tmp_da;
1465                 sg_dev_max = tmp_dev_max;
1466         }
1467
1468         for (k = 0; k < sg_dev_max; k++)
1469                 if (!sg_dev_arr[k])
1470                         break;
1471         if (unlikely(k >= SG_MAX_DEVS))
1472                 goto overflow;
1473
1474         memset(sdp, 0, sizeof(*sdp));
1475         SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1476         sprintf(disk->disk_name, "sg%d", k);
1477         disk->first_minor = k;
1478         sdp->disk = disk;
1479         sdp->device = scsidp;
1480         init_waitqueue_head(&sdp->o_excl_wait);
1481         sdp->sg_tablesize = scsidp->host ? scsidp->host->sg_tablesize : 0;
1482
1483         sg_nr_dev++;
1484         sg_dev_arr[k] = sdp;
1485         write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1486         error = k;
1487
1488  out:
1489         if (error < 0)
1490                 kfree(sdp);
1491         kfree(old_sg_dev_arr);
1492         return error;
1493
1494  expand_failed:
1495         printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1496         error = -ENOMEM;
1497         goto out;
1498
1499  overflow:
1500         write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1501         printk(KERN_WARNING
1502                "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1503                "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
1504                scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
1505         error = -ENODEV;
1506         goto out;
1507 }
1508
1509 static int
1510 sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
1511 {
1512         struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1513         struct gendisk *disk;
1514         Sg_device *sdp = NULL;
1515         struct cdev * cdev = NULL;
1516         int error, k;
1517
1518         disk = alloc_disk(1);
1519         if (!disk) {
1520                 printk(KERN_WARNING "alloc_disk failed\n");
1521                 return -ENOMEM;
1522         }
1523         disk->major = SCSI_GENERIC_MAJOR;
1524
1525         error = -ENOMEM;
1526         cdev = cdev_alloc();
1527         if (!cdev) {
1528                 printk(KERN_WARNING "cdev_alloc failed\n");
1529                 goto out;
1530         }
1531         cdev->owner = THIS_MODULE;
1532         cdev->ops = &sg_fops;
1533
1534         error = sg_alloc(disk, scsidp);
1535         if (error < 0) {
1536                 printk(KERN_WARNING "sg_alloc failed\n");
1537                 goto out;
1538         }
1539         k = error;
1540         sdp = sg_dev_arr[k];
1541
1542         devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1543                         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1544                         "%s/generic", scsidp->devfs_name);
1545         error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1546         if (error) {
1547                 devfs_remove("%s/generic", scsidp->devfs_name);
1548                 goto out;
1549         }
1550         sdp->cdev = cdev;
1551         if (sg_sysfs_valid) {
1552                 struct class_device * sg_class_member;
1553
1554                 sg_class_member = class_device_create(sg_sysfs_class, NULL,
1555                                 MKDEV(SCSI_GENERIC_MAJOR, k), 
1556                                 cl_dev->dev, "%s", 
1557                                 disk->disk_name);
1558                 if (IS_ERR(sg_class_member))
1559                         printk(KERN_WARNING "sg_add: "
1560                                 "class_device_create failed\n");
1561                 class_set_devdata(sg_class_member, sdp);
1562                 error = sysfs_create_link(&scsidp->sdev_gendev.kobj, 
1563                                           &sg_class_member->kobj, "generic");
1564                 if (error)
1565                         printk(KERN_ERR "sg_add: unable to make symlink "
1566                                         "'generic' back to sg%d\n", k);
1567         } else
1568                 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1569
1570         printk(KERN_NOTICE
1571                "Attached scsi generic sg%d at scsi%d, channel"
1572                " %d, id %d, lun %d,  type %d\n", k,
1573                scsidp->host->host_no, scsidp->channel, scsidp->id,
1574                scsidp->lun, scsidp->type);
1575
1576         return 0;
1577
1578 out:
1579         put_disk(disk);
1580         if (cdev)
1581                 cdev_del(cdev);
1582         return error;
1583 }
1584
1585 static void
1586 sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
1587 {
1588         struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1589         Sg_device *sdp = NULL;
1590         unsigned long iflags;
1591         Sg_fd *sfp;
1592         Sg_fd *tsfp;
1593         Sg_request *srp;
1594         Sg_request *tsrp;
1595         int k, delay;
1596
1597         if (NULL == sg_dev_arr)
1598                 return;
1599         delay = 0;
1600         write_lock_irqsave(&sg_dev_arr_lock, iflags);
1601         for (k = 0; k < sg_dev_max; k++) {
1602                 sdp = sg_dev_arr[k];
1603                 if ((NULL == sdp) || (sdp->device != scsidp))
1604                         continue;       /* dirty but lowers nesting */
1605                 if (sdp->headfp) {
1606                         sdp->detached = 1;
1607                         for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1608                                 tsfp = sfp->nextfp;
1609                                 for (srp = sfp->headrp; srp; srp = tsrp) {
1610                                         tsrp = srp->nextrp;
1611                                         if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1612                                                 sg_finish_rem_req(srp);
1613                                 }
1614                                 if (sfp->closed) {
1615                                         scsi_device_put(sdp->device);
1616                                         __sg_remove_sfp(sdp, sfp);
1617                                 } else {
1618                                         delay = 1;
1619                                         wake_up_interruptible(&sfp->read_wait);
1620                                         kill_fasync(&sfp->async_qp, SIGPOLL,
1621                                                     POLL_HUP);
1622                                 }
1623                         }
1624                         SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1625                         if (NULL == sdp->headfp) {
1626                                 sg_dev_arr[k] = NULL;
1627                         }
1628                 } else {        /* nothing active, simple case */
1629                         SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1630                         sg_dev_arr[k] = NULL;
1631                 }
1632                 sg_nr_dev--;
1633                 break;
1634         }
1635         write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1636
1637         if (sdp) {
1638                 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1639                 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, k));
1640                 cdev_del(sdp->cdev);
1641                 sdp->cdev = NULL;
1642                 devfs_remove("%s/generic", scsidp->devfs_name);
1643                 put_disk(sdp->disk);
1644                 sdp->disk = NULL;
1645                 if (NULL == sdp->headfp)
1646                         kfree((char *) sdp);
1647         }
1648
1649         if (delay)
1650                 msleep(10);     /* dirty detach so delay device destruction */
1651 }
1652
1653 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1654  * of sysfs parameters (which module_param doesn't yet support).
1655  * Sysfs parameters defined explicitly below.
1656  */
1657 module_param_named(def_reserved_size, def_reserved_size, int, S_IRUGO);
1658 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1659
1660 MODULE_AUTHOR("Douglas Gilbert");
1661 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1662 MODULE_LICENSE("GPL");
1663 MODULE_VERSION(SG_VERSION_STR);
1664
1665 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1666 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1667
1668 static int __init
1669 init_sg(void)
1670 {
1671         int rc;
1672
1673         if (def_reserved_size >= 0)
1674                 sg_big_buff = def_reserved_size;
1675
1676         rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), 
1677                                     SG_MAX_DEVS, "sg");
1678         if (rc)
1679                 return rc;
1680         sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1681         if ( IS_ERR(sg_sysfs_class) ) {
1682                 rc = PTR_ERR(sg_sysfs_class);
1683                 goto err_out;
1684         }
1685         sg_sysfs_valid = 1;
1686         rc = scsi_register_interface(&sg_interface);
1687         if (0 == rc) {
1688 #ifdef CONFIG_SCSI_PROC_FS
1689                 sg_proc_init();
1690 #endif                          /* CONFIG_SCSI_PROC_FS */
1691                 return 0;
1692         }
1693         class_destroy(sg_sysfs_class);
1694 err_out:
1695         unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1696         return rc;
1697 }
1698
1699 static void __exit
1700 exit_sg(void)
1701 {
1702 #ifdef CONFIG_SCSI_PROC_FS
1703         sg_proc_cleanup();
1704 #endif                          /* CONFIG_SCSI_PROC_FS */
1705         scsi_unregister_interface(&sg_interface);
1706         class_destroy(sg_sysfs_class);
1707         sg_sysfs_valid = 0;
1708         unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1709                                  SG_MAX_DEVS);
1710         if (sg_dev_arr != NULL) {
1711                 kfree((char *) sg_dev_arr);
1712                 sg_dev_arr = NULL;
1713         }
1714         sg_dev_max = 0;
1715 }
1716
1717 static int
1718 sg_start_req(Sg_request * srp)
1719 {
1720         int res;
1721         Sg_fd *sfp = srp->parentfp;
1722         sg_io_hdr_t *hp = &srp->header;
1723         int dxfer_len = (int) hp->dxfer_len;
1724         int dxfer_dir = hp->dxfer_direction;
1725         Sg_scatter_hold *req_schp = &srp->data;
1726         Sg_scatter_hold *rsv_schp = &sfp->reserve;
1727
1728         SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1729         if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1730                 return 0;
1731         if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1732             (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1733             (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1734                 res = sg_build_direct(srp, sfp, dxfer_len);
1735                 if (res <= 0)   /* -ve -> error, 0 -> done, 1 -> try indirect */
1736                         return res;
1737         }
1738         if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1739                 sg_link_reserve(sfp, srp, dxfer_len);
1740         else {
1741                 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1742                 if (res) {
1743                         sg_remove_scat(req_schp);
1744                         return res;
1745                 }
1746         }
1747         return 0;
1748 }
1749
1750 static void
1751 sg_finish_rem_req(Sg_request * srp)
1752 {
1753         Sg_fd *sfp = srp->parentfp;
1754         Sg_scatter_hold *req_schp = &srp->data;
1755
1756         SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1757         if (srp->res_used)
1758                 sg_unlink_reserve(sfp, srp);
1759         else
1760                 sg_remove_scat(req_schp);
1761         sg_remove_request(sfp, srp);
1762 }
1763
1764 static int
1765 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1766 {
1767         int ret_sz;
1768         int elem_sz = sizeof (struct scatterlist);
1769         int sg_bufflen = tablesize * elem_sz;
1770         int mx_sc_elems = tablesize;
1771
1772         schp->buffer = sg_page_malloc(sg_bufflen, sfp->low_dma, &ret_sz);
1773         if (!schp->buffer)
1774                 return -ENOMEM;
1775         else if (ret_sz != sg_bufflen) {
1776                 sg_bufflen = ret_sz;
1777                 mx_sc_elems = sg_bufflen / elem_sz;
1778         }
1779         schp->sglist_len = sg_bufflen;
1780         memset(schp->buffer, 0, sg_bufflen);
1781         return mx_sc_elems;     /* number of scat_gath elements allocated */
1782 }
1783
1784 #ifdef SG_ALLOW_DIO_CODE
1785 /* vvvvvvvv  following code borrowed from st driver's direct IO vvvvvvvvv */
1786         /* hopefully this generic code will moved to a library */
1787
1788 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1789    - mapping of all pages not successful
1790    - any page is above max_pfn
1791    (i.e., either completely successful or fails)
1792 */
1793 static int 
1794 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages, 
1795                   unsigned long uaddr, size_t count, int rw,
1796                   unsigned long max_pfn)
1797 {
1798         unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1799         unsigned long start = uaddr >> PAGE_SHIFT;
1800         const int nr_pages = end - start;
1801         int res, i, j;
1802         struct page **pages;
1803
1804         /* User attempted Overflow! */
1805         if ((uaddr + count) < uaddr)
1806                 return -EINVAL;
1807
1808         /* Too big */
1809         if (nr_pages > max_pages)
1810                 return -ENOMEM;
1811
1812         /* Hmm? */
1813         if (count == 0)
1814                 return 0;
1815
1816         if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1817                 return -ENOMEM;
1818
1819         /* Try to fault in all of the necessary pages */
1820         down_read(&current->mm->mmap_sem);
1821         /* rw==READ means read from drive, write into memory area */
1822         res = get_user_pages(
1823                 current,
1824                 current->mm,
1825                 uaddr,
1826                 nr_pages,
1827                 rw == READ,
1828                 0, /* don't force */
1829                 pages,
1830                 NULL);
1831         up_read(&current->mm->mmap_sem);
1832
1833         /* Errors and no page mapped should return here */
1834         if (res < nr_pages)
1835                 goto out_unmap;
1836
1837         for (i=0; i < nr_pages; i++) {
1838                 /* FIXME: flush superflous for rw==READ,
1839                  * probably wrong function for rw==WRITE
1840                  */
1841                 flush_dcache_page(pages[i]);
1842                 if (page_to_pfn(pages[i]) > max_pfn)
1843                         goto out_unlock;
1844                 /* ?? Is locking needed? I don't think so */
1845                 /* if (TestSetPageLocked(pages[i]))
1846                    goto out_unlock; */
1847         }
1848
1849         /* Populate the scatter/gather list */
1850         sgl[0].page = pages[0]; 
1851         sgl[0].offset = uaddr & ~PAGE_MASK;
1852         if (nr_pages > 1) {
1853                 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1854                 count -= sgl[0].length;
1855                 for (i=1; i < nr_pages ; i++) {
1856                         sgl[i].offset = 0;
1857                         sgl[i].page = pages[i]; 
1858                         sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1859                         count -= PAGE_SIZE;
1860                 }
1861         }
1862         else {
1863                 sgl[0].length = count;
1864         }
1865
1866         kfree(pages);
1867         return nr_pages;
1868
1869  out_unlock:
1870         /* for (j=0; j < i; j++)
1871            unlock_page(pages[j]); */
1872         res = 0;
1873  out_unmap:
1874         if (res > 0)
1875                 for (j=0; j < res; j++)
1876                         page_cache_release(pages[j]);
1877         kfree(pages);
1878         return res;
1879 }
1880
1881
1882 /* And unmap them... */
1883 static int 
1884 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1885                     int dirtied)
1886 {
1887         int i;
1888
1889         for (i=0; i < nr_pages; i++) {
1890                 struct page *page = sgl[i].page;
1891
1892                 /* XXX: just for debug. Remove when PageReserved is removed */
1893                 BUG_ON(PageReserved(page));
1894                 if (dirtied)
1895                         SetPageDirty(page);
1896                 /* unlock_page(page); */
1897                 /* FIXME: cache flush missing for rw==READ
1898                  * FIXME: call the correct reference counting function
1899                  */
1900                 page_cache_release(page);
1901         }
1902
1903         return 0;
1904 }
1905
1906 /* ^^^^^^^^  above code borrowed from st driver's direct IO ^^^^^^^^^ */
1907 #endif
1908
1909
1910 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1911 static int
1912 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1913 {
1914 #ifdef SG_ALLOW_DIO_CODE
1915         sg_io_hdr_t *hp = &srp->header;
1916         Sg_scatter_hold *schp = &srp->data;
1917         int sg_tablesize = sfp->parentdp->sg_tablesize;
1918         struct scatterlist *sgl;
1919         int mx_sc_elems, res;
1920         struct scsi_device *sdev = sfp->parentdp->device;
1921
1922         if (((unsigned long)hp->dxferp &
1923                         queue_dma_alignment(sdev->request_queue)) != 0)
1924                 return 1;
1925         mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1926         if (mx_sc_elems <= 0) {
1927                 return 1;
1928         }
1929         sgl = (struct scatterlist *)schp->buffer;
1930         res = st_map_user_pages(sgl, mx_sc_elems, (unsigned long)hp->dxferp, dxfer_len, 
1931                                 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0, ULONG_MAX);
1932         if (res <= 0)
1933                 return 1;
1934         schp->k_use_sg = res;
1935         schp->dio_in_use = 1;
1936         hp->info |= SG_INFO_DIRECT_IO;
1937         return 0;
1938 #else
1939         return 1;
1940 #endif
1941 }
1942
1943 static int
1944 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1945 {
1946         int ret_sz;
1947         int blk_size = buff_size;
1948         unsigned char *p = NULL;
1949
1950         if ((blk_size < 0) || (!sfp))
1951                 return -EFAULT;
1952         if (0 == blk_size)
1953                 ++blk_size;     /* don't know why */
1954 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1955         blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1956         SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1957                                    buff_size, blk_size));
1958         if (blk_size <= SG_SCATTER_SZ) {
1959                 p = sg_page_malloc(blk_size, sfp->low_dma, &ret_sz);
1960                 if (!p)
1961                         return -ENOMEM;
1962                 if (blk_size == ret_sz) {       /* got it on the first attempt */
1963                         schp->k_use_sg = 0;
1964                         schp->buffer = p;
1965                         schp->bufflen = blk_size;
1966                         schp->b_malloc_len = blk_size;
1967                         return 0;
1968                 }
1969         } else {
1970                 p = sg_page_malloc(SG_SCATTER_SZ, sfp->low_dma, &ret_sz);
1971                 if (!p)
1972                         return -ENOMEM;
1973         }
1974 /* Want some local declarations, so start new block ... */
1975         {                       /* lets try and build a scatter gather list */
1976                 struct scatterlist *sclp;
1977                 int k, rem_sz, num;
1978                 int mx_sc_elems;
1979                 int sg_tablesize = sfp->parentdp->sg_tablesize;
1980                 int first = 1;
1981
1982                 /* N.B. ret_sz carried into this block ... */
1983                 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1984                 if (mx_sc_elems < 0)
1985                         return mx_sc_elems;     /* most likely -ENOMEM */
1986
1987                 for (k = 0, sclp = schp->buffer, rem_sz = blk_size;
1988                      (rem_sz > 0) && (k < mx_sc_elems);
1989                      ++k, rem_sz -= ret_sz, ++sclp) {
1990                         if (first)
1991                                 first = 0;
1992                         else {
1993                                 num =
1994                                     (rem_sz >
1995                                      SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1996                                 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1997                                 if (!p)
1998                                         break;
1999                         }
2000                         sg_set_buf(sclp, p, ret_sz);
2001
2002                         SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
2003                                           k, sg_scatg2virt(sclp), ret_sz));
2004                 }               /* end of for loop */
2005                 schp->k_use_sg = k;
2006                 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
2007                 schp->bufflen = blk_size;
2008                 if (rem_sz > 0) /* must have failed */
2009                         return -ENOMEM;
2010         }
2011         return 0;
2012 }
2013
2014 static int
2015 sg_write_xfer(Sg_request * srp)
2016 {
2017         sg_io_hdr_t *hp = &srp->header;
2018         Sg_scatter_hold *schp = &srp->data;
2019         int num_xfer = 0;
2020         int j, k, onum, usglen, ksglen, res;
2021         int iovec_count = (int) hp->iovec_count;
2022         int dxfer_dir = hp->dxfer_direction;
2023         unsigned char *p;
2024         unsigned char __user *up;
2025         int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2026
2027         if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
2028             (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2029                 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
2030                 if (schp->bufflen < num_xfer)
2031                         num_xfer = schp->bufflen;
2032         }
2033         if ((num_xfer <= 0) || (schp->dio_in_use) ||
2034             (new_interface
2035              && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2036                 return 0;
2037
2038         SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2039                           num_xfer, iovec_count, schp->k_use_sg));
2040         if (iovec_count) {
2041                 onum = iovec_count;
2042                 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2043                         return -EFAULT;
2044         } else
2045                 onum = 1;
2046
2047         if (0 == schp->k_use_sg) {      /* kernel has single buffer */
2048                 for (j = 0, p = schp->buffer; j < onum; ++j) {
2049                         res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
2050                         if (res)
2051                                 return res;
2052                         usglen = (num_xfer > usglen) ? usglen : num_xfer;
2053                         if (__copy_from_user(p, up, usglen))
2054                                 return -EFAULT;
2055                         p += usglen;
2056                         num_xfer -= usglen;
2057                         if (num_xfer <= 0)
2058                                 return 0;
2059                 }
2060         } else {                /* kernel using scatter gather list */
2061                 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2062
2063                 ksglen = (int) sclp->length;
2064                 p = sg_scatg2virt(sclp);
2065                 for (j = 0, k = 0; j < onum; ++j) {
2066                         res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
2067                         if (res)
2068                                 return res;
2069
2070                         for (; p; ++sclp, ksglen = (int) sclp->length,
2071                                   p = sg_scatg2virt(sclp)) {
2072                                 if (usglen <= 0)
2073                                         break;
2074                                 if (ksglen > usglen) {
2075                                         if (usglen >= num_xfer) {
2076                                                 if (__copy_from_user
2077                                                     (p, up, num_xfer))
2078                                                         return -EFAULT;
2079                                                 return 0;
2080                                         }
2081                                         if (__copy_from_user(p, up, usglen))
2082                                                 return -EFAULT;
2083                                         p += usglen;
2084                                         ksglen -= usglen;
2085                                         break;
2086                                 } else {
2087                                         if (ksglen >= num_xfer) {
2088                                                 if (__copy_from_user
2089                                                     (p, up, num_xfer))
2090                                                         return -EFAULT;
2091                                                 return 0;
2092                                         }
2093                                         if (__copy_from_user(p, up, ksglen))
2094                                                 return -EFAULT;
2095                                         up += ksglen;
2096                                         usglen -= ksglen;
2097                                 }
2098                                 ++k;
2099                                 if (k >= schp->k_use_sg)
2100                                         return 0;
2101                         }
2102                 }
2103         }
2104         return 0;
2105 }
2106
2107 static int
2108 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
2109            int wr_xf, int *countp, unsigned char __user **up)
2110 {
2111         int num_xfer = (int) hp->dxfer_len;
2112         unsigned char __user *p = hp->dxferp;
2113         int count;
2114
2115         if (0 == sg_num) {
2116                 if (wr_xf && ('\0' == hp->interface_id))
2117                         count = (int) hp->flags;        /* holds "old" input_size */
2118                 else
2119                         count = num_xfer;
2120         } else {
2121                 sg_iovec_t iovec;
2122                 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2123                         return -EFAULT;
2124                 p = iovec.iov_base;
2125                 count = (int) iovec.iov_len;
2126         }
2127         if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2128                 return -EFAULT;
2129         if (up)
2130                 *up = p;
2131         if (countp)
2132                 *countp = count;
2133         return 0;
2134 }
2135
2136 static void
2137 sg_remove_scat(Sg_scatter_hold * schp)
2138 {
2139         SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2140         if (schp->buffer && (schp->sglist_len > 0)) {
2141                 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2142
2143                 if (schp->dio_in_use) {
2144 #ifdef SG_ALLOW_DIO_CODE
2145                         st_unmap_user_pages(sclp, schp->k_use_sg, TRUE);
2146 #endif
2147                 } else {
2148                         int k;
2149
2150                         for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2151                              ++k, ++sclp) {
2152                                 SCSI_LOG_TIMEOUT(5, printk(
2153                                     "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2154                                     k, sg_scatg2virt(sclp), sclp->length));
2155                                 sg_page_free(sg_scatg2virt(sclp), sclp->length);
2156                                 sclp->page = NULL;
2157                                 sclp->offset = 0;
2158                                 sclp->length = 0;
2159                         }
2160                 }
2161                 sg_page_free(schp->buffer, schp->sglist_len);
2162         } else if (schp->buffer)
2163                 sg_page_free(schp->buffer, schp->b_malloc_len);
2164         memset(schp, 0, sizeof (*schp));
2165 }
2166
2167 static int
2168 sg_read_xfer(Sg_request * srp)
2169 {
2170         sg_io_hdr_t *hp = &srp->header;
2171         Sg_scatter_hold *schp = &srp->data;
2172         int num_xfer = 0;
2173         int j, k, onum, usglen, ksglen, res;
2174         int iovec_count = (int) hp->iovec_count;
2175         int dxfer_dir = hp->dxfer_direction;
2176         unsigned char *p;
2177         unsigned char __user *up;
2178         int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2179
2180         if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2181             || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2182                 num_xfer = hp->dxfer_len;
2183                 if (schp->bufflen < num_xfer)
2184                         num_xfer = schp->bufflen;
2185         }
2186         if ((num_xfer <= 0) || (schp->dio_in_use) ||
2187             (new_interface
2188              && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2189                 return 0;
2190
2191         SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2192                           num_xfer, iovec_count, schp->k_use_sg));
2193         if (iovec_count) {
2194                 onum = iovec_count;
2195                 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2196                         return -EFAULT;
2197         } else
2198                 onum = 1;
2199
2200         if (0 == schp->k_use_sg) {      /* kernel has single buffer */
2201                 for (j = 0, p = schp->buffer; j < onum; ++j) {
2202                         res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2203                         if (res)
2204                                 return res;
2205                         usglen = (num_xfer > usglen) ? usglen : num_xfer;
2206                         if (__copy_to_user(up, p, usglen))
2207                                 return -EFAULT;
2208                         p += usglen;
2209                         num_xfer -= usglen;
2210                         if (num_xfer <= 0)
2211                                 return 0;
2212                 }
2213         } else {                /* kernel using scatter gather list */
2214                 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2215
2216                 ksglen = (int) sclp->length;
2217                 p = sg_scatg2virt(sclp);
2218                 for (j = 0, k = 0; j < onum; ++j) {
2219                         res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2220                         if (res)
2221                                 return res;
2222
2223                         for (; p; ++sclp, ksglen = (int) sclp->length,
2224                                   p = sg_scatg2virt(sclp)) {
2225                                 if (usglen <= 0)
2226                                         break;
2227                                 if (ksglen > usglen) {
2228                                         if (usglen >= num_xfer) {
2229                                                 if (__copy_to_user
2230                                                     (up, p, num_xfer))
2231                                                         return -EFAULT;
2232                                                 return 0;
2233                                         }
2234                                         if (__copy_to_user(up, p, usglen))
2235                                                 return -EFAULT;
2236                                         p += usglen;
2237                                         ksglen -= usglen;
2238                                         break;
2239                                 } else {
2240                                         if (ksglen >= num_xfer) {
2241                                                 if (__copy_to_user
2242                                                     (up, p, num_xfer))
2243                                                         return -EFAULT;
2244                                                 return 0;
2245                                         }
2246                                         if (__copy_to_user(up, p, ksglen))
2247                                                 return -EFAULT;
2248                                         up += ksglen;
2249                                         usglen -= ksglen;
2250                                 }
2251                                 ++k;
2252                                 if (k >= schp->k_use_sg)
2253                                         return 0;
2254                         }
2255                 }
2256         }
2257         return 0;
2258 }
2259
2260 static int
2261 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2262 {
2263         Sg_scatter_hold *schp = &srp->data;
2264
2265         SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2266                                    num_read_xfer));
2267         if ((!outp) || (num_read_xfer <= 0))
2268                 return 0;
2269         if (schp->k_use_sg > 0) {
2270                 int k, num;
2271                 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2272
2273                 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2274                      ++k, ++sclp) {
2275                         num = (int) sclp->length;
2276                         if (num > num_read_xfer) {
2277                                 if (__copy_to_user
2278                                     (outp, sg_scatg2virt(sclp), num_read_xfer))
2279                                         return -EFAULT;
2280                                 break;
2281                         } else {
2282                                 if (__copy_to_user
2283                                     (outp, sg_scatg2virt(sclp), num))
2284                                         return -EFAULT;
2285                                 num_read_xfer -= num;
2286                                 if (num_read_xfer <= 0)
2287                                         break;
2288                                 outp += num;
2289                         }
2290                 }
2291         } else {
2292                 if (__copy_to_user(outp, schp->buffer, num_read_xfer))
2293                         return -EFAULT;
2294         }
2295         return 0;
2296 }
2297
2298 static void
2299 sg_build_reserve(Sg_fd * sfp, int req_size)
2300 {
2301         Sg_scatter_hold *schp = &sfp->reserve;
2302
2303         SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2304         do {
2305                 if (req_size < PAGE_SIZE)
2306                         req_size = PAGE_SIZE;
2307                 if (0 == sg_build_indirect(schp, sfp, req_size))
2308                         return;
2309                 else
2310                         sg_remove_scat(schp);
2311                 req_size >>= 1; /* divide by 2 */
2312         } while (req_size > (PAGE_SIZE / 2));
2313 }
2314
2315 static void
2316 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2317 {
2318         Sg_scatter_hold *req_schp = &srp->data;
2319         Sg_scatter_hold *rsv_schp = &sfp->reserve;
2320
2321         srp->res_used = 1;
2322         SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2323         size = (size + 1) & (~1);       /* round to even for aha1542 */
2324         if (rsv_schp->k_use_sg > 0) {
2325                 int k, num;
2326                 int rem = size;
2327                 struct scatterlist *sclp =
2328                     (struct scatterlist *) rsv_schp->buffer;
2329
2330                 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
2331                         num = (int) sclp->length;
2332                         if (rem <= num) {
2333                                 if (0 == k) {
2334                                         req_schp->k_use_sg = 0;
2335                                         req_schp->buffer = sg_scatg2virt(sclp);
2336                                 } else {
2337                                         sfp->save_scat_len = num;
2338                                         sclp->length = (unsigned) rem;
2339                                         req_schp->k_use_sg = k + 1;
2340                                         req_schp->sglist_len =
2341                                             rsv_schp->sglist_len;
2342                                         req_schp->buffer = rsv_schp->buffer;
2343                                 }
2344                                 req_schp->bufflen = size;
2345                                 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2346                                 break;
2347                         } else
2348                                 rem -= num;
2349                 }
2350                 if (k >= rsv_schp->k_use_sg)
2351                         SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2352         } else {
2353                 req_schp->k_use_sg = 0;
2354                 req_schp->bufflen = size;
2355                 req_schp->buffer = rsv_schp->buffer;
2356                 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2357         }
2358 }
2359
2360 static void
2361 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2362 {
2363         Sg_scatter_hold *req_schp = &srp->data;
2364         Sg_scatter_hold *rsv_schp = &sfp->reserve;
2365
2366         SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2367                                    (int) req_schp->k_use_sg));
2368         if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2369                 struct scatterlist *sclp =
2370                     (struct scatterlist *) rsv_schp->buffer;
2371
2372                 if (sfp->save_scat_len > 0)
2373                         (sclp + (req_schp->k_use_sg - 1))->length =
2374                             (unsigned) sfp->save_scat_len;
2375                 else
2376                         SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2377         }
2378         req_schp->k_use_sg = 0;
2379         req_schp->bufflen = 0;
2380         req_schp->buffer = NULL;
2381         req_schp->sglist_len = 0;
2382         sfp->save_scat_len = 0;
2383         srp->res_used = 0;
2384 }
2385
2386 static Sg_request *
2387 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2388 {
2389         Sg_request *resp;
2390         unsigned long iflags;
2391
2392         write_lock_irqsave(&sfp->rq_list_lock, iflags);
2393         for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2394                 /* look for requests that are ready + not SG_IO owned */
2395                 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2396                     ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2397                         resp->done = 2; /* guard against other readers */
2398                         break;
2399                 }
2400         }
2401         write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2402         return resp;
2403 }
2404
2405 #ifdef CONFIG_SCSI_PROC_FS
2406 static Sg_request *
2407 sg_get_nth_request(Sg_fd * sfp, int nth)
2408 {
2409         Sg_request *resp;
2410         unsigned long iflags;
2411         int k;
2412
2413         read_lock_irqsave(&sfp->rq_list_lock, iflags);
2414         for (k = 0, resp = sfp->headrp; resp && (k < nth);
2415              ++k, resp = resp->nextrp) ;
2416         read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2417         return resp;
2418 }
2419 #endif
2420
2421 /* always adds to end of list */
2422 static Sg_request *
2423 sg_add_request(Sg_fd * sfp)
2424 {
2425         int k;
2426         unsigned long iflags;
2427         Sg_request *resp;
2428         Sg_request *rp = sfp->req_arr;
2429
2430         write_lock_irqsave(&sfp->rq_list_lock, iflags);
2431         resp = sfp->headrp;
2432         if (!resp) {
2433                 memset(rp, 0, sizeof (Sg_request));
2434                 rp->parentfp = sfp;
2435                 resp = rp;
2436                 sfp->headrp = resp;
2437         } else {
2438                 if (0 == sfp->cmd_q)
2439                         resp = NULL;    /* command queuing disallowed */
2440                 else {
2441                         for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2442                                 if (!rp->parentfp)
2443                                         break;
2444                         }
2445                         if (k < SG_MAX_QUEUE) {
2446                                 memset(rp, 0, sizeof (Sg_request));
2447                                 rp->parentfp = sfp;
2448                                 while (resp->nextrp)
2449                                         resp = resp->nextrp;
2450                                 resp->nextrp = rp;
2451                                 resp = rp;
2452                         } else
2453                                 resp = NULL;
2454                 }
2455         }
2456         if (resp) {
2457                 resp->nextrp = NULL;
2458                 resp->header.duration = jiffies_to_msecs(jiffies);
2459                 resp->my_cmdp = NULL;
2460         }
2461         write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2462         return resp;
2463 }
2464
2465 /* Return of 1 for found; 0 for not found */
2466 static int
2467 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2468 {
2469         Sg_request *prev_rp;
2470         Sg_request *rp;
2471         unsigned long iflags;
2472         int res = 0;
2473
2474         if ((!sfp) || (!srp) || (!sfp->headrp))
2475                 return res;
2476         write_lock_irqsave(&sfp->rq_list_lock, iflags);
2477         if (srp->my_cmdp)
2478                 srp->my_cmdp->upper_private_data = NULL;
2479         prev_rp = sfp->headrp;
2480         if (srp == prev_rp) {
2481                 sfp->headrp = prev_rp->nextrp;
2482                 prev_rp->parentfp = NULL;
2483                 res = 1;
2484         } else {
2485                 while ((rp = prev_rp->nextrp)) {
2486                         if (srp == rp) {
2487                                 prev_rp->nextrp = rp->nextrp;
2488                                 rp->parentfp = NULL;
2489                                 res = 1;
2490                                 break;
2491                         }
2492                         prev_rp = rp;
2493                 }
2494         }
2495         write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2496         return res;
2497 }
2498
2499 #ifdef CONFIG_SCSI_PROC_FS
2500 static Sg_fd *
2501 sg_get_nth_sfp(Sg_device * sdp, int nth)
2502 {
2503         Sg_fd *resp;
2504         unsigned long iflags;
2505         int k;
2506
2507         read_lock_irqsave(&sg_dev_arr_lock, iflags);
2508         for (k = 0, resp = sdp->headfp; resp && (k < nth);
2509              ++k, resp = resp->nextfp) ;
2510         read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2511         return resp;
2512 }
2513 #endif
2514
2515 static Sg_fd *
2516 sg_add_sfp(Sg_device * sdp, int dev)
2517 {
2518         Sg_fd *sfp;
2519         unsigned long iflags;
2520
2521         sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
2522         if (!sfp)
2523                 return NULL;
2524         memset(sfp, 0, sizeof (Sg_fd));
2525         init_waitqueue_head(&sfp->read_wait);
2526         rwlock_init(&sfp->rq_list_lock);
2527
2528         sfp->timeout = SG_DEFAULT_TIMEOUT;
2529         sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2530         sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2531         sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2532             sdp->device->host->unchecked_isa_dma : 1;
2533         sfp->cmd_q = SG_DEF_COMMAND_Q;
2534         sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2535         sfp->parentdp = sdp;
2536         write_lock_irqsave(&sg_dev_arr_lock, iflags);
2537         if (!sdp->headfp)
2538                 sdp->headfp = sfp;
2539         else {                  /* add to tail of existing list */
2540                 Sg_fd *pfp = sdp->headfp;
2541                 while (pfp->nextfp)
2542                         pfp = pfp->nextfp;
2543                 pfp->nextfp = sfp;
2544         }
2545         write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2546         SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2547         sg_build_reserve(sfp, sg_big_buff);
2548         SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp:   bufflen=%d, k_use_sg=%d\n",
2549                            sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2550         return sfp;
2551 }
2552
2553 static void
2554 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2555 {
2556         Sg_fd *fp;
2557         Sg_fd *prev_fp;
2558
2559         prev_fp = sdp->headfp;
2560         if (sfp == prev_fp)
2561                 sdp->headfp = prev_fp->nextfp;
2562         else {
2563                 while ((fp = prev_fp->nextfp)) {
2564                         if (sfp == fp) {
2565                                 prev_fp->nextfp = fp->nextfp;
2566                                 break;
2567                         }
2568                         prev_fp = fp;
2569                 }
2570         }
2571         if (sfp->reserve.bufflen > 0) {
2572                 SCSI_LOG_TIMEOUT(6, 
2573                         printk("__sg_remove_sfp:    bufflen=%d, k_use_sg=%d\n",
2574                         (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2575                 if (sfp->mmap_called)
2576                         sg_rb_correct4mmap(&sfp->reserve, 0);   /* undo correction */
2577                 sg_remove_scat(&sfp->reserve);
2578         }
2579         sfp->parentdp = NULL;
2580         SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp:    sfp=0x%p\n", sfp));
2581         sg_page_free((char *) sfp, sizeof (Sg_fd));
2582 }
2583
2584 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2585 static int
2586 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2587 {
2588         Sg_request *srp;
2589         Sg_request *tsrp;
2590         int dirty = 0;
2591         int res = 0;
2592
2593         for (srp = sfp->headrp; srp; srp = tsrp) {
2594                 tsrp = srp->nextrp;
2595                 if (sg_srp_done(srp, sfp))
2596                         sg_finish_rem_req(srp);
2597                 else
2598                         ++dirty;
2599         }
2600         if (0 == dirty) {
2601                 unsigned long iflags;
2602
2603                 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2604                 __sg_remove_sfp(sdp, sfp);
2605                 if (sdp->detached && (NULL == sdp->headfp)) {
2606                         int k, maxd;
2607
2608                         maxd = sg_dev_max;
2609                         for (k = 0; k < maxd; ++k) {
2610                                 if (sdp == sg_dev_arr[k])
2611                                         break;
2612                         }
2613                         if (k < maxd)
2614                                 sg_dev_arr[k] = NULL;
2615                         kfree((char *) sdp);
2616                         res = 1;
2617                 }
2618                 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2619         } else {
2620                 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2621                 /* only bump the access_count if we actually succeeded in
2622                  * throwing another counter on the host module */
2623                 scsi_device_get(sdp->device);   /* XXX: retval ignored? */      
2624                 sfp->closed = 1;        /* flag dirty state on this fd */
2625                 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2626                                   dirty));
2627         }
2628         return res;
2629 }
2630
2631 static int
2632 sg_res_in_use(Sg_fd * sfp)
2633 {
2634         const Sg_request *srp;
2635         unsigned long iflags;
2636
2637         read_lock_irqsave(&sfp->rq_list_lock, iflags);
2638         for (srp = sfp->headrp; srp; srp = srp->nextrp)
2639                 if (srp->res_used)
2640                         break;
2641         read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2642         return srp ? 1 : 0;
2643 }
2644
2645 /* If retSzp==NULL want exact size or fail */
2646 static char *
2647 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2648 {
2649         char *resp = NULL;
2650         gfp_t page_mask;
2651         int order, a_size;
2652         int resSz = rqSz;
2653
2654         if (rqSz <= 0)
2655                 return resp;
2656
2657         if (lowDma)
2658                 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2659         else
2660                 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2661
2662         for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2663              order++, a_size <<= 1) ;
2664         resp = (char *) __get_free_pages(page_mask, order);
2665         while ((!resp) && order && retSzp) {
2666                 --order;
2667                 a_size >>= 1;   /* divide by 2, until PAGE_SIZE */
2668                 resp = (char *) __get_free_pages(page_mask, order);     /* try half */
2669                 resSz = a_size;
2670         }
2671         if (resp) {
2672                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2673                         memset(resp, 0, resSz);
2674                 if (retSzp)
2675                         *retSzp = resSz;
2676         }
2677         return resp;
2678 }
2679
2680 static void
2681 sg_page_free(char *buff, int size)
2682 {
2683         int order, a_size;
2684
2685         if (!buff)
2686                 return;
2687         for (order = 0, a_size = PAGE_SIZE; a_size < size;
2688              order++, a_size <<= 1) ;
2689         free_pages((unsigned long) buff, order);
2690 }
2691
2692 #ifndef MAINTENANCE_IN_CMD
2693 #define MAINTENANCE_IN_CMD 0xa3
2694 #endif
2695
2696 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2697         INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2698         READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2699         SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2700 };
2701
2702 static int
2703 sg_allow_access(unsigned char opcode, char dev_type)
2704 {
2705         int k;
2706
2707         if (TYPE_SCANNER == dev_type)   /* TYPE_ROM maybe burner */
2708                 return 1;
2709         for (k = 0; k < sizeof (allow_ops); ++k) {
2710                 if (opcode == allow_ops[k])
2711                         return 1;
2712         }
2713         return 0;
2714 }
2715
2716 #ifdef CONFIG_SCSI_PROC_FS
2717 static int
2718 sg_last_dev(void)
2719 {
2720         int k;
2721         unsigned long iflags;
2722
2723         read_lock_irqsave(&sg_dev_arr_lock, iflags);
2724         for (k = sg_dev_max - 1; k >= 0; --k)
2725                 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2726                         break;
2727         read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2728         return k + 1;           /* origin 1 */
2729 }
2730 #endif
2731
2732 static Sg_device *
2733 sg_get_dev(int dev)
2734 {
2735         Sg_device *sdp = NULL;
2736         unsigned long iflags;
2737
2738         if (sg_dev_arr && (dev >= 0)) {
2739                 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2740                 if (dev < sg_dev_max)
2741                         sdp = sg_dev_arr[dev];
2742                 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2743         }
2744         return sdp;
2745 }
2746
2747 #ifdef CONFIG_SCSI_PROC_FS
2748
2749 static struct proc_dir_entry *sg_proc_sgp = NULL;
2750
2751 static char sg_proc_sg_dirname[] = "scsi/sg";
2752
2753 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2754
2755 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2756 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2757                                   size_t count, loff_t *off);
2758 static struct file_operations adio_fops = {
2759         /* .owner, .read and .llseek added in sg_proc_init() */
2760         .open = sg_proc_single_open_adio,
2761         .write = sg_proc_write_adio,
2762         .release = single_release,
2763 };
2764
2765 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2766 static ssize_t sg_proc_write_dressz(struct file *filp, 
2767                 const char __user *buffer, size_t count, loff_t *off);
2768 static struct file_operations dressz_fops = {
2769         .open = sg_proc_single_open_dressz,
2770         .write = sg_proc_write_dressz,
2771         .release = single_release,
2772 };
2773
2774 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2775 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2776 static struct file_operations version_fops = {
2777         .open = sg_proc_single_open_version,
2778         .release = single_release,
2779 };
2780
2781 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2782 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2783 static struct file_operations devhdr_fops = {
2784         .open = sg_proc_single_open_devhdr,
2785         .release = single_release,
2786 };
2787
2788 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2789 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2790 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2791 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2792 static void dev_seq_stop(struct seq_file *s, void *v);
2793 static struct file_operations dev_fops = {
2794         .open = sg_proc_open_dev,
2795         .release = seq_release,
2796 };
2797 static struct seq_operations dev_seq_ops = {
2798         .start = dev_seq_start,
2799         .next  = dev_seq_next,
2800         .stop  = dev_seq_stop,
2801         .show  = sg_proc_seq_show_dev,
2802 };
2803
2804 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2805 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2806 static struct file_operations devstrs_fops = {
2807         .open = sg_proc_open_devstrs,
2808         .release = seq_release,
2809 };
2810 static struct seq_operations devstrs_seq_ops = {
2811         .start = dev_seq_start,
2812         .next  = dev_seq_next,
2813         .stop  = dev_seq_stop,
2814         .show  = sg_proc_seq_show_devstrs,
2815 };
2816
2817 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2818 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2819 static struct file_operations debug_fops = {
2820         .open = sg_proc_open_debug,
2821         .release = seq_release,
2822 };
2823 static struct seq_operations debug_seq_ops = {
2824         .start = dev_seq_start,
2825         .next  = dev_seq_next,
2826         .stop  = dev_seq_stop,
2827         .show  = sg_proc_seq_show_debug,
2828 };
2829
2830
2831 struct sg_proc_leaf {
2832         const char * name;
2833         struct file_operations * fops;
2834 };
2835
2836 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2837         {"allow_dio", &adio_fops},
2838         {"debug", &debug_fops},
2839         {"def_reserved_size", &dressz_fops},
2840         {"device_hdr", &devhdr_fops},
2841         {"devices", &dev_fops},
2842         {"device_strs", &devstrs_fops},
2843         {"version", &version_fops}
2844 };
2845
2846 static int
2847 sg_proc_init(void)
2848 {
2849         int k, mask;
2850         int num_leaves =
2851             sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2852         struct proc_dir_entry *pdep;
2853         struct sg_proc_leaf * leaf;
2854
2855         sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2856         if (!sg_proc_sgp)
2857                 return 1;
2858         for (k = 0; k < num_leaves; ++k) {
2859                 leaf = &sg_proc_leaf_arr[k];
2860                 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2861                 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2862                 if (pdep) {
2863                         leaf->fops->owner = THIS_MODULE,
2864                         leaf->fops->read = seq_read,
2865                         leaf->fops->llseek = seq_lseek,
2866                         pdep->proc_fops = leaf->fops;
2867                 }
2868         }
2869         return 0;
2870 }
2871
2872 static void
2873 sg_proc_cleanup(void)
2874 {
2875         int k;
2876         int num_leaves =
2877             sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2878
2879         if (!sg_proc_sgp)
2880                 return;
2881         for (k = 0; k < num_leaves; ++k)
2882                 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2883         remove_proc_entry(sg_proc_sg_dirname, NULL);
2884 }
2885
2886
2887 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2888 {
2889         seq_printf(s, "%d\n", *((int *)s->private));
2890         return 0;
2891 }
2892
2893 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2894 {
2895         return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2896 }
2897
2898 static ssize_t 
2899 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2900                    size_t count, loff_t *off)
2901 {
2902         int num;
2903         char buff[11];
2904
2905         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2906                 return -EACCES;
2907         num = (count < 10) ? count : 10;
2908         if (copy_from_user(buff, buffer, num))
2909                 return -EFAULT;
2910         buff[num] = '\0';
2911         sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2912         return count;
2913 }
2914
2915 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2916 {
2917         return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2918 }
2919
2920 static ssize_t 
2921 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2922                      size_t count, loff_t *off)
2923 {
2924         int num;
2925         unsigned long k = ULONG_MAX;
2926         char buff[11];
2927
2928         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2929                 return -EACCES;
2930         num = (count < 10) ? count : 10;
2931         if (copy_from_user(buff, buffer, num))
2932                 return -EFAULT;
2933         buff[num] = '\0';
2934         k = simple_strtoul(buff, NULL, 10);
2935         if (k <= 1048576) {     /* limit "big buff" to 1 MB */
2936                 sg_big_buff = k;
2937                 return count;
2938         }
2939         return -ERANGE;
2940 }
2941
2942 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2943 {
2944         seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2945                    sg_version_date);
2946         return 0;
2947 }
2948
2949 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2950 {
2951         return single_open(file, sg_proc_seq_show_version, NULL);
2952 }
2953
2954 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2955 {
2956         seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2957                    "online\n");
2958         return 0;
2959 }
2960
2961 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2962 {
2963         return single_open(file, sg_proc_seq_show_devhdr, NULL);
2964 }
2965
2966 struct sg_proc_deviter {
2967         loff_t  index;
2968         size_t  max;
2969 };
2970
2971 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2972 {
2973         struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2974
2975         s->private = it;
2976         if (! it)
2977                 return NULL;
2978
2979         if (NULL == sg_dev_arr)
2980                 return NULL;
2981         it->index = *pos;
2982         it->max = sg_last_dev();
2983         if (it->index >= it->max)
2984                 return NULL;
2985         return it;
2986 }
2987
2988 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2989 {
2990         struct sg_proc_deviter * it = s->private;
2991
2992         *pos = ++it->index;
2993         return (it->index < it->max) ? it : NULL;
2994 }
2995
2996 static void dev_seq_stop(struct seq_file *s, void *v)
2997 {
2998         kfree(s->private);
2999 }
3000
3001 static int sg_proc_open_dev(struct inode *inode, struct file *file)
3002 {
3003         return seq_open(file, &dev_seq_ops);
3004 }
3005
3006 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
3007 {
3008         struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3009         Sg_device *sdp;
3010         struct scsi_device *scsidp;
3011
3012         sdp = it ? sg_get_dev(it->index) : NULL;
3013         if (sdp && (scsidp = sdp->device) && (!sdp->detached))
3014                 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
3015                               scsidp->host->host_no, scsidp->channel,
3016                               scsidp->id, scsidp->lun, (int) scsidp->type,
3017                               1,
3018                               (int) scsidp->queue_depth,
3019                               (int) scsidp->device_busy,
3020                               (int) scsi_device_online(scsidp));
3021         else
3022                 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
3023         return 0;
3024 }
3025
3026 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
3027 {
3028         return seq_open(file, &devstrs_seq_ops);
3029 }
3030
3031 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
3032 {
3033         struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3034         Sg_device *sdp;
3035         struct scsi_device *scsidp;
3036
3037         sdp = it ? sg_get_dev(it->index) : NULL;
3038         if (sdp && (scsidp = sdp->device) && (!sdp->detached))
3039                 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
3040                            scsidp->vendor, scsidp->model, scsidp->rev);
3041         else
3042                 seq_printf(s, "<no active device>\n");
3043         return 0;
3044 }
3045
3046 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
3047 {
3048         int k, m, new_interface, blen, usg;
3049         Sg_request *srp;
3050         Sg_fd *fp;
3051         const sg_io_hdr_t *hp;
3052         const char * cp;
3053         unsigned int ms;
3054
3055         for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
3056                 seq_printf(s, "   FD(%d): timeout=%dms bufflen=%d "
3057                            "(res)sgat=%d low_dma=%d\n", k + 1,
3058                            jiffies_to_msecs(fp->timeout),
3059                            fp->reserve.bufflen,
3060                            (int) fp->reserve.k_use_sg,
3061                            (int) fp->low_dma);
3062                 seq_printf(s, "   cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
3063                            (int) fp->cmd_q, (int) fp->force_packid,
3064                            (int) fp->keep_orphan, (int) fp->closed);
3065                 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
3066                         hp = &srp->header;
3067                         new_interface = (hp->interface_id == '\0') ? 0 : 1;
3068                         if (srp->res_used) {
3069                                 if (new_interface && 
3070                                     (SG_FLAG_MMAP_IO & hp->flags))
3071                                         cp = "     mmap>> ";
3072                                 else
3073                                         cp = "     rb>> ";
3074                         } else {
3075                                 if (SG_INFO_DIRECT_IO_MASK & hp->info)
3076                                         cp = "     dio>> ";
3077                                 else
3078                                         cp = "     ";
3079                         }
3080                         seq_printf(s, cp);
3081                         blen = srp->my_cmdp ? 
3082                                 srp->my_cmdp->sr_bufflen : srp->data.bufflen;
3083                         usg = srp->my_cmdp ? 
3084                                 srp->my_cmdp->sr_use_sg : srp->data.k_use_sg;
3085                         seq_printf(s, srp->done ? 
3086                                    ((1 == srp->done) ?  "rcv:" : "fin:")
3087                                    : (srp->my_cmdp ? "act:" : "prior:"));
3088                         seq_printf(s, " id=%d blen=%d",
3089                                    srp->header.pack_id, blen);
3090                         if (srp->done)
3091                                 seq_printf(s, " dur=%d", hp->duration);
3092                         else {
3093                                 ms = jiffies_to_msecs(jiffies);
3094                                 seq_printf(s, " t_o/elap=%d/%d",
3095                                         (new_interface ? hp->timeout :
3096                                                   jiffies_to_msecs(fp->timeout)),
3097                                         (ms > hp->duration ? ms - hp->duration : 0));
3098                         }
3099                         seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
3100                                    (int) srp->data.cmd_opcode);
3101                 }
3102                 if (0 == m)
3103                         seq_printf(s, "     No requests active\n");
3104         }
3105 }
3106
3107 static int sg_proc_open_debug(struct inode *inode, struct file *file)
3108 {
3109         return seq_open(file, &debug_seq_ops);
3110 }
3111
3112 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
3113 {
3114         struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3115         Sg_device *sdp;
3116
3117         if (it && (0 == it->index)) {
3118                 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
3119                            "(origin 1)\n", sg_dev_max, (int)it->max);
3120                 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
3121         }
3122         sdp = it ? sg_get_dev(it->index) : NULL;
3123         if (sdp) {
3124                 struct scsi_device *scsidp = sdp->device;
3125
3126                 if (NULL == scsidp) {
3127                         seq_printf(s, "device %d detached ??\n", 
3128                                    (int)it->index);
3129                         return 0;
3130                 }
3131
3132                 if (sg_get_nth_sfp(sdp, 0)) {
3133                         seq_printf(s, " >>> device=%s ",
3134                                 sdp->disk->disk_name);
3135                         if (sdp->detached)
3136                                 seq_printf(s, "detached pending close ");
3137                         else
3138                                 seq_printf
3139                                     (s, "scsi%d chan=%d id=%d lun=%d   em=%d",
3140                                      scsidp->host->host_no,
3141                                      scsidp->channel, scsidp->id,
3142                                      scsidp->lun,
3143                                      scsidp->host->hostt->emulated);
3144                         seq_printf(s, " sg_tablesize=%d excl=%d\n",
3145                                    sdp->sg_tablesize, sdp->exclude);
3146                 }
3147                 sg_proc_debug_helper(s, sdp);
3148         }
3149         return 0;
3150 }
3151
3152 #endif                          /* CONFIG_SCSI_PROC_FS */
3153
3154 module_init(init_sg);
3155 module_exit(exit_sg);
3156 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);