blob: 6c2c5fa85d2fb6ea395e5e353415c86316936e4f [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))
Christoph Hellwiged1083b2016-02-24 19:24:04 +020039#define ISER_RX_PAYLOAD_SIZE (ISER_HEADERS_LEN + ISCSI_DEF_MAX_RECV_SEG_LEN)
Sagi Grimbergc6494152015-12-09 14:12:04 +020040
41/* QP settings */
42/* Maximal bounds on received asynchronous PDUs */
43#define ISERT_MAX_TX_MISC_PDUS 4 /* NOOP_IN(2) , ASYNC_EVENT(2) */
44
45#define ISERT_MAX_RX_MISC_PDUS 6 /*
46 * NOOP_OUT(2), TEXT(1),
47 * SCSI_TMFUNC(2), LOGOUT(1)
48 */
49
50#define ISCSI_DEF_XMIT_CMDS_MAX 128 /* from libiscsi.h, must be power of 2 */
51
52#define ISERT_QP_MAX_RECV_DTOS (ISCSI_DEF_XMIT_CMDS_MAX)
53
54#define ISERT_MIN_POSTED_RX (ISCSI_DEF_XMIT_CMDS_MAX >> 2)
55
56#define ISERT_INFLIGHT_DATAOUTS 8
57
58#define ISERT_QP_MAX_REQ_DTOS (ISCSI_DEF_XMIT_CMDS_MAX * \
59 (1 + ISERT_INFLIGHT_DATAOUTS) + \
60 ISERT_MAX_TX_MISC_PDUS + \
61 ISERT_MAX_RX_MISC_PDUS)
62
Christoph Hellwiged1083b2016-02-24 19:24:04 +020063#define ISER_RX_PAD_SIZE (ISCSI_DEF_MAX_RECV_SEG_LEN + 4096 - \
Sagi Grimbergc6494152015-12-09 14:12:04 +020064 (ISER_RX_PAYLOAD_SIZE + sizeof(u64) + sizeof(struct ib_sge)))
65
Vu Pham59464ef2013-08-28 23:23:35 +030066#define ISCSI_ISER_SG_TABLESIZE 256
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -080067#define ISER_FASTREG_LI_WRID 0xffffffffffffffffULL
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020068#define ISER_BEACON_WRID 0xfffffffffffffffeULL
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080069
70enum isert_desc_type {
71 ISCSI_TX_CONTROL,
72 ISCSI_TX_DATAIN
73};
74
75enum iser_ib_op_code {
76 ISER_IB_RECV,
77 ISER_IB_SEND,
78 ISER_IB_RDMA_WRITE,
79 ISER_IB_RDMA_READ,
80};
81
82enum iser_conn_state {
83 ISER_CONN_INIT,
84 ISER_CONN_UP,
Jenny Derzhavetzaea92982016-02-24 19:23:59 +020085 ISER_CONN_BOUND,
Sagi Grimberg128e9cc2014-12-02 16:57:20 +020086 ISER_CONN_FULL_FEATURE,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080087 ISER_CONN_TERMINATING,
88 ISER_CONN_DOWN,
89};
90
91struct iser_rx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +020092 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080093 struct iscsi_hdr iscsi_header;
Christoph Hellwiged1083b2016-02-24 19:24:04 +020094 char data[ISCSI_DEF_MAX_RECV_SEG_LEN];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080095 u64 dma_addr;
96 struct ib_sge rx_sg;
97 char pad[ISER_RX_PAD_SIZE];
98} __packed;
99
100struct iser_tx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200101 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800102 struct iscsi_hdr iscsi_header;
103 enum isert_desc_type type;
104 u64 dma_addr;
105 struct ib_sge tx_sg[2];
106 int num_sge;
107 struct isert_cmd *isert_cmd;
108 struct ib_send_wr send_wr;
109} __packed;
110
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200111enum isert_indicator {
112 ISERT_PROTECTED = 1 << 0,
113 ISERT_DATA_KEY_VALID = 1 << 1,
114 ISERT_PROT_KEY_VALID = 1 << 2,
115 ISERT_SIG_KEY_VALID = 1 << 3,
116};
117
118struct pi_context {
119 struct ib_mr *prot_mr;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200120 struct ib_mr *sig_mr;
121};
122
Vu Pham59464ef2013-08-28 23:23:35 +0300123struct fast_reg_descriptor {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200124 struct list_head list;
125 struct ib_mr *data_mr;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200126 u8 ind;
127 struct pi_context *pi_ctx;
Vu Pham59464ef2013-08-28 23:23:35 +0300128};
129
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +0200130struct isert_data_buf {
131 struct scatterlist *sg;
132 int nents;
133 u32 sg_off;
134 u32 len; /* cur_rdma_length */
135 u32 offset;
136 unsigned int dma_nents;
137 enum dma_data_direction dma_dir;
138};
139
Sagi Grimberg570db172014-12-02 16:57:31 +0200140enum {
141 DATA = 0,
142 PROT = 1,
143 SIG = 2,
144};
145
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800146struct isert_rdma_wr {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800147 struct isert_cmd *isert_cmd;
148 enum iser_ib_op_code iser_ib_op;
149 struct ib_sge *ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +0300150 struct ib_sge s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100151 int rdma_wr_num;
152 struct ib_rdma_wr *rdma_wr;
153 struct ib_rdma_wr s_rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +0200154 struct ib_sge ib_sg[3];
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +0200155 struct isert_data_buf data;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +0200156 struct isert_data_buf prot;
Vu Pham59464ef2013-08-28 23:23:35 +0300157 struct fast_reg_descriptor *fr_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158};
159
160struct isert_cmd {
161 uint32_t read_stag;
162 uint32_t write_stag;
163 uint64_t read_va;
164 uint64_t write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200165 uint32_t inv_rkey;
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -0700166 u64 pdu_buf_dma;
167 u32 pdu_buf_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800168 struct isert_conn *conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700169 struct iscsi_cmd *iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800170 struct iser_tx_desc tx_desc;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300171 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800172 struct isert_rdma_wr rdma_wr;
173 struct work_struct comp_work;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +0300174 struct scatterlist sg;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800175};
176
177struct isert_device;
178
179struct isert_conn {
180 enum iser_conn_state state;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800181 u32 responder_resources;
182 u32 initiator_depth;
Sagi Grimberg23a548e2014-12-02 16:57:35 +0200183 bool pi_support;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800184 u32 max_sge;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200185 struct iser_rx_desc *login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800186 char *login_rsp_buf;
187 u64 login_req_dma;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200188 int login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800189 u64 login_rsp_dma;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300190 struct iser_rx_desc *rx_descs;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300191 struct ib_recv_wr rx_wr[ISERT_QP_MAX_RECV_DTOS];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800192 struct iscsi_conn *conn;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300193 struct list_head node;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300194 struct completion login_comp;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200195 struct completion login_req_comp;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300196 struct iser_tx_desc login_tx_desc;
197 struct rdma_cm_id *cm_id;
198 struct ib_qp *qp;
199 struct isert_device *device;
200 struct mutex mutex;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300201 struct completion wait_comp_err;
202 struct kref kref;
203 struct list_head fr_pool;
204 int fr_pool_size;
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200205 /* lock to protect fastreg pool */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300206 spinlock_t pool_lock;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +0200207 struct work_struct release_work;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200208 struct ib_recv_wr beacon;
Sagi Grimberg991bb762014-12-07 13:12:01 +0200209 bool logout_posted;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200210 bool snd_w_inv;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800211};
212
213#define ISERT_MAX_CQ 64
214
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200215/**
216 * struct isert_comp - iSER completion context
217 *
218 * @device: pointer to device handle
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200219 * @cq: completion queue
Sagi Grimberg36ea63b2014-12-02 16:57:45 +0200220 * @wcs: work completion array
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200221 * @active_qps: Number of active QPs attached
222 * to completion context
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200223 * @work: completion work handle
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200224 */
225struct isert_comp {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200226 struct isert_device *device;
227 struct ib_cq *cq;
Sagi Grimberg36ea63b2014-12-02 16:57:45 +0200228 struct ib_wc wcs[16];
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200229 int active_qps;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200230 struct work_struct work;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800231};
232
233struct isert_device {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200234 int use_fastreg;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200235 bool pi_capable;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800236 int refcount;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800237 struct ib_device *ib_device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300238 struct ib_pd *pd;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200239 struct isert_comp *comps;
240 int comps_used;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800241 struct list_head dev_node;
Vu Phamd40945d2013-08-28 23:23:34 +0300242 int (*reg_rdma_mem)(struct iscsi_conn *conn,
243 struct iscsi_cmd *cmd,
244 struct isert_rdma_wr *wr);
245 void (*unreg_rdma_mem)(struct isert_cmd *isert_cmd,
246 struct isert_conn *isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800247};
248
249struct isert_np {
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200250 struct iscsi_np *np;
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300251 struct semaphore sem;
252 struct rdma_cm_id *cm_id;
253 struct mutex mutex;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300254 struct list_head accepted;
255 struct list_head pending;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800256};