blob: ca8cfebe26ca78fd18203c66eafb5c7cc6619f04 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002#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>
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02007#include <rdma/rw.h>
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02008#include <scsi/iser.h>
9
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080010
Sagi Grimberg24f412d2014-12-07 13:12:02 +020011#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
Sagi Grimbergc6494152015-12-09 14:12:04 +020038/* Constant PDU lengths calculations */
39#define ISER_HEADERS_LEN (sizeof(struct iser_ctrl) + \
40 sizeof(struct iscsi_hdr))
Christoph Hellwiged1083b2016-02-24 19:24:04 +020041#define ISER_RX_PAYLOAD_SIZE (ISER_HEADERS_LEN + ISCSI_DEF_MAX_RECV_SEG_LEN)
Sagi Grimbergc6494152015-12-09 14:12:04 +020042
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
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +020058#define ISERT_QP_MAX_REQ_DTOS (ISCSI_DEF_XMIT_CMDS_MAX + \
Sagi Grimbergc6494152015-12-09 14:12:04 +020059 ISERT_MAX_TX_MISC_PDUS + \
60 ISERT_MAX_RX_MISC_PDUS)
61
Sagi Grimberg0b089c12020-09-04 12:50:39 -070062/*
63 * RX size is default of 8k plus headers, but data needs to align to
64 * 512 boundary, so use 1024 to have the extra space for alignment.
65 */
66#define ISER_RX_SIZE (ISCSI_DEF_MAX_RECV_SEG_LEN + 1024)
Sagi Grimbergc6494152015-12-09 14:12:04 +020067
Max Gurtovoydae7a752020-10-19 12:46:28 +030068/* Minimum I/O size is 512KB */
69#define ISCSI_ISER_MIN_SG_TABLESIZE 128
70
Max Gurtovoy317000b2020-07-08 12:19:08 +030071/* Maximum support is 16MB I/O size */
72#define ISCSI_ISER_MAX_SG_TABLESIZE 4096
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080073
74enum isert_desc_type {
75 ISCSI_TX_CONTROL,
76 ISCSI_TX_DATAIN
77};
78
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080079enum iser_conn_state {
80 ISER_CONN_INIT,
81 ISER_CONN_UP,
Jenny Derzhavetzaea92982016-02-24 19:23:59 +020082 ISER_CONN_BOUND,
Sagi Grimberg128e9cc2014-12-02 16:57:20 +020083 ISER_CONN_FULL_FEATURE,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080084 ISER_CONN_TERMINATING,
85 ISER_CONN_DOWN,
86};
87
88struct iser_rx_desc {
Sagi Grimberg0b089c12020-09-04 12:50:39 -070089 char buf[ISER_RX_SIZE];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080090 u64 dma_addr;
91 struct ib_sge rx_sg;
Christoph Hellwig9679cc52016-02-24 19:24:06 +020092 struct ib_cqe rx_cqe;
Sagi Grimberg7a56dc82017-03-22 17:07:30 +020093 bool in_use;
Sagi Grimberg0b089c12020-09-04 12:50:39 -070094};
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080095
Christoph Hellwig9679cc52016-02-24 19:24:06 +020096static inline struct iser_rx_desc *cqe_to_rx_desc(struct ib_cqe *cqe)
97{
98 return container_of(cqe, struct iser_rx_desc, rx_cqe);
99}
100
Sagi Grimberg0b089c12020-09-04 12:50:39 -0700101static void *isert_get_iser_hdr(struct iser_rx_desc *desc)
102{
103 return PTR_ALIGN(desc->buf + ISER_HEADERS_LEN, 512) - ISER_HEADERS_LEN;
104}
105
106static size_t isert_get_hdr_offset(struct iser_rx_desc *desc)
107{
108 return isert_get_iser_hdr(desc) - (void *)desc->buf;
109}
110
111static void *isert_get_iscsi_hdr(struct iser_rx_desc *desc)
112{
113 return isert_get_iser_hdr(desc) + sizeof(struct iser_ctrl);
114}
115
116static void *isert_get_data(struct iser_rx_desc *desc)
117{
118 void *data = isert_get_iser_hdr(desc) + ISER_HEADERS_LEN;
119
120 WARN_ON((uintptr_t)data & 511);
121 return data;
122}
123
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800124struct iser_tx_desc {
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200125 struct iser_ctrl iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800126 struct iscsi_hdr iscsi_header;
127 enum isert_desc_type type;
128 u64 dma_addr;
129 struct ib_sge tx_sg[2];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200130 struct ib_cqe tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800131 int num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800132 struct ib_send_wr send_wr;
133} __packed;
134
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200135static inline struct iser_tx_desc *cqe_to_tx_desc(struct ib_cqe *cqe)
136{
137 return container_of(cqe, struct iser_tx_desc, tx_cqe);
138}
139
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800140struct isert_cmd {
141 uint32_t read_stag;
142 uint32_t write_stag;
143 uint64_t read_va;
144 uint64_t write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200145 uint32_t inv_rkey;
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -0700146 u64 pdu_buf_dma;
147 u32 pdu_buf_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800148 struct isert_conn *conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700149 struct iscsi_cmd *iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800150 struct iser_tx_desc tx_desc;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300151 struct iser_rx_desc *rx_desc;
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +0200152 struct rdma_rw_ctx rw;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800153 struct work_struct comp_work;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +0300154 struct scatterlist sg;
Bharat Potnuri66f53e62017-11-28 23:58:07 +0530155 bool ctx_init_done;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800156};
157
Christoph Hellwige3416ab2016-02-24 19:24:08 +0200158static inline struct isert_cmd *tx_desc_to_cmd(struct iser_tx_desc *desc)
159{
160 return container_of(desc, struct isert_cmd, tx_desc);
161}
162
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800163struct isert_device;
164
165struct isert_conn {
166 enum iser_conn_state state;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800167 u32 responder_resources;
168 u32 initiator_depth;
Sagi Grimberg23a548e2014-12-02 16:57:35 +0200169 bool pi_support;
Sagi Grimberg0b089c12020-09-04 12:50:39 -0700170 struct iser_rx_desc *login_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800171 char *login_rsp_buf;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200172 int login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800173 u64 login_rsp_dma;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300174 struct iser_rx_desc *rx_descs;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300175 struct ib_recv_wr rx_wr[ISERT_QP_MAX_RECV_DTOS];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800176 struct iscsi_conn *conn;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300177 struct list_head node;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300178 struct completion login_comp;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200179 struct completion login_req_comp;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300180 struct iser_tx_desc login_tx_desc;
181 struct rdma_cm_id *cm_id;
182 struct ib_qp *qp;
Yamin Friedmanc6e66302020-07-22 16:56:28 +0300183 struct ib_cq *cq;
184 u32 cq_size;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300185 struct isert_device *device;
186 struct mutex mutex;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300187 struct kref kref;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +0200188 struct work_struct release_work;
Sagi Grimberg991bb762014-12-07 13:12:01 +0200189 bool logout_posted;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200190 bool snd_w_inv;
Raju Rangoju63b268d2016-08-29 17:15:49 +0530191 wait_queue_head_t rem_wait;
192 bool dev_removed;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800193};
194
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800195struct isert_device {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200196 bool pi_capable;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800197 int refcount;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800198 struct ib_device *ib_device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300199 struct ib_pd *pd;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200200 struct isert_comp *comps;
201 int comps_used;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800202 struct list_head dev_node;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800203};
204
205struct isert_np {
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200206 struct iscsi_np *np;
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300207 struct semaphore sem;
208 struct rdma_cm_id *cm_id;
209 struct mutex mutex;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300210 struct list_head accepted;
211 struct list_head pending;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800212};