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