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