blob: f552797dc8639020e5a94e899336497c71db4981 [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 - \
Christoph Hellwig9679cc52016-02-24 19:24:06 +020064 (ISER_RX_PAYLOAD_SIZE + sizeof(u64) + sizeof(struct ib_sge) + \
65 sizeof(struct ib_cqe)))
Sagi Grimbergc6494152015-12-09 14:12:04 +020066
Vu Pham59464ef2013-08-28 23:23:35 +030067#define ISCSI_ISER_SG_TABLESIZE 256
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080068
69enum isert_desc_type {
70 ISCSI_TX_CONTROL,
71 ISCSI_TX_DATAIN
72};
73
74enum iser_ib_op_code {
75 ISER_IB_RECV,
76 ISER_IB_SEND,
77 ISER_IB_RDMA_WRITE,
78 ISER_IB_RDMA_READ,
79};
80
81enum iser_conn_state {
82 ISER_CONN_INIT,
83 ISER_CONN_UP,
Jenny Derzhavetzaea92982016-02-24 19:23:59 +020084 ISER_CONN_BOUND,
Sagi Grimberg128e9cc2014-12-02 16:57:20 +020085 ISER_CONN_FULL_FEATURE,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080086 ISER_CONN_TERMINATING,
87 ISER_CONN_DOWN,
88};
89
90struct iser_rx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +020091 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080092 struct iscsi_hdr iscsi_header;
Christoph Hellwiged1083b2016-02-24 19:24:04 +020093 char data[ISCSI_DEF_MAX_RECV_SEG_LEN];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080094 u64 dma_addr;
95 struct ib_sge rx_sg;
Christoph Hellwig9679cc52016-02-24 19:24:06 +020096 struct ib_cqe rx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080097 char pad[ISER_RX_PAD_SIZE];
98} __packed;
99
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200100static inline struct iser_rx_desc *cqe_to_rx_desc(struct ib_cqe *cqe)
101{
102 return container_of(cqe, struct iser_rx_desc, rx_cqe);
103}
104
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800105struct iser_tx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200106 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800107 struct iscsi_hdr iscsi_header;
108 enum isert_desc_type type;
109 u64 dma_addr;
110 struct ib_sge tx_sg[2];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200111 struct ib_cqe tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800112 int num_sge;
113 struct isert_cmd *isert_cmd;
114 struct ib_send_wr send_wr;
115} __packed;
116
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200117static inline struct iser_tx_desc *cqe_to_tx_desc(struct ib_cqe *cqe)
118{
119 return container_of(cqe, struct iser_tx_desc, tx_cqe);
120}
121
122
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200123enum isert_indicator {
124 ISERT_PROTECTED = 1 << 0,
125 ISERT_DATA_KEY_VALID = 1 << 1,
126 ISERT_PROT_KEY_VALID = 1 << 2,
127 ISERT_SIG_KEY_VALID = 1 << 3,
128};
129
130struct pi_context {
131 struct ib_mr *prot_mr;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200132 struct ib_mr *sig_mr;
133};
134
Vu Pham59464ef2013-08-28 23:23:35 +0300135struct fast_reg_descriptor {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200136 struct list_head list;
137 struct ib_mr *data_mr;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200138 u8 ind;
139 struct pi_context *pi_ctx;
Vu Pham59464ef2013-08-28 23:23:35 +0300140};
141
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +0200142struct isert_data_buf {
143 struct scatterlist *sg;
144 int nents;
145 u32 sg_off;
146 u32 len; /* cur_rdma_length */
147 u32 offset;
148 unsigned int dma_nents;
149 enum dma_data_direction dma_dir;
150};
151
Sagi Grimberg570db172014-12-02 16:57:31 +0200152enum {
153 DATA = 0,
154 PROT = 1,
155 SIG = 2,
156};
157
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158struct isert_cmd {
159 uint32_t read_stag;
160 uint32_t write_stag;
161 uint64_t read_va;
162 uint64_t write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200163 uint32_t inv_rkey;
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -0700164 u64 pdu_buf_dma;
165 u32 pdu_buf_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800166 struct isert_conn *conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700167 struct iscsi_cmd *iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800168 struct iser_tx_desc tx_desc;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300169 struct iser_rx_desc *rx_desc;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +0200170 enum iser_ib_op_code iser_ib_op;
171 struct ib_sge *ib_sge;
172 struct ib_sge s_ib_sge;
173 int rdma_wr_num;
174 struct ib_rdma_wr *rdma_wr;
175 struct ib_rdma_wr s_rdma_wr;
176 struct ib_sge ib_sg[3];
177 struct isert_data_buf data;
178 struct isert_data_buf prot;
179 struct fast_reg_descriptor *fr_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800180 struct work_struct comp_work;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +0300181 struct scatterlist sg;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800182};
183
184struct isert_device;
185
186struct isert_conn {
187 enum iser_conn_state state;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800188 u32 responder_resources;
189 u32 initiator_depth;
Sagi Grimberg23a548e2014-12-02 16:57:35 +0200190 bool pi_support;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800191 u32 max_sge;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200192 struct iser_rx_desc *login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800193 char *login_rsp_buf;
194 u64 login_req_dma;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200195 int login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800196 u64 login_rsp_dma;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300197 struct iser_rx_desc *rx_descs;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300198 struct ib_recv_wr rx_wr[ISERT_QP_MAX_RECV_DTOS];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800199 struct iscsi_conn *conn;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300200 struct list_head node;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300201 struct completion login_comp;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200202 struct completion login_req_comp;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300203 struct iser_tx_desc login_tx_desc;
204 struct rdma_cm_id *cm_id;
205 struct ib_qp *qp;
206 struct isert_device *device;
207 struct mutex mutex;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300208 struct completion wait_comp_err;
209 struct kref kref;
210 struct list_head fr_pool;
211 int fr_pool_size;
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200212 /* lock to protect fastreg pool */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300213 spinlock_t pool_lock;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +0200214 struct work_struct release_work;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200215 struct ib_recv_wr beacon;
Sagi Grimberg991bb762014-12-07 13:12:01 +0200216 bool logout_posted;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200217 bool snd_w_inv;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800218};
219
220#define ISERT_MAX_CQ 64
221
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200222/**
223 * struct isert_comp - iSER completion context
224 *
225 * @device: pointer to device handle
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200226 * @cq: completion queue
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200227 * @active_qps: Number of active QPs attached
228 * to completion context
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200229 */
230struct isert_comp {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200231 struct isert_device *device;
232 struct ib_cq *cq;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200233 int active_qps;
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;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +0200245 int (*reg_rdma_mem)(struct isert_cmd *isert_cmd,
246 struct iscsi_conn *conn);
Vu Phamd40945d2013-08-28 23:23:34 +0300247 void (*unreg_rdma_mem)(struct isert_cmd *isert_cmd,
248 struct isert_conn *isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800249};
250
251struct isert_np {
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200252 struct iscsi_np *np;
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300253 struct semaphore sem;
254 struct rdma_cm_id *cm_id;
255 struct mutex mutex;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300256 struct list_head accepted;
257 struct list_head pending;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800258};