Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 4 | * |
| 5 | * IB infrastructure: |
| 6 | * Establish SMC-R as an Infiniband Client to be notified about added and |
| 7 | * removed IB devices of type RDMA. |
| 8 | * Determine device and port characteristics for these IB devices. |
| 9 | * |
| 10 | * Copyright IBM Corp. 2016 |
| 11 | * |
| 12 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 13 | */ |
| 14 | |
| 15 | #include <linux/random.h> |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 16 | #include <linux/workqueue.h> |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 17 | #include <linux/scatterlist.h> |
Ursula Braun | 6dabd40 | 2019-11-16 17:47:29 +0100 | [diff] [blame] | 18 | #include <linux/wait.h> |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 20 | #include <rdma/ib_verbs.h> |
Parav Pandit | ddb457c | 2018-06-05 08:40:19 +0300 | [diff] [blame] | 21 | #include <rdma/ib_cache.h> |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 22 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 23 | #include "smc_pnet.h" |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 24 | #include "smc_ib.h" |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 25 | #include "smc_core.h" |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 26 | #include "smc_wr.h" |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 27 | #include "smc.h" |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 28 | #include "smc_netlink.h" |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 29 | |
Ursula Braun | c9f4c6c | 2018-03-14 11:01:00 +0100 | [diff] [blame] | 30 | #define SMC_MAX_CQE 32766 /* max. # of completion queue elements */ |
| 31 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 32 | #define SMC_QP_MIN_RNR_TIMER 5 |
| 33 | #define SMC_QP_TIMEOUT 15 /* 4096 * 2 ** timeout usec */ |
| 34 | #define SMC_QP_RETRY_CNT 7 /* 7: infinite */ |
| 35 | #define SMC_QP_RNR_RETRY 7 /* 7: infinite */ |
| 36 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 37 | struct smc_ib_devices smc_ib_devices = { /* smc-registered ib devices */ |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 38 | .mutex = __MUTEX_INITIALIZER(smc_ib_devices.mutex), |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 39 | .list = LIST_HEAD_INIT(smc_ib_devices.list), |
| 40 | }; |
| 41 | |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 42 | u8 local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */ |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 43 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 44 | static int smc_ib_modify_qp_init(struct smc_link *lnk) |
| 45 | { |
| 46 | struct ib_qp_attr qp_attr; |
| 47 | |
| 48 | memset(&qp_attr, 0, sizeof(qp_attr)); |
| 49 | qp_attr.qp_state = IB_QPS_INIT; |
| 50 | qp_attr.pkey_index = 0; |
| 51 | qp_attr.port_num = lnk->ibport; |
| 52 | qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE |
| 53 | | IB_ACCESS_REMOTE_WRITE; |
| 54 | return ib_modify_qp(lnk->roce_qp, &qp_attr, |
| 55 | IB_QP_STATE | IB_QP_PKEY_INDEX | |
| 56 | IB_QP_ACCESS_FLAGS | IB_QP_PORT); |
| 57 | } |
| 58 | |
| 59 | static int smc_ib_modify_qp_rtr(struct smc_link *lnk) |
| 60 | { |
| 61 | enum ib_qp_attr_mask qp_attr_mask = |
| 62 | IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | IB_QP_DEST_QPN | |
| 63 | IB_QP_RQ_PSN | IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER; |
| 64 | struct ib_qp_attr qp_attr; |
| 65 | |
| 66 | memset(&qp_attr, 0, sizeof(qp_attr)); |
| 67 | qp_attr.qp_state = IB_QPS_RTR; |
| 68 | qp_attr.path_mtu = min(lnk->path_mtu, lnk->peer_mtu); |
Dasaratharaman Chandramouli | 44c5848 | 2017-04-29 14:41:29 -0400 | [diff] [blame] | 69 | qp_attr.ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE; |
Dasaratharaman Chandramouli | d8966fc | 2017-04-29 14:41:28 -0400 | [diff] [blame] | 70 | rdma_ah_set_port_num(&qp_attr.ah_attr, lnk->ibport); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 71 | rdma_ah_set_grh(&qp_attr.ah_attr, NULL, 0, lnk->sgid_index, 1, 0); |
Dasaratharaman Chandramouli | d8966fc | 2017-04-29 14:41:28 -0400 | [diff] [blame] | 72 | rdma_ah_set_dgid_raw(&qp_attr.ah_attr, lnk->peer_gid); |
Dasaratharaman Chandramouli | 44c5848 | 2017-04-29 14:41:29 -0400 | [diff] [blame] | 73 | memcpy(&qp_attr.ah_attr.roce.dmac, lnk->peer_mac, |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 74 | sizeof(lnk->peer_mac)); |
| 75 | qp_attr.dest_qp_num = lnk->peer_qpn; |
| 76 | qp_attr.rq_psn = lnk->peer_psn; /* starting receive packet seq # */ |
| 77 | qp_attr.max_dest_rd_atomic = 1; /* max # of resources for incoming |
| 78 | * requests |
| 79 | */ |
| 80 | qp_attr.min_rnr_timer = SMC_QP_MIN_RNR_TIMER; |
| 81 | |
| 82 | return ib_modify_qp(lnk->roce_qp, &qp_attr, qp_attr_mask); |
| 83 | } |
| 84 | |
| 85 | int smc_ib_modify_qp_rts(struct smc_link *lnk) |
| 86 | { |
| 87 | struct ib_qp_attr qp_attr; |
| 88 | |
| 89 | memset(&qp_attr, 0, sizeof(qp_attr)); |
| 90 | qp_attr.qp_state = IB_QPS_RTS; |
| 91 | qp_attr.timeout = SMC_QP_TIMEOUT; /* local ack timeout */ |
| 92 | qp_attr.retry_cnt = SMC_QP_RETRY_CNT; /* retry count */ |
| 93 | qp_attr.rnr_retry = SMC_QP_RNR_RETRY; /* RNR retries, 7=infinite */ |
| 94 | qp_attr.sq_psn = lnk->psn_initial; /* starting send packet seq # */ |
| 95 | qp_attr.max_rd_atomic = 1; /* # of outstanding RDMA reads and |
| 96 | * atomic ops allowed |
| 97 | */ |
| 98 | return ib_modify_qp(lnk->roce_qp, &qp_attr, |
| 99 | IB_QP_STATE | IB_QP_TIMEOUT | IB_QP_RETRY_CNT | |
| 100 | IB_QP_SQ_PSN | IB_QP_RNR_RETRY | |
| 101 | IB_QP_MAX_QP_RD_ATOMIC); |
| 102 | } |
| 103 | |
| 104 | int smc_ib_modify_qp_reset(struct smc_link *lnk) |
| 105 | { |
| 106 | struct ib_qp_attr qp_attr; |
| 107 | |
| 108 | memset(&qp_attr, 0, sizeof(qp_attr)); |
| 109 | qp_attr.qp_state = IB_QPS_RESET; |
| 110 | return ib_modify_qp(lnk->roce_qp, &qp_attr, IB_QP_STATE); |
| 111 | } |
| 112 | |
| 113 | int smc_ib_ready_link(struct smc_link *lnk) |
| 114 | { |
Stefan Raspl | 00e5fb2 | 2018-07-23 13:53:10 +0200 | [diff] [blame] | 115 | struct smc_link_group *lgr = smc_get_lgr(lnk); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 116 | int rc = 0; |
| 117 | |
| 118 | rc = smc_ib_modify_qp_init(lnk); |
| 119 | if (rc) |
| 120 | goto out; |
| 121 | |
| 122 | rc = smc_ib_modify_qp_rtr(lnk); |
| 123 | if (rc) |
| 124 | goto out; |
| 125 | smc_wr_remember_qp_attr(lnk); |
| 126 | rc = ib_req_notify_cq(lnk->smcibdev->roce_cq_recv, |
| 127 | IB_CQ_SOLICITED_MASK); |
| 128 | if (rc) |
| 129 | goto out; |
| 130 | rc = smc_wr_rx_post_init(lnk); |
| 131 | if (rc) |
| 132 | goto out; |
| 133 | smc_wr_remember_qp_attr(lnk); |
| 134 | |
| 135 | if (lgr->role == SMC_SERV) { |
| 136 | rc = smc_ib_modify_qp_rts(lnk); |
| 137 | if (rc) |
| 138 | goto out; |
| 139 | smc_wr_remember_qp_attr(lnk); |
| 140 | } |
| 141 | out: |
| 142 | return rc; |
| 143 | } |
| 144 | |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 145 | static int smc_ib_fill_mac(struct smc_ib_device *smcibdev, u8 ibport) |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 146 | { |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 147 | const struct ib_gid_attr *attr; |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 148 | int rc; |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 149 | |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 150 | attr = rdma_get_gid_attr(smcibdev->ibdev, ibport, 0); |
| 151 | if (IS_ERR(attr)) |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 152 | return -ENODEV; |
| 153 | |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 154 | rc = rdma_read_gid_l2_fields(attr, NULL, smcibdev->mac[ibport - 1]); |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 155 | rdma_put_gid_attr(attr); |
| 156 | return rc; |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* Create an identifier unique for this instance of SMC-R. |
| 160 | * The MAC-address of the first active registered IB device |
| 161 | * plus a random 2-byte number is used to create this identifier. |
| 162 | * This name is delivered to the peer during connection initialization. |
| 163 | */ |
| 164 | static inline void smc_ib_define_local_systemid(struct smc_ib_device *smcibdev, |
| 165 | u8 ibport) |
| 166 | { |
| 167 | memcpy(&local_systemid[2], &smcibdev->mac[ibport - 1], |
| 168 | sizeof(smcibdev->mac[ibport - 1])); |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Hans Wippel | a082ec8 | 2020-02-25 22:41:22 +0100 | [diff] [blame] | 171 | bool smc_ib_is_valid_local_systemid(void) |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 172 | { |
| 173 | return !is_zero_ether_addr(&local_systemid[2]); |
| 174 | } |
| 175 | |
| 176 | static void smc_ib_init_local_systemid(void) |
| 177 | { |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 178 | get_random_bytes(&local_systemid[0], 2); |
| 179 | } |
| 180 | |
| 181 | bool smc_ib_port_active(struct smc_ib_device *smcibdev, u8 ibport) |
| 182 | { |
| 183 | return smcibdev->pattr[ibport - 1].state == IB_PORT_ACTIVE; |
| 184 | } |
| 185 | |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 186 | /* determine the gid for an ib-device port and vlan id */ |
| 187 | int smc_ib_determine_gid(struct smc_ib_device *smcibdev, u8 ibport, |
| 188 | unsigned short vlan_id, u8 gid[], u8 *sgid_index) |
| 189 | { |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 190 | const struct ib_gid_attr *attr; |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 191 | const struct net_device *ndev; |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 192 | int i; |
| 193 | |
| 194 | for (i = 0; i < smcibdev->pattr[ibport - 1].gid_tbl_len; i++) { |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 195 | attr = rdma_get_gid_attr(smcibdev->ibdev, ibport, i); |
| 196 | if (IS_ERR(attr)) |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 197 | continue; |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 198 | |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 199 | rcu_read_lock(); |
| 200 | ndev = rdma_read_gid_attr_ndev_rcu(attr); |
| 201 | if (!IS_ERR(ndev) && |
Karsten Graul | 41a0be3 | 2020-11-18 22:40:38 +0100 | [diff] [blame] | 202 | ((!vlan_id && !is_vlan_dev(ndev)) || |
| 203 | (vlan_id && is_vlan_dev(ndev) && |
| 204 | vlan_dev_vlan_id(ndev) == vlan_id)) && |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 205 | attr->gid_type == IB_GID_TYPE_ROCE) { |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 206 | rcu_read_unlock(); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 207 | if (gid) |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 208 | memcpy(gid, &attr->gid, SMC_GID_SIZE); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 209 | if (sgid_index) |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 210 | *sgid_index = attr->index; |
| 211 | rdma_put_gid_attr(attr); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 212 | return 0; |
| 213 | } |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 214 | rcu_read_unlock(); |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 215 | rdma_put_gid_attr(attr); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 216 | } |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 220 | static int smc_ib_remember_port_attr(struct smc_ib_device *smcibdev, u8 ibport) |
| 221 | { |
| 222 | int rc; |
| 223 | |
| 224 | memset(&smcibdev->pattr[ibport - 1], 0, |
| 225 | sizeof(smcibdev->pattr[ibport - 1])); |
| 226 | rc = ib_query_port(smcibdev->ibdev, ibport, |
| 227 | &smcibdev->pattr[ibport - 1]); |
| 228 | if (rc) |
| 229 | goto out; |
| 230 | /* the SMC protocol requires specification of the RoCE MAC address */ |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 231 | rc = smc_ib_fill_mac(smcibdev, ibport); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 232 | if (rc) |
| 233 | goto out; |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 234 | if (!smc_ib_is_valid_local_systemid() && |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 235 | smc_ib_port_active(smcibdev, ibport)) |
| 236 | /* create unique system identifier */ |
| 237 | smc_ib_define_local_systemid(smcibdev, ibport); |
| 238 | out: |
| 239 | return rc; |
| 240 | } |
| 241 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 242 | /* process context wrapper for might_sleep smc_ib_remember_port_attr */ |
| 243 | static void smc_ib_port_event_work(struct work_struct *work) |
| 244 | { |
| 245 | struct smc_ib_device *smcibdev = container_of( |
| 246 | work, struct smc_ib_device, port_event_work); |
| 247 | u8 port_idx; |
| 248 | |
| 249 | for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) { |
| 250 | smc_ib_remember_port_attr(smcibdev, port_idx + 1); |
| 251 | clear_bit(port_idx, &smcibdev->port_event_mask); |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 252 | if (!smc_ib_port_active(smcibdev, port_idx + 1)) { |
| 253 | set_bit(port_idx, smcibdev->ports_going_away); |
Karsten Graul | 541afa1 | 2020-05-01 12:48:08 +0200 | [diff] [blame] | 254 | smcr_port_err(smcibdev, port_idx + 1); |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 255 | } else { |
| 256 | clear_bit(port_idx, smcibdev->ports_going_away); |
Karsten Graul | 1f90a05 | 2020-05-01 12:48:07 +0200 | [diff] [blame] | 257 | smcr_port_add(smcibdev, port_idx + 1); |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 258 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | /* can be called in IRQ context */ |
| 263 | static void smc_ib_global_event_handler(struct ib_event_handler *handler, |
| 264 | struct ib_event *ibevent) |
| 265 | { |
| 266 | struct smc_ib_device *smcibdev; |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 267 | bool schedule = false; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 268 | u8 port_idx; |
| 269 | |
| 270 | smcibdev = container_of(handler, struct smc_ib_device, event_handler); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 271 | |
| 272 | switch (ibevent->event) { |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 273 | case IB_EVENT_DEVICE_FATAL: |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 274 | /* terminate all ports on device */ |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 275 | for (port_idx = 0; port_idx < SMC_MAX_PORTS; port_idx++) { |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 276 | set_bit(port_idx, &smcibdev->port_event_mask); |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 277 | if (!test_and_set_bit(port_idx, |
| 278 | smcibdev->ports_going_away)) |
| 279 | schedule = true; |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 280 | } |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 281 | if (schedule) |
| 282 | schedule_work(&smcibdev->port_event_work); |
| 283 | break; |
| 284 | case IB_EVENT_PORT_ACTIVE: |
| 285 | port_idx = ibevent->element.port_num - 1; |
| 286 | if (port_idx >= SMC_MAX_PORTS) |
| 287 | break; |
| 288 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 289 | if (test_and_clear_bit(port_idx, smcibdev->ports_going_away)) |
| 290 | schedule_work(&smcibdev->port_event_work); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 291 | break; |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 292 | case IB_EVENT_PORT_ERR: |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 293 | port_idx = ibevent->element.port_num - 1; |
| 294 | if (port_idx >= SMC_MAX_PORTS) |
| 295 | break; |
| 296 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 297 | if (!test_and_set_bit(port_idx, smcibdev->ports_going_away)) |
| 298 | schedule_work(&smcibdev->port_event_work); |
| 299 | break; |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 300 | case IB_EVENT_GID_CHANGE: |
| 301 | port_idx = ibevent->element.port_num - 1; |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 302 | if (port_idx >= SMC_MAX_PORTS) |
| 303 | break; |
| 304 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 305 | schedule_work(&smcibdev->port_event_work); |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 306 | break; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 307 | default: |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 312 | void smc_ib_dealloc_protection_domain(struct smc_link *lnk) |
| 313 | { |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 314 | if (lnk->roce_pd) |
| 315 | ib_dealloc_pd(lnk->roce_pd); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 316 | lnk->roce_pd = NULL; |
| 317 | } |
| 318 | |
| 319 | int smc_ib_create_protection_domain(struct smc_link *lnk) |
| 320 | { |
| 321 | int rc; |
| 322 | |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 323 | lnk->roce_pd = ib_alloc_pd(lnk->smcibdev->ibdev, 0); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 324 | rc = PTR_ERR_OR_ZERO(lnk->roce_pd); |
| 325 | if (IS_ERR(lnk->roce_pd)) |
| 326 | lnk->roce_pd = NULL; |
| 327 | return rc; |
| 328 | } |
| 329 | |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 330 | static bool smcr_diag_is_dev_critical(struct smc_lgr_list *smc_lgr, |
| 331 | struct smc_ib_device *smcibdev) |
| 332 | { |
| 333 | struct smc_link_group *lgr; |
| 334 | bool rc = false; |
| 335 | int i; |
| 336 | |
| 337 | spin_lock_bh(&smc_lgr->lock); |
| 338 | list_for_each_entry(lgr, &smc_lgr->list, list) { |
| 339 | if (lgr->is_smcd) |
| 340 | continue; |
| 341 | for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 342 | if (lgr->lnk[i].state == SMC_LNK_UNUSED || |
| 343 | lgr->lnk[i].smcibdev != smcibdev) |
| 344 | continue; |
| 345 | if (lgr->type == SMC_LGR_SINGLE || |
| 346 | lgr->type == SMC_LGR_ASYMMETRIC_LOCAL) { |
| 347 | rc = true; |
| 348 | goto out; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | out: |
| 353 | spin_unlock_bh(&smc_lgr->lock); |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | static int smc_nl_handle_dev_port(struct sk_buff *skb, |
| 358 | struct ib_device *ibdev, |
| 359 | struct smc_ib_device *smcibdev, |
| 360 | int port) |
| 361 | { |
| 362 | char smc_pnet[SMC_MAX_PNETID_LEN + 1]; |
| 363 | struct nlattr *port_attrs; |
| 364 | unsigned char port_state; |
| 365 | int lnk_count = 0; |
| 366 | |
| 367 | port_attrs = nla_nest_start(skb, SMC_NLA_DEV_PORT + port); |
| 368 | if (!port_attrs) |
| 369 | goto errout; |
| 370 | |
| 371 | if (nla_put_u8(skb, SMC_NLA_DEV_PORT_PNET_USR, |
| 372 | smcibdev->pnetid_by_user[port])) |
| 373 | goto errattr; |
Guvenc Gulce | 8a44653 | 2021-01-12 17:21:22 +0100 | [diff] [blame] | 374 | memcpy(smc_pnet, &smcibdev->pnetid[port], SMC_MAX_PNETID_LEN); |
| 375 | smc_pnet[SMC_MAX_PNETID_LEN] = 0; |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 376 | if (nla_put_string(skb, SMC_NLA_DEV_PORT_PNETID, smc_pnet)) |
| 377 | goto errattr; |
| 378 | if (nla_put_u32(skb, SMC_NLA_DEV_PORT_NETDEV, |
| 379 | smcibdev->ndev_ifidx[port])) |
| 380 | goto errattr; |
| 381 | if (nla_put_u8(skb, SMC_NLA_DEV_PORT_VALID, 1)) |
| 382 | goto errattr; |
| 383 | port_state = smc_ib_port_active(smcibdev, port + 1); |
| 384 | if (nla_put_u8(skb, SMC_NLA_DEV_PORT_STATE, port_state)) |
| 385 | goto errattr; |
| 386 | lnk_count = atomic_read(&smcibdev->lnk_cnt_by_port[port]); |
| 387 | if (nla_put_u32(skb, SMC_NLA_DEV_PORT_LNK_CNT, lnk_count)) |
| 388 | goto errattr; |
| 389 | nla_nest_end(skb, port_attrs); |
| 390 | return 0; |
| 391 | errattr: |
| 392 | nla_nest_cancel(skb, port_attrs); |
| 393 | errout: |
| 394 | return -EMSGSIZE; |
| 395 | } |
| 396 | |
Karsten Graul | 995433b | 2020-12-15 10:10:58 +0100 | [diff] [blame] | 397 | static bool smc_nl_handle_pci_values(const struct smc_pci_dev *smc_pci_dev, |
| 398 | struct sk_buff *skb) |
| 399 | { |
| 400 | if (nla_put_u32(skb, SMC_NLA_DEV_PCI_FID, smc_pci_dev->pci_fid)) |
| 401 | return false; |
| 402 | if (nla_put_u16(skb, SMC_NLA_DEV_PCI_CHID, smc_pci_dev->pci_pchid)) |
| 403 | return false; |
| 404 | if (nla_put_u16(skb, SMC_NLA_DEV_PCI_VENDOR, smc_pci_dev->pci_vendor)) |
| 405 | return false; |
| 406 | if (nla_put_u16(skb, SMC_NLA_DEV_PCI_DEVICE, smc_pci_dev->pci_device)) |
| 407 | return false; |
| 408 | if (nla_put_string(skb, SMC_NLA_DEV_PCI_ID, smc_pci_dev->pci_id)) |
| 409 | return false; |
| 410 | return true; |
| 411 | } |
| 412 | |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 413 | static int smc_nl_handle_smcr_dev(struct smc_ib_device *smcibdev, |
| 414 | struct sk_buff *skb, |
| 415 | struct netlink_callback *cb) |
| 416 | { |
Guvenc Gulce | 8a44653 | 2021-01-12 17:21:22 +0100 | [diff] [blame] | 417 | char smc_ibname[IB_DEVICE_NAME_MAX]; |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 418 | struct smc_pci_dev smc_pci_dev; |
| 419 | struct pci_dev *pci_dev; |
| 420 | unsigned char is_crit; |
| 421 | struct nlattr *attrs; |
| 422 | void *nlh; |
| 423 | int i; |
| 424 | |
| 425 | nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 426 | &smc_gen_nl_family, NLM_F_MULTI, |
| 427 | SMC_NETLINK_GET_DEV_SMCR); |
| 428 | if (!nlh) |
| 429 | goto errmsg; |
| 430 | attrs = nla_nest_start(skb, SMC_GEN_DEV_SMCR); |
| 431 | if (!attrs) |
| 432 | goto errout; |
| 433 | is_crit = smcr_diag_is_dev_critical(&smc_lgr_list, smcibdev); |
| 434 | if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRIT, is_crit)) |
| 435 | goto errattr; |
Karsten Graul | 995433b | 2020-12-15 10:10:58 +0100 | [diff] [blame] | 436 | if (smcibdev->ibdev->dev.parent) { |
| 437 | memset(&smc_pci_dev, 0, sizeof(smc_pci_dev)); |
| 438 | pci_dev = to_pci_dev(smcibdev->ibdev->dev.parent); |
| 439 | smc_set_pci_values(pci_dev, &smc_pci_dev); |
| 440 | if (!smc_nl_handle_pci_values(&smc_pci_dev, skb)) |
| 441 | goto errattr; |
| 442 | } |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 443 | snprintf(smc_ibname, sizeof(smc_ibname), "%s", smcibdev->ibdev->name); |
| 444 | if (nla_put_string(skb, SMC_NLA_DEV_IB_NAME, smc_ibname)) |
| 445 | goto errattr; |
| 446 | for (i = 1; i <= SMC_MAX_PORTS; i++) { |
| 447 | if (!rdma_is_port_valid(smcibdev->ibdev, i)) |
| 448 | continue; |
| 449 | if (smc_nl_handle_dev_port(skb, smcibdev->ibdev, |
| 450 | smcibdev, i - 1)) |
| 451 | goto errattr; |
| 452 | } |
| 453 | |
| 454 | nla_nest_end(skb, attrs); |
| 455 | genlmsg_end(skb, nlh); |
| 456 | return 0; |
| 457 | |
| 458 | errattr: |
| 459 | nla_nest_cancel(skb, attrs); |
| 460 | errout: |
| 461 | genlmsg_cancel(skb, nlh); |
| 462 | errmsg: |
| 463 | return -EMSGSIZE; |
| 464 | } |
| 465 | |
| 466 | static void smc_nl_prep_smcr_dev(struct smc_ib_devices *dev_list, |
| 467 | struct sk_buff *skb, |
| 468 | struct netlink_callback *cb) |
| 469 | { |
| 470 | struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); |
| 471 | struct smc_ib_device *smcibdev; |
| 472 | int snum = cb_ctx->pos[0]; |
| 473 | int num = 0; |
| 474 | |
| 475 | mutex_lock(&dev_list->mutex); |
| 476 | list_for_each_entry(smcibdev, &dev_list->list, list) { |
| 477 | if (num < snum) |
| 478 | goto next; |
| 479 | if (smc_nl_handle_smcr_dev(smcibdev, skb, cb)) |
| 480 | goto errout; |
| 481 | next: |
| 482 | num++; |
| 483 | } |
| 484 | errout: |
| 485 | mutex_unlock(&dev_list->mutex); |
| 486 | cb_ctx->pos[0] = num; |
| 487 | } |
| 488 | |
| 489 | int smcr_nl_get_device(struct sk_buff *skb, struct netlink_callback *cb) |
| 490 | { |
| 491 | smc_nl_prep_smcr_dev(&smc_ib_devices, skb, cb); |
| 492 | return skb->len; |
| 493 | } |
| 494 | |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 495 | static void smc_ib_qp_event_handler(struct ib_event *ibevent, void *priv) |
| 496 | { |
Karsten Graul | e5f3aa0 | 2019-01-30 18:51:07 +0100 | [diff] [blame] | 497 | struct smc_link *lnk = (struct smc_link *)priv; |
| 498 | struct smc_ib_device *smcibdev = lnk->smcibdev; |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 499 | u8 port_idx; |
| 500 | |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 501 | switch (ibevent->event) { |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 502 | case IB_EVENT_QP_FATAL: |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 503 | case IB_EVENT_QP_ACCESS_ERR: |
Karsten Graul | e5f3aa0 | 2019-01-30 18:51:07 +0100 | [diff] [blame] | 504 | port_idx = ibevent->element.qp->port - 1; |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 505 | if (port_idx >= SMC_MAX_PORTS) |
| 506 | break; |
| 507 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 508 | if (!test_and_set_bit(port_idx, smcibdev->ports_going_away)) |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 509 | schedule_work(&smcibdev->port_event_work); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 510 | break; |
| 511 | default: |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void smc_ib_destroy_queue_pair(struct smc_link *lnk) |
| 517 | { |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 518 | if (lnk->roce_qp) |
| 519 | ib_destroy_qp(lnk->roce_qp); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 520 | lnk->roce_qp = NULL; |
| 521 | } |
| 522 | |
| 523 | /* create a queue pair within the protection domain for a link */ |
| 524 | int smc_ib_create_queue_pair(struct smc_link *lnk) |
| 525 | { |
| 526 | struct ib_qp_init_attr qp_attr = { |
| 527 | .event_handler = smc_ib_qp_event_handler, |
| 528 | .qp_context = lnk, |
| 529 | .send_cq = lnk->smcibdev->roce_cq_send, |
| 530 | .recv_cq = lnk->smcibdev->roce_cq_recv, |
| 531 | .srq = NULL, |
| 532 | .cap = { |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 533 | /* include unsolicited rdma_writes as well, |
| 534 | * there are max. 2 RDMA_WRITE per 1 WR_SEND |
| 535 | */ |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 536 | .max_send_wr = SMC_WR_BUF_CNT * 3, |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 537 | .max_recv_wr = SMC_WR_BUF_CNT * 3, |
| 538 | .max_send_sge = SMC_IB_MAX_SEND_SGE, |
| 539 | .max_recv_sge = 1, |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 540 | }, |
| 541 | .sq_sig_type = IB_SIGNAL_REQ_WR, |
| 542 | .qp_type = IB_QPT_RC, |
| 543 | }; |
| 544 | int rc; |
| 545 | |
| 546 | lnk->roce_qp = ib_create_qp(lnk->roce_pd, &qp_attr); |
| 547 | rc = PTR_ERR_OR_ZERO(lnk->roce_qp); |
| 548 | if (IS_ERR(lnk->roce_qp)) |
| 549 | lnk->roce_qp = NULL; |
| 550 | else |
| 551 | smc_wr_remember_qp_attr(lnk); |
| 552 | return rc; |
| 553 | } |
| 554 | |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 555 | void smc_ib_put_memory_region(struct ib_mr *mr) |
| 556 | { |
| 557 | ib_dereg_mr(mr); |
| 558 | } |
| 559 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 560 | static int smc_ib_map_mr_sg(struct smc_buf_desc *buf_slot, u8 link_idx) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 561 | { |
| 562 | unsigned int offset = 0; |
| 563 | int sg_num; |
| 564 | |
| 565 | /* map the largest prefix of a dma mapped SG list */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 566 | sg_num = ib_map_mr_sg(buf_slot->mr_rx[link_idx], |
| 567 | buf_slot->sgt[link_idx].sgl, |
| 568 | buf_slot->sgt[link_idx].orig_nents, |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 569 | &offset, PAGE_SIZE); |
| 570 | |
| 571 | return sg_num; |
| 572 | } |
| 573 | |
| 574 | /* Allocate a memory region and map the dma mapped SG list of buf_slot */ |
| 575 | int smc_ib_get_memory_region(struct ib_pd *pd, int access_flags, |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 576 | struct smc_buf_desc *buf_slot, u8 link_idx) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 577 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 578 | if (buf_slot->mr_rx[link_idx]) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 579 | return 0; /* already done */ |
| 580 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 581 | buf_slot->mr_rx[link_idx] = |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 582 | ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, 1 << buf_slot->order); |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 583 | if (IS_ERR(buf_slot->mr_rx[link_idx])) { |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 584 | int rc; |
| 585 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 586 | rc = PTR_ERR(buf_slot->mr_rx[link_idx]); |
| 587 | buf_slot->mr_rx[link_idx] = NULL; |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 588 | return rc; |
| 589 | } |
| 590 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 591 | if (smc_ib_map_mr_sg(buf_slot, link_idx) != 1) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 592 | return -EINVAL; |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 597 | /* synchronize buffer usage for cpu access */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 598 | void smc_ib_sync_sg_for_cpu(struct smc_link *lnk, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 599 | struct smc_buf_desc *buf_slot, |
| 600 | enum dma_data_direction data_direction) |
| 601 | { |
| 602 | struct scatterlist *sg; |
| 603 | unsigned int i; |
| 604 | |
| 605 | /* for now there is just one DMA address */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 606 | for_each_sg(buf_slot->sgt[lnk->link_idx].sgl, sg, |
| 607 | buf_slot->sgt[lnk->link_idx].nents, i) { |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 608 | if (!sg_dma_len(sg)) |
| 609 | break; |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 610 | ib_dma_sync_single_for_cpu(lnk->smcibdev->ibdev, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 611 | sg_dma_address(sg), |
| 612 | sg_dma_len(sg), |
| 613 | data_direction); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* synchronize buffer usage for device access */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 618 | void smc_ib_sync_sg_for_device(struct smc_link *lnk, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 619 | struct smc_buf_desc *buf_slot, |
| 620 | enum dma_data_direction data_direction) |
| 621 | { |
| 622 | struct scatterlist *sg; |
| 623 | unsigned int i; |
| 624 | |
| 625 | /* for now there is just one DMA address */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 626 | for_each_sg(buf_slot->sgt[lnk->link_idx].sgl, sg, |
| 627 | buf_slot->sgt[lnk->link_idx].nents, i) { |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 628 | if (!sg_dma_len(sg)) |
| 629 | break; |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 630 | ib_dma_sync_single_for_device(lnk->smcibdev->ibdev, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 631 | sg_dma_address(sg), |
| 632 | sg_dma_len(sg), |
| 633 | data_direction); |
| 634 | } |
| 635 | } |
| 636 | |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 637 | /* Map a new TX or RX buffer SG-table to DMA */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 638 | int smc_ib_buf_map_sg(struct smc_link *lnk, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 639 | struct smc_buf_desc *buf_slot, |
| 640 | enum dma_data_direction data_direction) |
| 641 | { |
| 642 | int mapped_nents; |
| 643 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 644 | mapped_nents = ib_dma_map_sg(lnk->smcibdev->ibdev, |
| 645 | buf_slot->sgt[lnk->link_idx].sgl, |
| 646 | buf_slot->sgt[lnk->link_idx].orig_nents, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 647 | data_direction); |
| 648 | if (!mapped_nents) |
| 649 | return -ENOMEM; |
| 650 | |
| 651 | return mapped_nents; |
| 652 | } |
| 653 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 654 | void smc_ib_buf_unmap_sg(struct smc_link *lnk, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 655 | struct smc_buf_desc *buf_slot, |
| 656 | enum dma_data_direction data_direction) |
| 657 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 658 | if (!buf_slot->sgt[lnk->link_idx].sgl->dma_address) |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 659 | return; /* already unmapped */ |
| 660 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 661 | ib_dma_unmap_sg(lnk->smcibdev->ibdev, |
| 662 | buf_slot->sgt[lnk->link_idx].sgl, |
| 663 | buf_slot->sgt[lnk->link_idx].orig_nents, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 664 | data_direction); |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 665 | buf_slot->sgt[lnk->link_idx].sgl->dma_address = 0; |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 668 | long smc_ib_setup_per_ibdev(struct smc_ib_device *smcibdev) |
| 669 | { |
| 670 | struct ib_cq_init_attr cqattr = { |
Ursula Braun | c9f4c6c | 2018-03-14 11:01:00 +0100 | [diff] [blame] | 671 | .cqe = SMC_MAX_CQE, .comp_vector = 0 }; |
| 672 | int cqe_size_order, smc_order; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 673 | long rc; |
| 674 | |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 675 | mutex_lock(&smcibdev->mutex); |
| 676 | rc = 0; |
| 677 | if (smcibdev->initialized) |
| 678 | goto out; |
Ursula Braun | c9f4c6c | 2018-03-14 11:01:00 +0100 | [diff] [blame] | 679 | /* the calculated number of cq entries fits to mlx5 cq allocation */ |
| 680 | cqe_size_order = cache_line_size() == 128 ? 7 : 6; |
| 681 | smc_order = MAX_ORDER - cqe_size_order - 1; |
| 682 | if (SMC_MAX_CQE + 2 > (0x00000001 << smc_order) * PAGE_SIZE) |
| 683 | cqattr.cqe = (0x00000001 << smc_order) * PAGE_SIZE - 2; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 684 | smcibdev->roce_cq_send = ib_create_cq(smcibdev->ibdev, |
| 685 | smc_wr_tx_cq_handler, NULL, |
| 686 | smcibdev, &cqattr); |
| 687 | rc = PTR_ERR_OR_ZERO(smcibdev->roce_cq_send); |
| 688 | if (IS_ERR(smcibdev->roce_cq_send)) { |
| 689 | smcibdev->roce_cq_send = NULL; |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 690 | goto out; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 691 | } |
| 692 | smcibdev->roce_cq_recv = ib_create_cq(smcibdev->ibdev, |
| 693 | smc_wr_rx_cq_handler, NULL, |
| 694 | smcibdev, &cqattr); |
| 695 | rc = PTR_ERR_OR_ZERO(smcibdev->roce_cq_recv); |
| 696 | if (IS_ERR(smcibdev->roce_cq_recv)) { |
| 697 | smcibdev->roce_cq_recv = NULL; |
| 698 | goto err; |
| 699 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 700 | smc_wr_add_dev(smcibdev); |
| 701 | smcibdev->initialized = 1; |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 702 | goto out; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 703 | |
| 704 | err: |
| 705 | ib_destroy_cq(smcibdev->roce_cq_send); |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 706 | out: |
| 707 | mutex_unlock(&smcibdev->mutex); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 708 | return rc; |
| 709 | } |
| 710 | |
| 711 | static void smc_ib_cleanup_per_ibdev(struct smc_ib_device *smcibdev) |
| 712 | { |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 713 | mutex_lock(&smcibdev->mutex); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 714 | if (!smcibdev->initialized) |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 715 | goto out; |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 716 | smcibdev->initialized = 0; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 717 | ib_destroy_cq(smcibdev->roce_cq_recv); |
| 718 | ib_destroy_cq(smcibdev->roce_cq_send); |
Ursula Braun | 6a37ad3 | 2019-11-14 13:02:46 +0100 | [diff] [blame] | 719 | smc_wr_remove_dev(smcibdev); |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 720 | out: |
| 721 | mutex_unlock(&smcibdev->mutex); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 724 | static struct ib_client smc_ib_client; |
| 725 | |
Guvenc Gulce | 3d453f5 | 2020-12-01 20:20:40 +0100 | [diff] [blame] | 726 | static void smc_copy_netdev_ifindex(struct smc_ib_device *smcibdev, int port) |
| 727 | { |
| 728 | struct ib_device *ibdev = smcibdev->ibdev; |
| 729 | struct net_device *ndev; |
| 730 | |
| 731 | if (!ibdev->ops.get_netdev) |
| 732 | return; |
| 733 | ndev = ibdev->ops.get_netdev(ibdev, port + 1); |
| 734 | if (ndev) { |
| 735 | smcibdev->ndev_ifidx[port] = ndev->ifindex; |
| 736 | dev_put(ndev); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void smc_ib_ndev_change(struct net_device *ndev, unsigned long event) |
| 741 | { |
| 742 | struct smc_ib_device *smcibdev; |
| 743 | struct ib_device *libdev; |
| 744 | struct net_device *lndev; |
| 745 | u8 port_cnt; |
| 746 | int i; |
| 747 | |
| 748 | mutex_lock(&smc_ib_devices.mutex); |
| 749 | list_for_each_entry(smcibdev, &smc_ib_devices.list, list) { |
| 750 | port_cnt = smcibdev->ibdev->phys_port_cnt; |
| 751 | for (i = 0; i < min_t(size_t, port_cnt, SMC_MAX_PORTS); i++) { |
| 752 | libdev = smcibdev->ibdev; |
| 753 | if (!libdev->ops.get_netdev) |
| 754 | continue; |
| 755 | lndev = libdev->ops.get_netdev(libdev, i + 1); |
| 756 | if (lndev) |
| 757 | dev_put(lndev); |
| 758 | if (lndev != ndev) |
| 759 | continue; |
| 760 | if (event == NETDEV_REGISTER) |
| 761 | smcibdev->ndev_ifidx[i] = ndev->ifindex; |
| 762 | if (event == NETDEV_UNREGISTER) |
| 763 | smcibdev->ndev_ifidx[i] = 0; |
| 764 | } |
| 765 | } |
| 766 | mutex_unlock(&smc_ib_devices.mutex); |
| 767 | } |
| 768 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 769 | /* callback function for ib_register_client() */ |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 770 | static int smc_ib_add_dev(struct ib_device *ibdev) |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 771 | { |
| 772 | struct smc_ib_device *smcibdev; |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 773 | u8 port_cnt; |
| 774 | int i; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 775 | |
| 776 | if (ibdev->node_type != RDMA_NODE_IB_CA) |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 777 | return -EOPNOTSUPP; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 778 | |
| 779 | smcibdev = kzalloc(sizeof(*smcibdev), GFP_KERNEL); |
| 780 | if (!smcibdev) |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 781 | return -ENOMEM; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 782 | |
| 783 | smcibdev->ibdev = ibdev; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 784 | INIT_WORK(&smcibdev->port_event_work, smc_ib_port_event_work); |
Ursula Braun | 6dabd40 | 2019-11-16 17:47:29 +0100 | [diff] [blame] | 785 | atomic_set(&smcibdev->lnk_cnt, 0); |
| 786 | init_waitqueue_head(&smcibdev->lnks_deleted); |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 787 | mutex_init(&smcibdev->mutex); |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 788 | mutex_lock(&smc_ib_devices.mutex); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 789 | list_add_tail(&smcibdev->list, &smc_ib_devices.list); |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 790 | mutex_unlock(&smc_ib_devices.mutex); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 791 | ib_set_client_data(ibdev, &smc_ib_client, smcibdev); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 792 | INIT_IB_EVENT_HANDLER(&smcibdev->event_handler, smcibdev->ibdev, |
| 793 | smc_ib_global_event_handler); |
| 794 | ib_register_event_handler(&smcibdev->event_handler); |
| 795 | |
| 796 | /* trigger reading of the port attributes */ |
| 797 | port_cnt = smcibdev->ibdev->phys_port_cnt; |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 798 | pr_warn_ratelimited("smc: adding ib device %s with port count %d\n", |
| 799 | smcibdev->ibdev->name, port_cnt); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 800 | for (i = 0; |
| 801 | i < min_t(size_t, port_cnt, SMC_MAX_PORTS); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 802 | i++) { |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 803 | set_bit(i, &smcibdev->port_event_mask); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 804 | /* determine pnetids of the port */ |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 805 | if (smc_pnetid_by_dev_port(ibdev->dev.parent, i, |
| 806 | smcibdev->pnetid[i])) |
| 807 | smc_pnetid_by_table_ib(smcibdev, i + 1); |
Guvenc Gulce | 3d453f5 | 2020-12-01 20:20:40 +0100 | [diff] [blame] | 808 | smc_copy_netdev_ifindex(smcibdev, i); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 809 | pr_warn_ratelimited("smc: ib device %s port %d has pnetid " |
| 810 | "%.16s%s\n", |
| 811 | smcibdev->ibdev->name, i + 1, |
| 812 | smcibdev->pnetid[i], |
| 813 | smcibdev->pnetid_by_user[i] ? |
| 814 | " (user defined)" : |
| 815 | ""); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 816 | } |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 817 | schedule_work(&smcibdev->port_event_work); |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 818 | return 0; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Ursula Braun | 0b29ec6 | 2019-11-14 13:02:47 +0100 | [diff] [blame] | 821 | /* callback function for ib_unregister_client() */ |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 822 | static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data) |
| 823 | { |
Jason Gunthorpe | 1587982 | 2020-04-07 20:20:09 -0300 | [diff] [blame] | 824 | struct smc_ib_device *smcibdev = client_data; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 825 | |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 826 | mutex_lock(&smc_ib_devices.mutex); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 827 | list_del_init(&smcibdev->list); /* remove from smc_ib_devices */ |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 828 | mutex_unlock(&smc_ib_devices.mutex); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 829 | pr_warn_ratelimited("smc: removing ib device %s\n", |
| 830 | smcibdev->ibdev->name); |
Ursula Braun | 0b29ec6 | 2019-11-14 13:02:47 +0100 | [diff] [blame] | 831 | smc_smcr_terminate_all(smcibdev); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 832 | smc_ib_cleanup_per_ibdev(smcibdev); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 833 | ib_unregister_event_handler(&smcibdev->event_handler); |
Karsten Graul | ece0d7b | 2020-03-10 09:33:30 +0100 | [diff] [blame] | 834 | cancel_work_sync(&smcibdev->port_event_work); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 835 | kfree(smcibdev); |
| 836 | } |
| 837 | |
| 838 | static struct ib_client smc_ib_client = { |
| 839 | .name = "smc_ib", |
| 840 | .add = smc_ib_add_dev, |
| 841 | .remove = smc_ib_remove_dev, |
| 842 | }; |
| 843 | |
| 844 | int __init smc_ib_register_client(void) |
| 845 | { |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 846 | smc_ib_init_local_systemid(); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 847 | return ib_register_client(&smc_ib_client); |
| 848 | } |
| 849 | |
| 850 | void smc_ib_unregister_client(void) |
| 851 | { |
| 852 | ib_unregister_client(&smc_ib_client); |
| 853 | } |