Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  History:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *  Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *           to allow user process control of SCSI devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Development Sponsored by Killy Corp. NY NY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Original driver (sg.c):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *        Copyright (C) 1992 Lawrence Foard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Version 2 and 3 extensions to driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *        Copyright (C) 1998 - 2014 Douglas Gilbert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) static int sg_version_num = 30536;	/* 2 digits for each component */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #define SG_VERSION_STR "3.5.36"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *  D. P. Gilbert (dgilbert@interlog.com), notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *      - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *        the kernel/module needs to be built with CONFIG_SCSI_LOGGING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *        (otherwise the macros compile to empty statements).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/mtio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/blktrace_api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <linux/cred.h> /* for sg_check_file_access() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include "scsi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #include <scsi/scsi_dbg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include <scsi/scsi_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include <scsi/scsi_ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #include <scsi/sg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #include "scsi_logging.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #ifdef CONFIG_SCSI_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static char *sg_version_date = "20140603";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static int sg_proc_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define SG_ALLOW_DIO_DEF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define SG_MAX_DEVS 32768
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) /* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * than 16 bytes are "variable length" whose length is a multiple of 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define SG_MAX_CDB_SIZE 252
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define SG_DEFAULT_TIMEOUT mult_frac(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) int sg_big_buff = SG_DEF_RESERVED_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) /* N.B. This variable is readable and writeable via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)    /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)    of this size (or less if there is not enough memory) will be reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)    for use by this file descriptor. [Deprecated usage: this variable is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)    readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)    the kernel (i.e. it is not a module).] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) static int def_reserved_size = -1;	/* picks up init parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) static int sg_allow_dio = SG_ALLOW_DIO_DEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) static int scatter_elem_sz = SG_SCATTER_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static int scatter_elem_sz_prev = SG_SCATTER_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define SG_SECTOR_SZ 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) static int sg_add_device(struct device *, struct class_interface *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static void sg_remove_device(struct device *, struct class_interface *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) static DEFINE_IDR(sg_index_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) static DEFINE_RWLOCK(sg_index_lock);	/* Also used to lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 							   file descriptor list for device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) static struct class_interface sg_interface = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	.add_dev        = sg_add_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	.remove_dev     = sg_remove_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	unsigned bufflen;	/* Size of (aggregate) data buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	struct page **pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	int page_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	char dio_in_use;	/* 0->indirect IO (or mmap), 1->dio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	unsigned char cmd_opcode; /* first byte of command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) } Sg_scatter_hold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) struct sg_device;		/* forward declarations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) struct sg_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) typedef struct sg_request {	/* SG_MAX_QUEUE requests outstanding per file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct list_head entry;	/* list entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct sg_fd *parentfp;	/* NULL -> not in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	Sg_scatter_hold data;	/* hold buffer, perhaps scatter list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	sg_io_hdr_t header;	/* scsi command+info, see <scsi/sg.h> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	char res_used;		/* 1 -> using reserve buffer, 0 -> not ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	char orphan;		/* 1 -> drop on sight, 0 -> normal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	char sg_io_owned;	/* 1 -> packet belongs to SG_IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	/* done protected by rq_list_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	char done;		/* 0->before bh, 1->before read, 2->read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct execute_work ew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) } Sg_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) typedef struct sg_fd {		/* holds the state of a file descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct list_head sfd_siblings;  /* protected by device's sfd_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct sg_device *parentdp;	/* owning device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	wait_queue_head_t read_wait;	/* queue read until command done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	rwlock_t rq_list_lock;	/* protect access to list in req_arr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct mutex f_mutex;	/* protect against changes in this fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	int timeout;		/* defaults to SG_DEFAULT_TIMEOUT      */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	int timeout_user;	/* defaults to SG_DEFAULT_TIMEOUT_USER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	Sg_scatter_hold reserve;	/* buffer held for this file descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct list_head rq_list; /* head of request list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	struct fasync_struct *async_qp;	/* used by asynchronous notification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	Sg_request req_arr[SG_MAX_QUEUE];	/* used as singly-linked list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	char force_packid;	/* 1 -> pack_id input to read(), 0 -> ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	char cmd_q;		/* 1 -> allow command queuing, 0 -> don't */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	char keep_orphan;	/* 0 -> drop orphan (def), 1 -> keep for read() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	char mmap_called;	/* 0 -> mmap() never called on this fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	char res_in_use;	/* 1 -> 'reserve' array in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	struct kref f_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct execute_work ew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) } Sg_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) typedef struct sg_device { /* holds the state of each scsi generic device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	struct scsi_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	wait_queue_head_t open_wait;    /* queue open() when O_EXCL present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct mutex open_rel_lock;     /* held when in open() or release() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	int sg_tablesize;	/* adapter's max scatter-gather table size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	u32 index;		/* device index number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct list_head sfds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	rwlock_t sfd_lock;      /* protect access to sfd list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	atomic_t detaching;     /* 0->device usable, 1->device detaching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	bool exclude;		/* 1->open(O_EXCL) succeeded and is active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	int open_cnt;		/* count of opens (perhaps < num(sfds) ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	char sgdebug;		/* 0->off, 1->sense, 9->dump dev, 10-> all devs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	struct cdev * cdev;	/* char_dev [sysfs: /sys/cdev/major/sg<n>] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	struct kref d_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) } Sg_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /* tasklet or soft irq callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) static void sg_rq_end_io(struct request *rq, blk_status_t status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static int sg_start_req(Sg_request *srp, unsigned char *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) static int sg_finish_rem_req(Sg_request * srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			   Sg_request * srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 			const char __user *buf, size_t count, int blocking,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			int read_only, int sg_io_owned, Sg_request **o_srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			   unsigned char *cmnd, int timeout, int blocking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) static void sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) static void sg_build_reserve(Sg_fd * sfp, int req_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) static Sg_fd *sg_add_sfp(Sg_device * sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) static void sg_remove_sfp(struct kref *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) static Sg_request *sg_add_request(Sg_fd * sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static Sg_device *sg_get_dev(int dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static void sg_device_destroy(struct kref *kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) #define SZ_SG_HEADER sizeof(struct sg_header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) #define SZ_SG_IOVEC sizeof(sg_iovec_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) #define sg_printk(prefix, sdp, fmt, a...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	sdev_prefix_printk(prefix, (sdp)->device,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			   (sdp)->disk->disk_name, fmt, ##a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * The SCSI interfaces that use read() and write() as an asynchronous variant of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * ioctl(..., SG_IO, ...) are fundamentally unsafe, since there are lots of ways
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * to trigger read() and write() calls from various contexts with elevated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * privileges. This can lead to kernel memory corruption (e.g. if these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * interfaces are called through splice()) and privilege escalation inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * userspace (e.g. if a process with access to such a device passes a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * descriptor to a SUID binary as stdin/stdout/stderr).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * This function provides protection for the legacy API by restricting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  * calling context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static int sg_check_file_access(struct file *filp, const char *caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (filp->f_cred != current_real_cred()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		pr_err_once("%s: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			caller, task_tgid_vnr(current), current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (uaccess_kernel()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		pr_err_once("%s: process %d (%s) called from kernel context, this is not allowed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			caller, task_tgid_vnr(current), current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static int sg_allow_access(struct file *filp, unsigned char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	struct sg_fd *sfp = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (sfp->parentdp->device->type == TYPE_SCANNER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	return blk_verify_command(cmd, filp->f_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) open_wait(Sg_device *sdp, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (flags & O_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		while (sdp->open_cnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			mutex_unlock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 			retval = wait_event_interruptible(sdp->open_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 					(atomic_read(&sdp->detaching) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 					 !sdp->open_cnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 			mutex_lock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			if (retval) /* -ERESTARTSYS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 				return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 				return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		while (sdp->exclude) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			mutex_unlock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			retval = wait_event_interruptible(sdp->open_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 					(atomic_read(&sdp->detaching) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 					 !sdp->exclude));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			mutex_lock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 			if (retval) /* -ERESTARTSYS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 				return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 				return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) /* Returns 0 on success, else a negated errno value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) sg_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	int dev = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	int flags = filp->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	struct request_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	nonseekable_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if ((flags & O_EXCL) && (O_RDONLY == (flags & O_ACCMODE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		return -EPERM; /* Can't lock it with read only access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	sdp = sg_get_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (IS_ERR(sdp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		return PTR_ERR(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 				      "sg_open: flags=0x%x\n", flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	/* This driver's module count bumped by fops_get in <linux/fs.h> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	/* Prevent the device driver from vanishing while we sleep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	retval = scsi_device_get(sdp->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		goto sg_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	retval = scsi_autopm_get_device(sdp->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		goto sdp_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	/* scsi_block_when_processing_errors() may block so bypass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	 * check if O_NONBLOCK. Permits SCSI commands to be issued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	 * during error recovery. Tread carefully. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	if (!((flags & O_NONBLOCK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	      scsi_block_when_processing_errors(sdp->device))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		retval = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		/* we are in error recovery for this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	mutex_lock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		if (flags & O_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			if (sdp->open_cnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 				retval = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 				goto error_mutex_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			if (sdp->exclude) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 				retval = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 				goto error_mutex_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		retval = open_wait(sdp, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		if (retval) /* -ERESTARTSYS or -ENODEV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			goto error_mutex_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	/* N.B. at this point we are holding the open_rel_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (flags & O_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		sdp->exclude = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (sdp->open_cnt < 1) {  /* no existing opens */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		sdp->sgdebug = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		q = sdp->device->request_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		sdp->sg_tablesize = queue_max_segments(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	sfp = sg_add_sfp(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (IS_ERR(sfp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		retval = PTR_ERR(sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		goto out_undo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	filp->private_data = sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	sdp->open_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	mutex_unlock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) sg_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	kref_put(&sdp->d_ref, sg_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) out_undo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	if (flags & O_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		sdp->exclude = false;   /* undo if error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		wake_up_interruptible(&sdp->open_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) error_mutex_locked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	mutex_unlock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	scsi_autopm_put_device(sdp->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) sdp_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	scsi_device_put(sdp->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	goto sg_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) /* Release resources associated with a successful sg_open()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  * Returns 0 on success, else a negated errno value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) sg_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, "sg_release\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	mutex_lock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	scsi_autopm_put_device(sdp->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	kref_put(&sfp->f_ref, sg_remove_sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	sdp->open_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	/* possibly many open()s waiting on exlude clearing, start many;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	 * only open(O_EXCL)s wait on 0==open_cnt so only start one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (sdp->exclude) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		sdp->exclude = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		wake_up_interruptible_all(&sdp->open_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	} else if (0 == sdp->open_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		wake_up_interruptible(&sdp->open_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	mutex_unlock(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) static int get_sg_io_pack_id(int *pack_id, void __user *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	struct sg_header __user *old_hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	int reply_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	if (count >= SZ_SG_HEADER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		/* negative reply_len means v3 format, otherwise v1/v2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		if (get_user(reply_len, &old_hdr->reply_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		if (reply_len >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			return get_user(*pack_id, &old_hdr->pack_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		if (in_compat_syscall() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		    count >= sizeof(struct compat_sg_io_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			struct compat_sg_io_hdr __user *hp = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			return get_user(*pack_id, &hp->pack_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		if (count >= sizeof(struct sg_io_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			struct sg_io_hdr __user *hp = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			return get_user(*pack_id, &hp->pack_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	/* no valid header was passed, so ignore the pack_id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	*pack_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	int req_pack_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	sg_io_hdr_t *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	struct sg_header *old_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	 * This could cause a response to be stranded. Close the associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	 * file descriptor to free up any resources being held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	retval = sg_check_file_access(filp, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 				      "sg_read: count=%d\n", (int) count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	if (sfp->force_packid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		retval = get_sg_io_pack_id(&req_pack_id, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	srp = sg_get_rq_mark(sfp, req_pack_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	if (!srp) {		/* now wait on packet to arrive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		if (filp->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		retval = wait_event_interruptible(sfp->read_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			(atomic_read(&sdp->detaching) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			(srp = sg_get_rq_mark(sfp, req_pack_id))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			/* -ERESTARTSYS as signal hit process */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	if (srp->header.interface_id != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		return sg_new_read(sfp, buf, count, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	old_hdr = kzalloc(SZ_SG_HEADER, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (!old_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	old_hdr->reply_len = (int) hp->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	old_hdr->pack_id = hp->pack_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	old_hdr->twelve_byte =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	    ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	old_hdr->target_status = hp->masked_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	old_hdr->host_status = hp->host_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	old_hdr->driver_status = hp->driver_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if ((CHECK_CONDITION & hp->masked_status) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	    (DRIVER_SENSE & hp->driver_status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		memcpy(old_hdr->sense_buffer, srp->sense_b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		       sizeof (old_hdr->sense_buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	switch (hp->host_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	/* This setup of 'result' is for backward compatibility and is best
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	   ignored by the user who should use target, host + driver status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	case DID_OK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	case DID_PASSTHROUGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	case DID_SOFT_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		old_hdr->result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	case DID_NO_CONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	case DID_BUS_BUSY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	case DID_TIME_OUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		old_hdr->result = EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	case DID_BAD_TARGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	case DID_ABORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	case DID_PARITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	case DID_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	case DID_BAD_INTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		old_hdr->result = EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	case DID_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		old_hdr->result = (srp->sense_b[0] == 0 && 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 				  hp->masked_status == GOOD) ? 0 : EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		old_hdr->result = EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	/* Now copy the result back to the user buffer.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	if (count >= SZ_SG_HEADER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		if (copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			goto free_old_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		buf += SZ_SG_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		if (count > old_hdr->reply_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			count = old_hdr->reply_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (count > SZ_SG_HEADER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 				retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 				goto free_old_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		count = (old_hdr->result == 0) ? 0 : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	sg_finish_rem_req(srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	retval = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) free_old_hdr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	kfree(old_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	sg_io_hdr_t *hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	int err = 0, err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (in_compat_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		if (count < sizeof(struct compat_sg_io_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	} else if (count < SZ_SG_IO_HDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	hp->sb_len_wr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if ((hp->mx_sb_len > 0) && hp->sbp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		if ((CHECK_CONDITION & hp->masked_status) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		    (DRIVER_SENSE & hp->driver_status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			int sb_len = SCSI_SENSE_BUFFERSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			len = 8 + (int) srp->sense_b[7];	/* Additional sense length field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			len = (len > sb_len) ? sb_len : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			if (copy_to_user(hp->sbp, srp->sense_b, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 				err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 				goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			hp->sb_len_wr = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	if (hp->masked_status || hp->host_status || hp->driver_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		hp->info |= SG_INFO_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	err = put_sg_io_hdr(hp, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	err2 = sg_finish_rem_req(srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	return err ? : err2 ? : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	int mxsize, cmd_size, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	int input_size, blocking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	unsigned char opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	struct sg_header old_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	sg_io_hdr_t *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	unsigned char cmnd[SG_MAX_CDB_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	retval = sg_check_file_access(filp, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 				      "sg_write: count=%d\n", (int) count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (!((filp->f_flags & O_NONBLOCK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	      scsi_block_when_processing_errors(sdp->device)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	if (count < SZ_SG_HEADER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	if (copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	blocking = !(filp->f_flags & O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	if (old_hdr.reply_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		return sg_new_write(sfp, filp, buf, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				    blocking, 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (count < (SZ_SG_HEADER + 6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		return -EIO;	/* The minimum scsi command length is 6 bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	buf += SZ_SG_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (get_user(opcode, buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (!(srp = sg_add_request(sfp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 					      "sg_write: queue full\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		return -EDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	mutex_lock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (sfp->next_cmd_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		cmd_size = sfp->next_cmd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		sfp->next_cmd_len = 0;	/* reset so only this write() effected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		cmd_size = COMMAND_SIZE(opcode);	/* based on SCSI command group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		if ((opcode >= 0xc0) && old_hdr.twelve_byte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			cmd_size = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		"sg_write:   scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) /* Determine buffer size.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	input_size = count - cmd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	mxsize -= SZ_SG_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	input_size -= SZ_SG_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	if (input_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		return -EIO;	/* User did not pass enough bytes for this command. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	hp->interface_id = '\0';	/* indicator of old interface tunnelled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	hp->cmd_len = (unsigned char) cmd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	hp->iovec_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	hp->mx_sb_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (input_size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		    SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	hp->dxfer_len = mxsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if ((hp->dxfer_direction == SG_DXFER_TO_DEV) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	    (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		hp->dxferp = (char __user *)buf + cmd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		hp->dxferp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	hp->sbp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	hp->timeout = old_hdr.reply_len;	/* structure abuse ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	hp->flags = input_size;	/* structure abuse ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	hp->pack_id = old_hdr.pack_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	hp->usr_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (copy_from_user(cmnd, buf, cmd_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 * is a non-zero input_size, so emit a warning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		printk_ratelimited(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 				   "sg_write: data in/out %d/%d bytes "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				   "for SCSI command 0x%x-- guessing "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 				   "data in;\n   program %s not setting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 				   "count and/or reply_len properly\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 				   old_hdr.reply_len - (int)SZ_SG_HEADER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 				   input_size, (unsigned int) cmnd[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				   current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	return (k < 0) ? k : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		 size_t count, int blocking, int read_only, int sg_io_owned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		 Sg_request **o_srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	sg_io_hdr_t *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	unsigned char cmnd[SG_MAX_CDB_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	int timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	unsigned long ul_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (count < SZ_SG_IO_HDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	sfp->cmd_q = 1;	/* when sg_io_hdr seen, set command queuing on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	if (!(srp = sg_add_request(sfp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 					      "sg_new_write: queue full\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return -EDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	srp->sg_io_owned = sg_io_owned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (get_sg_io_hdr(hp, buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	if (hp->interface_id != 'S') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (hp->flags & SG_FLAG_MMAP_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		if (hp->dxfer_len > sfp->reserve.bufflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			return -ENOMEM;	/* MMAP_IO size must fit in reserve buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		if (hp->flags & SG_FLAG_DIRECT_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			return -EINVAL;	/* either MMAP_IO or DIRECT_IO (not both) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		if (sfp->res_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			return -EBUSY;	/* reserve buffer already being used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	ul_timeout = msecs_to_jiffies(srp->header.timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	if (read_only && sg_allow_access(file, cmnd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	if (k < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		return k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (o_srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		*o_srp = srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) sg_common_write(Sg_fd * sfp, Sg_request * srp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		unsigned char *cmnd, int timeout, int blocking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	int k, at_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	Sg_device *sdp = sfp->parentdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	sg_io_hdr_t *hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	srp->data.cmd_opcode = cmnd[0];	/* hold opcode of command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	hp->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	hp->masked_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	hp->msg_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	hp->info = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	hp->host_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	hp->driver_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	hp->resid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			"sg_common_write:  scsi opcode=0x%02x, cmd_size=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			(int) cmnd[0], (int) hp->cmd_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	if (hp->dxfer_len >= SZ_256M) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	k = sg_start_req(srp, cmnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			"sg_common_write: start_req err=%d\n", k));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		sg_finish_rem_req(srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		return k;	/* probably out of space --> ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (atomic_read(&sdp->detaching)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		if (srp->bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			scsi_req_free_cmd(scsi_req(srp->rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			blk_put_request(srp->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			srp->rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		sg_finish_rem_req(srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	hp->duration = jiffies_to_msecs(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (hp->interface_id != '\0' &&	/* v3 (or later) interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	    (SG_FLAG_Q_AT_TAIL & hp->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		at_head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		at_head = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	srp->rq->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			      srp->rq, at_head, sg_rq_end_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) static int srp_done(Sg_fd *sfp, Sg_request *srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	read_lock_irqsave(&sfp->rq_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	ret = srp->done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	read_unlock_irqrestore(&sfp->rq_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) static int max_sectors_bytes(struct request_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	unsigned int max_sectors = queue_max_sectors(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	return max_sectors << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) sg_fill_request_table(Sg_fd *sfp, sg_req_info_t *rinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	unsigned int ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	list_for_each_entry(srp, &sfp->rq_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		if (val >= SG_MAX_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		rinfo[val].req_state = srp->done + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		rinfo[val].problem =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			srp->header.masked_status &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			srp->header.host_status &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			srp->header.driver_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		if (srp->done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			rinfo[val].duration =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 				srp->header.duration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			ms = jiffies_to_msecs(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			rinfo[val].duration =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 				(ms > srp->header.duration) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 				(ms - srp->header.duration) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		rinfo[val].orphan = srp->orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		rinfo[val].sg_io_owned = srp->sg_io_owned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		rinfo[val].pack_id = srp->header.pack_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		rinfo[val].usr_ptr = srp->header.usr_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		val++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	char req_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	char orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	char sg_io_owned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	char problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	int pack_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	compat_uptr_t usr_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	unsigned int duration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	int unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) static int put_compat_request_table(struct compat_sg_req_info __user *o,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				    struct sg_req_info *rinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	for (i = 0; i < SG_MAX_QUEUE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		if (copy_to_user(o + i, rinfo + i, offsetof(sg_req_info_t, usr_ptr)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		    put_user((uintptr_t)rinfo[i].usr_ptr, &o[i].usr_ptr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		    put_user(rinfo[i].duration, &o[i].duration) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		    put_user(rinfo[i].unused, &o[i].unused))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		unsigned int cmd_in, void __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	int __user *ip = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	int result, val, read_only;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 				   "sg_ioctl: cmd=0x%x\n", (int) cmd_in));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	switch (cmd_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	case SG_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		if (!scsi_block_when_processing_errors(sdp->device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 				 1, read_only, 1, &srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		result = wait_event_interruptible(sfp->read_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 			(srp_done(sfp, srp) || atomic_read(&sdp->detaching)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		write_lock_irq(&sfp->rq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		if (srp->done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			srp->done = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			write_unlock_irq(&sfp->rq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			return (result < 0) ? result : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		srp->orphan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		write_unlock_irq(&sfp->rq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		return result;	/* -ERESTARTSYS because signal hit process */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	case SG_SET_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		if (val >= mult_frac((s64)INT_MAX, USER_HZ, HZ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			val = min_t(s64, mult_frac((s64)INT_MAX, USER_HZ, HZ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 				    INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		sfp->timeout_user = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		sfp->timeout = mult_frac(val, HZ, USER_HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	case SG_GET_TIMEOUT:	/* N.B. User receives timeout as return value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 				/* strange ..., for backward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		return sfp->timeout_user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	case SG_SET_FORCE_LOW_DMA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		 * N.B. This ioctl never worked properly, but failed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		 * return an error value. So returning '0' to keep compability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		 * with legacy applications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	case SG_GET_LOW_DMA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		return put_user((int) sdp->device->host->unchecked_isa_dma, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	case SG_GET_SCSI_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			sg_scsi_id_t v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 				return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			memset(&v, 0, sizeof(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			v.host_no = sdp->device->host->host_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			v.channel = sdp->device->channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			v.scsi_id = sdp->device->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			v.lun = sdp->device->lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			v.scsi_type = sdp->device->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			v.h_cmd_per_lun = sdp->device->host->cmd_per_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			v.d_queue_depth = sdp->device->queue_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			if (copy_to_user(p, &v, sizeof(sg_scsi_id_t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	case SG_SET_FORCE_PACK_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		sfp->force_packid = val ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	case SG_GET_PACK_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		read_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		list_for_each_entry(srp, &sfp->rq_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 			if ((1 == srp->done) && (!srp->sg_io_owned)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 				read_unlock_irqrestore(&sfp->rq_list_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 						       iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 				return put_user(srp->header.pack_id, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return put_user(-1, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	case SG_GET_NUM_WAITING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		read_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		list_for_each_entry(srp, &sfp->rq_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			if ((1 == srp->done) && (!srp->sg_io_owned))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 				++val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		return put_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	case SG_GET_SG_TABLESIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		return put_user(sdp->sg_tablesize, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	case SG_SET_RESERVED_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)                 if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)                         return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		val = min_t(int, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 			    max_sectors_bytes(sdp->device->request_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		mutex_lock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		if (val != sfp->reserve.bufflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			if (sfp->mmap_called ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			    sfp->res_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 				return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			sg_remove_scat(sfp, &sfp->reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			sg_build_reserve(sfp, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	case SG_GET_RESERVED_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		val = min_t(int, sfp->reserve.bufflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			    max_sectors_bytes(sdp->device->request_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		return put_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	case SG_SET_COMMAND_Q:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		sfp->cmd_q = val ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	case SG_GET_COMMAND_Q:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		return put_user((int) sfp->cmd_q, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	case SG_SET_KEEP_ORPHAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		sfp->keep_orphan = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	case SG_GET_KEEP_ORPHAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		return put_user((int) sfp->keep_orphan, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	case SG_NEXT_CMD_LEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		if (val > SG_MAX_CDB_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		sfp->next_cmd_len = (val > 0) ? val : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	case SG_GET_VERSION_NUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		return put_user(sg_version_num, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	case SG_GET_ACCESS_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		/* faked - we don't have a real access count anymore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		val = (sdp->device ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		return put_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	case SG_GET_REQUEST_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			sg_req_info_t *rinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			rinfo = kcalloc(SG_MAX_QUEUE, SZ_SG_REQ_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			if (!rinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			read_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			sg_fill_request_table(sfp, rinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	#ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			if (in_compat_syscall())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 				result = put_compat_request_table(p, rinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	#endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 				result = copy_to_user(p, rinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 						      SZ_SG_REQ_INFO * SG_MAX_QUEUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			result = result ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			kfree(rinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	case SG_EMULATED_HOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		return put_user(sdp->device->host->hostt->emulated, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	case SCSI_IOCTL_SEND_COMMAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	case SG_SET_DEBUG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		result = get_user(val, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		sdp->sgdebug = (char) val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	case BLKSECTGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		return put_user(max_sectors_bytes(sdp->device->request_queue),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 				ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	case BLKTRACESETUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		return blk_trace_setup(sdp->device->request_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 				       sdp->disk->disk_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				       MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 				       NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	case BLKTRACESTART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		return blk_trace_startstop(sdp->device->request_queue, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	case BLKTRACESTOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		return blk_trace_startstop(sdp->device->request_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	case BLKTRACETEARDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		return blk_trace_remove(sdp->device->request_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	case SCSI_IOCTL_GET_IDLUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	case SCSI_IOCTL_GET_BUS_NUMBER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	case SCSI_IOCTL_PROBE_HOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	case SG_GET_TRANSFORM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	case SG_SCSI_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		if (read_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			return -EPERM;	/* don't know so take safe approach */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	result = scsi_ioctl_block_when_processing_errors(sdp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			cmd_in, filp->f_flags & O_NDELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	void __user *p = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	ret = sg_ioctl_common(filp, sdp, sfp, cmd_in, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (ret != -ENOIOCTLCMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	return scsi_ioctl(sdp->device, cmd_in, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	void __user *p = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	ret = sg_ioctl_common(filp, sdp, sfp, cmd_in, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (ret != -ENOIOCTLCMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	return scsi_compat_ioctl(sdp->device, cmd_in, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) static __poll_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) sg_poll(struct file *filp, poll_table * wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	__poll_t res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	sfp = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	if (!sfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	sdp = sfp->parentdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	if (!sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	poll_wait(filp, &sfp->read_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	read_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	list_for_each_entry(srp, &sfp->rq_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		/* if any read waiting, flag it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			res = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		++count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		res |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	else if (!sfp->cmd_q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		if (0 == count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			res |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	} else if (count < SG_MAX_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		res |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 				      "sg_poll: res=0x%x\n", (__force u32) res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) sg_fasync(int fd, struct file *filp, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				      "sg_fasync: mode=%d\n", mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	return fasync_helper(fd, filp, mode, &sfp->async_qp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) static vm_fault_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) sg_vma_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	struct vm_area_struct *vma = vmf->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	unsigned long offset, len, sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	Sg_scatter_hold *rsv_schp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	int k, length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	rsv_schp = &sfp->reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	offset = vmf->pgoff << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	if (offset >= rsv_schp->bufflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				      "sg_vma_fault: offset=%lu, scatg=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				      offset, rsv_schp->k_use_sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	sa = vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		len = vma->vm_end - sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		len = (len < length) ? len : length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		if (offset < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			struct page *page = nth_page(rsv_schp->pages[k],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 						     offset >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			get_page(page);	/* increment page count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			vmf->page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			return 0; /* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		sa += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		offset -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) static const struct vm_operations_struct sg_mmap_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	.fault = sg_vma_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) sg_mmap(struct file *filp, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	unsigned long req_sz, len, sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	Sg_scatter_hold *rsv_schp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	int k, length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	req_sz = vma->vm_end - vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 				      "sg_mmap starting, vm_start=%p, len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 				      (void *) vma->vm_start, (int) req_sz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	if (vma->vm_pgoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		return -EINVAL;	/* want no offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	rsv_schp = &sfp->reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	mutex_lock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	if (req_sz > rsv_schp->bufflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		ret = -ENOMEM;	/* cannot map more than reserved buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	sa = vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		len = vma->vm_end - sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		len = (len < length) ? len : length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		sa += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	sfp->mmap_called = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	vma->vm_private_data = sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	vma->vm_ops = &sg_mmap_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) sg_rq_end_io_usercontext(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	struct sg_request *srp = container_of(work, struct sg_request, ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	struct sg_fd *sfp = srp->parentfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	sg_finish_rem_req(srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	sg_remove_request(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	kref_put(&sfp->f_ref, sg_remove_sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)  * This function is a "bottom half" handler that is called by the mid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  * level when a command is completed (or has failed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) sg_rq_end_io(struct request *rq, blk_status_t status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	struct sg_request *srp = rq->end_io_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	struct scsi_request *req = scsi_req(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	unsigned int ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	char *sense;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	int result, resid, done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	if (WARN_ON(srp->done != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	sfp = srp->parentfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (WARN_ON(sfp == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	sdp = sfp->parentdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	if (unlikely(atomic_read(&sdp->detaching)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		pr_info("%s: device detaching\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	sense = req->sense;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	result = req->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	resid = req->resid_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 				      "sg_cmd_done: pack_id=%d, res=0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 				      srp->header.pack_id, result));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	srp->header.resid = resid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	ms = jiffies_to_msecs(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	srp->header.duration = (ms > srp->header.duration) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 				(ms - srp->header.duration) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	if (0 != result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		struct scsi_sense_hdr sshdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		srp->header.status = 0xff & result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		srp->header.masked_status = status_byte(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		srp->header.msg_status = msg_byte(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		srp->header.host_status = host_byte(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		srp->header.driver_status = driver_byte(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		if ((sdp->sgdebug > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		    ((CHECK_CONDITION == srp->header.masked_status) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		     (COMMAND_TERMINATED == srp->header.masked_status)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			__scsi_print_sense(sdp->device, __func__, sense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 					   SCSI_SENSE_BUFFERSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		/* Following if statement is a patch supplied by Eric Youngdale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		if (driver_byte(result) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		    && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		    && !scsi_sense_is_deferred(&sshdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		    && sshdr.sense_key == UNIT_ATTENTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		    && sdp->device->removable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			/* Detected possible disc change. Set the bit - this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			/* may be used if there are filesystems using this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			sdp->device->changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (req->sense_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		memcpy(srp->sense_b, req->sense, SCSI_SENSE_BUFFERSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	/* Rely on write phase to clean out srp status values, so no "else" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	 * Free the request as soon as it is complete so that its resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	 * can be reused without waiting for userspace to read() the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	 * result.  But keep the associated bio (if any) around until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	 * blk_rq_unmap_user() can be called from user context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	srp->rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	scsi_req_free_cmd(scsi_req(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	blk_put_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	if (unlikely(srp->orphan)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		if (sfp->keep_orphan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			srp->sg_io_owned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 			done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	srp->done = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	if (likely(done)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		/* Now wake up any sg_read() that is waiting for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		 * packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		wake_up_interruptible(&sfp->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		kref_put(&sfp->f_ref, sg_remove_sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		schedule_work(&srp->ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) static const struct file_operations sg_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	.read = sg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	.write = sg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	.poll = sg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	.unlocked_ioctl = sg_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	.compat_ioctl = sg_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	.open = sg_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	.mmap = sg_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	.release = sg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	.fasync = sg_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	.llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) static struct class *sg_sysfs_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) static int sg_sysfs_valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) static Sg_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	struct request_queue *q = scsidp->request_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	u32 k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	if (!sdp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		sdev_printk(KERN_WARNING, scsidp, "%s: kmalloc Sg_device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			    "failure\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	write_lock_irqsave(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 			sdev_printk(KERN_WARNING, scsidp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 				    "Unable to attach sg device type=%d, minor number exceeds %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 				    scsidp->type, SG_MAX_DEVS - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 			error = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 			sdev_printk(KERN_WARNING, scsidp, "%s: idr "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 				    "allocation Sg_device failure: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 				    __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	k = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO, scsidp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 					"sg_alloc: dev=%d \n", k));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	sprintf(disk->disk_name, "sg%d", k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	disk->first_minor = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	sdp->disk = disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	sdp->device = scsidp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	mutex_init(&sdp->open_rel_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	INIT_LIST_HEAD(&sdp->sfds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	init_waitqueue_head(&sdp->open_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	atomic_set(&sdp->detaching, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	rwlock_init(&sdp->sfd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	sdp->sg_tablesize = queue_max_segments(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	sdp->index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	kref_init(&sdp->d_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	write_unlock_irqrestore(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		kfree(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	return sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) sg_add_device(struct device *cl_dev, struct class_interface *cl_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	Sg_device *sdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	struct cdev * cdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	disk = alloc_disk(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (!disk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		pr_warn("%s: alloc_disk failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	disk->major = SCSI_GENERIC_MAJOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	cdev = cdev_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (!cdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		pr_warn("%s: cdev_alloc failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	cdev->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	cdev->ops = &sg_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	sdp = sg_alloc(disk, scsidp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (IS_ERR(sdp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		pr_warn("%s: sg_alloc failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		error = PTR_ERR(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		goto cdev_add_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	sdp->cdev = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	if (sg_sysfs_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		struct device *sg_class_member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 						MKDEV(SCSI_GENERIC_MAJOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 						      sdp->index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 						sdp, "%s", disk->disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		if (IS_ERR(sg_class_member)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 			pr_err("%s: device_create failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			error = PTR_ERR(sg_class_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 			goto cdev_add_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 					  &sg_class_member->kobj, "generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			pr_err("%s: unable to make symlink 'generic' back "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			       "to sg%d\n", __func__, sdp->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		pr_warn("%s: sg_sys Invalid\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	sdev_printk(KERN_NOTICE, scsidp, "Attached scsi generic sg%d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		    "type %d\n", sdp->index, scsidp->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	dev_set_drvdata(cl_dev, sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) cdev_add_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	write_lock_irqsave(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	idr_remove(&sg_index_idr, sdp->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	write_unlock_irqrestore(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	kfree(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	put_disk(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	if (cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		cdev_del(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) sg_device_destroy(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	/* CAUTION!  Note that the device can still be found via idr_find()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	 * even though the refcount is 0.  Therefore, do idr_remove() BEFORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	 * any other cleanup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	write_lock_irqsave(&sg_index_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	idr_remove(&sg_index_idr, sdp->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	write_unlock_irqrestore(&sg_index_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	SCSI_LOG_TIMEOUT(3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		sg_printk(KERN_INFO, sdp, "sg_device_destroy\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	put_disk(sdp->disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	kfree(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) sg_remove_device(struct device *cl_dev, struct class_interface *cl_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	Sg_device *sdp = dev_get_drvdata(cl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	if (!sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	/* want sdp->detaching non-zero as soon as possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	val = atomic_inc_return(&sdp->detaching);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	if (val > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		return; /* only want to do following once per device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 				      "%s\n", __func__));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	read_lock_irqsave(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		wake_up_interruptible_all(&sfp->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	wake_up_interruptible_all(&sdp->open_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	read_unlock_irqrestore(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	cdev_del(sdp->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	sdp->cdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	kref_put(&sdp->d_ref, sg_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) module_param_named(def_reserved_size, def_reserved_size, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		   S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) MODULE_AUTHOR("Douglas Gilbert");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) MODULE_DESCRIPTION("SCSI generic (sg) driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) MODULE_VERSION(SG_VERSION_STR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)                 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) static int __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) init_sg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	if (scatter_elem_sz < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		scatter_elem_sz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		scatter_elem_sz_prev = scatter_elem_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	if (def_reserved_size >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		sg_big_buff = def_reserved_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		def_reserved_size = sg_big_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 				    SG_MAX_DEVS, "sg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)         sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)         if ( IS_ERR(sg_sysfs_class) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		rc = PTR_ERR(sg_sysfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)         }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	sg_sysfs_valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	rc = scsi_register_interface(&sg_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	if (0 == rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) #ifdef CONFIG_SCSI_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		sg_proc_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) #endif				/* CONFIG_SCSI_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	class_destroy(sg_sysfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) static void __exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) exit_sg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) #ifdef CONFIG_SCSI_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	remove_proc_subtree("scsi/sg", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) #endif				/* CONFIG_SCSI_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	scsi_unregister_interface(&sg_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	class_destroy(sg_sysfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	sg_sysfs_valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 				 SG_MAX_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	idr_destroy(&sg_index_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) sg_start_req(Sg_request *srp, unsigned char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	struct scsi_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	Sg_fd *sfp = srp->parentfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	sg_io_hdr_t *hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	int dxfer_len = (int) hp->dxfer_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	int dxfer_dir = hp->dxfer_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	unsigned int iov_count = hp->iovec_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	Sg_scatter_hold *req_schp = &srp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	Sg_scatter_hold *rsv_schp = &sfp->reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	struct request_queue *q = sfp->parentdp->device->request_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	struct rq_map_data *md, map_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	unsigned char *long_cmdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 				      "sg_start_req: dxfer_len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 				      dxfer_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	if (hp->cmd_len > BLK_MAX_CDB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		if (!long_cmdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	 * NOTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	 * With scsi-mq enabled, there are a fixed number of preallocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	 * requests equal in number to shost->can_queue.  If all of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	 * preallocated requests are already in use, then blk_get_request()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	 * will sleep until an active command completes, freeing up a request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	 * Although waiting in an asynchronous interface is less than ideal, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	 * do not want to use BLK_MQ_REQ_NOWAIT here because userspace might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	 * not expect an EWOULDBLOCK from this condition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	rq = blk_get_request(q, hp->dxfer_direction == SG_DXFER_TO_DEV ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	if (IS_ERR(rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		kfree(long_cmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		return PTR_ERR(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	req = scsi_req(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	if (hp->cmd_len > BLK_MAX_CDB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		req->cmd = long_cmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	memcpy(req->cmd, cmd, hp->cmd_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	req->cmd_len = hp->cmd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	srp->rq = rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	rq->end_io_data = srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	req->retries = SG_DEFAULT_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	    dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	    !sfp->parentdp->device->host->unchecked_isa_dma &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	    blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		md = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		md = &map_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	if (md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		mutex_lock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		if (dxfer_len <= rsv_schp->bufflen &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		    !sfp->res_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			sfp->res_in_use = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 			sg_link_reserve(sfp, srp, dxfer_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		} else if (hp->flags & SG_FLAG_MMAP_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 			res = -EBUSY; /* sfp->res_in_use == 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			if (dxfer_len > rsv_schp->bufflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 				res = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 			mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			res = sg_build_indirect(req_schp, sfp, dxfer_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 				mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 				return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		mutex_unlock(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		md->pages = req_schp->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		md->page_order = req_schp->page_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		md->nr_entries = req_schp->k_use_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		md->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		md->null_mapped = hp->dxferp ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 			md->from_user = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			md->from_user = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	if (iov_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		struct iovec *iov = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		struct iov_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		res = import_iovec(rw, hp->dxferp, iov_count, 0, &iov, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		iov_iter_truncate(&i, hp->dxfer_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		if (!iov_iter_count(&i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 			kfree(iov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		res = blk_rq_map_user_iov(q, rq, md, &i, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		kfree(iov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		res = blk_rq_map_user(q, rq, md, hp->dxferp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 				      hp->dxfer_len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		srp->bio = rq->bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		if (!md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 			req_schp->dio_in_use = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			hp->info |= SG_INFO_DIRECT_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) sg_finish_rem_req(Sg_request *srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	Sg_fd *sfp = srp->parentfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	Sg_scatter_hold *req_schp = &srp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 				      "sg_finish_rem_req: res_used=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 				      (int) srp->res_used));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	if (srp->bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		ret = blk_rq_unmap_user(srp->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	if (srp->rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		scsi_req_free_cmd(scsi_req(srp->rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		blk_put_request(srp->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	if (srp->res_used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		sg_unlink_reserve(sfp, srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		sg_remove_scat(sfp, req_schp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	int sg_bufflen = tablesize * sizeof(struct page *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	schp->pages = kzalloc(sg_bufflen, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	if (!schp->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	schp->sglist_len = sg_bufflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	return tablesize;	/* number of scat_gath elements allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	int sg_tablesize = sfp->parentdp->sg_tablesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	int blk_size = buff_size, order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	struct sg_device *sdp = sfp->parentdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	if (blk_size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	if (0 == blk_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		++blk_size;	/* don't know why */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	/* round request up to next highest SG_SECTOR_SZ byte boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		"sg_build_indirect: buff_size=%d, blk_size=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		buff_size, blk_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	/* N.B. ret_sz carried into this block ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	if (mx_sc_elems < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		return mx_sc_elems;	/* most likely -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	num = scatter_elem_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	if (unlikely(num != scatter_elem_sz_prev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		if (num < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 			scatter_elem_sz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 			scatter_elem_sz_prev = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 			scatter_elem_sz_prev = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	if (sdp->device->host->unchecked_isa_dma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		gfp_mask |= GFP_DMA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	order = get_order(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	ret_sz = 1 << (PAGE_SHIFT + order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	     k++, rem_sz -= ret_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		num = (rem_sz > scatter_elem_sz_prev) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 			scatter_elem_sz_prev : rem_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		schp->pages[k] = alloc_pages(gfp_mask, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		if (!schp->pages[k])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		if (num == scatter_elem_sz_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 			if (unlikely(ret_sz > scatter_elem_sz_prev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 				scatter_elem_sz = ret_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 				scatter_elem_sz_prev = ret_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 				 "sg_build_indirect: k=%d, num=%d, ret_sz=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 				 k, num, ret_sz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	}		/* end of for loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	schp->page_order = order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	schp->k_use_sg = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 			 "sg_build_indirect: k_use_sg=%d, rem_sz=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 			 k, rem_sz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	schp->bufflen = blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	if (rem_sz > 0)	/* must have failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	for (i = 0; i < k; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 		__free_pages(schp->pages[i], order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	if (--order >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 			 "sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	if (schp->pages && schp->sglist_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		if (!schp->dio_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 			int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 			for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 				SCSI_LOG_TIMEOUT(5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 					sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 					"sg_remove_scat: k=%d, pg=0x%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 					k, schp->pages[k]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 				__free_pages(schp->pages[k], schp->page_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 			kfree(schp->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	memset(schp, 0, sizeof (*schp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	Sg_scatter_hold *schp = &srp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	int k, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 			 "sg_read_oxfer: num_read_xfer=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 			 num_read_xfer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	if ((!outp) || (num_read_xfer <= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	num = 1 << (PAGE_SHIFT + schp->page_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		if (num > num_read_xfer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 			if (copy_to_user(outp, page_address(schp->pages[k]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 					   num_read_xfer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 			if (copy_to_user(outp, page_address(schp->pages[k]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 					   num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			num_read_xfer -= num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 			if (num_read_xfer <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 			outp += num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) sg_build_reserve(Sg_fd * sfp, int req_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	Sg_scatter_hold *schp = &sfp->reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			 "sg_build_reserve: req_size=%d\n", req_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		if (req_size < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			req_size = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		if (0 == sg_build_indirect(schp, sfp, req_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			sg_remove_scat(sfp, schp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		req_size >>= 1;	/* divide by 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	} while (req_size > (PAGE_SIZE / 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	Sg_scatter_hold *req_schp = &srp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	Sg_scatter_hold *rsv_schp = &sfp->reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	int k, num, rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	srp->res_used = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 			 "sg_link_reserve: size=%d\n", size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	rem = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	for (k = 0; k < rsv_schp->k_use_sg; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		if (rem <= num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 			req_schp->k_use_sg = k + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 			req_schp->sglist_len = rsv_schp->sglist_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			req_schp->pages = rsv_schp->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			req_schp->bufflen = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 			req_schp->page_order = rsv_schp->page_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 			rem -= num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	if (k >= rsv_schp->k_use_sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 				 "sg_link_reserve: BAD size\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	Sg_scatter_hold *req_schp = &srp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 				      "sg_unlink_reserve: req->k_use_sg=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 				      (int) req_schp->k_use_sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	req_schp->k_use_sg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	req_schp->bufflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	req_schp->pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	req_schp->page_order = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	req_schp->sglist_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	srp->res_used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	/* Called without mutex lock to avoid deadlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	sfp->res_in_use = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) static Sg_request *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) sg_get_rq_mark(Sg_fd * sfp, int pack_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	Sg_request *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	list_for_each_entry(resp, &sfp->rq_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		/* look for requests that are ready + not SG_IO owned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		if ((1 == resp->done) && (!resp->sg_io_owned) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		    ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			resp->done = 2;	/* guard against other readers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			return resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) /* always adds to end of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) static Sg_request *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) sg_add_request(Sg_fd * sfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	Sg_request *rp = sfp->req_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	if (!list_empty(&sfp->rq_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		if (!sfp->cmd_q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 			if (!rp->parentfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		if (k >= SG_MAX_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	memset(rp, 0, sizeof (Sg_request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	rp->parentfp = sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	rp->header.duration = jiffies_to_msecs(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	list_add_tail(&rp->entry, &sfp->rq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	return rp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) /* Return of 1 for found; 0 for not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) sg_remove_request(Sg_fd * sfp, Sg_request * srp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	if (!sfp || !srp || list_empty(&sfp->rq_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	if (!list_empty(&srp->entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		list_del(&srp->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		srp->parentfp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		res = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) static Sg_fd *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) sg_add_sfp(Sg_device * sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	Sg_fd *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	int bufflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	if (!sfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	init_waitqueue_head(&sfp->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	rwlock_init(&sfp->rq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	INIT_LIST_HEAD(&sfp->rq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	kref_init(&sfp->f_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	mutex_init(&sfp->f_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	sfp->timeout = SG_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	sfp->force_packid = SG_DEF_FORCE_PACK_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	sfp->cmd_q = SG_DEF_COMMAND_Q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	sfp->parentdp = sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	write_lock_irqsave(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	if (atomic_read(&sdp->detaching)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 		write_unlock_irqrestore(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 		kfree(sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	write_unlock_irqrestore(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 				      "sg_add_sfp: sfp=0x%p\n", sfp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	if (unlikely(sg_big_buff != def_reserved_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		sg_big_buff = def_reserved_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	bufflen = min_t(int, sg_big_buff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 			max_sectors_bytes(sdp->device->request_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	sg_build_reserve(sfp, bufflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 				      "sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 				      sfp->reserve.bufflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 				      sfp->reserve.k_use_sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	kref_get(&sdp->d_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	return sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) sg_remove_sfp_usercontext(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	struct sg_device *sdp = sfp->parentdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	/* Cleanup any responses which were never read(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	while (!list_empty(&sfp->rq_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		srp = list_first_entry(&sfp->rq_list, Sg_request, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 		sg_finish_rem_req(srp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 		list_del(&srp->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 		srp->parentfp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	if (sfp->reserve.bufflen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 		SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 				"sg_remove_sfp:    bufflen=%d, k_use_sg=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 				(int) sfp->reserve.bufflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 				(int) sfp->reserve.k_use_sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		sg_remove_scat(sfp, &sfp->reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 			"sg_remove_sfp: sfp=0x%p\n", sfp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	kfree(sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	scsi_device_put(sdp->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	kref_put(&sdp->d_ref, sg_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) sg_remove_sfp(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	struct sg_device *sdp = sfp->parentdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	write_lock_irqsave(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	list_del(&sfp->sfd_siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	write_unlock_irqrestore(&sdp->sfd_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	schedule_work(&sfp->ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) #ifdef CONFIG_SCSI_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) sg_idr_max_id(int id, void *p, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	int *k = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	if (*k < id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		*k = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) sg_last_dev(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	int k = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	read_lock_irqsave(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	read_unlock_irqrestore(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	return k + 1;		/* origin 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) /* must be called with sg_index_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) static Sg_device *sg_lookup_dev(int dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	return idr_find(&sg_index_idr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) static Sg_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) sg_get_dev(int dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	struct sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	read_lock_irqsave(&sg_index_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	sdp = sg_lookup_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	if (!sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 		sdp = ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	else if (atomic_read(&sdp->detaching)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		/* If sdp->detaching, then the refcount may already be 0, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 		 * which case it would be a bug to do kref_get().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 		sdp = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 		kref_get(&sdp->d_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	read_unlock_irqrestore(&sg_index_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	return sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) #ifdef CONFIG_SCSI_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) static int sg_proc_seq_show_int(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 			          size_t count, loff_t *off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) static const struct proc_ops adio_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	.proc_open	= sg_proc_single_open_adio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	.proc_read	= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	.proc_lseek	= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	.proc_write	= sg_proc_write_adio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	.proc_release	= single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) static ssize_t sg_proc_write_dressz(struct file *filp, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		const char __user *buffer, size_t count, loff_t *off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) static const struct proc_ops dressz_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	.proc_open	= sg_proc_single_open_dressz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	.proc_read	= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	.proc_lseek	= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	.proc_write	= sg_proc_write_dressz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	.proc_release	= single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) static int sg_proc_seq_show_version(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) static void * dev_seq_start(struct seq_file *s, loff_t *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) static void dev_seq_stop(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) static const struct seq_operations dev_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	.start = dev_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	.next  = dev_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	.stop  = dev_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	.show  = sg_proc_seq_show_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) static const struct seq_operations devstrs_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	.start = dev_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	.next  = dev_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	.stop  = dev_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	.show  = sg_proc_seq_show_devstrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) static const struct seq_operations debug_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	.start = dev_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	.next  = dev_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	.stop  = dev_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	.show  = sg_proc_seq_show_debug,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) sg_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	struct proc_dir_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	p = proc_mkdir("scsi/sg", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	proc_create("allow_dio", S_IRUGO | S_IWUSR, p, &adio_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	proc_create_seq("debug", S_IRUGO, p, &debug_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	proc_create("def_reserved_size", S_IRUGO | S_IWUSR, p, &dressz_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	proc_create_single("device_hdr", S_IRUGO, p, sg_proc_seq_show_devhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	proc_create_seq("devices", S_IRUGO, p, &dev_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	proc_create_seq("device_strs", S_IRUGO, p, &devstrs_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	proc_create_single("version", S_IRUGO, p, sg_proc_seq_show_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) static int sg_proc_seq_show_int(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	seq_printf(s, "%d\n", *((int *)s->private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) static ssize_t 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) sg_proc_write_adio(struct file *filp, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		   size_t count, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	unsigned long num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	err = kstrtoul_from_user(buffer, count, 0, &num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	sg_allow_dio = num ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) static ssize_t 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) sg_proc_write_dressz(struct file *filp, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 		     size_t count, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	unsigned long k = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	err = kstrtoul_from_user(buffer, count, 0, &k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	if (k <= 1048576) {	/* limit "big buff" to 1 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		sg_big_buff = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) static int sg_proc_seq_show_version(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		   sg_version_date);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	seq_puts(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) struct sg_proc_deviter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	loff_t	index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	size_t	max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) static void * dev_seq_start(struct seq_file *s, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	s->private = it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	if (! it)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	it->index = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	it->max = sg_last_dev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	if (it->index >= it->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	return it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	struct sg_proc_deviter * it = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	*pos = ++it->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	return (it->index < it->max) ? it : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) static void dev_seq_stop(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	kfree(s->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	struct scsi_device *scsidp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	read_lock_irqsave(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	sdp = it ? sg_lookup_dev(it->index) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	if ((NULL == sdp) || (NULL == sdp->device) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	    (atomic_read(&sdp->detaching)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		seq_puts(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		scsidp = sdp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		seq_printf(s, "%d\t%d\t%d\t%llu\t%d\t%d\t%d\t%d\t%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 			      scsidp->host->host_no, scsidp->channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 			      scsidp->id, scsidp->lun, (int) scsidp->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			      1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 			      (int) scsidp->queue_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 			      (int) atomic_read(&scsidp->device_busy),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 			      (int) scsi_device_online(scsidp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	read_unlock_irqrestore(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	struct scsi_device *scsidp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	read_lock_irqsave(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	sdp = it ? sg_lookup_dev(it->index) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	scsidp = sdp ? sdp->device : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	if (sdp && scsidp && (!atomic_read(&sdp->detaching)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 			   scsidp->vendor, scsidp->model, scsidp->rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 		seq_puts(s, "<no active device>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	read_unlock_irqrestore(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) /* must be called while holding sg_index_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	int k, new_interface, blen, usg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	Sg_request *srp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	Sg_fd *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	const sg_io_hdr_t *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	const char * cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	unsigned int ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 		k++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 		read_lock(&fp->rq_list_lock); /* irqs already disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		seq_printf(s, "   FD(%d): timeout=%dms bufflen=%d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 			   "(res)sgat=%d low_dma=%d\n", k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 			   jiffies_to_msecs(fp->timeout),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 			   fp->reserve.bufflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 			   (int) fp->reserve.k_use_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 			   (int) sdp->device->host->unchecked_isa_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 		seq_printf(s, "   cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 			   (int) fp->cmd_q, (int) fp->force_packid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 			   (int) fp->keep_orphan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 		list_for_each_entry(srp, &fp->rq_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 			hp = &srp->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 			new_interface = (hp->interface_id == '\0') ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 			if (srp->res_used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 				if (new_interface &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 				    (SG_FLAG_MMAP_IO & hp->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 					cp = "     mmap>> ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 					cp = "     rb>> ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 				if (SG_INFO_DIRECT_IO_MASK & hp->info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 					cp = "     dio>> ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 					cp = "     ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 			seq_puts(s, cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			blen = srp->data.bufflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			usg = srp->data.k_use_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 			seq_puts(s, srp->done ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 				 ((1 == srp->done) ?  "rcv:" : "fin:")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 				  : "act:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 			seq_printf(s, " id=%d blen=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 				   srp->header.pack_id, blen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 			if (srp->done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 				seq_printf(s, " dur=%d", hp->duration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 				ms = jiffies_to_msecs(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 				seq_printf(s, " t_o/elap=%d/%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 					(new_interface ? hp->timeout :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 						  jiffies_to_msecs(fp->timeout)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 					(ms > hp->duration ? ms - hp->duration : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 			seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 				   (int) srp->data.cmd_opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		if (list_empty(&fp->rq_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 			seq_puts(s, "     No requests active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 		read_unlock(&fp->rq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	Sg_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	if (it && (0 == it->index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 		seq_printf(s, "max_active_device=%d  def_reserved_size=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 			   (int)it->max, sg_big_buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	read_lock_irqsave(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	sdp = it ? sg_lookup_dev(it->index) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	if (NULL == sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 		goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	read_lock(&sdp->sfd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	if (!list_empty(&sdp->sfds)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		if (atomic_read(&sdp->detaching))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 			seq_puts(s, "detaching pending close ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		else if (sdp->device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 			struct scsi_device *scsidp = sdp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 			seq_printf(s, "%d:%d:%d:%llu   em=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 				   scsidp->host->host_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 				   scsidp->channel, scsidp->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 				   scsidp->lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 				   scsidp->host->hostt->emulated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 		seq_printf(s, " sg_tablesize=%d excl=%d open_cnt=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 			   sdp->sg_tablesize, sdp->exclude, sdp->open_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 		sg_proc_debug_helper(s, sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	read_unlock(&sdp->sfd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	read_unlock_irqrestore(&sg_index_lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) #endif				/* CONFIG_SCSI_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) module_init(init_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) module_exit(exit_sg);