Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 4 | * |
| 5 | * Basic Transport Functions exploiting Infiniband API |
| 6 | * |
| 7 | * Copyright IBM Corp. 2016 |
| 8 | * |
| 9 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/if_vlan.h> |
| 14 | #include <linux/random.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | #include <net/tcp.h> |
| 17 | #include <net/sock.h> |
| 18 | #include <rdma/ib_verbs.h> |
| 19 | |
| 20 | #include "smc.h" |
| 21 | #include "smc_clc.h" |
| 22 | #include "smc_core.h" |
| 23 | #include "smc_ib.h" |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 24 | #include "smc_wr.h" |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 25 | #include "smc_llc.h" |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 26 | #include "smc_cdc.h" |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 27 | #include "smc_close.h" |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 28 | |
Ursula Braun | 5bc11dd | 2017-09-21 09:16:31 +0200 | [diff] [blame] | 29 | #define SMC_LGR_NUM_INCR 256 |
| 30 | #define SMC_LGR_FREE_DELAY_SERV (600 * HZ) |
| 31 | #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10) |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 32 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 33 | static u32 smc_lgr_num; /* unique link group number */ |
| 34 | |
Karsten Graul | 97cdbc4 | 2018-03-14 11:01:02 +0100 | [diff] [blame] | 35 | static void smc_lgr_schedule_free_work(struct smc_link_group *lgr) |
| 36 | { |
| 37 | /* client link group creation always follows the server link group |
| 38 | * creation. For client use a somewhat higher removal delay time, |
| 39 | * otherwise there is a risk of out-of-sync link groups. |
| 40 | */ |
| 41 | mod_delayed_work(system_wq, &lgr->free_work, |
| 42 | lgr->role == SMC_CLNT ? SMC_LGR_FREE_DELAY_CLNT : |
| 43 | SMC_LGR_FREE_DELAY_SERV); |
| 44 | } |
| 45 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 46 | /* Register connection's alert token in our lookup structure. |
| 47 | * To use rbtrees we have to implement our own insert core. |
| 48 | * Requires @conns_lock |
| 49 | * @smc connection to register |
| 50 | * Returns 0 on success, != otherwise. |
| 51 | */ |
| 52 | static void smc_lgr_add_alert_token(struct smc_connection *conn) |
| 53 | { |
| 54 | struct rb_node **link, *parent = NULL; |
| 55 | u32 token = conn->alert_token_local; |
| 56 | |
| 57 | link = &conn->lgr->conns_all.rb_node; |
| 58 | while (*link) { |
| 59 | struct smc_connection *cur = rb_entry(*link, |
| 60 | struct smc_connection, alert_node); |
| 61 | |
| 62 | parent = *link; |
| 63 | if (cur->alert_token_local > token) |
| 64 | link = &parent->rb_left; |
| 65 | else |
| 66 | link = &parent->rb_right; |
| 67 | } |
| 68 | /* Put the new node there */ |
| 69 | rb_link_node(&conn->alert_node, parent, link); |
| 70 | rb_insert_color(&conn->alert_node, &conn->lgr->conns_all); |
| 71 | } |
| 72 | |
| 73 | /* Register connection in link group by assigning an alert token |
| 74 | * registered in a search tree. |
| 75 | * Requires @conns_lock |
| 76 | * Note that '0' is a reserved value and not assigned. |
| 77 | */ |
| 78 | static void smc_lgr_register_conn(struct smc_connection *conn) |
| 79 | { |
| 80 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); |
| 81 | static atomic_t nexttoken = ATOMIC_INIT(0); |
| 82 | |
| 83 | /* find a new alert_token_local value not yet used by some connection |
| 84 | * in this link group |
| 85 | */ |
| 86 | sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */ |
| 87 | while (!conn->alert_token_local) { |
| 88 | conn->alert_token_local = atomic_inc_return(&nexttoken); |
| 89 | if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr)) |
| 90 | conn->alert_token_local = 0; |
| 91 | } |
| 92 | smc_lgr_add_alert_token(conn); |
| 93 | conn->lgr->conns_num++; |
| 94 | } |
| 95 | |
| 96 | /* Unregister connection and reset the alert token of the given connection< |
| 97 | */ |
| 98 | static void __smc_lgr_unregister_conn(struct smc_connection *conn) |
| 99 | { |
| 100 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); |
| 101 | struct smc_link_group *lgr = conn->lgr; |
| 102 | |
| 103 | rb_erase(&conn->alert_node, &lgr->conns_all); |
| 104 | lgr->conns_num--; |
| 105 | conn->alert_token_local = 0; |
| 106 | conn->lgr = NULL; |
| 107 | sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */ |
| 108 | } |
| 109 | |
| 110 | /* Unregister connection and trigger lgr freeing if applicable |
| 111 | */ |
| 112 | static void smc_lgr_unregister_conn(struct smc_connection *conn) |
| 113 | { |
| 114 | struct smc_link_group *lgr = conn->lgr; |
| 115 | int reduced = 0; |
| 116 | |
| 117 | write_lock_bh(&lgr->conns_lock); |
| 118 | if (conn->alert_token_local) { |
| 119 | reduced = 1; |
| 120 | __smc_lgr_unregister_conn(conn); |
| 121 | } |
| 122 | write_unlock_bh(&lgr->conns_lock); |
Ursula Braun | 5bc11dd | 2017-09-21 09:16:31 +0200 | [diff] [blame] | 123 | if (!reduced || lgr->conns_num) |
| 124 | return; |
Karsten Graul | 97cdbc4 | 2018-03-14 11:01:02 +0100 | [diff] [blame] | 125 | smc_lgr_schedule_free_work(lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void smc_lgr_free_work(struct work_struct *work) |
| 129 | { |
| 130 | struct smc_link_group *lgr = container_of(to_delayed_work(work), |
| 131 | struct smc_link_group, |
| 132 | free_work); |
| 133 | bool conns; |
| 134 | |
| 135 | spin_lock_bh(&smc_lgr_list.lock); |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame] | 136 | if (list_empty(&lgr->list)) |
| 137 | goto free; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 138 | read_lock_bh(&lgr->conns_lock); |
| 139 | conns = RB_EMPTY_ROOT(&lgr->conns_all); |
| 140 | read_unlock_bh(&lgr->conns_lock); |
| 141 | if (!conns) { /* number of lgr connections is no longer zero */ |
| 142 | spin_unlock_bh(&smc_lgr_list.lock); |
| 143 | return; |
| 144 | } |
| 145 | list_del_init(&lgr->list); /* remove from smc_lgr_list */ |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame] | 146 | free: |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 147 | spin_unlock_bh(&smc_lgr_list.lock); |
Ursula Braun | 268ffcc | 2018-03-14 11:01:01 +0100 | [diff] [blame] | 148 | if (!delayed_work_pending(&lgr->free_work)) |
| 149 | smc_lgr_free(lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* create a new SMC link group */ |
Karsten Graul | be6d467 | 2018-03-01 13:51:28 +0100 | [diff] [blame] | 153 | static int smc_lgr_create(struct smc_sock *smc, |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 154 | struct smc_ib_device *smcibdev, u8 ibport, |
| 155 | char *peer_systemid, unsigned short vlan_id) |
| 156 | { |
| 157 | struct smc_link_group *lgr; |
| 158 | struct smc_link *lnk; |
| 159 | u8 rndvec[3]; |
| 160 | int rc = 0; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 161 | int i; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 162 | |
| 163 | lgr = kzalloc(sizeof(*lgr), GFP_KERNEL); |
| 164 | if (!lgr) { |
| 165 | rc = -ENOMEM; |
| 166 | goto out; |
| 167 | } |
| 168 | lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT; |
| 169 | lgr->sync_err = false; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 170 | memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN); |
| 171 | lgr->vlan_id = vlan_id; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 172 | rwlock_init(&lgr->sndbufs_lock); |
| 173 | rwlock_init(&lgr->rmbs_lock); |
| 174 | for (i = 0; i < SMC_RMBE_SIZES; i++) { |
| 175 | INIT_LIST_HEAD(&lgr->sndbufs[i]); |
| 176 | INIT_LIST_HEAD(&lgr->rmbs[i]); |
| 177 | } |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 178 | smc_lgr_num += SMC_LGR_NUM_INCR; |
| 179 | memcpy(&lgr->id, (u8 *)&smc_lgr_num, SMC_LGR_ID_SIZE); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 180 | INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work); |
| 181 | lgr->conns_all = RB_ROOT; |
| 182 | |
| 183 | lnk = &lgr->lnk[SMC_SINGLE_LINK]; |
| 184 | /* initialize link */ |
Karsten Graul | 52bedf3 | 2018-03-01 13:51:32 +0100 | [diff] [blame] | 185 | lnk->state = SMC_LNK_ACTIVATING; |
Karsten Graul | 2be922f | 2018-02-28 12:44:08 +0100 | [diff] [blame] | 186 | lnk->link_id = SMC_SINGLE_LINK; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 187 | lnk->smcibdev = smcibdev; |
| 188 | lnk->ibport = ibport; |
| 189 | lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 190 | if (!smcibdev->initialized) |
| 191 | smc_ib_setup_per_ibdev(smcibdev); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 192 | get_random_bytes(rndvec, sizeof(rndvec)); |
| 193 | lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 194 | rc = smc_wr_alloc_link_mem(lnk); |
| 195 | if (rc) |
| 196 | goto free_lgr; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 197 | rc = smc_ib_create_protection_domain(lnk); |
| 198 | if (rc) |
| 199 | goto free_link_mem; |
| 200 | rc = smc_ib_create_queue_pair(lnk); |
| 201 | if (rc) |
| 202 | goto dealloc_pd; |
| 203 | rc = smc_wr_create_link(lnk); |
| 204 | if (rc) |
| 205 | goto destroy_qp; |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 206 | init_completion(&lnk->llc_confirm); |
| 207 | init_completion(&lnk->llc_confirm_resp); |
Karsten Graul | 52bedf3 | 2018-03-01 13:51:32 +0100 | [diff] [blame] | 208 | init_completion(&lnk->llc_add); |
| 209 | init_completion(&lnk->llc_add_resp); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 210 | |
| 211 | smc->conn.lgr = lgr; |
| 212 | rwlock_init(&lgr->conns_lock); |
| 213 | spin_lock_bh(&smc_lgr_list.lock); |
| 214 | list_add(&lgr->list, &smc_lgr_list.list); |
| 215 | spin_unlock_bh(&smc_lgr_list.lock); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 216 | return 0; |
| 217 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 218 | destroy_qp: |
| 219 | smc_ib_destroy_queue_pair(lnk); |
| 220 | dealloc_pd: |
| 221 | smc_ib_dealloc_protection_domain(lnk); |
| 222 | free_link_mem: |
| 223 | smc_wr_free_link_mem(lnk); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 224 | free_lgr: |
| 225 | kfree(lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 226 | out: |
| 227 | return rc; |
| 228 | } |
| 229 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 230 | static void smc_buf_unuse(struct smc_connection *conn) |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 231 | { |
| 232 | if (conn->sndbuf_desc) { |
| 233 | conn->sndbuf_desc->used = 0; |
| 234 | conn->sndbuf_size = 0; |
| 235 | } |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 236 | if (conn->rmb_desc) { |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 237 | conn->rmb_desc->reused = true; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 238 | conn->rmb_desc->used = 0; |
| 239 | conn->rmbe_size = 0; |
| 240 | } |
| 241 | } |
| 242 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 243 | /* remove a finished connection from its link group */ |
| 244 | void smc_conn_free(struct smc_connection *conn) |
| 245 | { |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 246 | if (!conn->lgr) |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 247 | return; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 248 | smc_cdc_tx_dismiss_slots(conn); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 249 | smc_lgr_unregister_conn(conn); |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 250 | smc_buf_unuse(conn); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void smc_link_clear(struct smc_link *lnk) |
| 254 | { |
| 255 | lnk->peer_qpn = 0; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 256 | smc_ib_modify_qp_reset(lnk); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 257 | smc_wr_free_link(lnk); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 258 | smc_ib_destroy_queue_pair(lnk); |
| 259 | smc_ib_dealloc_protection_domain(lnk); |
Ursula Braun | f38ba179 | 2017-01-09 16:55:19 +0100 | [diff] [blame] | 260 | smc_wr_free_link_mem(lnk); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 263 | static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk, |
| 264 | bool is_rmb) |
| 265 | { |
| 266 | if (is_rmb) { |
| 267 | if (buf_desc->mr_rx[SMC_SINGLE_LINK]) |
| 268 | smc_ib_put_memory_region( |
| 269 | buf_desc->mr_rx[SMC_SINGLE_LINK]); |
| 270 | smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc, |
| 271 | DMA_FROM_DEVICE); |
| 272 | } else { |
| 273 | smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc, |
| 274 | DMA_TO_DEVICE); |
| 275 | } |
| 276 | sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]); |
| 277 | if (buf_desc->cpu_addr) |
| 278 | free_pages((unsigned long)buf_desc->cpu_addr, buf_desc->order); |
| 279 | kfree(buf_desc); |
| 280 | } |
| 281 | |
| 282 | static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb) |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 283 | { |
Ursula Braun | 9d8fb61 | 2017-07-28 13:56:19 +0200 | [diff] [blame] | 284 | struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 285 | struct smc_buf_desc *buf_desc, *bf_desc; |
| 286 | struct list_head *buf_list; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 287 | int i; |
| 288 | |
| 289 | for (i = 0; i < SMC_RMBE_SIZES; i++) { |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 290 | if (is_rmb) |
| 291 | buf_list = &lgr->rmbs[i]; |
| 292 | else |
| 293 | buf_list = &lgr->sndbufs[i]; |
| 294 | list_for_each_entry_safe(buf_desc, bf_desc, buf_list, |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 295 | list) { |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 296 | list_del(&buf_desc->list); |
| 297 | smc_buf_free(buf_desc, lnk, is_rmb); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 302 | static void smc_lgr_free_bufs(struct smc_link_group *lgr) |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 303 | { |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 304 | /* free send buffers */ |
| 305 | __smc_lgr_free_bufs(lgr, false); |
| 306 | /* free rmbs */ |
| 307 | __smc_lgr_free_bufs(lgr, true); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 310 | /* remove a link group */ |
| 311 | void smc_lgr_free(struct smc_link_group *lgr) |
| 312 | { |
Karsten Graul | 877ae5b | 2018-05-02 16:56:44 +0200 | [diff] [blame^] | 313 | smc_llc_link_flush(&lgr->lnk[SMC_SINGLE_LINK]); |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 314 | smc_lgr_free_bufs(lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 315 | smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]); |
| 316 | kfree(lgr); |
| 317 | } |
| 318 | |
Karsten Graul | 9651b93 | 2018-03-01 13:51:33 +0100 | [diff] [blame] | 319 | void smc_lgr_forget(struct smc_link_group *lgr) |
| 320 | { |
| 321 | spin_lock_bh(&smc_lgr_list.lock); |
| 322 | /* do not use this link group for new connections */ |
| 323 | if (!list_empty(&lgr->list)) |
| 324 | list_del_init(&lgr->list); |
| 325 | spin_unlock_bh(&smc_lgr_list.lock); |
| 326 | } |
| 327 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 328 | /* terminate linkgroup abnormally */ |
| 329 | void smc_lgr_terminate(struct smc_link_group *lgr) |
| 330 | { |
| 331 | struct smc_connection *conn; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 332 | struct smc_sock *smc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 333 | struct rb_node *node; |
| 334 | |
Karsten Graul | 9651b93 | 2018-03-01 13:51:33 +0100 | [diff] [blame] | 335 | smc_lgr_forget(lgr); |
Karsten Graul | 877ae5b | 2018-05-02 16:56:44 +0200 | [diff] [blame^] | 336 | smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 337 | |
| 338 | write_lock_bh(&lgr->conns_lock); |
| 339 | node = rb_first(&lgr->conns_all); |
| 340 | while (node) { |
| 341 | conn = rb_entry(node, struct smc_connection, alert_node); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 342 | smc = container_of(conn, struct smc_sock, conn); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 343 | sock_hold(&smc->sk); /* sock_put in close work */ |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 344 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 345 | __smc_lgr_unregister_conn(conn); |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 346 | write_unlock_bh(&lgr->conns_lock); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 347 | if (!schedule_work(&conn->close_work)) |
| 348 | sock_put(&smc->sk); |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 349 | write_lock_bh(&lgr->conns_lock); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 350 | node = rb_first(&lgr->conns_all); |
| 351 | } |
| 352 | write_unlock_bh(&lgr->conns_lock); |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 353 | wake_up(&lgr->lnk[SMC_SINGLE_LINK].wr_reg_wait); |
Karsten Graul | 97cdbc4 | 2018-03-14 11:01:02 +0100 | [diff] [blame] | 354 | smc_lgr_schedule_free_work(lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* Determine vlan of internal TCP socket. |
| 358 | * @vlan_id: address to store the determined vlan id into |
| 359 | */ |
| 360 | static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id) |
| 361 | { |
| 362 | struct dst_entry *dst = sk_dst_get(clcsock->sk); |
| 363 | int rc = 0; |
| 364 | |
| 365 | *vlan_id = 0; |
| 366 | if (!dst) { |
| 367 | rc = -ENOTCONN; |
| 368 | goto out; |
| 369 | } |
| 370 | if (!dst->dev) { |
| 371 | rc = -ENODEV; |
| 372 | goto out_rel; |
| 373 | } |
| 374 | |
| 375 | if (is_vlan_dev(dst->dev)) |
| 376 | *vlan_id = vlan_dev_vlan_id(dst->dev); |
| 377 | |
| 378 | out_rel: |
| 379 | dst_release(dst); |
| 380 | out: |
| 381 | return rc; |
| 382 | } |
| 383 | |
| 384 | /* determine the link gid matching the vlan id of the link group */ |
| 385 | static int smc_link_determine_gid(struct smc_link_group *lgr) |
| 386 | { |
| 387 | struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; |
| 388 | struct ib_gid_attr gattr; |
| 389 | union ib_gid gid; |
| 390 | int i; |
| 391 | |
| 392 | if (!lgr->vlan_id) { |
| 393 | lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1]; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len; |
| 398 | i++) { |
| 399 | if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid, |
| 400 | &gattr)) |
| 401 | continue; |
Ursula Braun | 43e2ada | 2017-10-11 13:47:23 +0200 | [diff] [blame] | 402 | if (gattr.ndev) { |
| 403 | if (is_vlan_dev(gattr.ndev) && |
| 404 | vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) { |
| 405 | lnk->gid = gid; |
| 406 | dev_put(gattr.ndev); |
| 407 | return 0; |
| 408 | } |
| 409 | dev_put(gattr.ndev); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | return -ENODEV; |
| 413 | } |
| 414 | |
| 415 | /* create a new SMC connection (and a new link group if necessary) */ |
Karsten Graul | be6d467 | 2018-03-01 13:51:28 +0100 | [diff] [blame] | 416 | int smc_conn_create(struct smc_sock *smc, |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 417 | struct smc_ib_device *smcibdev, u8 ibport, |
| 418 | struct smc_clc_msg_local *lcl, int srv_first_contact) |
| 419 | { |
| 420 | struct smc_connection *conn = &smc->conn; |
| 421 | struct smc_link_group *lgr; |
| 422 | unsigned short vlan_id; |
| 423 | enum smc_lgr_role role; |
| 424 | int local_contact = SMC_FIRST_CONTACT; |
| 425 | int rc = 0; |
| 426 | |
| 427 | role = smc->listen_smc ? SMC_SERV : SMC_CLNT; |
| 428 | rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id); |
| 429 | if (rc) |
| 430 | return rc; |
| 431 | |
| 432 | if ((role == SMC_CLNT) && srv_first_contact) |
| 433 | /* create new link group as well */ |
| 434 | goto create; |
| 435 | |
| 436 | /* determine if an existing link group can be reused */ |
| 437 | spin_lock_bh(&smc_lgr_list.lock); |
| 438 | list_for_each_entry(lgr, &smc_lgr_list.list, list) { |
| 439 | write_lock_bh(&lgr->conns_lock); |
| 440 | if (!memcmp(lgr->peer_systemid, lcl->id_for_peer, |
| 441 | SMC_SYSTEMID_LEN) && |
| 442 | !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid, |
| 443 | SMC_GID_SIZE) && |
| 444 | !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac, |
| 445 | sizeof(lcl->mac)) && |
| 446 | !lgr->sync_err && |
| 447 | (lgr->role == role) && |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 448 | (lgr->vlan_id == vlan_id) && |
| 449 | ((role == SMC_CLNT) || |
| 450 | (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 451 | /* link group found */ |
| 452 | local_contact = SMC_REUSE_CONTACT; |
| 453 | conn->lgr = lgr; |
| 454 | smc_lgr_register_conn(conn); /* add smc conn to lgr */ |
| 455 | write_unlock_bh(&lgr->conns_lock); |
| 456 | break; |
| 457 | } |
| 458 | write_unlock_bh(&lgr->conns_lock); |
| 459 | } |
| 460 | spin_unlock_bh(&smc_lgr_list.lock); |
| 461 | |
| 462 | if (role == SMC_CLNT && !srv_first_contact && |
| 463 | (local_contact == SMC_FIRST_CONTACT)) { |
| 464 | /* Server reuses a link group, but Client wants to start |
| 465 | * a new one |
| 466 | * send out_of_sync decline, reason synchr. error |
| 467 | */ |
| 468 | return -ENOLINK; |
| 469 | } |
| 470 | |
| 471 | create: |
| 472 | if (local_contact == SMC_FIRST_CONTACT) { |
Karsten Graul | be6d467 | 2018-03-01 13:51:28 +0100 | [diff] [blame] | 473 | rc = smc_lgr_create(smc, smcibdev, ibport, |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 474 | lcl->id_for_peer, vlan_id); |
| 475 | if (rc) |
| 476 | goto out; |
| 477 | smc_lgr_register_conn(conn); /* add smc conn to lgr */ |
| 478 | rc = smc_link_determine_gid(conn->lgr); |
| 479 | } |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 480 | conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE; |
Karsten Graul | cbba07a | 2018-02-28 12:44:07 +0100 | [diff] [blame] | 481 | conn->local_tx_ctrl.len = SMC_WR_TX_SIZE; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 482 | #ifndef KERNEL_HAS_ATOMIC64 |
| 483 | spin_lock_init(&conn->acurs_lock); |
| 484 | #endif |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 485 | |
| 486 | out: |
| 487 | return rc ? rc : local_contact; |
| 488 | } |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 489 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 490 | /* try to reuse a sndbuf or rmb description slot for a certain |
| 491 | * buffer size; if not available, return NULL |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 492 | */ |
| 493 | static inline |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 494 | struct smc_buf_desc *smc_buf_get_slot(struct smc_link_group *lgr, |
| 495 | int compressed_bufsize, |
| 496 | rwlock_t *lock, |
| 497 | struct list_head *buf_list) |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 498 | { |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 499 | struct smc_buf_desc *buf_slot; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 500 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 501 | read_lock_bh(lock); |
| 502 | list_for_each_entry(buf_slot, buf_list, list) { |
| 503 | if (cmpxchg(&buf_slot->used, 0, 1) == 0) { |
| 504 | read_unlock_bh(lock); |
| 505 | return buf_slot; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 506 | } |
| 507 | } |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 508 | read_unlock_bh(lock); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 509 | return NULL; |
| 510 | } |
| 511 | |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 512 | /* one of the conditions for announcing a receiver's current window size is |
| 513 | * that it "results in a minimum increase in the window size of 10% of the |
| 514 | * receive buffer space" [RFC7609] |
| 515 | */ |
| 516 | static inline int smc_rmb_wnd_update_limit(int rmbe_size) |
| 517 | { |
| 518 | return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2); |
| 519 | } |
| 520 | |
Ursula Braun | b33982c | 2017-07-28 13:56:21 +0200 | [diff] [blame] | 521 | static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr, |
| 522 | bool is_rmb, int bufsize) |
| 523 | { |
| 524 | struct smc_buf_desc *buf_desc; |
| 525 | struct smc_link *lnk; |
| 526 | int rc; |
| 527 | |
| 528 | /* try to alloc a new buffer */ |
| 529 | buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL); |
| 530 | if (!buf_desc) |
| 531 | return ERR_PTR(-ENOMEM); |
| 532 | |
| 533 | buf_desc->cpu_addr = |
| 534 | (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | |
| 535 | __GFP_NOMEMALLOC | |
| 536 | __GFP_NORETRY | __GFP_ZERO, |
| 537 | get_order(bufsize)); |
| 538 | if (!buf_desc->cpu_addr) { |
| 539 | kfree(buf_desc); |
| 540 | return ERR_PTR(-EAGAIN); |
| 541 | } |
| 542 | buf_desc->order = get_order(bufsize); |
| 543 | |
| 544 | /* build the sg table from the pages */ |
| 545 | lnk = &lgr->lnk[SMC_SINGLE_LINK]; |
| 546 | rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1, |
| 547 | GFP_KERNEL); |
| 548 | if (rc) { |
| 549 | smc_buf_free(buf_desc, lnk, is_rmb); |
| 550 | return ERR_PTR(rc); |
| 551 | } |
| 552 | sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl, |
| 553 | buf_desc->cpu_addr, bufsize); |
| 554 | |
| 555 | /* map sg table to DMA address */ |
| 556 | rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc, |
| 557 | is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 558 | /* SMC protocol depends on mapping to one DMA address only */ |
| 559 | if (rc != 1) { |
| 560 | smc_buf_free(buf_desc, lnk, is_rmb); |
| 561 | return ERR_PTR(-EAGAIN); |
| 562 | } |
| 563 | |
| 564 | /* create a new memory region for the RMB */ |
| 565 | if (is_rmb) { |
| 566 | rc = smc_ib_get_memory_region(lnk->roce_pd, |
| 567 | IB_ACCESS_REMOTE_WRITE | |
| 568 | IB_ACCESS_LOCAL_WRITE, |
| 569 | buf_desc); |
| 570 | if (rc) { |
| 571 | smc_buf_free(buf_desc, lnk, is_rmb); |
| 572 | return ERR_PTR(rc); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | return buf_desc; |
| 577 | } |
| 578 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 579 | static int __smc_buf_create(struct smc_sock *smc, bool is_rmb) |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 580 | { |
| 581 | struct smc_connection *conn = &smc->conn; |
| 582 | struct smc_link_group *lgr = conn->lgr; |
Geert Uytterhoeven | 6887037 | 2017-11-21 13:23:54 +0100 | [diff] [blame] | 583 | struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM); |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 584 | struct list_head *buf_list; |
Ursula Braun | c45abf3 | 2017-07-28 13:56:14 +0200 | [diff] [blame] | 585 | int bufsize, bufsize_short; |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 586 | int sk_buf_size; |
| 587 | rwlock_t *lock; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 588 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 589 | if (is_rmb) |
| 590 | /* use socket recv buffer size (w/o overhead) as start value */ |
| 591 | sk_buf_size = smc->sk.sk_rcvbuf / 2; |
| 592 | else |
| 593 | /* use socket send buffer size (w/o overhead) as start value */ |
| 594 | sk_buf_size = smc->sk.sk_sndbuf / 2; |
| 595 | |
Ursula Braun | 4e1061f | 2017-11-21 13:23:53 +0100 | [diff] [blame] | 596 | for (bufsize_short = smc_compress_bufsize(sk_buf_size); |
Ursula Braun | c45abf3 | 2017-07-28 13:56:14 +0200 | [diff] [blame] | 597 | bufsize_short >= 0; bufsize_short--) { |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 598 | |
| 599 | if (is_rmb) { |
| 600 | lock = &lgr->rmbs_lock; |
| 601 | buf_list = &lgr->rmbs[bufsize_short]; |
| 602 | } else { |
| 603 | lock = &lgr->sndbufs_lock; |
| 604 | buf_list = &lgr->sndbufs[bufsize_short]; |
| 605 | } |
Ursula Braun | c45abf3 | 2017-07-28 13:56:14 +0200 | [diff] [blame] | 606 | bufsize = smc_uncompress_bufsize(bufsize_short); |
Ursula Braun | 9d8fb61 | 2017-07-28 13:56:19 +0200 | [diff] [blame] | 607 | if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC) |
| 608 | continue; |
| 609 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 610 | /* check for reusable slot in the link group */ |
| 611 | buf_desc = smc_buf_get_slot(lgr, bufsize_short, lock, buf_list); |
| 612 | if (buf_desc) { |
| 613 | memset(buf_desc->cpu_addr, 0, bufsize); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 614 | break; /* found reusable slot */ |
| 615 | } |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 616 | |
Ursula Braun | b33982c | 2017-07-28 13:56:21 +0200 | [diff] [blame] | 617 | buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize); |
| 618 | if (PTR_ERR(buf_desc) == -ENOMEM) |
| 619 | break; |
| 620 | if (IS_ERR(buf_desc)) |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 621 | continue; |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 622 | |
| 623 | buf_desc->used = 1; |
| 624 | write_lock_bh(lock); |
| 625 | list_add(&buf_desc->list, buf_list); |
| 626 | write_unlock_bh(lock); |
| 627 | break; /* found */ |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 628 | } |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 629 | |
Ursula Braun | b33982c | 2017-07-28 13:56:21 +0200 | [diff] [blame] | 630 | if (IS_ERR(buf_desc)) |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 631 | return -ENOMEM; |
| 632 | |
| 633 | if (is_rmb) { |
| 634 | conn->rmb_desc = buf_desc; |
Ursula Braun | c45abf3 | 2017-07-28 13:56:14 +0200 | [diff] [blame] | 635 | conn->rmbe_size = bufsize; |
| 636 | conn->rmbe_size_short = bufsize_short; |
| 637 | smc->sk.sk_rcvbuf = bufsize * 2; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 638 | atomic_set(&conn->bytes_to_rcv, 0); |
Ursula Braun | c45abf3 | 2017-07-28 13:56:14 +0200 | [diff] [blame] | 639 | conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 640 | } else { |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 641 | conn->sndbuf_desc = buf_desc; |
| 642 | conn->sndbuf_size = bufsize; |
| 643 | smc->sk.sk_sndbuf = bufsize * 2; |
| 644 | atomic_set(&conn->sndbuf_space, bufsize); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 645 | } |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 649 | void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn) |
| 650 | { |
| 651 | struct smc_link_group *lgr = conn->lgr; |
| 652 | |
| 653 | smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev, |
| 654 | conn->sndbuf_desc, DMA_TO_DEVICE); |
| 655 | } |
| 656 | |
| 657 | void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn) |
| 658 | { |
| 659 | struct smc_link_group *lgr = conn->lgr; |
| 660 | |
| 661 | smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev, |
| 662 | conn->sndbuf_desc, DMA_TO_DEVICE); |
| 663 | } |
| 664 | |
| 665 | void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn) |
| 666 | { |
| 667 | struct smc_link_group *lgr = conn->lgr; |
| 668 | |
| 669 | smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev, |
| 670 | conn->rmb_desc, DMA_FROM_DEVICE); |
| 671 | } |
| 672 | |
| 673 | void smc_rmb_sync_sg_for_device(struct smc_connection *conn) |
| 674 | { |
| 675 | struct smc_link_group *lgr = conn->lgr; |
| 676 | |
| 677 | smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev, |
| 678 | conn->rmb_desc, DMA_FROM_DEVICE); |
| 679 | } |
| 680 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 681 | /* create the send and receive buffer for an SMC socket; |
| 682 | * receive buffers are called RMBs; |
| 683 | * (even though the SMC protocol allows more than one RMB-element per RMB, |
| 684 | * the Linux implementation uses just one RMB-element per RMB, i.e. uses an |
| 685 | * extra RMB for every connection in a link group |
| 686 | */ |
| 687 | int smc_buf_create(struct smc_sock *smc) |
| 688 | { |
| 689 | int rc; |
| 690 | |
| 691 | /* create send buffer */ |
| 692 | rc = __smc_buf_create(smc, false); |
| 693 | if (rc) |
| 694 | return rc; |
| 695 | /* create rmb */ |
| 696 | rc = __smc_buf_create(smc, true); |
| 697 | if (rc) |
| 698 | smc_buf_free(smc->conn.sndbuf_desc, |
| 699 | &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false); |
| 700 | return rc; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 701 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 702 | |
| 703 | static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr) |
| 704 | { |
| 705 | int i; |
| 706 | |
| 707 | for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) { |
| 708 | if (!test_and_set_bit(i, lgr->rtokens_used_mask)) |
| 709 | return i; |
| 710 | } |
| 711 | return -ENOSPC; |
| 712 | } |
| 713 | |
Karsten Graul | 4ed75de | 2018-03-01 13:51:30 +0100 | [diff] [blame] | 714 | /* add a new rtoken from peer */ |
| 715 | int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey) |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 716 | { |
Karsten Graul | 4ed75de | 2018-03-01 13:51:30 +0100 | [diff] [blame] | 717 | u64 dma_addr = be64_to_cpu(nw_vaddr); |
| 718 | u32 rkey = ntohl(nw_rkey); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 719 | int i; |
| 720 | |
| 721 | for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { |
| 722 | if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) && |
Ursula Braun | 263eec9 | 2017-05-15 17:33:37 +0200 | [diff] [blame] | 723 | (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) && |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 724 | test_bit(i, lgr->rtokens_used_mask)) { |
Karsten Graul | 4ed75de | 2018-03-01 13:51:30 +0100 | [diff] [blame] | 725 | /* already in list */ |
| 726 | return i; |
| 727 | } |
| 728 | } |
| 729 | i = smc_rmb_reserve_rtoken_idx(lgr); |
| 730 | if (i < 0) |
| 731 | return i; |
| 732 | lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey; |
| 733 | lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr; |
| 734 | return i; |
| 735 | } |
| 736 | |
| 737 | /* delete an rtoken */ |
| 738 | int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey) |
| 739 | { |
| 740 | u32 rkey = ntohl(nw_rkey); |
| 741 | int i; |
| 742 | |
| 743 | for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { |
| 744 | if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey && |
| 745 | test_bit(i, lgr->rtokens_used_mask)) { |
| 746 | lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0; |
| 747 | lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0; |
| 748 | |
| 749 | clear_bit(i, lgr->rtokens_used_mask); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | } |
Karsten Graul | 4ed75de | 2018-03-01 13:51:30 +0100 | [diff] [blame] | 753 | return -ENOENT; |
| 754 | } |
| 755 | |
| 756 | /* save rkey and dma_addr received from peer during clc handshake */ |
| 757 | int smc_rmb_rtoken_handling(struct smc_connection *conn, |
| 758 | struct smc_clc_msg_accept_confirm *clc) |
| 759 | { |
| 760 | conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr, |
| 761 | clc->rmb_rkey); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 762 | if (conn->rtoken_idx < 0) |
| 763 | return conn->rtoken_idx; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 764 | return 0; |
| 765 | } |