]>
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 <axboe@suse.de> SUSE Labs | |
5 | * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com> | |
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 | ||
ccf3209f | 141 | static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) |
3d6392cf | 142 | { |
2c9ecdf4 | 143 | struct request *rq, *next_rq = NULL; |
ccf3209f CH |
144 | struct bio *bio, *bidi_bio = NULL; |
145 | struct sg_io_v4 hdr; | |
aebf526b | 146 | int ret; |
c7a841f3 | 147 | |
ccf3209f CH |
148 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
149 | return -EFAULT; | |
3d6392cf | 150 | |
ccf3209f CH |
151 | if (!q->bsg_dev.class_dev) |
152 | return -ENXIO; | |
3d6392cf | 153 | |
ccf3209f CH |
154 | if (hdr.guard != 'Q') |
155 | return -EINVAL; | |
156 | ret = q->bsg_dev.ops->check_proto(&hdr); | |
3d6392cf | 157 | if (ret) |
ccf3209f | 158 | return ret; |
3d6392cf | 159 | |
ccf3209f | 160 | rq = blk_get_request(q, hdr.dout_xfer_len ? |
ff005a06 | 161 | REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0); |
a492f075 | 162 | if (IS_ERR(rq)) |
ccf3209f | 163 | return PTR_ERR(rq); |
f27b087b | 164 | |
ccf3209f | 165 | ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode); |
2c9ecdf4 FT |
166 | if (ret) |
167 | goto out; | |
168 | ||
ccf3209f | 169 | rq->timeout = msecs_to_jiffies(hdr.timeout); |
17cb960f CH |
170 | if (!rq->timeout) |
171 | rq->timeout = q->sg_timeout; | |
172 | if (!rq->timeout) | |
173 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; | |
174 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) | |
175 | rq->timeout = BLK_MIN_SG_TIMEOUT; | |
176 | ||
ccf3209f | 177 | if (hdr.dout_xfer_len && hdr.din_xfer_len) { |
2c9ecdf4 FT |
178 | if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) { |
179 | ret = -EOPNOTSUPP; | |
180 | goto out; | |
181 | } | |
182 | ||
2e5b2d7c CH |
183 | pr_warn_once( |
184 | "BIDI support in bsg has been deprecated and might be removed. " | |
185 | "Please report your use case to linux-scsi@vger.kernel.org\n"); | |
186 | ||
ff005a06 | 187 | next_rq = blk_get_request(q, REQ_OP_SCSI_IN, 0); |
a492f075 JL |
188 | if (IS_ERR(next_rq)) { |
189 | ret = PTR_ERR(next_rq); | |
2c9ecdf4 FT |
190 | goto out; |
191 | } | |
2c9ecdf4 | 192 | |
17cb960f | 193 | rq->next_rq = next_rq; |
ccf3209f CH |
194 | ret = blk_rq_map_user(q, next_rq, NULL, uptr64(hdr.din_xferp), |
195 | hdr.din_xfer_len, GFP_KERNEL); | |
2c9ecdf4 | 196 | if (ret) |
17cb960f | 197 | goto out_free_nextrq; |
3d6392cf JA |
198 | } |
199 | ||
ccf3209f CH |
200 | if (hdr.dout_xfer_len) { |
201 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp), | |
202 | hdr.dout_xfer_len, GFP_KERNEL); | |
203 | } else if (hdr.din_xfer_len) { | |
204 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp), | |
205 | hdr.din_xfer_len, GFP_KERNEL); | |
3d6392cf | 206 | } |
c1c20120 | 207 | |
17cb960f CH |
208 | if (ret) |
209 | goto out_unmap_nextrq; | |
17cb960f | 210 | |
ccf3209f | 211 | bio = rq->bio; |
17cb960f | 212 | if (rq->next_rq) |
ccf3209f | 213 | bidi_bio = rq->next_rq->bio; |
70e36ece | 214 | |
ccf3209f CH |
215 | blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
216 | ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); | |
70e36ece | 217 | |
2c9ecdf4 FT |
218 | if (rq->next_rq) { |
219 | blk_rq_unmap_user(bidi_bio); | |
220 | blk_put_request(rq->next_rq); | |
17cb960f | 221 | } |
2d507a01 | 222 | |
70e36ece | 223 | blk_rq_unmap_user(bio); |
17cb960f | 224 | rq->q->bsg_dev.ops->free_rq(rq); |
70e36ece | 225 | blk_put_request(rq); |
ccf3209f CH |
226 | |
227 | if (copy_to_user(uarg, &hdr, sizeof(hdr))) | |
228 | return -EFAULT; | |
229 | return ret; | |
230 | ||
231 | out_unmap_nextrq: | |
232 | if (rq->next_rq) | |
233 | blk_rq_unmap_user(rq->next_rq->bio); | |
234 | out_free_nextrq: | |
235 | if (rq->next_rq) | |
236 | blk_put_request(rq->next_rq); | |
237 | out: | |
238 | q->bsg_dev.ops->free_rq(rq); | |
239 | blk_put_request(rq); | |
70e36ece FT |
240 | return ret; |
241 | } | |
242 | ||
3d6392cf JA |
243 | static struct bsg_device *bsg_alloc_device(void) |
244 | { | |
3d6392cf | 245 | struct bsg_device *bd; |
3d6392cf JA |
246 | |
247 | bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); | |
248 | if (unlikely(!bd)) | |
249 | return NULL; | |
250 | ||
251 | spin_lock_init(&bd->lock); | |
5309cb38 | 252 | bd->max_queue = BSG_DEFAULT_CMDS; |
3d6392cf | 253 | INIT_HLIST_NODE(&bd->dev_list); |
3d6392cf | 254 | return bd; |
3d6392cf JA |
255 | } |
256 | ||
257 | static int bsg_put_device(struct bsg_device *bd) | |
258 | { | |
97f46ae4 | 259 | struct request_queue *q = bd->queue; |
3d6392cf JA |
260 | |
261 | mutex_lock(&bsg_mutex); | |
262 | ||
db193954 | 263 | if (!refcount_dec_and_test(&bd->ref_count)) { |
3f27e3ed | 264 | mutex_unlock(&bsg_mutex); |
28519c89 | 265 | return 0; |
3f27e3ed FT |
266 | } |
267 | ||
268 | hlist_del(&bd->dev_list); | |
269 | mutex_unlock(&bsg_mutex); | |
3d6392cf | 270 | |
3124b65d | 271 | bsg_dbg(bd, "tearing down\n"); |
3d6392cf JA |
272 | |
273 | /* | |
274 | * close can always block | |
275 | */ | |
5309cb38 | 276 | kfree(bd); |
28519c89 CH |
277 | blk_put_queue(q); |
278 | return 0; | |
3d6392cf JA |
279 | } |
280 | ||
281 | static struct bsg_device *bsg_add_device(struct inode *inode, | |
d351af01 | 282 | struct request_queue *rq, |
3d6392cf JA |
283 | struct file *file) |
284 | { | |
25fd1643 | 285 | struct bsg_device *bd; |
3d6392cf | 286 | unsigned char buf[32]; |
d9f97264 | 287 | |
d6c73964 AG |
288 | lockdep_assert_held(&bsg_mutex); |
289 | ||
09ac46c4 | 290 | if (!blk_get_queue(rq)) |
c3ff1b90 | 291 | return ERR_PTR(-ENXIO); |
3d6392cf JA |
292 | |
293 | bd = bsg_alloc_device(); | |
c3ff1b90 FT |
294 | if (!bd) { |
295 | blk_put_queue(rq); | |
3d6392cf | 296 | return ERR_PTR(-ENOMEM); |
c3ff1b90 | 297 | } |
3d6392cf | 298 | |
d351af01 | 299 | bd->queue = rq; |
0b07de85 | 300 | |
db193954 | 301 | refcount_set(&bd->ref_count, 1); |
842ea771 | 302 | hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
3d6392cf | 303 | |
3ada8b7e | 304 | strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); |
3124b65d | 305 | bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
9e69fbb5 | 306 | format_dev_t(buf, inode->i_rdev), bd->max_queue); |
3d6392cf | 307 | |
3d6392cf JA |
308 | return bd; |
309 | } | |
310 | ||
842ea771 | 311 | static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) |
3d6392cf | 312 | { |
43ac9e62 | 313 | struct bsg_device *bd; |
3d6392cf | 314 | |
d6c73964 | 315 | lockdep_assert_held(&bsg_mutex); |
3d6392cf | 316 | |
b67bfe0d | 317 | hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
842ea771 | 318 | if (bd->queue == q) { |
db193954 | 319 | refcount_inc(&bd->ref_count); |
43ac9e62 | 320 | goto found; |
3d6392cf | 321 | } |
3d6392cf | 322 | } |
43ac9e62 FT |
323 | bd = NULL; |
324 | found: | |
3d6392cf JA |
325 | return bd; |
326 | } | |
327 | ||
328 | static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) | |
329 | { | |
598443a2 FT |
330 | struct bsg_device *bd; |
331 | struct bsg_class_device *bcd; | |
3d6392cf | 332 | |
3d6392cf JA |
333 | /* |
334 | * find the class device | |
335 | */ | |
3d6392cf | 336 | mutex_lock(&bsg_mutex); |
598443a2 | 337 | bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
3d6392cf | 338 | |
d6c73964 AG |
339 | if (!bcd) { |
340 | bd = ERR_PTR(-ENODEV); | |
341 | goto out_unlock; | |
342 | } | |
3d6392cf | 343 | |
842ea771 | 344 | bd = __bsg_get_device(iminor(inode), bcd->queue); |
d6c73964 AG |
345 | if (!bd) |
346 | bd = bsg_add_device(inode, bcd->queue, file); | |
d45ac4fa | 347 | |
d6c73964 AG |
348 | out_unlock: |
349 | mutex_unlock(&bsg_mutex); | |
d45ac4fa | 350 | return bd; |
3d6392cf JA |
351 | } |
352 | ||
353 | static int bsg_open(struct inode *inode, struct file *file) | |
354 | { | |
75bd2ef1 JC |
355 | struct bsg_device *bd; |
356 | ||
75bd2ef1 | 357 | bd = bsg_get_device(inode, file); |
3d6392cf JA |
358 | |
359 | if (IS_ERR(bd)) | |
360 | return PTR_ERR(bd); | |
361 | ||
362 | file->private_data = bd; | |
363 | return 0; | |
364 | } | |
365 | ||
366 | static int bsg_release(struct inode *inode, struct file *file) | |
367 | { | |
368 | struct bsg_device *bd = file->private_data; | |
369 | ||
370 | file->private_data = NULL; | |
371 | return bsg_put_device(bd); | |
372 | } | |
373 | ||
ccf3209f CH |
374 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
375 | { | |
376 | return put_user(bd->max_queue, uarg); | |
377 | } | |
378 | ||
379 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) | |
380 | { | |
381 | int queue; | |
382 | ||
383 | if (get_user(queue, uarg)) | |
384 | return -EFAULT; | |
385 | if (queue < 1) | |
386 | return -EINVAL; | |
387 | ||
388 | spin_lock_irq(&bd->lock); | |
389 | bd->max_queue = queue; | |
390 | spin_unlock_irq(&bd->lock); | |
391 | return 0; | |
392 | } | |
393 | ||
25fd1643 | 394 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
3d6392cf JA |
395 | { |
396 | struct bsg_device *bd = file->private_data; | |
ccf3209f | 397 | void __user *uarg = (void __user *) arg; |
3d6392cf | 398 | |
3d6392cf | 399 | switch (cmd) { |
ccf3209f CH |
400 | /* |
401 | * Our own ioctls | |
402 | */ | |
3d6392cf | 403 | case SG_GET_COMMAND_Q: |
ccf3209f CH |
404 | return bsg_get_command_q(bd, uarg); |
405 | case SG_SET_COMMAND_Q: | |
406 | return bsg_set_command_q(bd, uarg); | |
3d6392cf JA |
407 | |
408 | /* | |
409 | * SCSI/sg ioctls | |
410 | */ | |
411 | case SG_GET_VERSION_NUM: | |
412 | case SCSI_IOCTL_GET_IDLUN: | |
413 | case SCSI_IOCTL_GET_BUS_NUMBER: | |
414 | case SG_SET_TIMEOUT: | |
415 | case SG_GET_TIMEOUT: | |
416 | case SG_GET_RESERVED_SIZE: | |
417 | case SG_SET_RESERVED_SIZE: | |
418 | case SG_EMULATED_HOST: | |
ccf3209f | 419 | case SCSI_IOCTL_SEND_COMMAND: |
74f3c8af | 420 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
ccf3209f CH |
421 | case SG_IO: |
422 | return bsg_sg_io(bd->queue, file->f_mode, uarg); | |
3d6392cf | 423 | default: |
3d6392cf | 424 | return -ENOTTY; |
3d6392cf JA |
425 | } |
426 | } | |
427 | ||
7344be05 | 428 | static const struct file_operations bsg_fops = { |
3d6392cf JA |
429 | .open = bsg_open, |
430 | .release = bsg_release, | |
25fd1643 | 431 | .unlocked_ioctl = bsg_ioctl, |
3d6392cf | 432 | .owner = THIS_MODULE, |
6038f373 | 433 | .llseek = default_llseek, |
3d6392cf JA |
434 | }; |
435 | ||
d351af01 | 436 | void bsg_unregister_queue(struct request_queue *q) |
3d6392cf | 437 | { |
d351af01 | 438 | struct bsg_class_device *bcd = &q->bsg_dev; |
3d6392cf | 439 | |
df468820 FT |
440 | if (!bcd->class_dev) |
441 | return; | |
3d6392cf JA |
442 | |
443 | mutex_lock(&bsg_mutex); | |
598443a2 | 444 | idr_remove(&bsg_minor_idr, bcd->minor); |
37b40adf SG |
445 | if (q->kobj.sd) |
446 | sysfs_remove_link(&q->kobj, "bsg"); | |
ee959b00 | 447 | device_unregister(bcd->class_dev); |
3d6392cf | 448 | bcd->class_dev = NULL; |
3d6392cf JA |
449 | mutex_unlock(&bsg_mutex); |
450 | } | |
4cf0723a | 451 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
3d6392cf | 452 | |
97f46ae4 | 453 | int bsg_register_queue(struct request_queue *q, struct device *parent, |
5de815a7 | 454 | const char *name, const struct bsg_ops *ops) |
3d6392cf | 455 | { |
598443a2 | 456 | struct bsg_class_device *bcd; |
3d6392cf | 457 | dev_t dev; |
bab998d6 | 458 | int ret; |
ee959b00 | 459 | struct device *class_dev = NULL; |
3d6392cf JA |
460 | |
461 | /* | |
462 | * we need a proper transport to send commands, not a stacked device | |
463 | */ | |
344e9ffc | 464 | if (!queue_is_mq(q)) |
3d6392cf JA |
465 | return 0; |
466 | ||
d351af01 | 467 | bcd = &q->bsg_dev; |
3d6392cf | 468 | memset(bcd, 0, sizeof(*bcd)); |
3d6392cf JA |
469 | |
470 | mutex_lock(&bsg_mutex); | |
292b7f27 | 471 | |
bab998d6 TH |
472 | ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); |
473 | if (ret < 0) { | |
474 | if (ret == -ENOSPC) { | |
475 | printk(KERN_ERR "bsg: too many bsg devices\n"); | |
476 | ret = -EINVAL; | |
477 | } | |
598443a2 | 478 | goto unlock; |
598443a2 FT |
479 | } |
480 | ||
bab998d6 | 481 | bcd->minor = ret; |
d351af01 | 482 | bcd->queue = q; |
17cb960f | 483 | bcd->ops = ops; |
46f6ef4a | 484 | dev = MKDEV(bsg_major, bcd->minor); |
5de815a7 | 485 | class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); |
4e2872d6 FT |
486 | if (IS_ERR(class_dev)) { |
487 | ret = PTR_ERR(class_dev); | |
5de815a7 | 488 | goto idr_remove; |
4e2872d6 FT |
489 | } |
490 | bcd->class_dev = class_dev; | |
491 | ||
abce891a | 492 | if (q->kobj.sd) { |
4e2872d6 FT |
493 | ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
494 | if (ret) | |
598443a2 | 495 | goto unregister_class_dev; |
4e2872d6 FT |
496 | } |
497 | ||
3d6392cf JA |
498 | mutex_unlock(&bsg_mutex); |
499 | return 0; | |
6826ee4f | 500 | |
598443a2 | 501 | unregister_class_dev: |
ee959b00 | 502 | device_unregister(class_dev); |
5de815a7 | 503 | idr_remove: |
bab998d6 | 504 | idr_remove(&bsg_minor_idr, bcd->minor); |
598443a2 | 505 | unlock: |
264a0472 | 506 | mutex_unlock(&bsg_mutex); |
4e2872d6 FT |
507 | return ret; |
508 | } | |
17cb960f CH |
509 | |
510 | int bsg_scsi_register_queue(struct request_queue *q, struct device *parent) | |
511 | { | |
512 | if (!blk_queue_scsi_passthrough(q)) { | |
513 | WARN_ONCE(true, "Attempt to register a non-SCSI queue\n"); | |
514 | return -EINVAL; | |
515 | } | |
516 | ||
5de815a7 | 517 | return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops); |
17cb960f CH |
518 | } |
519 | EXPORT_SYMBOL_GPL(bsg_scsi_register_queue); | |
4e2872d6 | 520 | |
7e7654a9 | 521 | static struct cdev bsg_cdev; |
292b7f27 | 522 | |
2c9ede55 | 523 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
2bdf9149 KS |
524 | { |
525 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); | |
526 | } | |
527 | ||
3d6392cf JA |
528 | static int __init bsg_init(void) |
529 | { | |
530 | int ret, i; | |
46f6ef4a | 531 | dev_t devid; |
3d6392cf | 532 | |
25fd1643 | 533 | for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
3d6392cf JA |
534 | INIT_HLIST_HEAD(&bsg_device_list[i]); |
535 | ||
536 | bsg_class = class_create(THIS_MODULE, "bsg"); | |
28519c89 CH |
537 | if (IS_ERR(bsg_class)) |
538 | return PTR_ERR(bsg_class); | |
e454cea2 | 539 | bsg_class->devnode = bsg_devnode; |
3d6392cf | 540 | |
46f6ef4a | 541 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
9b9f770c FT |
542 | if (ret) |
543 | goto destroy_bsg_class; | |
292b7f27 | 544 | |
46f6ef4a JA |
545 | bsg_major = MAJOR(devid); |
546 | ||
292b7f27 | 547 | cdev_init(&bsg_cdev, &bsg_fops); |
46f6ef4a | 548 | ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
9b9f770c FT |
549 | if (ret) |
550 | goto unregister_chrdev; | |
3d6392cf | 551 | |
5d3a8cd3 | 552 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
0ed081ce | 553 | " loaded (major %d)\n", bsg_major); |
3d6392cf | 554 | return 0; |
9b9f770c FT |
555 | unregister_chrdev: |
556 | unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); | |
557 | destroy_bsg_class: | |
558 | class_destroy(bsg_class); | |
9b9f770c | 559 | return ret; |
3d6392cf JA |
560 | } |
561 | ||
562 | MODULE_AUTHOR("Jens Axboe"); | |
0ed081ce | 563 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
3d6392cf JA |
564 | MODULE_LICENSE("GPL"); |
565 | ||
4e2872d6 | 566 | device_initcall(bsg_init); |