Skip to content

Commit a1e97ce

Browse files
Yang Xiuweiaxboe
authored andcommitted
bsg: add io_uring command support to generic layer
Add an io_uring command handler to the generic BSG layer. The new .uring_cmd file operation validates io_uring features and delegates handling to a per-queue bsg_uring_cmd_fn callback. Extend bsg_register_queue() so transport drivers can register both sg_io and io_uring command handlers. Signed-off-by: Yang Xiuwei <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 7da9261 commit a1e97ce

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

block/bsg-lib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
393393

394394
blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
395395

396-
bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn);
396+
bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn, NULL);
397397
if (IS_ERR(bset->bd)) {
398398
ret = PTR_ERR(bset->bd);
399399
goto out_cleanup_queue;

block/bsg.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/idr.h>
1313
#include <linux/bsg.h>
1414
#include <linux/slab.h>
15+
#include <linux/io_uring/cmd.h>
1516

1617
#include <scsi/scsi.h>
1718
#include <scsi/scsi_ioctl.h>
@@ -28,6 +29,7 @@ struct bsg_device {
2829
unsigned int timeout;
2930
unsigned int reserved_size;
3031
bsg_sg_io_fn *sg_io_fn;
32+
bsg_uring_cmd_fn *uring_cmd_fn;
3133
};
3234

3335
static inline struct bsg_device *to_bsg_device(struct inode *inode)
@@ -158,11 +160,38 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
158160
}
159161
}
160162

163+
static int bsg_check_uring_features(unsigned int issue_flags)
164+
{
165+
/* BSG passthrough requires big SQE/CQE support */
166+
if ((issue_flags & (IO_URING_F_SQE128|IO_URING_F_CQE32)) !=
167+
(IO_URING_F_SQE128|IO_URING_F_CQE32))
168+
return -EOPNOTSUPP;
169+
return 0;
170+
}
171+
172+
static int bsg_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
173+
{
174+
struct bsg_device *bd = to_bsg_device(file_inode(ioucmd->file));
175+
bool open_for_write = ioucmd->file->f_mode & FMODE_WRITE;
176+
struct request_queue *q = bd->queue;
177+
int ret;
178+
179+
ret = bsg_check_uring_features(issue_flags);
180+
if (ret)
181+
return ret;
182+
183+
if (!bd->uring_cmd_fn)
184+
return -EOPNOTSUPP;
185+
186+
return bd->uring_cmd_fn(q, ioucmd, issue_flags, open_for_write);
187+
}
188+
161189
static const struct file_operations bsg_fops = {
162190
.open = bsg_open,
163191
.release = bsg_release,
164192
.unlocked_ioctl = bsg_ioctl,
165193
.compat_ioctl = compat_ptr_ioctl,
194+
.uring_cmd = bsg_uring_cmd,
166195
.owner = THIS_MODULE,
167196
.llseek = default_llseek,
168197
};
@@ -187,7 +216,8 @@ void bsg_unregister_queue(struct bsg_device *bd)
187216
EXPORT_SYMBOL_GPL(bsg_unregister_queue);
188217

189218
struct bsg_device *bsg_register_queue(struct request_queue *q,
190-
struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
219+
struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn,
220+
bsg_uring_cmd_fn *uring_cmd_fn)
191221
{
192222
struct bsg_device *bd;
193223
int ret;
@@ -199,6 +229,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
199229
bd->reserved_size = INT_MAX;
200230
bd->queue = q;
201231
bd->sg_io_fn = sg_io_fn;
232+
bd->uring_cmd_fn = uring_cmd_fn;
202233

203234
ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
204235
if (ret < 0) {

drivers/scsi/scsi_bsg.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/bsg.h>
3+
#include <linux/io_uring/cmd.h>
34
#include <scsi/scsi.h>
45
#include <scsi/scsi_ioctl.h>
56
#include <scsi/scsi_cmnd.h>
@@ -9,6 +10,12 @@
910

1011
#define uptr64(val) ((void __user *)(uintptr_t)(val))
1112

13+
static int scsi_bsg_uring_cmd(struct request_queue *q, struct io_uring_cmd *ioucmd,
14+
unsigned int issue_flags, bool open_for_write)
15+
{
16+
return -EOPNOTSUPP;
17+
}
18+
1219
static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
1320
bool open_for_write, unsigned int timeout)
1421
{
@@ -99,5 +106,6 @@ static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
99106
struct bsg_device *scsi_bsg_register_queue(struct scsi_device *sdev)
100107
{
101108
return bsg_register_queue(sdev->request_queue, &sdev->sdev_gendev,
102-
dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn);
109+
dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn,
110+
scsi_bsg_uring_cmd);
103111
}

include/linux/bsg.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
struct bsg_device;
88
struct device;
99
struct request_queue;
10+
struct io_uring_cmd;
1011

1112
typedef int (bsg_sg_io_fn)(struct request_queue *, struct sg_io_v4 *hdr,
1213
bool open_for_write, unsigned int timeout);
1314

15+
typedef int (bsg_uring_cmd_fn)(struct request_queue *q, struct io_uring_cmd *ioucmd,
16+
unsigned int issue_flags, bool open_for_write);
17+
1418
struct bsg_device *bsg_register_queue(struct request_queue *q,
1519
struct device *parent, const char *name,
16-
bsg_sg_io_fn *sg_io_fn);
20+
bsg_sg_io_fn *sg_io_fn, bsg_uring_cmd_fn *uring_cmd_fn);
1721
void bsg_unregister_queue(struct bsg_device *bcd);
1822

1923
#endif /* _LINUX_BSG_H */

0 commit comments

Comments
 (0)