]>
Commit | Line | Data |
---|---|---|
3d6392cf | 1 | /* |
0c6a89ba | 2 | * bsg.c - block layer implementation of the sg v4 interface |
3d6392cf JA |
3 | * |
4 | * Copyright (C) 2004 Jens Axboe <[email protected]> SUSE Labs | |
5 | * Copyright (C) 2004 Peter M. Jones <[email protected]> | |
6 | * | |
7 | * This file is subject to the terms and conditions of the GNU General Public | |
8 | * License version 2. See the file "COPYING" in the main directory of this | |
9 | * archive for more details. | |
10 | * | |
11 | */ | |
3d6392cf JA |
12 | #include <linux/module.h> |
13 | #include <linux/init.h> | |
14 | #include <linux/file.h> | |
15 | #include <linux/blkdev.h> | |
3d6392cf | 16 | #include <linux/cdev.h> |
ad5ebd2f | 17 | #include <linux/jiffies.h> |
3d6392cf | 18 | #include <linux/percpu.h> |
598443a2 | 19 | #include <linux/idr.h> |
3d6392cf | 20 | #include <linux/bsg.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
3d6392cf JA |
22 | |
23 | #include <scsi/scsi.h> | |
24 | #include <scsi/scsi_ioctl.h> | |
25 | #include <scsi/scsi_cmnd.h> | |
4e2872d6 FT |
26 | #include <scsi/scsi_device.h> |
27 | #include <scsi/scsi_driver.h> | |
3d6392cf JA |
28 | #include <scsi/sg.h> |
29 | ||
0ed081ce FT |
30 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
31 | #define BSG_VERSION "0.4" | |
3d6392cf | 32 | |
3124b65d JT |
33 | #define bsg_dbg(bd, fmt, ...) \ |
34 | pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__) | |
35 | ||
3d6392cf | 36 | struct bsg_device { |
165125e1 | 37 | struct request_queue *queue; |
3d6392cf | 38 | spinlock_t lock; |
3d6392cf | 39 | struct hlist_node dev_list; |
db193954 | 40 | refcount_t ref_count; |
3ada8b7e | 41 | char name[20]; |
3d6392cf | 42 | int max_queue; |
3d6392cf JA |
43 | }; |
44 | ||
5309cb38 | 45 | #define BSG_DEFAULT_CMDS 64 |
292b7f27 | 46 | #define BSG_MAX_DEVS 32768 |
3d6392cf | 47 | |
3d6392cf | 48 | static DEFINE_MUTEX(bsg_mutex); |
598443a2 | 49 | static DEFINE_IDR(bsg_minor_idr); |
3d6392cf | 50 | |
25fd1643 | 51 | #define BSG_LIST_ARRAY_SIZE 8 |
25fd1643 | 52 | static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; |
3d6392cf JA |
53 | |
54 | static struct class *bsg_class; | |
46f6ef4a | 55 | static int bsg_major; |
3d6392cf | 56 | |
1c1133e1 | 57 | static inline struct hlist_head *bsg_dev_idx_hash(int index) |
3d6392cf | 58 | { |
1c1133e1 | 59 | return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)]; |
3d6392cf JA |
60 | } |
61 | ||
17cb960f CH |
62 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
63 | ||
64 | static int bsg_scsi_check_proto(struct sg_io_v4 *hdr) | |
65 | { | |
66 | if (hdr->protocol != BSG_PROTOCOL_SCSI || | |
67 | hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD) | |
68 | return -EINVAL; | |
69 | return 0; | |
70 | } | |
71 | ||
72 | static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr, | |
73 | fmode_t mode) | |
70e36ece | 74 | { |
17cb960f | 75 | struct scsi_request *sreq = scsi_req(rq); |
82ed4db4 | 76 | |
17cb960f CH |
77 | sreq->cmd_len = hdr->request_len; |
78 | if (sreq->cmd_len > BLK_MAX_CDB) { | |
79 | sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL); | |
80 | if (!sreq->cmd) | |
9f5de6b1 FT |
81 | return -ENOMEM; |
82 | } | |
70e36ece | 83 | |
17cb960f | 84 | if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len)) |
70e36ece | 85 | return -EFAULT; |
17cb960f | 86 | if (blk_verify_command(sreq->cmd, mode)) |
70e36ece | 87 | return -EPERM; |
70e36ece FT |
88 | return 0; |
89 | } | |
90 | ||
17cb960f | 91 | static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr) |
3d6392cf | 92 | { |
17cb960f | 93 | struct scsi_request *sreq = scsi_req(rq); |
15d10b61 FT |
94 | int ret = 0; |
95 | ||
17cb960f CH |
96 | /* |
97 | * fill in all the output members | |
98 | */ | |
99 | hdr->device_status = sreq->result & 0xff; | |
100 | hdr->transport_status = host_byte(sreq->result); | |
101 | hdr->driver_status = driver_byte(sreq->result); | |
102 | hdr->info = 0; | |
103 | if (hdr->device_status || hdr->transport_status || hdr->driver_status) | |
104 | hdr->info |= SG_INFO_CHECK; | |
105 | hdr->response_len = 0; | |
3d6392cf | 106 | |
17cb960f CH |
107 | if (sreq->sense_len && hdr->response) { |
108 | int len = min_t(unsigned int, hdr->max_response_len, | |
109 | sreq->sense_len); | |
110 | ||
111 | if (copy_to_user(uptr64(hdr->response), sreq->sense, len)) | |
112 | ret = -EFAULT; | |
113 | else | |
114 | hdr->response_len = len; | |
115 | } | |
116 | ||
117 | if (rq->next_rq) { | |
118 | hdr->dout_resid = sreq->resid_len; | |
119 | hdr->din_resid = scsi_req(rq->next_rq)->resid_len; | |
120 | } else if (rq_data_dir(rq) == READ) { | |
121 | hdr->din_resid = sreq->resid_len; | |
122 | } else { | |
123 | hdr->dout_resid = sreq->resid_len; | |
15d10b61 | 124 | } |
70e36ece | 125 | |
15d10b61 | 126 | return ret; |
3d6392cf JA |
127 | } |
128 | ||
17cb960f CH |
129 | static void bsg_scsi_free_rq(struct request *rq) |
130 | { | |
131 | scsi_req_free_cmd(scsi_req(rq)); | |
132 | } | |
133 | ||
134 | static const struct bsg_ops bsg_scsi_ops = { | |
135 | .check_proto = bsg_scsi_check_proto, | |
136 | .fill_hdr = bsg_scsi_fill_hdr, | |
137 | .complete_rq = bsg_scsi_complete_rq, | |
138 | .free_rq = bsg_scsi_free_rq, | |
139 | }; | |
140 | ||
3d6392cf | 141 | static struct request * |
17cb960f | 142 | bsg_map_hdr(struct request_queue *q, struct sg_io_v4 *hdr, fmode_t mode) |
3d6392cf | 143 | { |
2c9ecdf4 | 144 | struct request *rq, *next_rq = NULL; |
aebf526b | 145 | int ret; |
c7a841f3 | 146 | |
17cb960f | 147 | if (!q->bsg_dev.class_dev) |
c7a841f3 | 148 | return ERR_PTR(-ENXIO); |
3d6392cf | 149 | |
17cb960f CH |
150 | if (hdr->guard != 'Q') |
151 | return ERR_PTR(-EINVAL); | |
3d6392cf | 152 | |
17cb960f | 153 | ret = q->bsg_dev.ops->check_proto(hdr); |
3d6392cf JA |
154 | if (ret) |
155 | return ERR_PTR(ret); | |
156 | ||
17cb960f | 157 | rq = blk_get_request(q, hdr->dout_xfer_len ? |
ff005a06 | 158 | REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0); |
a492f075 JL |
159 | if (IS_ERR(rq)) |
160 | return rq; | |
f27b087b | 161 | |
17cb960f | 162 | ret = q->bsg_dev.ops->fill_hdr(rq, hdr, mode); |
2c9ecdf4 FT |
163 | if (ret) |
164 | goto out; | |
165 | ||
17cb960f CH |
166 | rq->timeout = msecs_to_jiffies(hdr->timeout); |
167 | if (!rq->timeout) | |
168 | rq->timeout = q->sg_timeout; | |
169 | if (!rq->timeout) | |
170 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; | |
171 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) | |
172 | rq->timeout = BLK_MIN_SG_TIMEOUT; | |
173 | ||
174 | if (hdr->dout_xfer_len && hdr->din_xfer_len) { | |
2c9ecdf4 FT |
175 | if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) { |
176 | ret = -EOPNOTSUPP; | |
177 | goto out; | |
178 | } | |
179 | ||
ff005a06 | 180 | next_rq = blk_get_request(q, REQ_OP_SCSI_IN, 0); |
a492f075 JL |
181 | if (IS_ERR(next_rq)) { |
182 | ret = PTR_ERR(next_rq); | |
2c9ecdf4 FT |
183 | goto out; |
184 | } | |
2c9ecdf4 | 185 | |
17cb960f CH |
186 | rq->next_rq = next_rq; |
187 | ret = blk_rq_map_user(q, next_rq, NULL, uptr64(hdr->din_xferp), | |
152e283f | 188 | hdr->din_xfer_len, GFP_KERNEL); |
2c9ecdf4 | 189 | if (ret) |
17cb960f | 190 | goto out_free_nextrq; |
3d6392cf JA |
191 | } |
192 | ||
70e36ece | 193 | if (hdr->dout_xfer_len) { |
17cb960f CH |
194 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->dout_xferp), |
195 | hdr->dout_xfer_len, GFP_KERNEL); | |
70e36ece | 196 | } else if (hdr->din_xfer_len) { |
17cb960f CH |
197 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->din_xferp), |
198 | hdr->din_xfer_len, GFP_KERNEL); | |
3d6392cf | 199 | } |
c1c20120 | 200 | |
17cb960f CH |
201 | if (ret) |
202 | goto out_unmap_nextrq; | |
3d6392cf | 203 | return rq; |
17cb960f CH |
204 | |
205 | out_unmap_nextrq: | |
206 | if (rq->next_rq) | |
207 | blk_rq_unmap_user(rq->next_rq->bio); | |
208 | out_free_nextrq: | |
209 | if (rq->next_rq) | |
210 | blk_put_request(rq->next_rq); | |
2c9ecdf4 | 211 | out: |
17cb960f | 212 | q->bsg_dev.ops->free_rq(rq); |
2c9ecdf4 | 213 | blk_put_request(rq); |
2c9ecdf4 | 214 | return ERR_PTR(ret); |
3d6392cf JA |
215 | } |
216 | ||
70e36ece | 217 | static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr, |
2c9ecdf4 | 218 | struct bio *bio, struct bio *bidi_bio) |
70e36ece | 219 | { |
17cb960f | 220 | int ret; |
70e36ece | 221 | |
17cb960f | 222 | ret = rq->q->bsg_dev.ops->complete_rq(rq, hdr); |
70e36ece | 223 | |
2c9ecdf4 FT |
224 | if (rq->next_rq) { |
225 | blk_rq_unmap_user(bidi_bio); | |
226 | blk_put_request(rq->next_rq); | |
17cb960f | 227 | } |
2d507a01 | 228 | |
70e36ece | 229 | blk_rq_unmap_user(bio); |
17cb960f | 230 | rq->q->bsg_dev.ops->free_rq(rq); |
70e36ece | 231 | blk_put_request(rq); |
70e36ece FT |
232 | return ret; |
233 | } | |
234 | ||
3d6392cf JA |
235 | static struct bsg_device *bsg_alloc_device(void) |
236 | { | |
3d6392cf | 237 | struct bsg_device *bd; |
3d6392cf JA |
238 | |
239 | bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); | |
240 | if (unlikely(!bd)) | |
241 | return NULL; | |
242 | ||
243 | spin_lock_init(&bd->lock); | |
5309cb38 | 244 | bd->max_queue = BSG_DEFAULT_CMDS; |
3d6392cf | 245 | INIT_HLIST_NODE(&bd->dev_list); |
3d6392cf | 246 | return bd; |
3d6392cf JA |
247 | } |
248 | ||
249 | static int bsg_put_device(struct bsg_device *bd) | |
250 | { | |
97f46ae4 | 251 | struct request_queue *q = bd->queue; |
3d6392cf JA |
252 | |
253 | mutex_lock(&bsg_mutex); | |
254 | ||
db193954 | 255 | if (!refcount_dec_and_test(&bd->ref_count)) { |
3f27e3ed | 256 | mutex_unlock(&bsg_mutex); |
28519c89 | 257 | return 0; |
3f27e3ed FT |
258 | } |
259 | ||
260 | hlist_del(&bd->dev_list); | |
261 | mutex_unlock(&bsg_mutex); | |
3d6392cf | 262 | |
3124b65d | 263 | bsg_dbg(bd, "tearing down\n"); |
3d6392cf JA |
264 | |
265 | /* | |
266 | * close can always block | |
267 | */ | |
5309cb38 | 268 | kfree(bd); |
28519c89 CH |
269 | blk_put_queue(q); |
270 | return 0; | |
3d6392cf JA |
271 | } |
272 | ||
273 | static struct bsg_device *bsg_add_device(struct inode *inode, | |
d351af01 | 274 | struct request_queue *rq, |
3d6392cf JA |
275 | struct file *file) |
276 | { | |
25fd1643 | 277 | struct bsg_device *bd; |
3d6392cf | 278 | unsigned char buf[32]; |
d9f97264 | 279 | |
d6c73964 AG |
280 | lockdep_assert_held(&bsg_mutex); |
281 | ||
09ac46c4 | 282 | if (!blk_get_queue(rq)) |
c3ff1b90 | 283 | return ERR_PTR(-ENXIO); |
3d6392cf JA |
284 | |
285 | bd = bsg_alloc_device(); | |
c3ff1b90 FT |
286 | if (!bd) { |
287 | blk_put_queue(rq); | |
3d6392cf | 288 | return ERR_PTR(-ENOMEM); |
c3ff1b90 | 289 | } |
3d6392cf | 290 | |
d351af01 | 291 | bd->queue = rq; |
0b07de85 | 292 | |
db193954 | 293 | refcount_set(&bd->ref_count, 1); |
842ea771 | 294 | hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
3d6392cf | 295 | |
3ada8b7e | 296 | strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); |
3124b65d | 297 | bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
9e69fbb5 | 298 | format_dev_t(buf, inode->i_rdev), bd->max_queue); |
3d6392cf | 299 | |
3d6392cf JA |
300 | return bd; |
301 | } | |
302 | ||
842ea771 | 303 | static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) |
3d6392cf | 304 | { |
43ac9e62 | 305 | struct bsg_device *bd; |
3d6392cf | 306 | |
d6c73964 | 307 | lockdep_assert_held(&bsg_mutex); |
3d6392cf | 308 | |
b67bfe0d | 309 | hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
842ea771 | 310 | if (bd->queue == q) { |
db193954 | 311 | refcount_inc(&bd->ref_count); |
43ac9e62 | 312 | goto found; |
3d6392cf | 313 | } |
3d6392cf | 314 | } |
43ac9e62 FT |
315 | bd = NULL; |
316 | found: | |
3d6392cf JA |
317 | return bd; |
318 | } | |
319 | ||
320 | static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) | |
321 | { | |
598443a2 FT |
322 | struct bsg_device *bd; |
323 | struct bsg_class_device *bcd; | |
3d6392cf | 324 | |
3d6392cf JA |
325 | /* |
326 | * find the class device | |
327 | */ | |
3d6392cf | 328 | mutex_lock(&bsg_mutex); |
598443a2 | 329 | bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
3d6392cf | 330 | |
d6c73964 AG |
331 | if (!bcd) { |
332 | bd = ERR_PTR(-ENODEV); | |
333 | goto out_unlock; | |
334 | } | |
3d6392cf | 335 | |
842ea771 | 336 | bd = __bsg_get_device(iminor(inode), bcd->queue); |
d6c73964 AG |
337 | if (!bd) |
338 | bd = bsg_add_device(inode, bcd->queue, file); | |
d45ac4fa | 339 | |
d6c73964 AG |
340 | out_unlock: |
341 | mutex_unlock(&bsg_mutex); | |
d45ac4fa | 342 | return bd; |
3d6392cf JA |
343 | } |
344 | ||
345 | static int bsg_open(struct inode *inode, struct file *file) | |
346 | { | |
75bd2ef1 JC |
347 | struct bsg_device *bd; |
348 | ||
75bd2ef1 | 349 | bd = bsg_get_device(inode, file); |
3d6392cf JA |
350 | |
351 | if (IS_ERR(bd)) | |
352 | return PTR_ERR(bd); | |
353 | ||
354 | file->private_data = bd; | |
355 | return 0; | |
356 | } | |
357 | ||
358 | static int bsg_release(struct inode *inode, struct file *file) | |
359 | { | |
360 | struct bsg_device *bd = file->private_data; | |
361 | ||
362 | file->private_data = NULL; | |
363 | return bsg_put_device(bd); | |
364 | } | |
365 | ||
25fd1643 | 366 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
3d6392cf JA |
367 | { |
368 | struct bsg_device *bd = file->private_data; | |
369 | int __user *uarg = (int __user *) arg; | |
2d507a01 | 370 | int ret; |
3d6392cf | 371 | |
3d6392cf JA |
372 | switch (cmd) { |
373 | /* | |
374 | * our own ioctls | |
375 | */ | |
376 | case SG_GET_COMMAND_Q: | |
377 | return put_user(bd->max_queue, uarg); | |
5309cb38 | 378 | case SG_SET_COMMAND_Q: { |
3d6392cf JA |
379 | int queue; |
380 | ||
381 | if (get_user(queue, uarg)) | |
382 | return -EFAULT; | |
5309cb38 | 383 | if (queue < 1) |
3d6392cf JA |
384 | return -EINVAL; |
385 | ||
5309cb38 | 386 | spin_lock_irq(&bd->lock); |
3d6392cf | 387 | bd->max_queue = queue; |
5309cb38 | 388 | spin_unlock_irq(&bd->lock); |
3d6392cf JA |
389 | return 0; |
390 | } | |
391 | ||
392 | /* | |
393 | * SCSI/sg ioctls | |
394 | */ | |
395 | case SG_GET_VERSION_NUM: | |
396 | case SCSI_IOCTL_GET_IDLUN: | |
397 | case SCSI_IOCTL_GET_BUS_NUMBER: | |
398 | case SG_SET_TIMEOUT: | |
399 | case SG_GET_TIMEOUT: | |
400 | case SG_GET_RESERVED_SIZE: | |
401 | case SG_SET_RESERVED_SIZE: | |
402 | case SG_EMULATED_HOST: | |
3d6392cf JA |
403 | case SCSI_IOCTL_SEND_COMMAND: { |
404 | void __user *uarg = (void __user *) arg; | |
74f3c8af | 405 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
3d6392cf | 406 | } |
10e8855b FT |
407 | case SG_IO: { |
408 | struct request *rq; | |
2c9ecdf4 | 409 | struct bio *bio, *bidi_bio = NULL; |
10e8855b | 410 | struct sg_io_v4 hdr; |
05378940 | 411 | int at_head; |
10e8855b FT |
412 | |
413 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) | |
414 | return -EFAULT; | |
415 | ||
17cb960f | 416 | rq = bsg_map_hdr(bd->queue, &hdr, file->f_mode); |
10e8855b FT |
417 | if (IS_ERR(rq)) |
418 | return PTR_ERR(rq); | |
419 | ||
420 | bio = rq->bio; | |
2c9ecdf4 FT |
421 | if (rq->next_rq) |
422 | bidi_bio = rq->next_rq->bio; | |
05378940 BH |
423 | |
424 | at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL)); | |
425 | blk_execute_rq(bd->queue, NULL, rq, at_head); | |
2d507a01 | 426 | ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio); |
10e8855b FT |
427 | |
428 | if (copy_to_user(uarg, &hdr, sizeof(hdr))) | |
429 | return -EFAULT; | |
b711afa6 | 430 | |
2d507a01 | 431 | return ret; |
10e8855b | 432 | } |
3d6392cf | 433 | default: |
3d6392cf | 434 | return -ENOTTY; |
3d6392cf JA |
435 | } |
436 | } | |
437 | ||
7344be05 | 438 | static const struct file_operations bsg_fops = { |
3d6392cf JA |
439 | .open = bsg_open, |
440 | .release = bsg_release, | |
25fd1643 | 441 | .unlocked_ioctl = bsg_ioctl, |
3d6392cf | 442 | .owner = THIS_MODULE, |
6038f373 | 443 | .llseek = default_llseek, |
3d6392cf JA |
444 | }; |
445 | ||
d351af01 | 446 | void bsg_unregister_queue(struct request_queue *q) |
3d6392cf | 447 | { |
d351af01 | 448 | struct bsg_class_device *bcd = &q->bsg_dev; |
3d6392cf | 449 | |
df468820 FT |
450 | if (!bcd->class_dev) |
451 | return; | |
3d6392cf JA |
452 | |
453 | mutex_lock(&bsg_mutex); | |
598443a2 | 454 | idr_remove(&bsg_minor_idr, bcd->minor); |
37b40adf SG |
455 | if (q->kobj.sd) |
456 | sysfs_remove_link(&q->kobj, "bsg"); | |
ee959b00 | 457 | device_unregister(bcd->class_dev); |
3d6392cf | 458 | bcd->class_dev = NULL; |
3d6392cf JA |
459 | mutex_unlock(&bsg_mutex); |
460 | } | |
4cf0723a | 461 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
3d6392cf | 462 | |
97f46ae4 | 463 | int bsg_register_queue(struct request_queue *q, struct device *parent, |
5de815a7 | 464 | const char *name, const struct bsg_ops *ops) |
3d6392cf | 465 | { |
598443a2 | 466 | struct bsg_class_device *bcd; |
3d6392cf | 467 | dev_t dev; |
bab998d6 | 468 | int ret; |
ee959b00 | 469 | struct device *class_dev = NULL; |
3d6392cf JA |
470 | |
471 | /* | |
472 | * we need a proper transport to send commands, not a stacked device | |
473 | */ | |
49fd524f | 474 | if (!queue_is_rq_based(q)) |
3d6392cf JA |
475 | return 0; |
476 | ||
d351af01 | 477 | bcd = &q->bsg_dev; |
3d6392cf | 478 | memset(bcd, 0, sizeof(*bcd)); |
3d6392cf JA |
479 | |
480 | mutex_lock(&bsg_mutex); | |
292b7f27 | 481 | |
bab998d6 TH |
482 | ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); |
483 | if (ret < 0) { | |
484 | if (ret == -ENOSPC) { | |
485 | printk(KERN_ERR "bsg: too many bsg devices\n"); | |
486 | ret = -EINVAL; | |
487 | } | |
598443a2 | 488 | goto unlock; |
598443a2 FT |
489 | } |
490 | ||
bab998d6 | 491 | bcd->minor = ret; |
d351af01 | 492 | bcd->queue = q; |
17cb960f | 493 | bcd->ops = ops; |
46f6ef4a | 494 | dev = MKDEV(bsg_major, bcd->minor); |
5de815a7 | 495 | class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); |
4e2872d6 FT |
496 | if (IS_ERR(class_dev)) { |
497 | ret = PTR_ERR(class_dev); | |
5de815a7 | 498 | goto idr_remove; |
4e2872d6 FT |
499 | } |
500 | bcd->class_dev = class_dev; | |
501 | ||
abce891a | 502 | if (q->kobj.sd) { |
4e2872d6 FT |
503 | ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
504 | if (ret) | |
598443a2 | 505 | goto unregister_class_dev; |
4e2872d6 FT |
506 | } |
507 | ||
3d6392cf JA |
508 | mutex_unlock(&bsg_mutex); |
509 | return 0; | |
6826ee4f | 510 | |
598443a2 | 511 | unregister_class_dev: |
ee959b00 | 512 | device_unregister(class_dev); |
5de815a7 | 513 | idr_remove: |
bab998d6 | 514 | idr_remove(&bsg_minor_idr, bcd->minor); |
598443a2 | 515 | unlock: |
264a0472 | 516 | mutex_unlock(&bsg_mutex); |
4e2872d6 FT |
517 | return ret; |
518 | } | |
17cb960f CH |
519 | |
520 | int bsg_scsi_register_queue(struct request_queue *q, struct device *parent) | |
521 | { | |
522 | if (!blk_queue_scsi_passthrough(q)) { | |
523 | WARN_ONCE(true, "Attempt to register a non-SCSI queue\n"); | |
524 | return -EINVAL; | |
525 | } | |
526 | ||
5de815a7 | 527 | return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops); |
17cb960f CH |
528 | } |
529 | EXPORT_SYMBOL_GPL(bsg_scsi_register_queue); | |
4e2872d6 | 530 | |
7e7654a9 | 531 | static struct cdev bsg_cdev; |
292b7f27 | 532 | |
2c9ede55 | 533 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
2bdf9149 KS |
534 | { |
535 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); | |
536 | } | |
537 | ||
3d6392cf JA |
538 | static int __init bsg_init(void) |
539 | { | |
540 | int ret, i; | |
46f6ef4a | 541 | dev_t devid; |
3d6392cf | 542 | |
25fd1643 | 543 | for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
3d6392cf JA |
544 | INIT_HLIST_HEAD(&bsg_device_list[i]); |
545 | ||
546 | bsg_class = class_create(THIS_MODULE, "bsg"); | |
28519c89 CH |
547 | if (IS_ERR(bsg_class)) |
548 | return PTR_ERR(bsg_class); | |
e454cea2 | 549 | bsg_class->devnode = bsg_devnode; |
3d6392cf | 550 | |
46f6ef4a | 551 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
9b9f770c FT |
552 | if (ret) |
553 | goto destroy_bsg_class; | |
292b7f27 | 554 | |
46f6ef4a JA |
555 | bsg_major = MAJOR(devid); |
556 | ||
292b7f27 | 557 | cdev_init(&bsg_cdev, &bsg_fops); |
46f6ef4a | 558 | ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
9b9f770c FT |
559 | if (ret) |
560 | goto unregister_chrdev; | |
3d6392cf | 561 | |
5d3a8cd3 | 562 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
0ed081ce | 563 | " loaded (major %d)\n", bsg_major); |
3d6392cf | 564 | return 0; |
9b9f770c FT |
565 | unregister_chrdev: |
566 | unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); | |
567 | destroy_bsg_class: | |
568 | class_destroy(bsg_class); | |
9b9f770c | 569 | return ret; |
3d6392cf JA |
570 | } |
571 | ||
572 | MODULE_AUTHOR("Jens Axboe"); | |
0ed081ce | 573 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
3d6392cf JA |
574 | MODULE_LICENSE("GPL"); |
575 | ||
4e2872d6 | 576 | device_initcall(bsg_init); |