1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/socket.h>
3 #include <linux/in.h>
4 #include <linux/in6.h>
5 #include <rdma/ib_verbs.h>
6 #include <rdma/rdma_cm.h>
7 #include <rdma/rw.h>
8 #include <scsi/iser.h>
9 
10 
11 #define DRV_NAME	"isert"
12 #define PFX		DRV_NAME ": "
13 
14 #define isert_dbg(fmt, arg...)				 \
15 	do {						 \
16 		if (unlikely(isert_debug_level > 2))	 \
17 			printk(KERN_DEBUG PFX "%s: " fmt,\
18 				__func__ , ## arg);	 \
19 	} while (0)
20 
21 #define isert_warn(fmt, arg...)				\
22 	do {						\
23 		if (unlikely(isert_debug_level > 0))	\
24 			pr_warn(PFX "%s: " fmt,         \
25 				__func__ , ## arg);	\
26 	} while (0)
27 
28 #define isert_info(fmt, arg...)				\
29 	do {						\
30 		if (unlikely(isert_debug_level > 1))	\
31 			pr_info(PFX "%s: " fmt,         \
32 				__func__ , ## arg);	\
33 	} while (0)
34 
35 #define isert_err(fmt, arg...) \
36 	pr_err(PFX "%s: " fmt, __func__ , ## arg)
37 
38 /* Constant PDU lengths calculations */
39 #define ISER_HEADERS_LEN	(sizeof(struct iser_ctrl) + \
40 				 sizeof(struct iscsi_hdr))
41 #define ISER_RX_PAYLOAD_SIZE	(ISER_HEADERS_LEN + ISCSI_DEF_MAX_RECV_SEG_LEN)
42 
43 /* QP settings */
44 /* Maximal bounds on received asynchronous PDUs */
45 #define ISERT_MAX_TX_MISC_PDUS	4 /* NOOP_IN(2) , ASYNC_EVENT(2)   */
46 
47 #define ISERT_MAX_RX_MISC_PDUS	6 /*
48 				   * NOOP_OUT(2), TEXT(1),
49 				   * SCSI_TMFUNC(2), LOGOUT(1)
50 				   */
51 
52 #define ISCSI_DEF_XMIT_CMDS_MAX 128 /* from libiscsi.h, must be power of 2 */
53 
54 #define ISERT_QP_MAX_RECV_DTOS	(ISCSI_DEF_XMIT_CMDS_MAX)
55 
56 #define ISERT_MIN_POSTED_RX	(ISCSI_DEF_XMIT_CMDS_MAX >> 2)
57 
58 #define ISERT_QP_MAX_REQ_DTOS	(ISCSI_DEF_XMIT_CMDS_MAX +    \
59 				ISERT_MAX_TX_MISC_PDUS	+ \
60 				ISERT_MAX_RX_MISC_PDUS)
61 
62 #define ISER_RX_PAD_SIZE	(ISCSI_DEF_MAX_RECV_SEG_LEN + 4096 - \
63 		(ISER_RX_PAYLOAD_SIZE + sizeof(u64) + sizeof(struct ib_sge) + \
64 		 sizeof(struct ib_cqe) + sizeof(bool)))
65 
66 /* Maximum support is 16MB I/O size */
67 #define ISCSI_ISER_MAX_SG_TABLESIZE	4096
68 
69 enum isert_desc_type {
70 	ISCSI_TX_CONTROL,
71 	ISCSI_TX_DATAIN
72 };
73 
74 enum iser_conn_state {
75 	ISER_CONN_INIT,
76 	ISER_CONN_UP,
77 	ISER_CONN_BOUND,
78 	ISER_CONN_FULL_FEATURE,
79 	ISER_CONN_TERMINATING,
80 	ISER_CONN_DOWN,
81 };
82 
83 struct iser_rx_desc {
84 	struct iser_ctrl iser_header;
85 	struct iscsi_hdr iscsi_header;
86 	char		data[ISCSI_DEF_MAX_RECV_SEG_LEN];
87 	u64		dma_addr;
88 	struct ib_sge	rx_sg;
89 	struct ib_cqe	rx_cqe;
90 	bool		in_use;
91 	char		pad[ISER_RX_PAD_SIZE];
92 } __packed;
93 
94 static inline struct iser_rx_desc *cqe_to_rx_desc(struct ib_cqe *cqe)
95 {
96 	return container_of(cqe, struct iser_rx_desc, rx_cqe);
97 }
98 
99 struct iser_tx_desc {
100 	struct iser_ctrl iser_header;
101 	struct iscsi_hdr iscsi_header;
102 	enum isert_desc_type type;
103 	u64		dma_addr;
104 	struct ib_sge	tx_sg[2];
105 	struct ib_cqe	tx_cqe;
106 	int		num_sge;
107 	struct ib_send_wr send_wr;
108 } __packed;
109 
110 static inline struct iser_tx_desc *cqe_to_tx_desc(struct ib_cqe *cqe)
111 {
112 	return container_of(cqe, struct iser_tx_desc, tx_cqe);
113 }
114 
115 struct isert_cmd {
116 	uint32_t		read_stag;
117 	uint32_t		write_stag;
118 	uint64_t		read_va;
119 	uint64_t		write_va;
120 	uint32_t		inv_rkey;
121 	u64			pdu_buf_dma;
122 	u32			pdu_buf_len;
123 	struct isert_conn	*conn;
124 	struct iscsi_cmd	*iscsi_cmd;
125 	struct iser_tx_desc	tx_desc;
126 	struct iser_rx_desc	*rx_desc;
127 	struct rdma_rw_ctx	rw;
128 	struct work_struct	comp_work;
129 	struct scatterlist	sg;
130 	bool			ctx_init_done;
131 };
132 
133 static inline struct isert_cmd *tx_desc_to_cmd(struct iser_tx_desc *desc)
134 {
135 	return container_of(desc, struct isert_cmd, tx_desc);
136 }
137 
138 struct isert_device;
139 
140 struct isert_conn {
141 	enum iser_conn_state	state;
142 	u32			responder_resources;
143 	u32			initiator_depth;
144 	bool			pi_support;
145 	struct iser_rx_desc	*login_req_buf;
146 	char			*login_rsp_buf;
147 	u64			login_req_dma;
148 	int			login_req_len;
149 	u64			login_rsp_dma;
150 	struct iser_rx_desc	*rx_descs;
151 	struct ib_recv_wr	rx_wr[ISERT_QP_MAX_RECV_DTOS];
152 	struct iscsi_conn	*conn;
153 	struct list_head	node;
154 	struct completion	login_comp;
155 	struct completion	login_req_comp;
156 	struct iser_tx_desc	login_tx_desc;
157 	struct rdma_cm_id	*cm_id;
158 	struct ib_qp		*qp;
159 	struct ib_cq		*cq;
160 	u32			cq_size;
161 	struct isert_device	*device;
162 	struct mutex		mutex;
163 	struct kref		kref;
164 	struct work_struct	release_work;
165 	bool                    logout_posted;
166 	bool                    snd_w_inv;
167 	wait_queue_head_t	rem_wait;
168 	bool			dev_removed;
169 };
170 
171 struct isert_device {
172 	bool			pi_capable;
173 	int			refcount;
174 	struct ib_device	*ib_device;
175 	struct ib_pd		*pd;
176 	struct isert_comp	*comps;
177 	int                     comps_used;
178 	struct list_head	dev_node;
179 };
180 
181 struct isert_np {
182 	struct iscsi_np         *np;
183 	struct semaphore	sem;
184 	struct rdma_cm_id	*cm_id;
185 	struct mutex		mutex;
186 	struct list_head	accepted;
187 	struct list_head	pending;
188 };
189