1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2b8d26b3bSNicholas Bellinger #include <linux/socket.h>
3b8d26b3bSNicholas Bellinger #include <linux/in.h>
4b8d26b3bSNicholas Bellinger #include <linux/in6.h>
5b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
6b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
738a2d0d4SChristoph Hellwig #include <rdma/rw.h>
8d3cf81f9SSagi Grimberg #include <scsi/iser.h>
9d3cf81f9SSagi Grimberg
10b8d26b3bSNicholas Bellinger
1124f412ddSSagi Grimberg #define DRV_NAME "isert"
1224f412ddSSagi Grimberg #define PFX DRV_NAME ": "
1324f412ddSSagi Grimberg
1424f412ddSSagi Grimberg #define isert_dbg(fmt, arg...) \
1524f412ddSSagi Grimberg do { \
1624f412ddSSagi Grimberg if (unlikely(isert_debug_level > 2)) \
1724f412ddSSagi Grimberg printk(KERN_DEBUG PFX "%s: " fmt,\
1824f412ddSSagi Grimberg __func__ , ## arg); \
1924f412ddSSagi Grimberg } while (0)
2024f412ddSSagi Grimberg
2124f412ddSSagi Grimberg #define isert_warn(fmt, arg...) \
2224f412ddSSagi Grimberg do { \
2324f412ddSSagi Grimberg if (unlikely(isert_debug_level > 0)) \
2424f412ddSSagi Grimberg pr_warn(PFX "%s: " fmt, \
2524f412ddSSagi Grimberg __func__ , ## arg); \
2624f412ddSSagi Grimberg } while (0)
2724f412ddSSagi Grimberg
2824f412ddSSagi Grimberg #define isert_info(fmt, arg...) \
2924f412ddSSagi Grimberg do { \
3024f412ddSSagi Grimberg if (unlikely(isert_debug_level > 1)) \
3124f412ddSSagi Grimberg pr_info(PFX "%s: " fmt, \
3224f412ddSSagi Grimberg __func__ , ## arg); \
3324f412ddSSagi Grimberg } while (0)
3424f412ddSSagi Grimberg
3524f412ddSSagi Grimberg #define isert_err(fmt, arg...) \
3624f412ddSSagi Grimberg pr_err(PFX "%s: " fmt, __func__ , ## arg)
3724f412ddSSagi Grimberg
38c6494153SSagi Grimberg /* Constant PDU lengths calculations */
39c6494153SSagi Grimberg #define ISER_HEADERS_LEN (sizeof(struct iser_ctrl) + \
40c6494153SSagi Grimberg sizeof(struct iscsi_hdr))
41ed1083b2SChristoph Hellwig #define ISER_RX_PAYLOAD_SIZE (ISER_HEADERS_LEN + ISCSI_DEF_MAX_RECV_SEG_LEN)
42c6494153SSagi Grimberg
43c6494153SSagi Grimberg /* QP settings */
44c6494153SSagi Grimberg /* Maximal bounds on received asynchronous PDUs */
45c6494153SSagi Grimberg #define ISERT_MAX_TX_MISC_PDUS 4 /* NOOP_IN(2) , ASYNC_EVENT(2) */
46c6494153SSagi Grimberg
47c6494153SSagi Grimberg #define ISERT_MAX_RX_MISC_PDUS 6 /*
48c6494153SSagi Grimberg * NOOP_OUT(2), TEXT(1),
49c6494153SSagi Grimberg * SCSI_TMFUNC(2), LOGOUT(1)
50c6494153SSagi Grimberg */
51c6494153SSagi Grimberg
52c6494153SSagi Grimberg #define ISCSI_DEF_XMIT_CMDS_MAX 128 /* from libiscsi.h, must be power of 2 */
53c6494153SSagi Grimberg
54c6494153SSagi Grimberg #define ISERT_QP_MAX_RECV_DTOS (ISCSI_DEF_XMIT_CMDS_MAX)
55c6494153SSagi Grimberg
56c6494153SSagi Grimberg #define ISERT_MIN_POSTED_RX (ISCSI_DEF_XMIT_CMDS_MAX >> 2)
57c6494153SSagi Grimberg
5838a2d0d4SChristoph Hellwig #define ISERT_QP_MAX_REQ_DTOS (ISCSI_DEF_XMIT_CMDS_MAX + \
59c6494153SSagi Grimberg ISERT_MAX_TX_MISC_PDUS + \
60c6494153SSagi Grimberg ISERT_MAX_RX_MISC_PDUS)
61c6494153SSagi Grimberg
620b089c1eSSagi Grimberg /*
630b089c1eSSagi Grimberg * RX size is default of 8k plus headers, but data needs to align to
640b089c1eSSagi Grimberg * 512 boundary, so use 1024 to have the extra space for alignment.
650b089c1eSSagi Grimberg */
660b089c1eSSagi Grimberg #define ISER_RX_SIZE (ISCSI_DEF_MAX_RECV_SEG_LEN + 1024)
67c6494153SSagi Grimberg
68dae7a75fSMax Gurtovoy /* Minimum I/O size is 512KB */
69dae7a75fSMax Gurtovoy #define ISCSI_ISER_MIN_SG_TABLESIZE 128
70dae7a75fSMax Gurtovoy
71317000b9SMax Gurtovoy /* Maximum support is 16MB I/O size */
72317000b9SMax Gurtovoy #define ISCSI_ISER_MAX_SG_TABLESIZE 4096
73b8d26b3bSNicholas Bellinger
74b8d26b3bSNicholas Bellinger enum isert_desc_type {
75b8d26b3bSNicholas Bellinger ISCSI_TX_CONTROL,
76b8d26b3bSNicholas Bellinger ISCSI_TX_DATAIN
77b8d26b3bSNicholas Bellinger };
78b8d26b3bSNicholas Bellinger
79b8d26b3bSNicholas Bellinger enum iser_conn_state {
80b8d26b3bSNicholas Bellinger ISER_CONN_INIT,
81b8d26b3bSNicholas Bellinger ISER_CONN_UP,
82aea92980SJenny Derzhavetz ISER_CONN_BOUND,
83128e9cc8SSagi Grimberg ISER_CONN_FULL_FEATURE,
84b8d26b3bSNicholas Bellinger ISER_CONN_TERMINATING,
85b8d26b3bSNicholas Bellinger ISER_CONN_DOWN,
86b8d26b3bSNicholas Bellinger };
87b8d26b3bSNicholas Bellinger
88b8d26b3bSNicholas Bellinger struct iser_rx_desc {
890b089c1eSSagi Grimberg char buf[ISER_RX_SIZE];
90b8d26b3bSNicholas Bellinger u64 dma_addr;
91b8d26b3bSNicholas Bellinger struct ib_sge rx_sg;
929679cc51SChristoph Hellwig struct ib_cqe rx_cqe;
937a56dc88SSagi Grimberg bool in_use;
940b089c1eSSagi Grimberg };
95b8d26b3bSNicholas Bellinger
cqe_to_rx_desc(struct ib_cqe * cqe)969679cc51SChristoph Hellwig static inline struct iser_rx_desc *cqe_to_rx_desc(struct ib_cqe *cqe)
979679cc51SChristoph Hellwig {
989679cc51SChristoph Hellwig return container_of(cqe, struct iser_rx_desc, rx_cqe);
999679cc51SChristoph Hellwig }
1009679cc51SChristoph Hellwig
isert_get_iser_hdr(struct iser_rx_desc * desc)1010b089c1eSSagi Grimberg static void *isert_get_iser_hdr(struct iser_rx_desc *desc)
1020b089c1eSSagi Grimberg {
1030b089c1eSSagi Grimberg return PTR_ALIGN(desc->buf + ISER_HEADERS_LEN, 512) - ISER_HEADERS_LEN;
1040b089c1eSSagi Grimberg }
1050b089c1eSSagi Grimberg
isert_get_hdr_offset(struct iser_rx_desc * desc)1060b089c1eSSagi Grimberg static size_t isert_get_hdr_offset(struct iser_rx_desc *desc)
1070b089c1eSSagi Grimberg {
1080b089c1eSSagi Grimberg return isert_get_iser_hdr(desc) - (void *)desc->buf;
1090b089c1eSSagi Grimberg }
1100b089c1eSSagi Grimberg
isert_get_iscsi_hdr(struct iser_rx_desc * desc)1110b089c1eSSagi Grimberg static void *isert_get_iscsi_hdr(struct iser_rx_desc *desc)
1120b089c1eSSagi Grimberg {
1130b089c1eSSagi Grimberg return isert_get_iser_hdr(desc) + sizeof(struct iser_ctrl);
1140b089c1eSSagi Grimberg }
1150b089c1eSSagi Grimberg
isert_get_data(struct iser_rx_desc * desc)1160b089c1eSSagi Grimberg static void *isert_get_data(struct iser_rx_desc *desc)
1170b089c1eSSagi Grimberg {
1180b089c1eSSagi Grimberg void *data = isert_get_iser_hdr(desc) + ISER_HEADERS_LEN;
1190b089c1eSSagi Grimberg
1200b089c1eSSagi Grimberg WARN_ON((uintptr_t)data & 511);
1210b089c1eSSagi Grimberg return data;
1220b089c1eSSagi Grimberg }
1230b089c1eSSagi Grimberg
124b8d26b3bSNicholas Bellinger struct iser_tx_desc {
125d3cf81f9SSagi Grimberg struct iser_ctrl iser_header;
126b8d26b3bSNicholas Bellinger struct iscsi_hdr iscsi_header;
127b8d26b3bSNicholas Bellinger enum isert_desc_type type;
128b8d26b3bSNicholas Bellinger u64 dma_addr;
129b8d26b3bSNicholas Bellinger struct ib_sge tx_sg[2];
1309679cc51SChristoph Hellwig struct ib_cqe tx_cqe;
131b8d26b3bSNicholas Bellinger int num_sge;
132b8d26b3bSNicholas Bellinger struct ib_send_wr send_wr;
133b8d26b3bSNicholas Bellinger } __packed;
134b8d26b3bSNicholas Bellinger
cqe_to_tx_desc(struct ib_cqe * cqe)1359679cc51SChristoph Hellwig static inline struct iser_tx_desc *cqe_to_tx_desc(struct ib_cqe *cqe)
1369679cc51SChristoph Hellwig {
1379679cc51SChristoph Hellwig return container_of(cqe, struct iser_tx_desc, tx_cqe);
1389679cc51SChristoph Hellwig }
1399679cc51SChristoph Hellwig
140b8d26b3bSNicholas Bellinger struct isert_cmd {
141b8d26b3bSNicholas Bellinger uint32_t read_stag;
142b8d26b3bSNicholas Bellinger uint32_t write_stag;
143b8d26b3bSNicholas Bellinger uint64_t read_va;
144b8d26b3bSNicholas Bellinger uint64_t write_va;
145422bd0acSJenny Derzhavetz uint32_t inv_rkey;
146dbbc5d11SNicholas Bellinger u64 pdu_buf_dma;
147dbbc5d11SNicholas Bellinger u32 pdu_buf_len;
148b8d26b3bSNicholas Bellinger struct isert_conn *conn;
14966cd9d4eSMax Gurtovoy struct iscsit_cmd *iscsit_cmd;
150b8d26b3bSNicholas Bellinger struct iser_tx_desc tx_desc;
1514366b19cSJenny Derzhavetz struct iser_rx_desc *rx_desc;
15238a2d0d4SChristoph Hellwig struct rdma_rw_ctx rw;
153b8d26b3bSNicholas Bellinger struct work_struct comp_work;
1549fd60088SJenny Derzhavetz struct scatterlist sg;
15566f53e6fSBharat Potnuri bool ctx_init_done;
156b8d26b3bSNicholas Bellinger };
157b8d26b3bSNicholas Bellinger
tx_desc_to_cmd(struct iser_tx_desc * desc)158e3416ab2SChristoph Hellwig static inline struct isert_cmd *tx_desc_to_cmd(struct iser_tx_desc *desc)
159e3416ab2SChristoph Hellwig {
160e3416ab2SChristoph Hellwig return container_of(desc, struct isert_cmd, tx_desc);
161e3416ab2SChristoph Hellwig }
162e3416ab2SChristoph Hellwig
163b8d26b3bSNicholas Bellinger struct isert_device;
164b8d26b3bSNicholas Bellinger
165b8d26b3bSNicholas Bellinger struct isert_conn {
166b8d26b3bSNicholas Bellinger enum iser_conn_state state;
167b8d26b3bSNicholas Bellinger u32 responder_resources;
168b8d26b3bSNicholas Bellinger u32 initiator_depth;
16923a548eeSSagi Grimberg bool pi_support;
1700b089c1eSSagi Grimberg struct iser_rx_desc *login_desc;
171b8d26b3bSNicholas Bellinger char *login_rsp_buf;
1722371e5daSSagi Grimberg int login_req_len;
173b8d26b3bSNicholas Bellinger u64 login_rsp_dma;
174dac6ab30SSagi Grimberg struct iser_rx_desc *rx_descs;
1754366b19cSJenny Derzhavetz struct ib_recv_wr rx_wr[ISERT_QP_MAX_RECV_DTOS];
176*be36d683SMax Gurtovoy struct iscsit_conn *conn;
177bd379220SJenny Derzhavetz struct list_head node;
178dac6ab30SSagi Grimberg struct completion login_comp;
1792371e5daSSagi Grimberg struct completion login_req_comp;
180dac6ab30SSagi Grimberg struct iser_tx_desc login_tx_desc;
181dac6ab30SSagi Grimberg struct rdma_cm_id *cm_id;
182dac6ab30SSagi Grimberg struct ib_qp *qp;
183c6e66307SYamin Friedman struct ib_cq *cq;
184c6e66307SYamin Friedman u32 cq_size;
185dac6ab30SSagi Grimberg struct isert_device *device;
186dac6ab30SSagi Grimberg struct mutex mutex;
187dac6ab30SSagi Grimberg struct kref kref;
188b02efbfcSSagi Grimberg struct work_struct release_work;
189991bb764SSagi Grimberg bool logout_posted;
190422bd0acSJenny Derzhavetz bool snd_w_inv;
19163b268d2SRaju Rangoju wait_queue_head_t rem_wait;
19263b268d2SRaju Rangoju bool dev_removed;
193b8d26b3bSNicholas Bellinger };
194b8d26b3bSNicholas Bellinger
195b8d26b3bSNicholas Bellinger struct isert_device {
196d3e125daSSagi Grimberg bool pi_capable;
197b8d26b3bSNicholas Bellinger int refcount;
198b8d26b3bSNicholas Bellinger struct ib_device *ib_device;
19967cb3949SSagi Grimberg struct ib_pd *pd;
2004a295baeSSagi Grimberg struct isert_comp *comps;
2014a295baeSSagi Grimberg int comps_used;
202b8d26b3bSNicholas Bellinger struct list_head dev_node;
203b8d26b3bSNicholas Bellinger };
204b8d26b3bSNicholas Bellinger
205b8d26b3bSNicholas Bellinger struct isert_np {
206ca6c1d82SSagi Grimberg struct iscsi_np *np;
207ed8cb0a4SJenny Derzhavetz struct semaphore sem;
208ed8cb0a4SJenny Derzhavetz struct rdma_cm_id *cm_id;
209ed8cb0a4SJenny Derzhavetz struct mutex mutex;
210bd379220SJenny Derzhavetz struct list_head accepted;
211bd379220SJenny Derzhavetz struct list_head pending;
212b8d26b3bSNicholas Bellinger };
213