blob: 9f916b12cf4006ab4b57d2d1cd51fe2868d9a51d [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001#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>
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02006#include <scsi/iser.h>
7
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08008
Sagi Grimberg24f412d2014-12-07 13:12:02 +02009#define DRV_NAME "isert"
10#define PFX DRV_NAME ": "
11
12#define isert_dbg(fmt, arg...) \
13 do { \
14 if (unlikely(isert_debug_level > 2)) \
15 printk(KERN_DEBUG PFX "%s: " fmt,\
16 __func__ , ## arg); \
17 } while (0)
18
19#define isert_warn(fmt, arg...) \
20 do { \
21 if (unlikely(isert_debug_level > 0)) \
22 pr_warn(PFX "%s: " fmt, \
23 __func__ , ## arg); \
24 } while (0)
25
26#define isert_info(fmt, arg...) \
27 do { \
28 if (unlikely(isert_debug_level > 1)) \
29 pr_info(PFX "%s: " fmt, \
30 __func__ , ## arg); \
31 } while (0)
32
33#define isert_err(fmt, arg...) \
34 pr_err(PFX "%s: " fmt, __func__ , ## arg)
35
Sagi Grimbergc6494152015-12-09 14:12:04 +020036/* Constant PDU lengths calculations */
37#define ISER_HEADERS_LEN (sizeof(struct iser_ctrl) + \
38 sizeof(struct iscsi_hdr))
39#define ISER_RECV_DATA_SEG_LEN 8192
40#define ISER_RX_PAYLOAD_SIZE (ISER_HEADERS_LEN + ISER_RECV_DATA_SEG_LEN)
41#define ISER_RX_LOGIN_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_INFLIGHT_DATAOUTS 8
59
60#define ISERT_QP_MAX_REQ_DTOS (ISCSI_DEF_XMIT_CMDS_MAX * \
61 (1 + ISERT_INFLIGHT_DATAOUTS) + \
62 ISERT_MAX_TX_MISC_PDUS + \
63 ISERT_MAX_RX_MISC_PDUS)
64
65#define ISER_RX_PAD_SIZE (ISER_RECV_DATA_SEG_LEN + 4096 - \
66 (ISER_RX_PAYLOAD_SIZE + sizeof(u64) + sizeof(struct ib_sge)))
67
Vu Pham59464ef2013-08-28 23:23:35 +030068#define ISCSI_ISER_SG_TABLESIZE 256
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -080069#define ISER_FASTREG_LI_WRID 0xffffffffffffffffULL
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020070#define ISER_BEACON_WRID 0xfffffffffffffffeULL
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080071
72enum isert_desc_type {
73 ISCSI_TX_CONTROL,
74 ISCSI_TX_DATAIN
75};
76
77enum iser_ib_op_code {
78 ISER_IB_RECV,
79 ISER_IB_SEND,
80 ISER_IB_RDMA_WRITE,
81 ISER_IB_RDMA_READ,
82};
83
84enum iser_conn_state {
85 ISER_CONN_INIT,
86 ISER_CONN_UP,
Jenny Derzhavetzaea92982016-02-24 19:23:59 +020087 ISER_CONN_BOUND,
Sagi Grimberg128e9cc2014-12-02 16:57:20 +020088 ISER_CONN_FULL_FEATURE,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080089 ISER_CONN_TERMINATING,
90 ISER_CONN_DOWN,
91};
92
93struct iser_rx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +020094 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080095 struct iscsi_hdr iscsi_header;
96 char data[ISER_RECV_DATA_SEG_LEN];
97 u64 dma_addr;
98 struct ib_sge rx_sg;
99 char pad[ISER_RX_PAD_SIZE];
100} __packed;
101
102struct iser_tx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200103 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104 struct iscsi_hdr iscsi_header;
105 enum isert_desc_type type;
106 u64 dma_addr;
107 struct ib_sge tx_sg[2];
108 int num_sge;
109 struct isert_cmd *isert_cmd;
110 struct ib_send_wr send_wr;
111} __packed;
112
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200113enum isert_indicator {
114 ISERT_PROTECTED = 1 << 0,
115 ISERT_DATA_KEY_VALID = 1 << 1,
116 ISERT_PROT_KEY_VALID = 1 << 2,
117 ISERT_SIG_KEY_VALID = 1 << 3,
118};
119
120struct pi_context {
121 struct ib_mr *prot_mr;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200122 struct ib_mr *sig_mr;
123};
124
Vu Pham59464ef2013-08-28 23:23:35 +0300125struct fast_reg_descriptor {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200126 struct list_head list;
127 struct ib_mr *data_mr;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200128 u8 ind;
129 struct pi_context *pi_ctx;
Vu Pham59464ef2013-08-28 23:23:35 +0300130};
131
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +0200132struct isert_data_buf {
133 struct scatterlist *sg;
134 int nents;
135 u32 sg_off;
136 u32 len; /* cur_rdma_length */
137 u32 offset;
138 unsigned int dma_nents;
139 enum dma_data_direction dma_dir;
140};
141
Sagi Grimberg570db172014-12-02 16:57:31 +0200142enum {
143 DATA = 0,
144 PROT = 1,
145 SIG = 2,
146};
147
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800148struct isert_rdma_wr {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800149 struct isert_cmd *isert_cmd;
150 enum iser_ib_op_code iser_ib_op;
151 struct ib_sge *ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +0300152 struct ib_sge s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100153 int rdma_wr_num;
154 struct ib_rdma_wr *rdma_wr;
155 struct ib_rdma_wr s_rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +0200156 struct ib_sge ib_sg[3];
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +0200157 struct isert_data_buf data;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +0200158 struct isert_data_buf prot;
Vu Pham59464ef2013-08-28 23:23:35 +0300159 struct fast_reg_descriptor *fr_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800160};
161
162struct isert_cmd {
163 uint32_t read_stag;
164 uint32_t write_stag;
165 uint64_t read_va;
166 uint64_t write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200167 uint32_t inv_rkey;
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -0700168 u64 pdu_buf_dma;
169 u32 pdu_buf_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800170 struct isert_conn *conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700171 struct iscsi_cmd *iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800172 struct iser_tx_desc tx_desc;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300173 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800174 struct isert_rdma_wr rdma_wr;
175 struct work_struct comp_work;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +0300176 struct scatterlist sg;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800177};
178
179struct isert_device;
180
181struct isert_conn {
182 enum iser_conn_state state;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800183 u32 responder_resources;
184 u32 initiator_depth;
Sagi Grimberg23a548e2014-12-02 16:57:35 +0200185 bool pi_support;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800186 u32 max_sge;
187 char *login_buf;
188 char *login_req_buf;
189 char *login_rsp_buf;
190 u64 login_req_dma;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200191 int login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800192 u64 login_rsp_dma;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300193 struct iser_rx_desc *rx_descs;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300194 struct ib_recv_wr rx_wr[ISERT_QP_MAX_RECV_DTOS];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800195 struct iscsi_conn *conn;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300196 struct list_head node;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300197 struct completion login_comp;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200198 struct completion login_req_comp;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300199 struct iser_tx_desc login_tx_desc;
200 struct rdma_cm_id *cm_id;
201 struct ib_qp *qp;
202 struct isert_device *device;
203 struct mutex mutex;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300204 struct completion wait_comp_err;
205 struct kref kref;
206 struct list_head fr_pool;
207 int fr_pool_size;
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200208 /* lock to protect fastreg pool */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300209 spinlock_t pool_lock;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +0200210 struct work_struct release_work;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200211 struct ib_recv_wr beacon;
Sagi Grimberg991bb762014-12-07 13:12:01 +0200212 bool logout_posted;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200213 bool snd_w_inv;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800214};
215
216#define ISERT_MAX_CQ 64
217
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200218/**
219 * struct isert_comp - iSER completion context
220 *
221 * @device: pointer to device handle
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200222 * @cq: completion queue
Sagi Grimberg36ea63b2014-12-02 16:57:45 +0200223 * @wcs: work completion array
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200224 * @active_qps: Number of active QPs attached
225 * to completion context
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200226 * @work: completion work handle
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200227 */
228struct isert_comp {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200229 struct isert_device *device;
230 struct ib_cq *cq;
Sagi Grimberg36ea63b2014-12-02 16:57:45 +0200231 struct ib_wc wcs[16];
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200232 int active_qps;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200233 struct work_struct work;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800234};
235
236struct isert_device {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200237 int use_fastreg;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200238 bool pi_capable;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800239 int refcount;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800240 struct ib_device *ib_device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300241 struct ib_pd *pd;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200242 struct isert_comp *comps;
243 int comps_used;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244 struct list_head dev_node;
Vu Phamd40945d2013-08-28 23:23:34 +0300245 int (*reg_rdma_mem)(struct iscsi_conn *conn,
246 struct iscsi_cmd *cmd,
247 struct isert_rdma_wr *wr);
248 void (*unreg_rdma_mem)(struct isert_cmd *isert_cmd,
249 struct isert_conn *isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800250};
251
252struct isert_np {
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200253 struct iscsi_np *np;
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300254 struct semaphore sem;
255 struct rdma_cm_id *cm_id;
256 struct mutex mutex;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300257 struct list_head accepted;
258 struct list_head pending;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800259};