-
Notifications
You must be signed in to change notification settings - Fork 3
/
hisi_blk_scsi_kern.c
295 lines (257 loc) · 6.65 KB
/
hisi_blk_scsi_kern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* hisi_ufs_ioctl.c - block layer implementation of the sg v4 interface
*
* Copyright (C) 2004 Jens Axboe <[email protected]> SUSE Labs
* Copyright (C) 2004 Peter M. Jones <[email protected]>
*
* This file is subject to the terms and conditions of the GNU General Public
* License version 2. See the file "COPYING" in the main directory of this
* archive for more details.
*
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/blkdev.h>
#include <linux/poll.h>
#include <linux/cdev.h>
#include <linux/jiffies.h>
#include <linux/percpu.h>
#include <linux/uio.h>
#include <linux/idr.h>
#include <linux/bsg.h>
#include <linux/slab.h>
#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
#include <scsi/sg.h>
struct blk_scsi_device {
struct request_queue *queue;
spinlock_t lock;
struct list_head busy_list;
struct list_head done_list;
struct hlist_node dev_list;
atomic_t ref_count;
int queued_cmds;
int done_cmds;
wait_queue_head_t wq_done;
wait_queue_head_t wq_free;
char name[20];
int max_queue;
unsigned long flags;
};
static long hisi_blk_kern_copy_data(void *to, const void *from, unsigned long n)
{
memcpy(to, from, n);
return 0;
}
static int hisi_blk_kern_fill_sgv4_hdr_rq(struct request_queue *q,
struct request *rq,
struct sg_io_v4 *hdr)
{
if (hdr->request_len > BLK_MAX_CDB) {
rq->cmd = kzalloc((size_t)hdr->request_len, GFP_KERNEL);
if (!rq->cmd)
return -ENOMEM;
}
if (hisi_blk_kern_copy_data((void *)rq->cmd,
(void *)(unsigned long)hdr->request,
(unsigned long)hdr->request_len))
return -EFAULT;
/*
* fill in request structure
*/
rq->cmd_len = (unsigned short)hdr->request_len;
rq->timeout = (unsigned int)msecs_to_jiffies(hdr->timeout);
if (!rq->timeout)
rq->timeout = q->sg_timeout;
if (!rq->timeout)
rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
if (rq->timeout < BLK_MIN_SG_TIMEOUT)
rq->timeout = BLK_MIN_SG_TIMEOUT;
return 0;
}
/*
* Check if sg_io_v4 from kernel is allowed and valid
*/
static int hisi_blk_kern_validate_sgv4_hdr(struct sg_io_v4 *hdr,
int *rw)
{
int ret = 0;
if (hdr->guard != 'Q')
return -EINVAL;
switch (hdr->protocol) {
case BSG_PROTOCOL_SCSI:
switch (hdr->subprotocol) {
case BSG_SUB_PROTOCOL_SCSI_CMD:
case BSG_SUB_PROTOCOL_SCSI_TRANSPORT:
break;
default:
ret = -EINVAL;
}
break;
default:
ret = -EINVAL;
}
*rw = hdr->dout_xfer_len ? WRITE : READ;
return ret;
}
/*
* map sg_io_v4 to a request.
*/
static struct request *hisi_blk_kern_map_hdr(struct blk_scsi_device *bd,
struct sg_io_v4 *hdr,
u8 *sense)
{
struct request_queue *q = bd->queue;
struct request *rq = NULL, *next_rq = NULL;
int ret, rw;
unsigned int dxfer_len;
void *dxferp = NULL;
struct bsg_class_device *bcd = &q->bsg_dev;
/* if the LLD has been removed then the bsg_unregister_queue will
* eventually be called and the class_dev was freed, so we can no
* longer use this request_queue. Return no such address.
*/
if (!bcd->class_dev)
return ERR_PTR(-ENXIO);
ret = hisi_blk_kern_validate_sgv4_hdr(hdr, &rw);
if (ret)
return ERR_PTR(ret);
/*
* map scatter-gather elements separately and string them to request
*/
rq = blk_get_request(q, rw, GFP_KERNEL);
if (IS_ERR(rq))
return rq;
blk_rq_set_block_pc(rq);
ret = hisi_blk_kern_fill_sgv4_hdr_rq(q, rq, hdr);
if (ret)
goto out;
if (rw == WRITE && hdr->din_xfer_len) {
if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
ret = -EOPNOTSUPP;
goto out;
}
next_rq = blk_get_request(q, READ, GFP_KERNEL);
if (IS_ERR(next_rq)) {
ret = PTR_ERR(next_rq);
next_rq = NULL;
goto out;
}
rq->next_rq = next_rq;
next_rq->cmd_type = rq->cmd_type;
dxferp = (void __user *)(unsigned long)hdr->din_xferp;
ret = blk_rq_map_kern(q, next_rq, dxferp, hdr->din_xfer_len,
GFP_KERNEL);
if (ret)
goto out;
}
if (hdr->dout_xfer_len) {
dxfer_len = hdr->dout_xfer_len;
dxferp = (void *)(unsigned long)hdr->dout_xferp;
} else if (hdr->din_xfer_len) {
dxfer_len = hdr->din_xfer_len;
dxferp = (void *)(unsigned long)hdr->din_xferp;
} else
dxfer_len = 0;
if (dxfer_len) {
ret = blk_rq_map_kern(q, rq, dxferp, dxfer_len, GFP_KERNEL);
if (ret)
goto out;
}
rq->sense = sense;
rq->sense_len = 0;
return rq;
out:
if (rq->cmd != rq->__cmd)
kfree(rq->cmd);
blk_put_request(rq);
if (next_rq) {
blk_put_request(next_rq);
}
return ERR_PTR(ret);
}
static int hisi_blk_kern_complete_hdr_rq(struct request *rq,
struct sg_io_v4 *hdr)
{
int ret = 0;
/*
* fill in all the output members
*/
hdr->device_status = rq->errors & 0xff;
hdr->transport_status = host_byte(rq->errors);
hdr->driver_status = driver_byte(rq->errors);
hdr->info = 0;
if (hdr->device_status || hdr->transport_status || hdr->driver_status)
hdr->info |= SG_INFO_CHECK;
hdr->response_len = 0;
if (rq->sense_len && hdr->response) {
unsigned int len = min_t(unsigned int, hdr->max_response_len,
rq->sense_len);
ret = hisi_blk_kern_copy_data(
(void *)(unsigned long)hdr->response, rq->sense, (unsigned long)len);
if (!ret)
hdr->response_len = len;
else
ret = -EFAULT;
}
if (rq->next_rq) {
hdr->dout_resid = (__s32)rq->resid_len;
hdr->din_resid = (__s32)rq->next_rq->resid_len;
blk_put_request(rq->next_rq);
} else if (rq_data_dir(rq) == READ)
hdr->din_resid = (__s32)rq->resid_len;
else
hdr->dout_resid = (__s32)rq->resid_len;
/*
* If the request generated a negative error number, return it
* (providing we aren't already returning an error); if it's
* just a protocol response (i.e. non negative), that gets
* processed above.
*/
if (!ret && rq->errors < 0)
ret = rq->errors;
if (rq->cmd != rq->__cmd)
kfree(rq->cmd);
blk_put_request(rq);
return ret;
}
long blk_scsi_kern_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
{
int *uarg = (int *)arg;
int ret;
struct request *rq;
struct sg_io_v4 hdr;
int at_head;
u8 sense[SCSI_SENSE_BUFFERSIZE];
struct blk_scsi_device *bd;
struct file *pfile = fget(fd);
if (pfile == NULL)
return -EFAULT;
bd = (struct blk_scsi_device *)pfile->private_data;
switch (cmd) {
case SG_IO:
if (hisi_blk_kern_copy_data(&hdr, uarg, sizeof(hdr)))
return -EFAULT;
rq = hisi_blk_kern_map_hdr(bd, &hdr, sense);
if (IS_ERR(rq))
return PTR_ERR(rq);
at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL));
blk_execute_rq(bd->queue, NULL, rq, at_head);
ret = hisi_blk_kern_complete_hdr_rq(rq, &hdr);
if (hisi_blk_kern_copy_data(uarg, &hdr, sizeof(hdr)))
return -EFAULT;
break;
/*
* block device ioctls
*/
default:
ret = -ENOTTY;
}
fput(pfile);
return ret;
}