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