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