Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 2 | #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 Hellwig | 38a2d0d4 | 2016-05-03 18:01:13 +0200 | [diff] [blame] | 7 | #include <rdma/rw.h> |
Sagi Grimberg | d3cf81f | 2015-12-09 14:12:03 +0200 | [diff] [blame] | 8 | #include <scsi/iser.h> |
| 9 | |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 10 | |
Sagi Grimberg | 24f412d | 2014-12-07 13:12:02 +0200 | [diff] [blame] | 11 | #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 Grimberg | c649415 | 2015-12-09 14:12:04 +0200 | [diff] [blame] | 38 | /* Constant PDU lengths calculations */ |
| 39 | #define ISER_HEADERS_LEN (sizeof(struct iser_ctrl) + \ |
| 40 | sizeof(struct iscsi_hdr)) |
Christoph Hellwig | ed1083b | 2016-02-24 19:24:04 +0200 | [diff] [blame] | 41 | #define ISER_RX_PAYLOAD_SIZE (ISER_HEADERS_LEN + ISCSI_DEF_MAX_RECV_SEG_LEN) |
Sagi Grimberg | c649415 | 2015-12-09 14:12:04 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | 38a2d0d4 | 2016-05-03 18:01:13 +0200 | [diff] [blame] | 58 | #define ISERT_QP_MAX_REQ_DTOS (ISCSI_DEF_XMIT_CMDS_MAX + \ |
Sagi Grimberg | c649415 | 2015-12-09 14:12:04 +0200 | [diff] [blame] | 59 | ISERT_MAX_TX_MISC_PDUS + \ |
| 60 | ISERT_MAX_RX_MISC_PDUS) |
| 61 | |
Sagi Grimberg | 0b089c1 | 2020-09-04 12:50:39 -0700 | [diff] [blame] | 62 | /* |
| 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 Grimberg | c649415 | 2015-12-09 14:12:04 +0200 | [diff] [blame] | 67 | |
Max Gurtovoy | dae7a75 | 2020-10-19 12:46:28 +0300 | [diff] [blame] | 68 | /* Minimum I/O size is 512KB */ |
| 69 | #define ISCSI_ISER_MIN_SG_TABLESIZE 128 |
| 70 | |
Max Gurtovoy | 317000b | 2020-07-08 12:19:08 +0300 | [diff] [blame] | 71 | /* Maximum support is 16MB I/O size */ |
| 72 | #define ISCSI_ISER_MAX_SG_TABLESIZE 4096 |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 73 | |
| 74 | enum isert_desc_type { |
| 75 | ISCSI_TX_CONTROL, |
| 76 | ISCSI_TX_DATAIN |
| 77 | }; |
| 78 | |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 79 | enum iser_conn_state { |
| 80 | ISER_CONN_INIT, |
| 81 | ISER_CONN_UP, |
Jenny Derzhavetz | aea9298 | 2016-02-24 19:23:59 +0200 | [diff] [blame] | 82 | ISER_CONN_BOUND, |
Sagi Grimberg | 128e9cc | 2014-12-02 16:57:20 +0200 | [diff] [blame] | 83 | ISER_CONN_FULL_FEATURE, |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 84 | ISER_CONN_TERMINATING, |
| 85 | ISER_CONN_DOWN, |
| 86 | }; |
| 87 | |
| 88 | struct iser_rx_desc { |
Sagi Grimberg | 0b089c1 | 2020-09-04 12:50:39 -0700 | [diff] [blame] | 89 | char buf[ISER_RX_SIZE]; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 90 | u64 dma_addr; |
| 91 | struct ib_sge rx_sg; |
Christoph Hellwig | 9679cc5 | 2016-02-24 19:24:06 +0200 | [diff] [blame] | 92 | struct ib_cqe rx_cqe; |
Sagi Grimberg | 7a56dc8 | 2017-03-22 17:07:30 +0200 | [diff] [blame] | 93 | bool in_use; |
Sagi Grimberg | 0b089c1 | 2020-09-04 12:50:39 -0700 | [diff] [blame] | 94 | }; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 95 | |
Christoph Hellwig | 9679cc5 | 2016-02-24 19:24:06 +0200 | [diff] [blame] | 96 | static 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 Grimberg | 0b089c1 | 2020-09-04 12:50:39 -0700 | [diff] [blame] | 101 | static 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 | |
| 106 | static size_t isert_get_hdr_offset(struct iser_rx_desc *desc) |
| 107 | { |
| 108 | return isert_get_iser_hdr(desc) - (void *)desc->buf; |
| 109 | } |
| 110 | |
| 111 | static void *isert_get_iscsi_hdr(struct iser_rx_desc *desc) |
| 112 | { |
| 113 | return isert_get_iser_hdr(desc) + sizeof(struct iser_ctrl); |
| 114 | } |
| 115 | |
| 116 | static 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 Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 124 | struct iser_tx_desc { |
Sagi Grimberg | d3cf81f | 2015-12-09 14:12:03 +0200 | [diff] [blame] | 125 | struct iser_ctrl iser_header; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 126 | struct iscsi_hdr iscsi_header; |
| 127 | enum isert_desc_type type; |
| 128 | u64 dma_addr; |
| 129 | struct ib_sge tx_sg[2]; |
Christoph Hellwig | 9679cc5 | 2016-02-24 19:24:06 +0200 | [diff] [blame] | 130 | struct ib_cqe tx_cqe; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 131 | int num_sge; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 132 | struct ib_send_wr send_wr; |
| 133 | } __packed; |
| 134 | |
Christoph Hellwig | 9679cc5 | 2016-02-24 19:24:06 +0200 | [diff] [blame] | 135 | static 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 Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 140 | struct isert_cmd { |
| 141 | uint32_t read_stag; |
| 142 | uint32_t write_stag; |
| 143 | uint64_t read_va; |
| 144 | uint64_t write_va; |
Jenny Derzhavetz | 422bd0a | 2015-12-09 14:12:06 +0200 | [diff] [blame] | 145 | uint32_t inv_rkey; |
Nicholas Bellinger | dbbc5d1 | 2013-07-03 19:39:37 -0700 | [diff] [blame] | 146 | u64 pdu_buf_dma; |
| 147 | u32 pdu_buf_len; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 148 | struct isert_conn *conn; |
Nicholas Bellinger | d703ce2 | 2013-08-17 14:27:56 -0700 | [diff] [blame] | 149 | struct iscsi_cmd *iscsi_cmd; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 150 | struct iser_tx_desc tx_desc; |
Jenny Derzhavetz | 4366b19 | 2015-09-06 14:52:25 +0300 | [diff] [blame] | 151 | struct iser_rx_desc *rx_desc; |
Christoph Hellwig | 38a2d0d4 | 2016-05-03 18:01:13 +0200 | [diff] [blame] | 152 | struct rdma_rw_ctx rw; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 153 | struct work_struct comp_work; |
Jenny Derzhavetz | 9fd6008 | 2015-09-06 14:52:26 +0300 | [diff] [blame] | 154 | struct scatterlist sg; |
Bharat Potnuri | 66f53e6 | 2017-11-28 23:58:07 +0530 | [diff] [blame] | 155 | bool ctx_init_done; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Christoph Hellwig | e3416ab | 2016-02-24 19:24:08 +0200 | [diff] [blame] | 158 | static 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 Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 163 | struct isert_device; |
| 164 | |
| 165 | struct isert_conn { |
| 166 | enum iser_conn_state state; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 167 | u32 responder_resources; |
| 168 | u32 initiator_depth; |
Sagi Grimberg | 23a548e | 2014-12-02 16:57:35 +0200 | [diff] [blame] | 169 | bool pi_support; |
Sagi Grimberg | 0b089c1 | 2020-09-04 12:50:39 -0700 | [diff] [blame] | 170 | struct iser_rx_desc *login_desc; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 171 | char *login_rsp_buf; |
Sagi Grimberg | 2371e5d | 2014-12-02 16:57:21 +0200 | [diff] [blame] | 172 | int login_req_len; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 173 | u64 login_rsp_dma; |
Sagi Grimberg | dac6ab3 | 2015-03-29 15:52:19 +0300 | [diff] [blame] | 174 | struct iser_rx_desc *rx_descs; |
Jenny Derzhavetz | 4366b19 | 2015-09-06 14:52:25 +0300 | [diff] [blame] | 175 | struct ib_recv_wr rx_wr[ISERT_QP_MAX_RECV_DTOS]; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 176 | struct iscsi_conn *conn; |
Jenny Derzhavetz | bd37922 | 2015-09-06 14:52:24 +0300 | [diff] [blame] | 177 | struct list_head node; |
Sagi Grimberg | dac6ab3 | 2015-03-29 15:52:19 +0300 | [diff] [blame] | 178 | struct completion login_comp; |
Sagi Grimberg | 2371e5d | 2014-12-02 16:57:21 +0200 | [diff] [blame] | 179 | struct completion login_req_comp; |
Sagi Grimberg | dac6ab3 | 2015-03-29 15:52:19 +0300 | [diff] [blame] | 180 | struct iser_tx_desc login_tx_desc; |
| 181 | struct rdma_cm_id *cm_id; |
| 182 | struct ib_qp *qp; |
Yamin Friedman | c6e6630 | 2020-07-22 16:56:28 +0300 | [diff] [blame] | 183 | struct ib_cq *cq; |
| 184 | u32 cq_size; |
Sagi Grimberg | dac6ab3 | 2015-03-29 15:52:19 +0300 | [diff] [blame] | 185 | struct isert_device *device; |
| 186 | struct mutex mutex; |
Sagi Grimberg | dac6ab3 | 2015-03-29 15:52:19 +0300 | [diff] [blame] | 187 | struct kref kref; |
Sagi Grimberg | b02efbf | 2014-12-02 16:57:29 +0200 | [diff] [blame] | 188 | struct work_struct release_work; |
Sagi Grimberg | 991bb76 | 2014-12-07 13:12:01 +0200 | [diff] [blame] | 189 | bool logout_posted; |
Jenny Derzhavetz | 422bd0a | 2015-12-09 14:12:06 +0200 | [diff] [blame] | 190 | bool snd_w_inv; |
Raju Rangoju | 63b268d | 2016-08-29 17:15:49 +0530 | [diff] [blame] | 191 | wait_queue_head_t rem_wait; |
| 192 | bool dev_removed; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 195 | struct isert_device { |
Sagi Grimberg | d3e125d | 2014-02-19 17:50:23 +0200 | [diff] [blame] | 196 | bool pi_capable; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 197 | int refcount; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 198 | struct ib_device *ib_device; |
Sagi Grimberg | 67cb394 | 2015-03-29 15:52:05 +0300 | [diff] [blame] | 199 | struct ib_pd *pd; |
Sagi Grimberg | 4a295ba | 2014-12-02 16:57:40 +0200 | [diff] [blame] | 200 | struct isert_comp *comps; |
| 201 | int comps_used; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 202 | struct list_head dev_node; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | struct isert_np { |
Sagi Grimberg | ca6c1d8 | 2014-12-02 16:57:27 +0200 | [diff] [blame] | 206 | struct iscsi_np *np; |
Jenny Derzhavetz | ed8cb0a | 2015-09-06 14:52:23 +0300 | [diff] [blame] | 207 | struct semaphore sem; |
| 208 | struct rdma_cm_id *cm_id; |
| 209 | struct mutex mutex; |
Jenny Derzhavetz | bd37922 | 2015-09-06 14:52:24 +0300 | [diff] [blame] | 210 | struct list_head accepted; |
| 211 | struct list_head pending; |
Nicholas Bellinger | b8d26b3 | 2013-03-07 00:56:19 -0800 | [diff] [blame] | 212 | }; |