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 | |
Karsten Graul | e5c4744 | 2021-10-16 11:37:45 +0200 | [diff] [blame^] | 186 | int smc_ib_find_route(__be32 saddr, __be32 daddr, |
| 187 | u8 nexthop_mac[], u8 *uses_gateway) |
| 188 | { |
| 189 | struct neighbour *neigh = NULL; |
| 190 | struct rtable *rt = NULL; |
| 191 | struct flowi4 fl4 = { |
| 192 | .saddr = saddr, |
| 193 | .daddr = daddr |
| 194 | }; |
| 195 | |
| 196 | if (daddr == cpu_to_be32(INADDR_NONE)) |
| 197 | goto out; |
| 198 | rt = ip_route_output_flow(&init_net, &fl4, NULL); |
| 199 | if (IS_ERR(rt)) |
| 200 | goto out; |
| 201 | if (rt->rt_uses_gateway && rt->rt_gw_family != AF_INET) |
| 202 | goto out; |
| 203 | neigh = rt->dst.ops->neigh_lookup(&rt->dst, NULL, &fl4.daddr); |
| 204 | if (neigh) { |
| 205 | memcpy(nexthop_mac, neigh->ha, ETH_ALEN); |
| 206 | *uses_gateway = rt->rt_uses_gateway; |
| 207 | return 0; |
| 208 | } |
| 209 | out: |
| 210 | return -ENOENT; |
| 211 | } |
| 212 | |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 213 | /* determine the gid for an ib-device port and vlan id */ |
| 214 | int smc_ib_determine_gid(struct smc_ib_device *smcibdev, u8 ibport, |
| 215 | unsigned short vlan_id, u8 gid[], u8 *sgid_index) |
| 216 | { |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 217 | const struct ib_gid_attr *attr; |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 218 | const struct net_device *ndev; |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < smcibdev->pattr[ibport - 1].gid_tbl_len; i++) { |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 222 | attr = rdma_get_gid_attr(smcibdev->ibdev, ibport, i); |
| 223 | if (IS_ERR(attr)) |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 224 | continue; |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 225 | |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 226 | rcu_read_lock(); |
| 227 | ndev = rdma_read_gid_attr_ndev_rcu(attr); |
| 228 | if (!IS_ERR(ndev) && |
Karsten Graul | 41a0be3 | 2020-11-18 22:40:38 +0100 | [diff] [blame] | 229 | ((!vlan_id && !is_vlan_dev(ndev)) || |
| 230 | (vlan_id && is_vlan_dev(ndev) && |
| 231 | vlan_dev_vlan_id(ndev) == vlan_id)) && |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 232 | attr->gid_type == IB_GID_TYPE_ROCE) { |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 233 | rcu_read_unlock(); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 234 | if (gid) |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 235 | memcpy(gid, &attr->gid, SMC_GID_SIZE); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 236 | if (sgid_index) |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 237 | *sgid_index = attr->index; |
| 238 | rdma_put_gid_attr(attr); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 239 | return 0; |
| 240 | } |
Parav Pandit | 5102eca | 2019-05-02 10:48:06 +0300 | [diff] [blame] | 241 | rcu_read_unlock(); |
Jason Gunthorpe | b4c296f | 2018-08-17 16:45:51 -0600 | [diff] [blame] | 242 | rdma_put_gid_attr(attr); |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 243 | } |
| 244 | return -ENODEV; |
| 245 | } |
| 246 | |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 247 | static int smc_ib_remember_port_attr(struct smc_ib_device *smcibdev, u8 ibport) |
| 248 | { |
| 249 | int rc; |
| 250 | |
| 251 | memset(&smcibdev->pattr[ibport - 1], 0, |
| 252 | sizeof(smcibdev->pattr[ibport - 1])); |
| 253 | rc = ib_query_port(smcibdev->ibdev, ibport, |
| 254 | &smcibdev->pattr[ibport - 1]); |
| 255 | if (rc) |
| 256 | goto out; |
| 257 | /* the SMC protocol requires specification of the RoCE MAC address */ |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 258 | rc = smc_ib_fill_mac(smcibdev, ibport); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 259 | if (rc) |
| 260 | goto out; |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 261 | if (!smc_ib_is_valid_local_systemid() && |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 262 | smc_ib_port_active(smcibdev, ibport)) |
| 263 | /* create unique system identifier */ |
| 264 | smc_ib_define_local_systemid(smcibdev, ibport); |
| 265 | out: |
| 266 | return rc; |
| 267 | } |
| 268 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 269 | /* process context wrapper for might_sleep smc_ib_remember_port_attr */ |
| 270 | static void smc_ib_port_event_work(struct work_struct *work) |
| 271 | { |
| 272 | struct smc_ib_device *smcibdev = container_of( |
| 273 | work, struct smc_ib_device, port_event_work); |
| 274 | u8 port_idx; |
| 275 | |
| 276 | for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) { |
| 277 | smc_ib_remember_port_attr(smcibdev, port_idx + 1); |
| 278 | clear_bit(port_idx, &smcibdev->port_event_mask); |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 279 | if (!smc_ib_port_active(smcibdev, port_idx + 1)) { |
| 280 | set_bit(port_idx, smcibdev->ports_going_away); |
Karsten Graul | 541afa1 | 2020-05-01 12:48:08 +0200 | [diff] [blame] | 281 | smcr_port_err(smcibdev, port_idx + 1); |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 282 | } else { |
| 283 | clear_bit(port_idx, smcibdev->ports_going_away); |
Karsten Graul | 1f90a05 | 2020-05-01 12:48:07 +0200 | [diff] [blame] | 284 | smcr_port_add(smcibdev, port_idx + 1); |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 285 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /* can be called in IRQ context */ |
| 290 | static void smc_ib_global_event_handler(struct ib_event_handler *handler, |
| 291 | struct ib_event *ibevent) |
| 292 | { |
| 293 | struct smc_ib_device *smcibdev; |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 294 | bool schedule = false; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 295 | u8 port_idx; |
| 296 | |
| 297 | smcibdev = container_of(handler, struct smc_ib_device, event_handler); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 298 | |
| 299 | switch (ibevent->event) { |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 300 | case IB_EVENT_DEVICE_FATAL: |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 301 | /* terminate all ports on device */ |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 302 | for (port_idx = 0; port_idx < SMC_MAX_PORTS; port_idx++) { |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 303 | set_bit(port_idx, &smcibdev->port_event_mask); |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 304 | if (!test_and_set_bit(port_idx, |
| 305 | smcibdev->ports_going_away)) |
| 306 | schedule = true; |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 307 | } |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 308 | if (schedule) |
| 309 | schedule_work(&smcibdev->port_event_work); |
| 310 | break; |
| 311 | case IB_EVENT_PORT_ACTIVE: |
| 312 | port_idx = ibevent->element.port_num - 1; |
| 313 | if (port_idx >= SMC_MAX_PORTS) |
| 314 | break; |
| 315 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 316 | if (test_and_clear_bit(port_idx, smcibdev->ports_going_away)) |
| 317 | schedule_work(&smcibdev->port_event_work); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 318 | break; |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 319 | case IB_EVENT_PORT_ERR: |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 320 | port_idx = ibevent->element.port_num - 1; |
| 321 | if (port_idx >= SMC_MAX_PORTS) |
| 322 | break; |
| 323 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 324 | if (!test_and_set_bit(port_idx, smcibdev->ports_going_away)) |
| 325 | schedule_work(&smcibdev->port_event_work); |
| 326 | break; |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 327 | case IB_EVENT_GID_CHANGE: |
| 328 | port_idx = ibevent->element.port_num - 1; |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 329 | if (port_idx >= SMC_MAX_PORTS) |
| 330 | break; |
| 331 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 332 | schedule_work(&smcibdev->port_event_work); |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 333 | break; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 339 | void smc_ib_dealloc_protection_domain(struct smc_link *lnk) |
| 340 | { |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 341 | if (lnk->roce_pd) |
| 342 | ib_dealloc_pd(lnk->roce_pd); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 343 | lnk->roce_pd = NULL; |
| 344 | } |
| 345 | |
| 346 | int smc_ib_create_protection_domain(struct smc_link *lnk) |
| 347 | { |
| 348 | int rc; |
| 349 | |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 350 | lnk->roce_pd = ib_alloc_pd(lnk->smcibdev->ibdev, 0); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 351 | rc = PTR_ERR_OR_ZERO(lnk->roce_pd); |
| 352 | if (IS_ERR(lnk->roce_pd)) |
| 353 | lnk->roce_pd = NULL; |
| 354 | return rc; |
| 355 | } |
| 356 | |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 357 | static bool smcr_diag_is_dev_critical(struct smc_lgr_list *smc_lgr, |
| 358 | struct smc_ib_device *smcibdev) |
| 359 | { |
| 360 | struct smc_link_group *lgr; |
| 361 | bool rc = false; |
| 362 | int i; |
| 363 | |
| 364 | spin_lock_bh(&smc_lgr->lock); |
| 365 | list_for_each_entry(lgr, &smc_lgr->list, list) { |
| 366 | if (lgr->is_smcd) |
| 367 | continue; |
| 368 | for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 369 | if (lgr->lnk[i].state == SMC_LNK_UNUSED || |
| 370 | lgr->lnk[i].smcibdev != smcibdev) |
| 371 | continue; |
| 372 | if (lgr->type == SMC_LGR_SINGLE || |
| 373 | lgr->type == SMC_LGR_ASYMMETRIC_LOCAL) { |
| 374 | rc = true; |
| 375 | goto out; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | out: |
| 380 | spin_unlock_bh(&smc_lgr->lock); |
| 381 | return rc; |
| 382 | } |
| 383 | |
| 384 | static int smc_nl_handle_dev_port(struct sk_buff *skb, |
| 385 | struct ib_device *ibdev, |
| 386 | struct smc_ib_device *smcibdev, |
| 387 | int port) |
| 388 | { |
| 389 | char smc_pnet[SMC_MAX_PNETID_LEN + 1]; |
| 390 | struct nlattr *port_attrs; |
| 391 | unsigned char port_state; |
| 392 | int lnk_count = 0; |
| 393 | |
| 394 | port_attrs = nla_nest_start(skb, SMC_NLA_DEV_PORT + port); |
| 395 | if (!port_attrs) |
| 396 | goto errout; |
| 397 | |
| 398 | if (nla_put_u8(skb, SMC_NLA_DEV_PORT_PNET_USR, |
| 399 | smcibdev->pnetid_by_user[port])) |
| 400 | goto errattr; |
Guvenc Gulce | 8a44653 | 2021-01-12 17:21:22 +0100 | [diff] [blame] | 401 | memcpy(smc_pnet, &smcibdev->pnetid[port], SMC_MAX_PNETID_LEN); |
| 402 | smc_pnet[SMC_MAX_PNETID_LEN] = 0; |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 403 | if (nla_put_string(skb, SMC_NLA_DEV_PORT_PNETID, smc_pnet)) |
| 404 | goto errattr; |
| 405 | if (nla_put_u32(skb, SMC_NLA_DEV_PORT_NETDEV, |
| 406 | smcibdev->ndev_ifidx[port])) |
| 407 | goto errattr; |
| 408 | if (nla_put_u8(skb, SMC_NLA_DEV_PORT_VALID, 1)) |
| 409 | goto errattr; |
| 410 | port_state = smc_ib_port_active(smcibdev, port + 1); |
| 411 | if (nla_put_u8(skb, SMC_NLA_DEV_PORT_STATE, port_state)) |
| 412 | goto errattr; |
| 413 | lnk_count = atomic_read(&smcibdev->lnk_cnt_by_port[port]); |
| 414 | if (nla_put_u32(skb, SMC_NLA_DEV_PORT_LNK_CNT, lnk_count)) |
| 415 | goto errattr; |
| 416 | nla_nest_end(skb, port_attrs); |
| 417 | return 0; |
| 418 | errattr: |
| 419 | nla_nest_cancel(skb, port_attrs); |
| 420 | errout: |
| 421 | return -EMSGSIZE; |
| 422 | } |
| 423 | |
Karsten Graul | 995433b | 2020-12-15 10:10:58 +0100 | [diff] [blame] | 424 | static bool smc_nl_handle_pci_values(const struct smc_pci_dev *smc_pci_dev, |
| 425 | struct sk_buff *skb) |
| 426 | { |
| 427 | if (nla_put_u32(skb, SMC_NLA_DEV_PCI_FID, smc_pci_dev->pci_fid)) |
| 428 | return false; |
| 429 | if (nla_put_u16(skb, SMC_NLA_DEV_PCI_CHID, smc_pci_dev->pci_pchid)) |
| 430 | return false; |
| 431 | if (nla_put_u16(skb, SMC_NLA_DEV_PCI_VENDOR, smc_pci_dev->pci_vendor)) |
| 432 | return false; |
| 433 | if (nla_put_u16(skb, SMC_NLA_DEV_PCI_DEVICE, smc_pci_dev->pci_device)) |
| 434 | return false; |
| 435 | if (nla_put_string(skb, SMC_NLA_DEV_PCI_ID, smc_pci_dev->pci_id)) |
| 436 | return false; |
| 437 | return true; |
| 438 | } |
| 439 | |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 440 | static int smc_nl_handle_smcr_dev(struct smc_ib_device *smcibdev, |
| 441 | struct sk_buff *skb, |
| 442 | struct netlink_callback *cb) |
| 443 | { |
Guvenc Gulce | 8a44653 | 2021-01-12 17:21:22 +0100 | [diff] [blame] | 444 | char smc_ibname[IB_DEVICE_NAME_MAX]; |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 445 | struct smc_pci_dev smc_pci_dev; |
| 446 | struct pci_dev *pci_dev; |
| 447 | unsigned char is_crit; |
| 448 | struct nlattr *attrs; |
| 449 | void *nlh; |
| 450 | int i; |
| 451 | |
| 452 | nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 453 | &smc_gen_nl_family, NLM_F_MULTI, |
| 454 | SMC_NETLINK_GET_DEV_SMCR); |
| 455 | if (!nlh) |
| 456 | goto errmsg; |
| 457 | attrs = nla_nest_start(skb, SMC_GEN_DEV_SMCR); |
| 458 | if (!attrs) |
| 459 | goto errout; |
| 460 | is_crit = smcr_diag_is_dev_critical(&smc_lgr_list, smcibdev); |
| 461 | if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRIT, is_crit)) |
| 462 | goto errattr; |
Karsten Graul | 995433b | 2020-12-15 10:10:58 +0100 | [diff] [blame] | 463 | if (smcibdev->ibdev->dev.parent) { |
| 464 | memset(&smc_pci_dev, 0, sizeof(smc_pci_dev)); |
| 465 | pci_dev = to_pci_dev(smcibdev->ibdev->dev.parent); |
| 466 | smc_set_pci_values(pci_dev, &smc_pci_dev); |
| 467 | if (!smc_nl_handle_pci_values(&smc_pci_dev, skb)) |
| 468 | goto errattr; |
| 469 | } |
Guvenc Gulce | a3db10e | 2020-12-01 20:20:49 +0100 | [diff] [blame] | 470 | snprintf(smc_ibname, sizeof(smc_ibname), "%s", smcibdev->ibdev->name); |
| 471 | if (nla_put_string(skb, SMC_NLA_DEV_IB_NAME, smc_ibname)) |
| 472 | goto errattr; |
| 473 | for (i = 1; i <= SMC_MAX_PORTS; i++) { |
| 474 | if (!rdma_is_port_valid(smcibdev->ibdev, i)) |
| 475 | continue; |
| 476 | if (smc_nl_handle_dev_port(skb, smcibdev->ibdev, |
| 477 | smcibdev, i - 1)) |
| 478 | goto errattr; |
| 479 | } |
| 480 | |
| 481 | nla_nest_end(skb, attrs); |
| 482 | genlmsg_end(skb, nlh); |
| 483 | return 0; |
| 484 | |
| 485 | errattr: |
| 486 | nla_nest_cancel(skb, attrs); |
| 487 | errout: |
| 488 | genlmsg_cancel(skb, nlh); |
| 489 | errmsg: |
| 490 | return -EMSGSIZE; |
| 491 | } |
| 492 | |
| 493 | static void smc_nl_prep_smcr_dev(struct smc_ib_devices *dev_list, |
| 494 | struct sk_buff *skb, |
| 495 | struct netlink_callback *cb) |
| 496 | { |
| 497 | struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); |
| 498 | struct smc_ib_device *smcibdev; |
| 499 | int snum = cb_ctx->pos[0]; |
| 500 | int num = 0; |
| 501 | |
| 502 | mutex_lock(&dev_list->mutex); |
| 503 | list_for_each_entry(smcibdev, &dev_list->list, list) { |
| 504 | if (num < snum) |
| 505 | goto next; |
| 506 | if (smc_nl_handle_smcr_dev(smcibdev, skb, cb)) |
| 507 | goto errout; |
| 508 | next: |
| 509 | num++; |
| 510 | } |
| 511 | errout: |
| 512 | mutex_unlock(&dev_list->mutex); |
| 513 | cb_ctx->pos[0] = num; |
| 514 | } |
| 515 | |
| 516 | int smcr_nl_get_device(struct sk_buff *skb, struct netlink_callback *cb) |
| 517 | { |
| 518 | smc_nl_prep_smcr_dev(&smc_ib_devices, skb, cb); |
| 519 | return skb->len; |
| 520 | } |
| 521 | |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 522 | static void smc_ib_qp_event_handler(struct ib_event *ibevent, void *priv) |
| 523 | { |
Karsten Graul | e5f3aa0 | 2019-01-30 18:51:07 +0100 | [diff] [blame] | 524 | struct smc_link *lnk = (struct smc_link *)priv; |
| 525 | struct smc_ib_device *smcibdev = lnk->smcibdev; |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 526 | u8 port_idx; |
| 527 | |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 528 | switch (ibevent->event) { |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 529 | case IB_EVENT_QP_FATAL: |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 530 | case IB_EVENT_QP_ACCESS_ERR: |
Karsten Graul | e5f3aa0 | 2019-01-30 18:51:07 +0100 | [diff] [blame] | 531 | port_idx = ibevent->element.qp->port - 1; |
Ursula Braun | 5613f20 | 2020-02-17 16:24:55 +0100 | [diff] [blame] | 532 | if (port_idx >= SMC_MAX_PORTS) |
| 533 | break; |
| 534 | set_bit(port_idx, &smcibdev->port_event_mask); |
| 535 | if (!test_and_set_bit(port_idx, smcibdev->ports_going_away)) |
Karsten Graul | 81cf643 | 2019-02-12 16:29:55 +0100 | [diff] [blame] | 536 | schedule_work(&smcibdev->port_event_work); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 537 | break; |
| 538 | default: |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void smc_ib_destroy_queue_pair(struct smc_link *lnk) |
| 544 | { |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 545 | if (lnk->roce_qp) |
| 546 | ib_destroy_qp(lnk->roce_qp); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 547 | lnk->roce_qp = NULL; |
| 548 | } |
| 549 | |
| 550 | /* create a queue pair within the protection domain for a link */ |
| 551 | int smc_ib_create_queue_pair(struct smc_link *lnk) |
| 552 | { |
| 553 | struct ib_qp_init_attr qp_attr = { |
| 554 | .event_handler = smc_ib_qp_event_handler, |
| 555 | .qp_context = lnk, |
| 556 | .send_cq = lnk->smcibdev->roce_cq_send, |
| 557 | .recv_cq = lnk->smcibdev->roce_cq_recv, |
| 558 | .srq = NULL, |
| 559 | .cap = { |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 560 | /* include unsolicited rdma_writes as well, |
| 561 | * there are max. 2 RDMA_WRITE per 1 WR_SEND |
| 562 | */ |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 563 | .max_send_wr = SMC_WR_BUF_CNT * 3, |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 564 | .max_recv_wr = SMC_WR_BUF_CNT * 3, |
| 565 | .max_send_sge = SMC_IB_MAX_SEND_SGE, |
| 566 | .max_recv_sge = 1, |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 567 | }, |
| 568 | .sq_sig_type = IB_SIGNAL_REQ_WR, |
| 569 | .qp_type = IB_QPT_RC, |
| 570 | }; |
| 571 | int rc; |
| 572 | |
| 573 | lnk->roce_qp = ib_create_qp(lnk->roce_pd, &qp_attr); |
| 574 | rc = PTR_ERR_OR_ZERO(lnk->roce_qp); |
| 575 | if (IS_ERR(lnk->roce_qp)) |
| 576 | lnk->roce_qp = NULL; |
| 577 | else |
| 578 | smc_wr_remember_qp_attr(lnk); |
| 579 | return rc; |
| 580 | } |
| 581 | |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 582 | void smc_ib_put_memory_region(struct ib_mr *mr) |
| 583 | { |
| 584 | ib_dereg_mr(mr); |
| 585 | } |
| 586 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 587 | 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] | 588 | { |
| 589 | unsigned int offset = 0; |
| 590 | int sg_num; |
| 591 | |
| 592 | /* map the largest prefix of a dma mapped SG list */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 593 | sg_num = ib_map_mr_sg(buf_slot->mr_rx[link_idx], |
| 594 | buf_slot->sgt[link_idx].sgl, |
| 595 | buf_slot->sgt[link_idx].orig_nents, |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 596 | &offset, PAGE_SIZE); |
| 597 | |
| 598 | return sg_num; |
| 599 | } |
| 600 | |
| 601 | /* Allocate a memory region and map the dma mapped SG list of buf_slot */ |
| 602 | 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] | 603 | struct smc_buf_desc *buf_slot, u8 link_idx) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 604 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 605 | if (buf_slot->mr_rx[link_idx]) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 606 | return 0; /* already done */ |
| 607 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 608 | buf_slot->mr_rx[link_idx] = |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 609 | 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] | 610 | if (IS_ERR(buf_slot->mr_rx[link_idx])) { |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 611 | int rc; |
| 612 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 613 | rc = PTR_ERR(buf_slot->mr_rx[link_idx]); |
| 614 | buf_slot->mr_rx[link_idx] = NULL; |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 615 | return rc; |
| 616 | } |
| 617 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 618 | if (smc_ib_map_mr_sg(buf_slot, link_idx) != 1) |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 619 | return -EINVAL; |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 624 | /* synchronize buffer usage for cpu access */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 625 | void smc_ib_sync_sg_for_cpu(struct smc_link *lnk, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 626 | struct smc_buf_desc *buf_slot, |
| 627 | enum dma_data_direction data_direction) |
| 628 | { |
| 629 | struct scatterlist *sg; |
| 630 | unsigned int i; |
| 631 | |
| 632 | /* for now there is just one DMA address */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 633 | for_each_sg(buf_slot->sgt[lnk->link_idx].sgl, sg, |
| 634 | buf_slot->sgt[lnk->link_idx].nents, i) { |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 635 | if (!sg_dma_len(sg)) |
| 636 | break; |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 637 | ib_dma_sync_single_for_cpu(lnk->smcibdev->ibdev, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 638 | sg_dma_address(sg), |
| 639 | sg_dma_len(sg), |
| 640 | data_direction); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /* synchronize buffer usage for device access */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 645 | void smc_ib_sync_sg_for_device(struct smc_link *lnk, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 646 | struct smc_buf_desc *buf_slot, |
| 647 | enum dma_data_direction data_direction) |
| 648 | { |
| 649 | struct scatterlist *sg; |
| 650 | unsigned int i; |
| 651 | |
| 652 | /* for now there is just one DMA address */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 653 | for_each_sg(buf_slot->sgt[lnk->link_idx].sgl, sg, |
| 654 | buf_slot->sgt[lnk->link_idx].nents, i) { |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 655 | if (!sg_dma_len(sg)) |
| 656 | break; |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 657 | ib_dma_sync_single_for_device(lnk->smcibdev->ibdev, |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 658 | sg_dma_address(sg), |
| 659 | sg_dma_len(sg), |
| 660 | data_direction); |
| 661 | } |
| 662 | } |
| 663 | |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 664 | /* Map a new TX or RX buffer SG-table to DMA */ |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 665 | int smc_ib_buf_map_sg(struct smc_link *lnk, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 666 | struct smc_buf_desc *buf_slot, |
| 667 | enum dma_data_direction data_direction) |
| 668 | { |
| 669 | int mapped_nents; |
| 670 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 671 | mapped_nents = ib_dma_map_sg(lnk->smcibdev->ibdev, |
| 672 | buf_slot->sgt[lnk->link_idx].sgl, |
| 673 | buf_slot->sgt[lnk->link_idx].orig_nents, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 674 | data_direction); |
| 675 | if (!mapped_nents) |
| 676 | return -ENOMEM; |
| 677 | |
| 678 | return mapped_nents; |
| 679 | } |
| 680 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 681 | void smc_ib_buf_unmap_sg(struct smc_link *lnk, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 682 | struct smc_buf_desc *buf_slot, |
| 683 | enum dma_data_direction data_direction) |
| 684 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 685 | if (!buf_slot->sgt[lnk->link_idx].sgl->dma_address) |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 686 | return; /* already unmapped */ |
| 687 | |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 688 | ib_dma_unmap_sg(lnk->smcibdev->ibdev, |
| 689 | buf_slot->sgt[lnk->link_idx].sgl, |
| 690 | buf_slot->sgt[lnk->link_idx].orig_nents, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 691 | data_direction); |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 692 | buf_slot->sgt[lnk->link_idx].sgl->dma_address = 0; |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 695 | long smc_ib_setup_per_ibdev(struct smc_ib_device *smcibdev) |
| 696 | { |
| 697 | struct ib_cq_init_attr cqattr = { |
Ursula Braun | c9f4c6c | 2018-03-14 11:01:00 +0100 | [diff] [blame] | 698 | .cqe = SMC_MAX_CQE, .comp_vector = 0 }; |
| 699 | int cqe_size_order, smc_order; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 700 | long rc; |
| 701 | |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 702 | mutex_lock(&smcibdev->mutex); |
| 703 | rc = 0; |
| 704 | if (smcibdev->initialized) |
| 705 | goto out; |
Ursula Braun | c9f4c6c | 2018-03-14 11:01:00 +0100 | [diff] [blame] | 706 | /* the calculated number of cq entries fits to mlx5 cq allocation */ |
| 707 | cqe_size_order = cache_line_size() == 128 ? 7 : 6; |
| 708 | smc_order = MAX_ORDER - cqe_size_order - 1; |
| 709 | if (SMC_MAX_CQE + 2 > (0x00000001 << smc_order) * PAGE_SIZE) |
| 710 | cqattr.cqe = (0x00000001 << smc_order) * PAGE_SIZE - 2; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 711 | smcibdev->roce_cq_send = ib_create_cq(smcibdev->ibdev, |
| 712 | smc_wr_tx_cq_handler, NULL, |
| 713 | smcibdev, &cqattr); |
| 714 | rc = PTR_ERR_OR_ZERO(smcibdev->roce_cq_send); |
| 715 | if (IS_ERR(smcibdev->roce_cq_send)) { |
| 716 | smcibdev->roce_cq_send = NULL; |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 717 | goto out; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 718 | } |
| 719 | smcibdev->roce_cq_recv = ib_create_cq(smcibdev->ibdev, |
| 720 | smc_wr_rx_cq_handler, NULL, |
| 721 | smcibdev, &cqattr); |
| 722 | rc = PTR_ERR_OR_ZERO(smcibdev->roce_cq_recv); |
| 723 | if (IS_ERR(smcibdev->roce_cq_recv)) { |
| 724 | smcibdev->roce_cq_recv = NULL; |
| 725 | goto err; |
| 726 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 727 | smc_wr_add_dev(smcibdev); |
| 728 | smcibdev->initialized = 1; |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 729 | goto out; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 730 | |
| 731 | err: |
| 732 | ib_destroy_cq(smcibdev->roce_cq_send); |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 733 | out: |
| 734 | mutex_unlock(&smcibdev->mutex); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 735 | return rc; |
| 736 | } |
| 737 | |
| 738 | static void smc_ib_cleanup_per_ibdev(struct smc_ib_device *smcibdev) |
| 739 | { |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 740 | mutex_lock(&smcibdev->mutex); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 741 | if (!smcibdev->initialized) |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 742 | goto out; |
Ursula Braun | da05bf2 | 2018-01-26 09:28:46 +0100 | [diff] [blame] | 743 | smcibdev->initialized = 0; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 744 | ib_destroy_cq(smcibdev->roce_cq_recv); |
| 745 | ib_destroy_cq(smcibdev->roce_cq_send); |
Ursula Braun | 6a37ad3 | 2019-11-14 13:02:46 +0100 | [diff] [blame] | 746 | smc_wr_remove_dev(smcibdev); |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 747 | out: |
| 748 | mutex_unlock(&smcibdev->mutex); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 751 | static struct ib_client smc_ib_client; |
| 752 | |
Guvenc Gulce | 3d453f5 | 2020-12-01 20:20:40 +0100 | [diff] [blame] | 753 | static void smc_copy_netdev_ifindex(struct smc_ib_device *smcibdev, int port) |
| 754 | { |
| 755 | struct ib_device *ibdev = smcibdev->ibdev; |
| 756 | struct net_device *ndev; |
| 757 | |
| 758 | if (!ibdev->ops.get_netdev) |
| 759 | return; |
| 760 | ndev = ibdev->ops.get_netdev(ibdev, port + 1); |
| 761 | if (ndev) { |
| 762 | smcibdev->ndev_ifidx[port] = ndev->ifindex; |
| 763 | dev_put(ndev); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | void smc_ib_ndev_change(struct net_device *ndev, unsigned long event) |
| 768 | { |
| 769 | struct smc_ib_device *smcibdev; |
| 770 | struct ib_device *libdev; |
| 771 | struct net_device *lndev; |
| 772 | u8 port_cnt; |
| 773 | int i; |
| 774 | |
| 775 | mutex_lock(&smc_ib_devices.mutex); |
| 776 | list_for_each_entry(smcibdev, &smc_ib_devices.list, list) { |
| 777 | port_cnt = smcibdev->ibdev->phys_port_cnt; |
| 778 | for (i = 0; i < min_t(size_t, port_cnt, SMC_MAX_PORTS); i++) { |
| 779 | libdev = smcibdev->ibdev; |
| 780 | if (!libdev->ops.get_netdev) |
| 781 | continue; |
| 782 | lndev = libdev->ops.get_netdev(libdev, i + 1); |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 783 | dev_put(lndev); |
Guvenc Gulce | 3d453f5 | 2020-12-01 20:20:40 +0100 | [diff] [blame] | 784 | if (lndev != ndev) |
| 785 | continue; |
| 786 | if (event == NETDEV_REGISTER) |
| 787 | smcibdev->ndev_ifidx[i] = ndev->ifindex; |
| 788 | if (event == NETDEV_UNREGISTER) |
| 789 | smcibdev->ndev_ifidx[i] = 0; |
| 790 | } |
| 791 | } |
| 792 | mutex_unlock(&smc_ib_devices.mutex); |
| 793 | } |
| 794 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 795 | /* callback function for ib_register_client() */ |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 796 | static int smc_ib_add_dev(struct ib_device *ibdev) |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 797 | { |
| 798 | struct smc_ib_device *smcibdev; |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 799 | u8 port_cnt; |
| 800 | int i; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 801 | |
| 802 | if (ibdev->node_type != RDMA_NODE_IB_CA) |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 803 | return -EOPNOTSUPP; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 804 | |
| 805 | smcibdev = kzalloc(sizeof(*smcibdev), GFP_KERNEL); |
| 806 | if (!smcibdev) |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 807 | return -ENOMEM; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 808 | |
| 809 | smcibdev->ibdev = ibdev; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 810 | INIT_WORK(&smcibdev->port_event_work, smc_ib_port_event_work); |
Ursula Braun | 6dabd40 | 2019-11-16 17:47:29 +0100 | [diff] [blame] | 811 | atomic_set(&smcibdev->lnk_cnt, 0); |
| 812 | init_waitqueue_head(&smcibdev->lnks_deleted); |
Karsten Graul | 6367359 | 2020-07-18 15:06:12 +0200 | [diff] [blame] | 813 | mutex_init(&smcibdev->mutex); |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 814 | mutex_lock(&smc_ib_devices.mutex); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 815 | list_add_tail(&smcibdev->list, &smc_ib_devices.list); |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 816 | mutex_unlock(&smc_ib_devices.mutex); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 817 | ib_set_client_data(ibdev, &smc_ib_client, smcibdev); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 818 | INIT_IB_EVENT_HANDLER(&smcibdev->event_handler, smcibdev->ibdev, |
| 819 | smc_ib_global_event_handler); |
| 820 | ib_register_event_handler(&smcibdev->event_handler); |
| 821 | |
| 822 | /* trigger reading of the port attributes */ |
| 823 | port_cnt = smcibdev->ibdev->phys_port_cnt; |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 824 | pr_warn_ratelimited("smc: adding ib device %s with port count %d\n", |
| 825 | smcibdev->ibdev->name, port_cnt); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 826 | for (i = 0; |
| 827 | i < min_t(size_t, port_cnt, SMC_MAX_PORTS); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 828 | i++) { |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 829 | set_bit(i, &smcibdev->port_event_mask); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 830 | /* determine pnetids of the port */ |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 831 | if (smc_pnetid_by_dev_port(ibdev->dev.parent, i, |
| 832 | smcibdev->pnetid[i])) |
| 833 | smc_pnetid_by_table_ib(smcibdev, i + 1); |
Guvenc Gulce | 3d453f5 | 2020-12-01 20:20:40 +0100 | [diff] [blame] | 834 | smc_copy_netdev_ifindex(smcibdev, i); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 835 | pr_warn_ratelimited("smc: ib device %s port %d has pnetid " |
| 836 | "%.16s%s\n", |
| 837 | smcibdev->ibdev->name, i + 1, |
| 838 | smcibdev->pnetid[i], |
| 839 | smcibdev->pnetid_by_user[i] ? |
| 840 | " (user defined)" : |
| 841 | ""); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 842 | } |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 843 | schedule_work(&smcibdev->port_event_work); |
Jason Gunthorpe | 11a0ae4 | 2020-04-21 20:24:40 +0300 | [diff] [blame] | 844 | return 0; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Ursula Braun | 0b29ec6 | 2019-11-14 13:02:47 +0100 | [diff] [blame] | 847 | /* callback function for ib_unregister_client() */ |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 848 | static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data) |
| 849 | { |
Jason Gunthorpe | 1587982 | 2020-04-07 20:20:09 -0300 | [diff] [blame] | 850 | struct smc_ib_device *smcibdev = client_data; |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 851 | |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 852 | mutex_lock(&smc_ib_devices.mutex); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 853 | list_del_init(&smcibdev->list); /* remove from smc_ib_devices */ |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 854 | mutex_unlock(&smc_ib_devices.mutex); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 855 | pr_warn_ratelimited("smc: removing ib device %s\n", |
| 856 | smcibdev->ibdev->name); |
Ursula Braun | 0b29ec6 | 2019-11-14 13:02:47 +0100 | [diff] [blame] | 857 | smc_smcr_terminate_all(smcibdev); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 858 | smc_ib_cleanup_per_ibdev(smcibdev); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 859 | ib_unregister_event_handler(&smcibdev->event_handler); |
Karsten Graul | ece0d7b | 2020-03-10 09:33:30 +0100 | [diff] [blame] | 860 | cancel_work_sync(&smcibdev->port_event_work); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 861 | kfree(smcibdev); |
| 862 | } |
| 863 | |
| 864 | static struct ib_client smc_ib_client = { |
| 865 | .name = "smc_ib", |
| 866 | .add = smc_ib_add_dev, |
| 867 | .remove = smc_ib_remove_dev, |
| 868 | }; |
| 869 | |
| 870 | int __init smc_ib_register_client(void) |
| 871 | { |
Hans Wippel | 366bb24 | 2020-02-25 22:41:21 +0100 | [diff] [blame] | 872 | smc_ib_init_local_systemid(); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 873 | return ib_register_client(&smc_ib_client); |
| 874 | } |
| 875 | |
| 876 | void smc_ib_unregister_client(void) |
| 877 | { |
| 878 | ib_unregister_client(&smc_ib_client); |
| 879 | } |