2 * bsg.c - block layer implementation of the sg v3 interface
4 * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs
5 * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com>
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.
14 * - Should this get merged, block/scsi_ioctl.c will be migrated into
15 * this file. To keep maintenance down, it's easier to have them
16 * seperated right now.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/file.h>
22 #include <linux/blkdev.h>
23 #include <linux/poll.h>
24 #include <linux/cdev.h>
25 #include <linux/percpu.h>
26 #include <linux/uio.h>
27 #include <linux/bsg.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_ioctl.h>
31 #include <scsi/scsi_cmnd.h>
34 static char bsg_version
[] = "block layer sg (bsg) 0.4";
40 request_queue_t
*queue
;
42 struct list_head busy_list
;
43 struct list_head done_list
;
44 struct hlist_node dev_list
;
49 unsigned long *cmd_bitmap
;
50 struct bsg_command
*cmd_map
;
51 wait_queue_head_t wq_done
;
52 wait_queue_head_t wq_free
;
53 char name
[BDEVNAME_SIZE
];
64 * command allocation bitmap defines
66 #define BSG_CMDS_PAGE_ORDER (1)
67 #define BSG_CMDS_PER_LONG (sizeof(unsigned long) * 8)
68 #define BSG_CMDS_MASK (BSG_CMDS_PER_LONG - 1)
69 #define BSG_CMDS_BYTES (PAGE_SIZE * (1 << BSG_CMDS_PAGE_ORDER))
70 #define BSG_CMDS (BSG_CMDS_BYTES / sizeof(struct bsg_command))
75 #define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
77 #define dprintk(fmt, args...)
80 #define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
85 #define BSG_MAJOR (240)
87 static DEFINE_MUTEX(bsg_mutex
);
88 static int bsg_device_nr
;
90 #define BSG_LIST_SIZE (8)
91 #define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
92 static struct hlist_head bsg_device_list
[BSG_LIST_SIZE
];
94 static struct class *bsg_class
;
95 static LIST_HEAD(bsg_class_list
);
98 * our internal command type
101 struct bsg_device
*bd
;
102 struct list_head list
;
107 struct sg_io_v4 __user
*uhdr
;
108 char sense
[SCSI_SENSE_BUFFERSIZE
];
111 static void bsg_free_command(struct bsg_command
*bc
)
113 struct bsg_device
*bd
= bc
->bd
;
114 unsigned long bitnr
= bc
- bd
->cmd_map
;
117 dprintk("%s: command bit offset %lu\n", bd
->name
, bitnr
);
119 spin_lock_irqsave(&bd
->lock
, flags
);
121 __clear_bit(bitnr
, bd
->cmd_bitmap
);
122 spin_unlock_irqrestore(&bd
->lock
, flags
);
124 wake_up(&bd
->wq_free
);
127 static struct bsg_command
*__bsg_alloc_command(struct bsg_device
*bd
)
129 struct bsg_command
*bc
= NULL
;
133 spin_lock_irq(&bd
->lock
);
135 if (bd
->queued_cmds
>= bd
->max_queue
)
138 for (free_nr
= 0, map
= bd
->cmd_bitmap
; *map
== ~0UL; map
++)
139 free_nr
+= BSG_CMDS_PER_LONG
;
141 BUG_ON(*map
== ~0UL);
144 free_nr
+= ffz(*map
);
145 __set_bit(free_nr
, bd
->cmd_bitmap
);
146 spin_unlock_irq(&bd
->lock
);
148 bc
= bd
->cmd_map
+ free_nr
;
149 memset(bc
, 0, sizeof(*bc
));
151 INIT_LIST_HEAD(&bc
->list
);
152 dprintk("%s: returning free cmd %p (bit %d)\n", bd
->name
, bc
, free_nr
);
155 dprintk("%s: failed (depth %d)\n", bd
->name
, bd
->queued_cmds
);
156 spin_unlock_irq(&bd
->lock
);
161 bsg_del_done_cmd(struct bsg_device
*bd
, struct bsg_command
*bc
)
168 bsg_add_done_cmd(struct bsg_device
*bd
, struct bsg_command
*bc
)
171 list_add_tail(&bc
->list
, &bd
->done_list
);
172 wake_up(&bd
->wq_done
);
175 static inline int bsg_io_schedule(struct bsg_device
*bd
, int state
)
180 spin_lock_irq(&bd
->lock
);
182 BUG_ON(bd
->done_cmds
> bd
->queued_cmds
);
185 * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
186 * work to do", even though we return -ENOSPC after this same test
187 * during bsg_write() -- there, it means our buffer can't have more
188 * bsg_commands added to it, thus has no space left.
190 if (bd
->done_cmds
== bd
->queued_cmds
) {
195 if (!test_bit(BSG_F_BLOCK
, &bd
->flags
)) {
200 prepare_to_wait(&bd
->wq_done
, &wait
, state
);
201 spin_unlock_irq(&bd
->lock
);
203 finish_wait(&bd
->wq_done
, &wait
);
205 if ((state
== TASK_INTERRUPTIBLE
) && signal_pending(current
))
210 spin_unlock_irq(&bd
->lock
);
215 * get a new free command, blocking if needed and specified
217 static struct bsg_command
*bsg_get_command(struct bsg_device
*bd
)
219 struct bsg_command
*bc
;
223 bc
= __bsg_alloc_command(bd
);
227 ret
= bsg_io_schedule(bd
, TASK_INTERRUPTIBLE
);
238 static int blk_fill_sgv4_hdr_rq(request_queue_t
*q
, struct request
*rq
,
239 struct sg_io_v4
*hdr
, int has_write_perm
)
241 memset(rq
->cmd
, 0, BLK_MAX_CDB
); /* ATAPI hates garbage after CDB */
243 if (copy_from_user(rq
->cmd
, (void *)(unsigned long)hdr
->request
,
246 if (blk_verify_command(rq
->cmd
, has_write_perm
))
250 * fill in request structure
252 rq
->cmd_len
= hdr
->request_len
;
253 rq
->cmd_type
= REQ_TYPE_BLOCK_PC
;
255 rq
->timeout
= (hdr
->timeout
* HZ
) / 1000;
257 rq
->timeout
= q
->sg_timeout
;
259 rq
->timeout
= BLK_DEFAULT_SG_TIMEOUT
;
265 * Check if sg_io_v4 from user is allowed and valid
268 bsg_validate_sgv4_hdr(request_queue_t
*q
, struct sg_io_v4
*hdr
, int *rw
)
270 if (hdr
->guard
!= 'Q')
272 if (hdr
->request_len
> BLK_MAX_CDB
)
274 if (hdr
->dout_xfer_len
> (q
->max_sectors
<< 9) ||
275 hdr
->din_xfer_len
> (q
->max_sectors
<< 9))
278 /* not supported currently */
279 if (hdr
->protocol
|| hdr
->subprotocol
)
283 * looks sane, if no data then it should be fine from our POV
285 if (!hdr
->dout_xfer_len
&& !hdr
->din_xfer_len
)
288 /* not supported currently */
289 if (hdr
->dout_xfer_len
&& hdr
->din_xfer_len
)
292 *rw
= hdr
->dout_xfer_len
? WRITE
: READ
;
298 * map sg_io_v4 to a request.
300 static struct request
*
301 bsg_map_hdr(struct bsg_device
*bd
, struct sg_io_v4
*hdr
)
303 request_queue_t
*q
= bd
->queue
;
306 unsigned int dxfer_len
;
309 dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr
->dout_xferp
,
310 hdr
->dout_xfer_len
, (unsigned long long) hdr
->din_xferp
,
313 ret
= bsg_validate_sgv4_hdr(q
, hdr
, &rw
);
318 * map scatter-gather elements seperately and string them to request
320 rq
= blk_get_request(q
, rw
, GFP_KERNEL
);
321 ret
= blk_fill_sgv4_hdr_rq(q
, rq
, hdr
, test_bit(BSG_F_WRITE_PERM
,
328 if (hdr
->dout_xfer_len
) {
329 dxfer_len
= hdr
->dout_xfer_len
;
330 dxferp
= (void*)(unsigned long)hdr
->dout_xferp
;
331 } else if (hdr
->din_xfer_len
) {
332 dxfer_len
= hdr
->din_xfer_len
;
333 dxferp
= (void*)(unsigned long)hdr
->din_xferp
;
338 ret
= blk_rq_map_user(q
, rq
, dxferp
, dxfer_len
);
340 dprintk("failed map at %d\n", ret
);
350 * async completion call-back from the block layer, when scsi/ide/whatever
351 * calls end_that_request_last() on a request
353 static void bsg_rq_end_io(struct request
*rq
, int uptodate
)
355 struct bsg_command
*bc
= rq
->end_io_data
;
356 struct bsg_device
*bd
= bc
->bd
;
359 dprintk("%s: finished rq %p bc %p, bio %p offset %Zd stat %d\n",
360 bd
->name
, rq
, bc
, bc
->bio
, bc
- bd
->cmd_map
, uptodate
);
362 bc
->hdr
.duration
= jiffies_to_msecs(jiffies
- bc
->hdr
.duration
);
364 spin_lock_irqsave(&bd
->lock
, flags
);
366 bsg_add_done_cmd(bd
, bc
);
367 spin_unlock_irqrestore(&bd
->lock
, flags
);
371 * do final setup of a 'bc' and submit the matching 'rq' to the block
374 static void bsg_add_command(struct bsg_device
*bd
, request_queue_t
*q
,
375 struct bsg_command
*bc
, struct request
*rq
)
377 rq
->sense
= bc
->sense
;
381 * add bc command to busy queue and submit rq for io
385 bc
->hdr
.duration
= jiffies
;
386 spin_lock_irq(&bd
->lock
);
387 list_add_tail(&bc
->list
, &bd
->busy_list
);
388 spin_unlock_irq(&bd
->lock
);
390 dprintk("%s: queueing rq %p, bc %p\n", bd
->name
, rq
, bc
);
392 rq
->end_io_data
= bc
;
393 blk_execute_rq_nowait(q
, bd
->disk
, rq
, 1, bsg_rq_end_io
);
396 static inline struct bsg_command
*bsg_next_done_cmd(struct bsg_device
*bd
)
398 struct bsg_command
*bc
= NULL
;
400 spin_lock_irq(&bd
->lock
);
402 bc
= list_entry_bc(bd
->done_list
.next
);
403 bsg_del_done_cmd(bd
, bc
);
405 spin_unlock_irq(&bd
->lock
);
411 * Get a finished command from the done list
413 static struct bsg_command
*__bsg_get_done_cmd(struct bsg_device
*bd
, int state
)
415 struct bsg_command
*bc
;
419 bc
= bsg_next_done_cmd(bd
);
423 ret
= bsg_io_schedule(bd
, state
);
430 dprintk("%s: returning done %p\n", bd
->name
, bc
);
435 static struct bsg_command
*
436 bsg_get_done_cmd(struct bsg_device
*bd
, const struct iovec
*iov
)
438 return __bsg_get_done_cmd(bd
, TASK_INTERRUPTIBLE
);
441 static struct bsg_command
*
442 bsg_get_done_cmd_nosignals(struct bsg_device
*bd
)
444 return __bsg_get_done_cmd(bd
, TASK_UNINTERRUPTIBLE
);
447 static int blk_complete_sgv4_hdr_rq(struct request
*rq
, struct sg_io_v4
*hdr
,
452 dprintk("rq %p bio %p %u\n", rq
, bio
, rq
->errors
);
454 * fill in all the output members
456 hdr
->device_status
= status_byte(rq
->errors
);
457 hdr
->transport_status
= host_byte(rq
->errors
);
458 hdr
->driver_status
= driver_byte(rq
->errors
);
460 if (hdr
->device_status
|| hdr
->transport_status
|| hdr
->driver_status
)
461 hdr
->info
|= SG_INFO_CHECK
;
462 hdr
->din_resid
= rq
->data_len
;
463 hdr
->response_len
= 0;
465 if (rq
->sense_len
&& hdr
->response
) {
466 int len
= min((unsigned int) hdr
->max_response_len
,
469 ret
= copy_to_user((void*)(unsigned long)hdr
->response
,
472 hdr
->response_len
= len
;
477 blk_rq_unmap_user(bio
);
483 static int bsg_complete_all_commands(struct bsg_device
*bd
)
485 struct bsg_command
*bc
;
488 dprintk("%s: entered\n", bd
->name
);
490 set_bit(BSG_F_BLOCK
, &bd
->flags
);
493 * wait for all commands to complete
497 ret
= bsg_io_schedule(bd
, TASK_UNINTERRUPTIBLE
);
499 * look for -ENODATA specifically -- we'll sometimes get
500 * -ERESTARTSYS when we've taken a signal, but we can't
501 * return until we're done freeing the queue, so ignore
502 * it. The signal will get handled when we're done freeing
505 } while (ret
!= -ENODATA
);
508 * discard done commands
512 bc
= bsg_get_done_cmd_nosignals(bd
);
515 * we _must_ complete before restarting, because
516 * bsg_release can't handle this failing.
518 if (PTR_ERR(bc
) == -ERESTARTSYS
)
525 tret
= blk_complete_sgv4_hdr_rq(bc
->rq
, &bc
->hdr
, bc
->bio
);
529 bsg_free_command(bc
);
535 typedef struct bsg_command
*(*bsg_command_callback
)(struct bsg_device
*bd
, const struct iovec
*iov
);
538 __bsg_read(char __user
*buf
, size_t count
, bsg_command_callback get_bc
,
539 struct bsg_device
*bd
, const struct iovec
*iov
, ssize_t
*bytes_read
)
541 struct bsg_command
*bc
;
542 int nr_commands
, ret
;
544 if (count
% sizeof(struct sg_io_v4
))
548 nr_commands
= count
/ sizeof(struct sg_io_v4
);
549 while (nr_commands
) {
550 bc
= get_bc(bd
, iov
);
557 * this is the only case where we need to copy data back
558 * after completing the request. so do that here,
559 * bsg_complete_work() cannot do that for us
561 ret
= blk_complete_sgv4_hdr_rq(bc
->rq
, &bc
->hdr
, bc
->bio
);
563 if (copy_to_user(buf
, (char *) &bc
->hdr
, sizeof(bc
->hdr
)))
566 bsg_free_command(bc
);
571 buf
+= sizeof(struct sg_io_v4
);
572 *bytes_read
+= sizeof(struct sg_io_v4
);
579 static inline void bsg_set_block(struct bsg_device
*bd
, struct file
*file
)
581 if (file
->f_flags
& O_NONBLOCK
)
582 clear_bit(BSG_F_BLOCK
, &bd
->flags
);
584 set_bit(BSG_F_BLOCK
, &bd
->flags
);
587 static inline void bsg_set_write_perm(struct bsg_device
*bd
, struct file
*file
)
589 if (file
->f_mode
& FMODE_WRITE
)
590 set_bit(BSG_F_WRITE_PERM
, &bd
->flags
);
592 clear_bit(BSG_F_WRITE_PERM
, &bd
->flags
);
595 static inline int err_block_err(int ret
)
597 if (ret
&& ret
!= -ENOSPC
&& ret
!= -ENODATA
&& ret
!= -EAGAIN
)
604 bsg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
606 struct bsg_device
*bd
= file
->private_data
;
610 dprintk("%s: read %Zd bytes\n", bd
->name
, count
);
612 bsg_set_block(bd
, file
);
614 ret
= __bsg_read(buf
, count
, bsg_get_done_cmd
,
615 bd
, NULL
, &bytes_read
);
618 if (!bytes_read
|| (bytes_read
&& err_block_err(ret
)))
624 static ssize_t
__bsg_write(struct bsg_device
*bd
, const char __user
*buf
,
625 size_t count
, ssize_t
*bytes_read
)
627 struct bsg_command
*bc
;
629 int ret
, nr_commands
;
631 if (count
% sizeof(struct sg_io_v4
))
634 nr_commands
= count
/ sizeof(struct sg_io_v4
);
638 while (nr_commands
) {
639 request_queue_t
*q
= bd
->queue
;
641 bc
= bsg_get_command(bd
);
650 bc
->uhdr
= (struct sg_io_v4 __user
*) buf
;
651 if (copy_from_user(&bc
->hdr
, buf
, sizeof(bc
->hdr
))) {
657 * get a request, fill in the blanks, and add to request queue
659 rq
= bsg_map_hdr(bd
, &bc
->hdr
);
666 bsg_add_command(bd
, q
, bc
, rq
);
670 buf
+= sizeof(struct sg_io_v4
);
671 *bytes_read
+= sizeof(struct sg_io_v4
);
675 bsg_free_command(bc
);
681 bsg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
683 struct bsg_device
*bd
= file
->private_data
;
687 dprintk("%s: write %Zd bytes\n", bd
->name
, count
);
689 bsg_set_block(bd
, file
);
690 bsg_set_write_perm(bd
, file
);
693 ret
= __bsg_write(bd
, buf
, count
, &bytes_read
);
697 * return bytes written on non-fatal errors
699 if (!bytes_read
|| (bytes_read
&& err_block_err(ret
)))
702 dprintk("%s: returning %Zd\n", bd
->name
, bytes_read
);
706 static void bsg_free_device(struct bsg_device
*bd
)
709 free_pages((unsigned long) bd
->cmd_map
, BSG_CMDS_PAGE_ORDER
);
711 kfree(bd
->cmd_bitmap
);
715 static struct bsg_device
*bsg_alloc_device(void)
717 struct bsg_command
*cmd_map
;
718 unsigned long *cmd_bitmap
;
719 struct bsg_device
*bd
;
722 bd
= kzalloc(sizeof(struct bsg_device
), GFP_KERNEL
);
726 spin_lock_init(&bd
->lock
);
728 bd
->max_queue
= BSG_CMDS
;
730 bits
= (BSG_CMDS
/ BSG_CMDS_PER_LONG
) + 1;
731 cmd_bitmap
= kzalloc(bits
* sizeof(unsigned long), GFP_KERNEL
);
734 bd
->cmd_bitmap
= cmd_bitmap
;
736 cmd_map
= (void *) __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
737 BSG_CMDS_PAGE_ORDER
);
739 goto out_free_bitmap
;
740 bd
->cmd_map
= cmd_map
;
742 INIT_LIST_HEAD(&bd
->busy_list
);
743 INIT_LIST_HEAD(&bd
->done_list
);
744 INIT_HLIST_NODE(&bd
->dev_list
);
746 init_waitqueue_head(&bd
->wq_free
);
747 init_waitqueue_head(&bd
->wq_done
);
757 static int bsg_put_device(struct bsg_device
*bd
)
761 mutex_lock(&bsg_mutex
);
763 if (!atomic_dec_and_test(&bd
->ref_count
))
766 dprintk("%s: tearing down\n", bd
->name
);
769 * close can always block
771 set_bit(BSG_F_BLOCK
, &bd
->flags
);
774 * correct error detection baddies here again. it's the responsibility
775 * of the app to properly reap commands before close() if it wants
776 * fool-proof error detection
778 ret
= bsg_complete_all_commands(bd
);
780 blk_put_queue(bd
->queue
);
781 hlist_del(&bd
->dev_list
);
784 mutex_unlock(&bsg_mutex
);
788 static struct bsg_device
*bsg_add_device(struct inode
*inode
,
789 struct gendisk
*disk
,
792 struct bsg_device
*bd
= NULL
;
794 unsigned char buf
[32];
797 bd
= bsg_alloc_device();
799 return ERR_PTR(-ENOMEM
);
802 bd
->queue
= disk
->queue
;
803 kobject_get(&disk
->queue
->kobj
);
804 bsg_set_block(bd
, file
);
806 atomic_set(&bd
->ref_count
, 1);
807 bd
->minor
= iminor(inode
);
808 mutex_lock(&bsg_mutex
);
809 hlist_add_head(&bd
->dev_list
,&bsg_device_list
[bsg_list_idx(bd
->minor
)]);
811 strncpy(bd
->name
, disk
->disk_name
, sizeof(bd
->name
) - 1);
812 dprintk("bound to <%s>, max queue %d\n",
813 format_dev_t(buf
, inode
->i_rdev
), bd
->max_queue
);
815 mutex_unlock(&bsg_mutex
);
819 static struct bsg_device
*__bsg_get_device(int minor
)
821 struct hlist_head
*list
= &bsg_device_list
[bsg_list_idx(minor
)];
822 struct bsg_device
*bd
= NULL
;
823 struct hlist_node
*entry
;
825 mutex_lock(&bsg_mutex
);
827 hlist_for_each(entry
, list
) {
828 bd
= hlist_entry(entry
, struct bsg_device
, dev_list
);
829 if (bd
->minor
== minor
) {
830 atomic_inc(&bd
->ref_count
);
837 mutex_unlock(&bsg_mutex
);
841 static struct bsg_device
*bsg_get_device(struct inode
*inode
, struct file
*file
)
843 struct bsg_device
*bd
= __bsg_get_device(iminor(inode
));
844 struct bsg_class_device
*bcd
, *__bcd
;
850 * find the class device
853 mutex_lock(&bsg_mutex
);
854 list_for_each_entry(__bcd
, &bsg_class_list
, list
) {
855 if (__bcd
->minor
== iminor(inode
)) {
860 mutex_unlock(&bsg_mutex
);
863 return ERR_PTR(-ENODEV
);
865 return bsg_add_device(inode
, bcd
->disk
, file
);
868 static int bsg_open(struct inode
*inode
, struct file
*file
)
870 struct bsg_device
*bd
= bsg_get_device(inode
, file
);
875 file
->private_data
= bd
;
879 static int bsg_release(struct inode
*inode
, struct file
*file
)
881 struct bsg_device
*bd
= file
->private_data
;
883 file
->private_data
= NULL
;
884 return bsg_put_device(bd
);
887 static unsigned int bsg_poll(struct file
*file
, poll_table
*wait
)
889 struct bsg_device
*bd
= file
->private_data
;
890 unsigned int mask
= 0;
892 poll_wait(file
, &bd
->wq_done
, wait
);
893 poll_wait(file
, &bd
->wq_free
, wait
);
895 spin_lock_irq(&bd
->lock
);
896 if (!list_empty(&bd
->done_list
))
897 mask
|= POLLIN
| POLLRDNORM
;
898 if (bd
->queued_cmds
>= bd
->max_queue
)
900 spin_unlock_irq(&bd
->lock
);
906 bsg_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
909 struct bsg_device
*bd
= file
->private_data
;
910 int __user
*uarg
= (int __user
*) arg
;
919 case SG_GET_COMMAND_Q
:
920 return put_user(bd
->max_queue
, uarg
);
921 case SG_SET_COMMAND_Q
: {
924 if (get_user(queue
, uarg
))
926 if (queue
> BSG_CMDS
|| queue
< 1)
929 bd
->max_queue
= queue
;
936 case SG_GET_VERSION_NUM
:
937 case SCSI_IOCTL_GET_IDLUN
:
938 case SCSI_IOCTL_GET_BUS_NUMBER
:
941 case SG_GET_RESERVED_SIZE
:
942 case SG_SET_RESERVED_SIZE
:
943 case SG_EMULATED_HOST
:
944 case SCSI_IOCTL_SEND_COMMAND
: {
945 void __user
*uarg
= (void __user
*) arg
;
946 return scsi_cmd_ioctl(file
, bd
->disk
, cmd
, uarg
);
951 * block device ioctls
955 return ioctl_by_bdev(bd
->bdev
, cmd
, arg
);
962 static struct file_operations bsg_fops
= {
967 .release
= bsg_release
,
969 .owner
= THIS_MODULE
,
972 void bsg_unregister_disk(struct gendisk
*disk
)
974 struct bsg_class_device
*bcd
= &disk
->bsg_dev
;
979 mutex_lock(&bsg_mutex
);
980 sysfs_remove_link(&bcd
->disk
->queue
->kobj
, "bsg");
981 class_device_destroy(bsg_class
, MKDEV(BSG_MAJOR
, bcd
->minor
));
982 bcd
->class_dev
= NULL
;
983 list_del_init(&bcd
->list
);
984 mutex_unlock(&bsg_mutex
);
987 int bsg_register_disk(struct gendisk
*disk
)
989 request_queue_t
*q
= disk
->queue
;
990 struct bsg_class_device
*bcd
;
994 * we need a proper transport to send commands, not a stacked device
999 bcd
= &disk
->bsg_dev
;
1000 memset(bcd
, 0, sizeof(*bcd
));
1001 INIT_LIST_HEAD(&bcd
->list
);
1003 mutex_lock(&bsg_mutex
);
1004 dev
= MKDEV(BSG_MAJOR
, bsg_device_nr
);
1005 bcd
->minor
= bsg_device_nr
;
1008 bcd
->class_dev
= class_device_create(bsg_class
, NULL
, dev
, bcd
->dev
, "%s", disk
->disk_name
);
1009 list_add_tail(&bcd
->list
, &bsg_class_list
);
1010 sysfs_create_link(&q
->kobj
, &bcd
->class_dev
->kobj
, "bsg");
1011 mutex_unlock(&bsg_mutex
);
1015 static int __init
bsg_init(void)
1019 for (i
= 0; i
< BSG_LIST_SIZE
; i
++)
1020 INIT_HLIST_HEAD(&bsg_device_list
[i
]);
1022 bsg_class
= class_create(THIS_MODULE
, "bsg");
1023 if (IS_ERR(bsg_class
))
1024 return PTR_ERR(bsg_class
);
1026 ret
= register_chrdev(BSG_MAJOR
, "bsg", &bsg_fops
);
1028 class_destroy(bsg_class
);
1032 printk(KERN_INFO
"%s loaded\n", bsg_version
);
1036 MODULE_AUTHOR("Jens Axboe");
1037 MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
1038 MODULE_LICENSE("GPL");
1040 subsys_initcall(bsg_init
);