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