xref: /openbmc/linux/drivers/block/rnbd/rnbd-clt.h (revision 61bf3293)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * RDMA Network Block Driver
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 
10 #ifndef RNBD_CLT_H
11 #define RNBD_CLT_H
12 
13 #include <linux/wait.h>
14 #include <linux/in.h>
15 #include <linux/inet.h>
16 #include <linux/blk-mq.h>
17 #include <linux/refcount.h>
18 
19 #include <rtrs.h>
20 #include "rnbd-proto.h"
21 #include "rnbd-log.h"
22 
23 /* Max. number of segments per IO request, Mellanox Connect X ~ Connect X5,
24  * choose minimial 30 for all, minus 1 for internal protocol, so 29.
25  */
26 #define BMAX_SEGMENTS 29
27 /*  time in seconds between reconnect tries, default to 30 s */
28 #define RECONNECT_DELAY 30
29 /*
30  * Number of times to reconnect on error before giving up, 0 for * disabled,
31  * -1 for forever
32  */
33 #define MAX_RECONNECTS -1
34 
35 enum rnbd_clt_dev_state {
36 	DEV_STATE_INIT,
37 	DEV_STATE_MAPPED,
38 	DEV_STATE_MAPPED_DISCONNECTED,
39 	DEV_STATE_UNMAPPED,
40 };
41 
42 struct rnbd_iu_comp {
43 	wait_queue_head_t wait;
44 	int errno;
45 };
46 
47 struct rnbd_iu {
48 	union {
49 		struct request *rq; /* for block io */
50 		void *buf; /* for user messages */
51 	};
52 	struct rtrs_permit	*permit;
53 	union {
54 		/* use to send msg associated with a dev */
55 		struct rnbd_clt_dev *dev;
56 		/* use to send msg associated with a sess */
57 		struct rnbd_clt_session *sess;
58 	};
59 	struct scatterlist	sglist[BMAX_SEGMENTS];
60 	struct work_struct	work;
61 	int			errno;
62 	struct rnbd_iu_comp	comp;
63 	atomic_t		refcount;
64 };
65 
66 struct rnbd_cpu_qlist {
67 	struct list_head	requeue_list;
68 	spinlock_t		requeue_lock;
69 	unsigned int		cpu;
70 };
71 
72 struct rnbd_clt_session {
73 	struct list_head        list;
74 	struct rtrs_clt        *rtrs;
75 	wait_queue_head_t       rtrs_waitq;
76 	bool                    rtrs_ready;
77 	struct rnbd_cpu_qlist	__percpu
78 				*cpu_queues;
79 	DECLARE_BITMAP(cpu_queues_bm, NR_CPUS);
80 	int	__percpu	*cpu_rr; /* per-cpu var for CPU round-robin */
81 	atomic_t		busy;
82 	int			queue_depth;
83 	u32			max_io_size;
84 	struct blk_mq_tag_set	tag_set;
85 	struct mutex		lock; /* protects state and devs_list */
86 	struct list_head        devs_list; /* list of struct rnbd_clt_dev */
87 	refcount_t		refcount;
88 	char			sessname[NAME_MAX];
89 	u8			ver; /* protocol version */
90 };
91 
92 /**
93  * Submission queues.
94  */
95 struct rnbd_queue {
96 	struct list_head	requeue_list;
97 	unsigned long		in_list;
98 	struct rnbd_clt_dev	*dev;
99 	struct blk_mq_hw_ctx	*hctx;
100 };
101 
102 struct rnbd_clt_dev {
103 	struct rnbd_clt_session	*sess;
104 	struct request_queue	*queue;
105 	struct rnbd_queue	*hw_queues;
106 	u32			device_id;
107 	/* local Idr index - used to track minor number allocations. */
108 	u32			clt_device_id;
109 	struct mutex		lock;
110 	enum rnbd_clt_dev_state	dev_state;
111 	char			pathname[NAME_MAX];
112 	enum rnbd_access_mode	access_mode;
113 	bool			read_only;
114 	bool			rotational;
115 	u32			max_hw_sectors;
116 	u32			max_write_same_sectors;
117 	u32			max_discard_sectors;
118 	u32			discard_granularity;
119 	u32			discard_alignment;
120 	u16			secure_discard;
121 	u16			physical_block_size;
122 	u16			logical_block_size;
123 	u16			max_segments;
124 	size_t			nsectors;
125 	u64			size;		/* device size in bytes */
126 	struct list_head        list;
127 	struct gendisk		*gd;
128 	struct kobject		kobj;
129 	char			blk_symlink_name[NAME_MAX];
130 	refcount_t		refcount;
131 	struct work_struct	unmap_on_rmmod_work;
132 };
133 
134 /* rnbd-clt.c */
135 
136 struct rnbd_clt_dev *rnbd_clt_map_device(const char *sessname,
137 					   struct rtrs_addr *paths,
138 					   size_t path_cnt, u16 port_nr,
139 					   const char *pathname,
140 					   enum rnbd_access_mode access_mode);
141 int rnbd_clt_unmap_device(struct rnbd_clt_dev *dev, bool force,
142 			   const struct attribute *sysfs_self);
143 
144 int rnbd_clt_remap_device(struct rnbd_clt_dev *dev);
145 int rnbd_clt_resize_disk(struct rnbd_clt_dev *dev, size_t newsize);
146 
147 /* rnbd-clt-sysfs.c */
148 
149 int rnbd_clt_create_sysfs_files(void);
150 
151 void rnbd_clt_destroy_sysfs_files(void);
152 void rnbd_clt_destroy_default_group(void);
153 
154 void rnbd_clt_remove_dev_symlink(struct rnbd_clt_dev *dev);
155 
156 #endif /* RNBD_CLT_H */
157