smc_core.c 37.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *  Shared Memory Communications over RDMA (SMC-R) and RoCE
 *
 *  Basic Transport Functions exploiting Infiniband API
 *
 *  Copyright IBM Corp. 2016
 *
 *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
 */

#include <linux/socket.h>
#include <linux/if_vlan.h>
#include <linux/random.h>
#include <linux/workqueue.h>
16
#include <linux/wait.h>
17
#include <linux/reboot.h>
18 19 20
#include <net/tcp.h>
#include <net/sock.h>
#include <rdma/ib_verbs.h>
21
#include <rdma/ib_cache.h>
22 23 24 25 26

#include "smc.h"
#include "smc_clc.h"
#include "smc_core.h"
#include "smc_ib.h"
27
#include "smc_wr.h"
U
Ursula Braun 已提交
28
#include "smc_llc.h"
29
#include "smc_cdc.h"
30
#include "smc_close.h"
31
#include "smc_ism.h"
32

33 34
#define SMC_LGR_NUM_INCR		256
#define SMC_LGR_FREE_DELAY_SERV		(600 * HZ)
35
#define SMC_LGR_FREE_DELAY_CLNT		(SMC_LGR_FREE_DELAY_SERV + 10 * HZ)
36
#define SMC_LGR_FREE_DELAY_FAST		(8 * HZ)
37

38 39 40 41 42
static struct smc_lgr_list smc_lgr_list = {	/* established link groups */
	.lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
	.list = LIST_HEAD_INIT(smc_lgr_list.list),
	.num = 0,
};
U
Ursula Braun 已提交
43

44
static atomic_t lgr_cnt = ATOMIC_INIT(0); /* number of existing link groups */
45 46
static DECLARE_WAIT_QUEUE_HEAD(lgrs_deleted);

47 48
static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
			 struct smc_buf_desc *buf_desc);
49
static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft);
50

51 52 53 54 55 56 57 58 59 60 61 62 63
/* return head of link group list and its lock for a given link group */
static inline struct list_head *smc_lgr_list_head(struct smc_link_group *lgr,
						  spinlock_t **lgr_lock)
{
	if (lgr->is_smcd) {
		*lgr_lock = &lgr->smcd->lgr_lock;
		return &lgr->smcd->lgr_list;
	}

	*lgr_lock = &smc_lgr_list.lock;
	return &smc_lgr_list.list;
}

64 65 66 67 68 69
static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
{
	/* client link group creation always follows the server link group
	 * creation. For client use a somewhat higher removal delay time,
	 * otherwise there is a risk of out-of-sync link groups.
	 */
U
Ursula Braun 已提交
70 71 72 73 74 75
	if (!lgr->freeing && !lgr->freefast) {
		mod_delayed_work(system_wq, &lgr->free_work,
				 (!lgr->is_smcd && lgr->role == SMC_CLNT) ?
						SMC_LGR_FREE_DELAY_CLNT :
						SMC_LGR_FREE_DELAY_SERV);
	}
76 77
}

78 79
void smc_lgr_schedule_free_work_fast(struct smc_link_group *lgr)
{
U
Ursula Braun 已提交
80 81 82 83 84
	if (!lgr->freeing && !lgr->freefast) {
		lgr->freefast = 1;
		mod_delayed_work(system_wq, &lgr->free_work,
				 SMC_LGR_FREE_DELAY_FAST);
	}
85 86
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* Register connection's alert token in our lookup structure.
 * To use rbtrees we have to implement our own insert core.
 * Requires @conns_lock
 * @smc		connection to register
 * Returns 0 on success, != otherwise.
 */
static void smc_lgr_add_alert_token(struct smc_connection *conn)
{
	struct rb_node **link, *parent = NULL;
	u32 token = conn->alert_token_local;

	link = &conn->lgr->conns_all.rb_node;
	while (*link) {
		struct smc_connection *cur = rb_entry(*link,
					struct smc_connection, alert_node);

		parent = *link;
		if (cur->alert_token_local > token)
			link = &parent->rb_left;
		else
			link = &parent->rb_right;
	}
	/* Put the new node there */
	rb_link_node(&conn->alert_node, parent, link);
	rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
}

/* Register connection in link group by assigning an alert token
 * registered in a search tree.
 * Requires @conns_lock
 * Note that '0' is a reserved value and not assigned.
 */
119
static int smc_lgr_register_conn(struct smc_connection *conn)
120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
	static atomic_t nexttoken = ATOMIC_INIT(0);

	/* find a new alert_token_local value not yet used by some connection
	 * in this link group
	 */
	sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
	while (!conn->alert_token_local) {
		conn->alert_token_local = atomic_inc_return(&nexttoken);
		if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
			conn->alert_token_local = 0;
	}
	smc_lgr_add_alert_token(conn);
134 135

	/* assign the new connection to a link */
136 137 138
	if (!conn->lgr->is_smcd) {
		struct smc_link *lnk;
		int i;
139

140 141 142 143 144 145 146 147 148 149
		/* tbd - link balancing */
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
			lnk = &conn->lgr->lnk[i];
			if (lnk->state == SMC_LNK_ACTIVATING ||
			    lnk->state == SMC_LNK_ACTIVE)
				conn->lnk = lnk;
		}
		if (!conn->lnk)
			return SMC_CLC_DECL_NOACTLINK;
	}
150
	conn->lgr->conns_num++;
151
	return 0;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

/* Unregister connection and reset the alert token of the given connection<
 */
static void __smc_lgr_unregister_conn(struct smc_connection *conn)
{
	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
	struct smc_link_group *lgr = conn->lgr;

	rb_erase(&conn->alert_node, &lgr->conns_all);
	lgr->conns_num--;
	conn->alert_token_local = 0;
	sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
}

167
/* Unregister connection from lgr
168 169 170 171 172
 */
static void smc_lgr_unregister_conn(struct smc_connection *conn)
{
	struct smc_link_group *lgr = conn->lgr;

173 174
	if (!lgr)
		return;
175 176 177 178 179
	write_lock_bh(&lgr->conns_lock);
	if (conn->alert_token_local) {
		__smc_lgr_unregister_conn(conn);
	}
	write_unlock_bh(&lgr->conns_lock);
180
	conn->lgr = NULL;
181 182
}

183 184 185 186 187 188 189 190 191 192 193 194
void smc_lgr_cleanup_early(struct smc_connection *conn)
{
	struct smc_link_group *lgr = conn->lgr;

	if (!lgr)
		return;

	smc_conn_free(conn);
	smc_lgr_forget(lgr);
	smc_lgr_schedule_free_work_fast(lgr);
}

195 196 197 198
/* Send delete link, either as client to request the initiation
 * of the DELETE LINK sequence from server; or as server to
 * initiate the delete processing. See smc_llc_rx_delete_link().
 */
199
static int smcr_link_send_delete(struct smc_link *lnk, bool orderly)
200 201
{
	if (lnk->state == SMC_LNK_ACTIVE &&
202
	    !smc_llc_send_delete_link(lnk, SMC_LLC_REQ, orderly)) {
203 204 205 206 207 208
		smc_llc_link_deleting(lnk);
		return 0;
	}
	return -ENOTCONN;
}

U
Ursula Braun 已提交
209 210
static void smc_lgr_free(struct smc_link_group *lgr);

211 212 213 214 215
static void smc_lgr_free_work(struct work_struct *work)
{
	struct smc_link_group *lgr = container_of(to_delayed_work(work),
						  struct smc_link_group,
						  free_work);
216
	spinlock_t *lgr_lock;
217
	bool conns;
218
	int i;
219

220 221
	smc_lgr_list_head(lgr, &lgr_lock);
	spin_lock_bh(lgr_lock);
U
Ursula Braun 已提交
222 223 224 225
	if (lgr->freeing) {
		spin_unlock_bh(lgr_lock);
		return;
	}
226 227 228 229
	read_lock_bh(&lgr->conns_lock);
	conns = RB_EMPTY_ROOT(&lgr->conns_all);
	read_unlock_bh(&lgr->conns_lock);
	if (!conns) { /* number of lgr connections is no longer zero */
230
		spin_unlock_bh(lgr_lock);
231 232
		return;
	}
233
	list_del_init(&lgr->list); /* remove from smc_lgr_list */
234 235

	if (!lgr->is_smcd && !lgr->terminating)	{
236 237 238 239 240 241 242 243 244 245 246 247 248
		bool do_wait = false;

		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
			struct smc_link *lnk = &lgr->lnk[i];
			/* try to send del link msg, on err free immediately */
			if (lnk->state == SMC_LNK_ACTIVE &&
			    !smcr_link_send_delete(lnk, true)) {
				/* reschedule in case we never receive a resp */
				smc_lgr_schedule_free_work(lgr);
				do_wait = true;
			}
		}
		if (do_wait) {
U
Ursula Braun 已提交
249
			spin_unlock_bh(lgr_lock);
250
			return; /* wait for resp, see smc_llc_rx_delete_link */
251 252
		}
	}
U
Ursula Braun 已提交
253 254 255
	lgr->freeing = 1; /* this instance does the freeing, no new schedule */
	spin_unlock_bh(lgr_lock);
	cancel_delayed_work(&lgr->free_work);
256

257
	if (lgr->is_smcd && !lgr->terminating)
U
Ursula Braun 已提交
258
		smc_ism_signal_shutdown(lgr);
259 260 261 262
	if (!lgr->is_smcd) {
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
			struct smc_link *lnk = &lgr->lnk[i];

263
			if (smc_link_usable(lnk))
264 265 266
				smc_llc_link_inactive(lnk);
		}
	}
U
Ursula Braun 已提交
267
	smc_lgr_free(lgr);
268 269
}

270 271 272 273 274
static void smc_lgr_terminate_work(struct work_struct *work)
{
	struct smc_link_group *lgr = container_of(work, struct smc_link_group,
						  terminate_work);

275
	__smc_lgr_terminate(lgr, true);
276 277
}

278 279 280 281 282 283 284 285 286 287 288
/* return next unique link id for the lgr */
static u8 smcr_next_link_id(struct smc_link_group *lgr)
{
	u8 link_id;
	int i;

	while (1) {
		link_id = ++lgr->next_link_id;
		if (!link_id)	/* skip zero as link_id */
			link_id = ++lgr->next_link_id;
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
289
			if (smc_link_usable(&lgr->lnk[i]) &&
290 291 292 293 294 295 296 297 298 299
			    lgr->lnk[i].link_id == link_id)
				continue;
		}
		break;
	}
	return link_id;
}

static int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
			  u8 link_idx, struct smc_init_info *ini)
300 301 302 303 304 305 306
{
	u8 rndvec[3];
	int rc;

	get_device(&ini->ib_dev->ibdev->dev);
	atomic_inc(&ini->ib_dev->lnk_cnt);
	lnk->state = SMC_LNK_ACTIVATING;
307
	lnk->link_id = smcr_next_link_id(lgr);
308
	lnk->lgr = lgr;
309
	lnk->link_idx = link_idx;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	lnk->smcibdev = ini->ib_dev;
	lnk->ibport = ini->ib_port;
	lnk->path_mtu = ini->ib_dev->pattr[ini->ib_port - 1].active_mtu;
	if (!ini->ib_dev->initialized) {
		rc = (int)smc_ib_setup_per_ibdev(ini->ib_dev);
		if (rc)
			goto out;
	}
	get_random_bytes(rndvec, sizeof(rndvec));
	lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
		(rndvec[2] << 16);
	rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
				  ini->vlan_id, lnk->gid, &lnk->sgid_index);
	if (rc)
		goto out;
	rc = smc_llc_link_init(lnk);
	if (rc)
		goto out;
	rc = smc_wr_alloc_link_mem(lnk);
	if (rc)
		goto clear_llc_lnk;
	rc = smc_ib_create_protection_domain(lnk);
	if (rc)
		goto free_link_mem;
	rc = smc_ib_create_queue_pair(lnk);
	if (rc)
		goto dealloc_pd;
	rc = smc_wr_create_link(lnk);
	if (rc)
		goto destroy_qp;
	return 0;

destroy_qp:
	smc_ib_destroy_queue_pair(lnk);
dealloc_pd:
	smc_ib_dealloc_protection_domain(lnk);
free_link_mem:
	smc_wr_free_link_mem(lnk);
clear_llc_lnk:
	smc_llc_link_clear(lnk);
out:
	put_device(&ini->ib_dev->ibdev->dev);
	memset(lnk, 0, sizeof(struct smc_link));
353
	lnk->state = SMC_LNK_UNUSED;
354 355 356 357 358
	if (!atomic_dec_return(&ini->ib_dev->lnk_cnt))
		wake_up(&ini->ib_dev->lnks_deleted);
	return rc;
}

359
/* create a new SMC link group */
360
static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
361 362
{
	struct smc_link_group *lgr;
363
	struct list_head *lgr_list;
364
	struct smc_link *lnk;
365
	spinlock_t *lgr_lock;
366
	u8 link_idx;
367
	int rc = 0;
U
Ursula Braun 已提交
368
	int i;
369

370
	if (ini->is_smcd && ini->vlan_id) {
371 372
		if (smc_ism_get_vlan(ini->ism_dev, ini->vlan_id)) {
			rc = SMC_CLC_DECL_ISMVLANERR;
373
			goto out;
374
		}
375 376
	}

377 378
	lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
	if (!lgr) {
379
		rc = SMC_CLC_DECL_MEM;
380
		goto ism_put_vlan;
381
	}
382
	lgr->is_smcd = ini->is_smcd;
383
	lgr->sync_err = 0;
U
Ursula Braun 已提交
384 385 386
	lgr->terminating = 0;
	lgr->freefast = 0;
	lgr->freeing = 0;
387
	lgr->vlan_id = ini->vlan_id;
U
Ursula Braun 已提交
388 389
	rwlock_init(&lgr->sndbufs_lock);
	rwlock_init(&lgr->rmbs_lock);
390
	rwlock_init(&lgr->conns_lock);
U
Ursula Braun 已提交
391 392 393 394
	for (i = 0; i < SMC_RMBE_SIZES; i++) {
		INIT_LIST_HEAD(&lgr->sndbufs[i]);
		INIT_LIST_HEAD(&lgr->rmbs[i]);
	}
395
	lgr->next_link_id = 0;
396 397
	smc_lgr_list.num += SMC_LGR_NUM_INCR;
	memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
398
	INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
399
	INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work);
400
	lgr->conns_all = RB_ROOT;
401
	if (ini->is_smcd) {
402
		/* SMC-D specific settings */
403
		get_device(&ini->ism_dev->dev);
404 405
		lgr->peer_gid = ini->ism_gid;
		lgr->smcd = ini->ism_dev;
406
		lgr_list = &ini->ism_dev->lgr_list;
407
		lgr_lock = &lgr->smcd->lgr_lock;
408
		lgr->peer_shutdown = 0;
409
		atomic_inc(&ini->ism_dev->lgr_cnt);
410 411 412
	} else {
		/* SMC-R specific settings */
		lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
413 414
		memcpy(lgr->peer_systemid, ini->ib_lcl->id_for_peer,
		       SMC_SYSTEMID_LEN);
415

416 417 418
		link_idx = SMC_SINGLE_LINK;
		lnk = &lgr->lnk[link_idx];
		rc = smcr_link_init(lgr, lnk, link_idx, ini);
419 420
		if (rc)
			goto free_lgr;
421 422
		lgr_list = &smc_lgr_list.list;
		lgr_lock = &smc_lgr_list.lock;
423
		atomic_inc(&lgr_cnt);
424
	}
425
	smc->conn.lgr = lgr;
426
	spin_lock_bh(lgr_lock);
427
	list_add(&lgr->list, lgr_list);
428
	spin_unlock_bh(lgr_lock);
429 430 431 432
	return 0;

free_lgr:
	kfree(lgr);
433 434 435
ism_put_vlan:
	if (ini->is_smcd && ini->vlan_id)
		smc_ism_put_vlan(ini->ism_dev, ini->vlan_id);
436
out:
437 438 439 440 441 442
	if (rc < 0) {
		if (rc == -ENOMEM)
			rc = SMC_CLC_DECL_MEM;
		else
			rc = SMC_CLC_DECL_INTERR;
	}
443 444 445
	return rc;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static void smcr_buf_unuse(struct smc_buf_desc *rmb_desc,
			   struct smc_link *lnk)
{
	struct smc_link_group *lgr = lnk->lgr;

	if (rmb_desc->is_conf_rkey && !list_empty(&lgr->list)) {
		/* unregister rmb with peer */
		smc_llc_do_delete_rkey(lnk, rmb_desc);
		rmb_desc->is_conf_rkey = false;
	}
	if (rmb_desc->is_reg_err) {
		/* buf registration failed, reuse not possible */
		write_lock_bh(&lgr->rmbs_lock);
		list_del(&rmb_desc->list);
		write_unlock_bh(&lgr->rmbs_lock);

		smc_buf_free(lgr, true, rmb_desc);
	} else {
		rmb_desc->used = 0;
	}
}

468 469
static void smc_buf_unuse(struct smc_connection *conn,
			  struct smc_link_group *lgr)
U
Ursula Braun 已提交
470
{
471
	if (conn->sndbuf_desc)
U
Ursula Braun 已提交
472
		conn->sndbuf_desc->used = 0;
473 474 475 476
	if (conn->rmb_desc && lgr->is_smcd)
		conn->rmb_desc->used = 0;
	else if (conn->rmb_desc)
		smcr_buf_unuse(conn->rmb_desc, conn->lnk);
U
Ursula Braun 已提交
477 478
}

479 480 481
/* remove a finished connection from its link group */
void smc_conn_free(struct smc_connection *conn)
{
482 483 484
	struct smc_link_group *lgr = conn->lgr;

	if (!lgr)
485
		return;
486
	if (lgr->is_smcd) {
487 488
		if (!list_empty(&lgr->list))
			smc_ism_unset_conn(conn);
489 490
		tasklet_kill(&conn->rx_tsklet);
	} else {
491
		smc_cdc_tx_dismiss_slots(conn);
492
	}
493 494 495 496
	if (!list_empty(&lgr->list)) {
		smc_lgr_unregister_conn(conn);
		smc_buf_unuse(conn, lgr); /* allow buffer reuse */
	}
497 498 499

	if (!lgr->conns_num)
		smc_lgr_schedule_free_work(lgr);
500 501
}

502
static void smcr_link_clear(struct smc_link *lnk)
503
{
504 505
	struct smc_ib_device *smcibdev;

506 507
	if (lnk->peer_qpn == 0)
		return;
508
	lnk->peer_qpn = 0;
509
	smc_llc_link_clear(lnk);
510
	smc_ib_modify_qp_reset(lnk);
511
	smc_wr_free_link(lnk);
512 513
	smc_ib_destroy_queue_pair(lnk);
	smc_ib_dealloc_protection_domain(lnk);
514
	smc_wr_free_link_mem(lnk);
515
	put_device(&lnk->smcibdev->ibdev->dev);
516 517 518 519 520
	smcibdev = lnk->smcibdev;
	memset(lnk, 0, sizeof(struct smc_link));
	lnk->state = SMC_LNK_UNUSED;
	if (!atomic_dec_return(&smcibdev->lnk_cnt))
		wake_up(&smcibdev->lnks_deleted);
521 522
}

523 524
static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
			  struct smc_buf_desc *buf_desc)
U
Ursula Braun 已提交
525
{
526 527
	struct smc_link *lnk;
	int i;
528

529 530 531 532 533 534 535 536 537 538 539 540 541
	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
		lnk = &lgr->lnk[i];
		if (!buf_desc->is_map_ib[lnk->link_idx])
			continue;
		if (is_rmb) {
			if (buf_desc->mr_rx[lnk->link_idx])
				smc_ib_put_memory_region(
						buf_desc->mr_rx[lnk->link_idx]);
			smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE);
		} else {
			smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE);
		}
		sg_free_table(&buf_desc->sgt[lnk->link_idx]);
U
Ursula Braun 已提交
542
	}
543

544 545
	if (buf_desc->pages)
		__free_pages(buf_desc->pages, buf_desc->order);
546
	kfree(buf_desc);
U
Ursula Braun 已提交
547 548
}

549 550 551
static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
			  struct smc_buf_desc *buf_desc)
{
552 553 554
	if (is_dmb) {
		/* restore original buf len */
		buf_desc->len += sizeof(struct smcd_cdc_msg);
555
		smc_ism_unregister_dmb(lgr->smcd, buf_desc);
556
	} else {
557
		kfree(buf_desc->cpu_addr);
558
	}
559 560 561 562 563 564 565 566 567 568 569 570
	kfree(buf_desc);
}

static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
			 struct smc_buf_desc *buf_desc)
{
	if (lgr->is_smcd)
		smcd_buf_free(lgr, is_rmb, buf_desc);
	else
		smcr_buf_free(lgr, is_rmb, buf_desc);
}

571
static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
U
Ursula Braun 已提交
572
{
573 574
	struct smc_buf_desc *buf_desc, *bf_desc;
	struct list_head *buf_list;
U
Ursula Braun 已提交
575 576 577
	int i;

	for (i = 0; i < SMC_RMBE_SIZES; i++) {
578 579 580 581 582
		if (is_rmb)
			buf_list = &lgr->rmbs[i];
		else
			buf_list = &lgr->sndbufs[i];
		list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
U
Ursula Braun 已提交
583
					 list) {
584
			list_del(&buf_desc->list);
585
			smc_buf_free(lgr, is_rmb, buf_desc);
U
Ursula Braun 已提交
586 587 588 589
		}
	}
}

590 591 592 593 594 595 596 597
static void smc_lgr_free_bufs(struct smc_link_group *lgr)
{
	/* free send buffers */
	__smc_lgr_free_bufs(lgr, false);
	/* free rmbs */
	__smc_lgr_free_bufs(lgr, true);
}

598
/* remove a link group */
U
Ursula Braun 已提交
599
static void smc_lgr_free(struct smc_link_group *lgr)
600
{
601 602
	int i;

603
	smc_lgr_free_bufs(lgr);
604
	if (lgr->is_smcd) {
605 606 607 608
		if (!lgr->terminating) {
			smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
			put_device(&lgr->smcd->dev);
		}
609 610
		if (!atomic_dec_return(&lgr->smcd->lgr_cnt))
			wake_up(&lgr->smcd->lgrs_deleted);
611
	} else {
612
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
613 614
			if (lgr->lnk[i].state != SMC_LNK_UNUSED)
				smcr_link_clear(&lgr->lnk[i]);
615
		}
616 617
		if (!atomic_dec_return(&lgr_cnt))
			wake_up(&lgrs_deleted);
618
	}
619 620 621
	kfree(lgr);
}

622 623
void smc_lgr_forget(struct smc_link_group *lgr)
{
624 625 626 627 628
	struct list_head *lgr_list;
	spinlock_t *lgr_lock;

	lgr_list = smc_lgr_list_head(lgr, &lgr_lock);
	spin_lock_bh(lgr_lock);
629
	/* do not use this link group for new connections */
630 631 632
	if (!list_empty(lgr_list))
		list_del_init(lgr_list);
	spin_unlock_bh(lgr_lock);
633 634
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648
static void smcd_unregister_all_dmbs(struct smc_link_group *lgr)
{
	int i;

	for (i = 0; i < SMC_RMBE_SIZES; i++) {
		struct smc_buf_desc *buf_desc;

		list_for_each_entry(buf_desc, &lgr->rmbs[i], list) {
			buf_desc->len += sizeof(struct smcd_cdc_msg);
			smc_ism_unregister_dmb(lgr->smcd, buf_desc);
		}
	}
}

649 650 651 652 653 654 655 656
static void smc_sk_wake_ups(struct smc_sock *smc)
{
	smc->sk.sk_write_space(&smc->sk);
	smc->sk.sk_data_ready(&smc->sk);
	smc->sk.sk_state_change(&smc->sk);
}

/* kill a connection */
657
static void smc_conn_kill(struct smc_connection *conn, bool soft)
658 659 660
{
	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);

661 662 663 664
	if (conn->lgr->is_smcd && conn->lgr->peer_shutdown)
		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
	else
		smc_close_abort(conn);
665
	conn->killed = 1;
666
	smc->sk.sk_err = ECONNABORTED;
667
	smc_sk_wake_ups(smc);
668 669
	if (conn->lgr->is_smcd) {
		smc_ism_unset_conn(conn);
670 671 672 673
		if (soft)
			tasklet_kill(&conn->rx_tsklet);
		else
			tasklet_unlock_wait(&conn->rx_tsklet);
674 675
	} else {
		smc_cdc_tx_dismiss_slots(conn);
676
	}
677
	smc_lgr_unregister_conn(conn);
U
Ursula Braun 已提交
678
	smc_close_active_abort(smc);
679 680
}

681 682
static void smc_lgr_cleanup(struct smc_link_group *lgr)
{
683 684
	int i;

685 686 687 688 689 690
	if (lgr->is_smcd) {
		smc_ism_signal_shutdown(lgr);
		smcd_unregister_all_dmbs(lgr);
		smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
		put_device(&lgr->smcd->dev);
	} else {
691 692
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
			struct smc_link *lnk = &lgr->lnk[i];
693

694
			if (smc_link_usable(lnk))
695 696
				smc_llc_link_inactive(lnk);
		}
697 698 699
	}
}

700 701 702 703
/* terminate link group
 * @soft: true if link group shutdown can take its time
 *	  false if immediate link group shutdown is required
 */
704
static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
705 706
{
	struct smc_connection *conn;
707
	struct smc_sock *smc;
708
	struct rb_node *node;
709
	int i;
710

711 712
	if (lgr->terminating)
		return;	/* lgr already terminating */
713 714
	if (!soft)
		cancel_delayed_work_sync(&lgr->free_work);
715
	lgr->terminating = 1;
716
	if (!lgr->is_smcd)
717 718
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
			smc_llc_link_inactive(&lgr->lnk[i]);
719

720 721
	/* kill remaining link group connections */
	read_lock_bh(&lgr->conns_lock);
722 723
	node = rb_first(&lgr->conns_all);
	while (node) {
724
		read_unlock_bh(&lgr->conns_lock);
725
		conn = rb_entry(node, struct smc_connection, alert_node);
726
		smc = container_of(conn, struct smc_sock, conn);
U
Ursula Braun 已提交
727
		sock_hold(&smc->sk); /* sock_put below */
728
		lock_sock(&smc->sk);
729
		smc_conn_kill(conn, soft);
730
		release_sock(&smc->sk);
U
Ursula Braun 已提交
731
		sock_put(&smc->sk); /* sock_hold above */
732
		read_lock_bh(&lgr->conns_lock);
733 734
		node = rb_first(&lgr->conns_all);
	}
735
	read_unlock_bh(&lgr->conns_lock);
736
	smc_lgr_cleanup(lgr);
737 738 739 740
	if (soft)
		smc_lgr_schedule_free_work_fast(lgr);
	else
		smc_lgr_free(lgr);
741 742
}

743 744
/* unlink link group and schedule termination */
void smc_lgr_terminate_sched(struct smc_link_group *lgr)
745
{
746 747 748 749
	spinlock_t *lgr_lock;

	smc_lgr_list_head(lgr, &lgr_lock);
	spin_lock_bh(lgr_lock);
750
	if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) {
751 752 753 754
		spin_unlock_bh(lgr_lock);
		return;	/* lgr already terminating */
	}
	list_del_init(&lgr->list);
755
	spin_unlock_bh(lgr_lock);
756
	schedule_work(&lgr->terminate_work);
757 758
}

759 760 761 762
/* Called when IB port is terminated */
void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport)
{
	struct smc_link_group *lgr, *l;
763
	LIST_HEAD(lgr_free_list);
764
	int i;
765

766
	spin_lock_bh(&smc_lgr_list.lock);
767
	list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) {
768 769 770 771
		if (lgr->is_smcd)
			continue;
		/* tbd - terminate only when no more links are active */
		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
772
			if (!smc_link_usable(&lgr->lnk[i]) ||
773 774 775 776 777 778 779
			    lgr->lnk[i].state == SMC_LNK_DELETING)
				continue;
			if (lgr->lnk[i].smcibdev == smcibdev &&
			    lgr->lnk[i].ibport == ibport) {
				list_move(&lgr->list, &lgr_free_list);
				lgr->freeing = 1;
			}
780
		}
781
	}
782
	spin_unlock_bh(&smc_lgr_list.lock);
783 784 785

	list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
		list_del_init(&lgr->list);
786
		__smc_lgr_terminate(lgr, false);
787
	}
788 789
}

790
/* Called when peer lgr shutdown (regularly or abnormally) is received */
H
Hans Wippel 已提交
791
void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan)
792 793 794 795 796
{
	struct smc_link_group *lgr, *l;
	LIST_HEAD(lgr_free_list);

	/* run common cleanup function and build free list */
797
	spin_lock_bh(&dev->lgr_lock);
798 799
	list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) {
		if ((!peer_gid || lgr->peer_gid == peer_gid) &&
H
Hans Wippel 已提交
800
		    (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) {
801 802
			if (peer_gid) /* peer triggered termination */
				lgr->peer_shutdown = 1;
803 804 805
			list_move(&lgr->list, &lgr_free_list);
		}
	}
806
	spin_unlock_bh(&dev->lgr_lock);
807 808 809 810

	/* cancel the regular free workers and actually free lgrs */
	list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
		list_del_init(&lgr->list);
811
		schedule_work(&lgr->terminate_work);
812 813 814
	}
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/* Called when an SMCD device is removed or the smc module is unloaded */
void smc_smcd_terminate_all(struct smcd_dev *smcd)
{
	struct smc_link_group *lgr, *lg;
	LIST_HEAD(lgr_free_list);

	spin_lock_bh(&smcd->lgr_lock);
	list_splice_init(&smcd->lgr_list, &lgr_free_list);
	list_for_each_entry(lgr, &lgr_free_list, list)
		lgr->freeing = 1;
	spin_unlock_bh(&smcd->lgr_lock);

	list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
		list_del_init(&lgr->list);
		__smc_lgr_terminate(lgr, false);
	}
831 832 833

	if (atomic_read(&smcd->lgr_cnt))
		wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt));
834 835
}

836 837 838 839 840 841 842 843
/* Called when an SMCR device is removed or the smc module is unloaded.
 * If smcibdev is given, all SMCR link groups using this device are terminated.
 * If smcibdev is NULL, all SMCR link groups are terminated.
 */
void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
{
	struct smc_link_group *lgr, *lg;
	LIST_HEAD(lgr_free_list);
844
	int i;
845 846 847 848 849 850 851 852

	spin_lock_bh(&smc_lgr_list.lock);
	if (!smcibdev) {
		list_splice_init(&smc_lgr_list.list, &lgr_free_list);
		list_for_each_entry(lgr, &lgr_free_list, list)
			lgr->freeing = 1;
	} else {
		list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
853 854 855 856 857 858
			for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
				if (lgr->lnk[i].smcibdev == smcibdev) {
					list_move(&lgr->list, &lgr_free_list);
					lgr->freeing = 1;
					break;
				}
859 860 861 862 863 864 865 866 867
			}
		}
	}
	spin_unlock_bh(&smc_lgr_list.lock);

	list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
		list_del_init(&lgr->list);
		__smc_lgr_terminate(lgr, false);
	}
868 869 870 871 872 873 874 875 876

	if (smcibdev) {
		if (atomic_read(&smcibdev->lnk_cnt))
			wait_event(smcibdev->lnks_deleted,
				   !atomic_read(&smcibdev->lnk_cnt));
	} else {
		if (atomic_read(&lgr_cnt))
			wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
	}
877 878
}

879 880 881
/* Determine vlan of internal TCP socket.
 * @vlan_id: address to store the determined vlan id into
 */
882
int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
883 884
{
	struct dst_entry *dst = sk_dst_get(clcsock->sk);
885 886
	struct net_device *ndev;
	int i, nest_lvl, rc = 0;
887

888
	ini->vlan_id = 0;
889 890 891 892 893 894 895 896 897
	if (!dst) {
		rc = -ENOTCONN;
		goto out;
	}
	if (!dst->dev) {
		rc = -ENODEV;
		goto out_rel;
	}

898 899
	ndev = dst->dev;
	if (is_vlan_dev(ndev)) {
900
		ini->vlan_id = vlan_dev_vlan_id(ndev);
901 902 903 904
		goto out_rel;
	}

	rtnl_lock();
905
	nest_lvl = ndev->lower_level;
906 907 908 909 910 911 912 913
	for (i = 0; i < nest_lvl; i++) {
		struct list_head *lower = &ndev->adj_list.lower;

		if (list_empty(lower))
			break;
		lower = lower->next;
		ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
		if (is_vlan_dev(ndev)) {
914
			ini->vlan_id = vlan_dev_vlan_id(ndev);
915 916 917 918
			break;
		}
	}
	rtnl_unlock();
919 920 921 922 923 924 925

out_rel:
	dst_release(dst);
out:
	return rc;
}

926 927
static bool smcr_lgr_match(struct smc_link_group *lgr,
			   struct smc_clc_msg_local *lcl,
928
			   enum smc_lgr_role role, u32 clcqpn)
929
{
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	int i;

	if (memcmp(lgr->peer_systemid, lcl->id_for_peer, SMC_SYSTEMID_LEN) ||
	    lgr->role != role)
		return false;

	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
		if (lgr->lnk[i].state != SMC_LNK_ACTIVE)
			continue;
		if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) &&
		    !memcmp(lgr->lnk[i].peer_gid, &lcl->gid, SMC_GID_SIZE) &&
		    !memcmp(lgr->lnk[i].peer_mac, lcl->mac, sizeof(lcl->mac)))
			return true;
	}
	return false;
945
}
946

947 948 949 950
static bool smcd_lgr_match(struct smc_link_group *lgr,
			   struct smcd_dev *smcismdev, u64 peer_gid)
{
	return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
951 952 953
}

/* create a new SMC connection (and a new link group if necessary) */
954
int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
955 956
{
	struct smc_connection *conn = &smc->conn;
957
	struct list_head *lgr_list;
958 959
	struct smc_link_group *lgr;
	enum smc_lgr_role role;
960
	spinlock_t *lgr_lock;
961 962
	int rc = 0;

963
	lgr_list = ini->is_smcd ? &ini->ism_dev->lgr_list : &smc_lgr_list.list;
964
	lgr_lock = ini->is_smcd ? &ini->ism_dev->lgr_lock : &smc_lgr_list.lock;
965
	ini->cln_first_contact = SMC_FIRST_CONTACT;
966
	role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
967
	if (role == SMC_CLNT && ini->srv_first_contact)
968 969 970 971
		/* create new link group as well */
		goto create;

	/* determine if an existing link group can be reused */
972
	spin_lock_bh(lgr_lock);
973
	list_for_each_entry(lgr, lgr_list, list) {
974
		write_lock_bh(&lgr->conns_lock);
975 976 977
		if ((ini->is_smcd ?
		     smcd_lgr_match(lgr, ini->ism_dev, ini->ism_gid) :
		     smcr_lgr_match(lgr, ini->ib_lcl, role, ini->ib_clcqpn)) &&
978
		    !lgr->sync_err &&
979
		    lgr->vlan_id == ini->vlan_id &&
980 981
		    (role == SMC_CLNT ||
		     lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
982
			/* link group found */
983
			ini->cln_first_contact = SMC_REUSE_CONTACT;
984
			conn->lgr = lgr;
985
			rc = smc_lgr_register_conn(conn); /* add conn to lgr */
986
			write_unlock_bh(&lgr->conns_lock);
987 988
			if (!rc && delayed_work_pending(&lgr->free_work))
				cancel_delayed_work(&lgr->free_work);
989 990 991 992
			break;
		}
		write_unlock_bh(&lgr->conns_lock);
	}
993
	spin_unlock_bh(lgr_lock);
994 995
	if (rc)
		return rc;
996

997
	if (role == SMC_CLNT && !ini->srv_first_contact &&
998
	    ini->cln_first_contact == SMC_FIRST_CONTACT) {
999 1000 1001 1002
		/* Server reuses a link group, but Client wants to start
		 * a new one
		 * send out_of_sync decline, reason synchr. error
		 */
1003
		return SMC_CLC_DECL_SYNCERR;
1004 1005 1006
	}

create:
1007
	if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
1008
		rc = smc_lgr_create(smc, ini);
1009 1010
		if (rc)
			goto out;
1011 1012
		lgr = conn->lgr;
		write_lock_bh(&lgr->conns_lock);
1013
		rc = smc_lgr_register_conn(conn); /* add smc conn to lgr */
1014
		write_unlock_bh(&lgr->conns_lock);
1015 1016
		if (rc)
			goto out;
1017
	}
1018
	conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1019
	conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
S
Stefan Raspl 已提交
1020
	conn->urg_state = SMC_URG_READ;
1021
	if (ini->is_smcd) {
1022 1023 1024
		conn->rx_off = sizeof(struct smcd_cdc_msg);
		smcd_cdc_rx_init(conn); /* init tasklet for this conn */
	}
1025 1026 1027
#ifndef KERNEL_HAS_ATOMIC64
	spin_lock_init(&conn->acurs_lock);
#endif
1028 1029

out:
1030
	return rc;
1031
}
U
Ursula Braun 已提交
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
/* convert the RMB size into the compressed notation - minimum 16K.
 * In contrast to plain ilog2, this rounds towards the next power of 2,
 * so the socket application gets at least its desired sndbuf / rcvbuf size.
 */
static u8 smc_compress_bufsize(int size)
{
	u8 compressed;

	if (size <= SMC_BUF_MIN_SIZE)
		return 0;

	size = (size - 1) >> 14;
	compressed = ilog2(size) + 1;
	if (compressed >= SMC_RMBE_SIZES)
		compressed = SMC_RMBE_SIZES - 1;
	return compressed;
}

/* convert the RMB size from compressed notation into integer */
int smc_uncompress_bufsize(u8 compressed)
{
	u32 size;

	size = 0x00000001 << (((int)compressed) + 14);
	return (int)size;
}

1060 1061
/* try to reuse a sndbuf or rmb description slot for a certain
 * buffer size; if not available, return NULL
U
Ursula Braun 已提交
1062
 */
1063 1064 1065
static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
					     rwlock_t *lock,
					     struct list_head *buf_list)
U
Ursula Braun 已提交
1066
{
1067
	struct smc_buf_desc *buf_slot;
U
Ursula Braun 已提交
1068

1069 1070 1071 1072 1073
	read_lock_bh(lock);
	list_for_each_entry(buf_slot, buf_list, list) {
		if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
			read_unlock_bh(lock);
			return buf_slot;
U
Ursula Braun 已提交
1074 1075
		}
	}
1076
	read_unlock_bh(lock);
U
Ursula Braun 已提交
1077 1078 1079
	return NULL;
}

U
Ursula Braun 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088
/* one of the conditions for announcing a receiver's current window size is
 * that it "results in a minimum increase in the window size of 10% of the
 * receive buffer space" [RFC7609]
 */
static inline int smc_rmb_wnd_update_limit(int rmbe_size)
{
	return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
/* map an rmb buf to a link */
static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
			     struct smc_link *lnk)
{
	int rc;

	if (buf_desc->is_map_ib[lnk->link_idx])
		return 0;

	rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL);
	if (rc)
		return rc;
	sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
		   buf_desc->cpu_addr, buf_desc->len);

	/* map sg table to DMA address */
	rc = smc_ib_buf_map_sg(lnk, buf_desc,
			       is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
	/* SMC protocol depends on mapping to one DMA address only */
	if (rc != 1) {
		rc = -EAGAIN;
		goto free_table;
	}

	/* create a new memory region for the RMB */
	if (is_rmb) {
		rc = smc_ib_get_memory_region(lnk->roce_pd,
					      IB_ACCESS_REMOTE_WRITE |
					      IB_ACCESS_LOCAL_WRITE,
					      buf_desc, lnk->link_idx);
		if (rc)
			goto buf_unmap;
		smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE);
	}
	buf_desc->is_map_ib[lnk->link_idx] = true;
	return 0;

buf_unmap:
	smc_ib_buf_unmap_sg(lnk, buf_desc,
			    is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
free_table:
	sg_free_table(&buf_desc->sgt[lnk->link_idx]);
	return rc;
}

1134 1135
static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
						bool is_rmb, int bufsize)
1136 1137 1138 1139 1140 1141 1142 1143
{
	struct smc_buf_desc *buf_desc;

	/* try to alloc a new buffer */
	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
	if (!buf_desc)
		return ERR_PTR(-ENOMEM);

1144 1145 1146 1147 1148 1149
	buf_desc->order = get_order(bufsize);
	buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
				      __GFP_NOMEMALLOC | __GFP_COMP |
				      __GFP_NORETRY | __GFP_ZERO,
				      buf_desc->order);
	if (!buf_desc->pages) {
1150 1151 1152
		kfree(buf_desc);
		return ERR_PTR(-EAGAIN);
	}
1153
	buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
1154 1155 1156
	buf_desc->len = bufsize;
	return buf_desc;
}
1157

1158 1159 1160 1161 1162 1163 1164
/* map buf_desc on all usable links,
 * unused buffers stay mapped as long as the link is up
 */
static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
				     struct smc_buf_desc *buf_desc, bool is_rmb)
{
	int i, rc = 0;
1165

1166 1167
	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
		struct smc_link *lnk = &lgr->lnk[i];
1168

1169
		if (!smc_link_usable(lnk))
1170 1171 1172 1173 1174
			continue;
		if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
			smcr_buf_unuse(buf_desc, lnk);
			rc = -ENOMEM;
			goto out;
1175 1176
		}
	}
1177 1178
out:
	return rc;
1179 1180
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
#define SMCD_DMBE_SIZES		7 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */

static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
						bool is_dmb, int bufsize)
{
	struct smc_buf_desc *buf_desc;
	int rc;

	if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES)
		return ERR_PTR(-EAGAIN);

	/* try to alloc a new DMB */
	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
	if (!buf_desc)
		return ERR_PTR(-ENOMEM);
	if (is_dmb) {
		rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
		if (rc) {
			kfree(buf_desc);
			return ERR_PTR(-EAGAIN);
		}
1202 1203 1204
		buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
		/* CDC header stored in buf. So, pretend it was smaller */
		buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	} else {
		buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
					     __GFP_NOWARN | __GFP_NORETRY |
					     __GFP_NOMEMALLOC);
		if (!buf_desc->cpu_addr) {
			kfree(buf_desc);
			return ERR_PTR(-EAGAIN);
		}
		buf_desc->len = bufsize;
	}
	return buf_desc;
}

static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
U
Ursula Braun 已提交
1219
{
1220
	struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
U
Ursula Braun 已提交
1221 1222
	struct smc_connection *conn = &smc->conn;
	struct smc_link_group *lgr = conn->lgr;
1223
	struct list_head *buf_list;
1224
	int bufsize, bufsize_short;
1225 1226
	int sk_buf_size;
	rwlock_t *lock;
U
Ursula Braun 已提交
1227

1228 1229 1230 1231 1232 1233 1234
	if (is_rmb)
		/* use socket recv buffer size (w/o overhead) as start value */
		sk_buf_size = smc->sk.sk_rcvbuf / 2;
	else
		/* use socket send buffer size (w/o overhead) as start value */
		sk_buf_size = smc->sk.sk_sndbuf / 2;

1235
	for (bufsize_short = smc_compress_bufsize(sk_buf_size);
1236
	     bufsize_short >= 0; bufsize_short--) {
1237

1238 1239 1240 1241 1242 1243
		if (is_rmb) {
			lock = &lgr->rmbs_lock;
			buf_list = &lgr->rmbs[bufsize_short];
		} else {
			lock = &lgr->sndbufs_lock;
			buf_list = &lgr->sndbufs[bufsize_short];
1244
		}
1245
		bufsize = smc_uncompress_bufsize(bufsize_short);
1246 1247 1248
		if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
			continue;

1249
		/* check for reusable slot in the link group */
1250
		buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
1251 1252
		if (buf_desc) {
			memset(buf_desc->cpu_addr, 0, bufsize);
U
Ursula Braun 已提交
1253 1254
			break; /* found reusable slot */
		}
1255

1256 1257 1258 1259 1260
		if (is_smcd)
			buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
		else
			buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);

1261 1262 1263
		if (PTR_ERR(buf_desc) == -ENOMEM)
			break;
		if (IS_ERR(buf_desc))
1264
			continue;
1265

1266 1267 1268 1269 1270
		buf_desc->used = 1;
		write_lock_bh(lock);
		list_add(&buf_desc->list, buf_list);
		write_unlock_bh(lock);
		break; /* found */
U
Ursula Braun 已提交
1271
	}
1272

1273
	if (IS_ERR(buf_desc))
1274 1275
		return -ENOMEM;

1276 1277 1278 1279 1280 1281
	if (!is_smcd) {
		if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
			return -ENOMEM;
		}
	}

1282 1283
	if (is_rmb) {
		conn->rmb_desc = buf_desc;
1284 1285
		conn->rmbe_size_short = bufsize_short;
		smc->sk.sk_rcvbuf = bufsize * 2;
1286
		atomic_set(&conn->bytes_to_rcv, 0);
1287 1288
		conn->rmbe_update_limit =
			smc_rmb_wnd_update_limit(buf_desc->len);
1289 1290
		if (is_smcd)
			smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
U
Ursula Braun 已提交
1291
	} else {
1292 1293 1294
		conn->sndbuf_desc = buf_desc;
		smc->sk.sk_sndbuf = bufsize * 2;
		atomic_set(&conn->sndbuf_space, bufsize);
U
Ursula Braun 已提交
1295
	}
1296 1297 1298
	return 0;
}

1299 1300
void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
{
1301
	if (!conn->lgr || conn->lgr->is_smcd || !smc_link_usable(conn->lnk))
1302
		return;
1303
	smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1304 1305 1306 1307
}

void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
{
1308
	if (!conn->lgr || conn->lgr->is_smcd || !smc_link_usable(conn->lnk))
1309
		return;
1310
	smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1311 1312 1313 1314
}

void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
{
1315
	int i;
1316

1317 1318
	if (!conn->lgr || conn->lgr->is_smcd)
		return;
1319
	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1320
		if (!smc_link_usable(&conn->lgr->lnk[i]))
1321 1322 1323 1324
			continue;
		smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
				       DMA_FROM_DEVICE);
	}
1325 1326 1327 1328
}

void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
{
1329
	int i;
1330

1331 1332
	if (!conn->lgr || conn->lgr->is_smcd)
		return;
1333
	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1334
		if (!smc_link_usable(&conn->lgr->lnk[i]))
1335 1336 1337 1338
			continue;
		smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc,
					  DMA_FROM_DEVICE);
	}
1339 1340
}

1341 1342 1343 1344 1345 1346
/* create the send and receive buffer for an SMC socket;
 * receive buffers are called RMBs;
 * (even though the SMC protocol allows more than one RMB-element per RMB,
 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
 * extra RMB for every connection in a link group
 */
1347
int smc_buf_create(struct smc_sock *smc, bool is_smcd)
1348 1349 1350 1351
{
	int rc;

	/* create send buffer */
1352
	rc = __smc_buf_create(smc, is_smcd, false);
1353 1354 1355
	if (rc)
		return rc;
	/* create rmb */
1356
	rc = __smc_buf_create(smc, is_smcd, true);
1357
	if (rc)
1358
		smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
1359
	return rc;
U
Ursula Braun 已提交
1360
}
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
{
	int i;

	for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
		if (!test_and_set_bit(i, lgr->rtokens_used_mask))
			return i;
	}
	return -ENOSPC;
}

1373
/* add a new rtoken from peer */
1374
int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
1375
{
1376
	struct smc_link_group *lgr = smc_get_lgr(lnk);
1377 1378
	u64 dma_addr = be64_to_cpu(nw_vaddr);
	u32 rkey = ntohl(nw_rkey);
1379 1380 1381
	int i;

	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1382 1383
		if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
		    lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
1384
		    test_bit(i, lgr->rtokens_used_mask)) {
1385 1386 1387 1388 1389 1390 1391
			/* already in list */
			return i;
		}
	}
	i = smc_rmb_reserve_rtoken_idx(lgr);
	if (i < 0)
		return i;
1392 1393
	lgr->rtokens[i][lnk->link_idx].rkey = rkey;
	lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
1394 1395 1396
	return i;
}

1397
/* delete an rtoken from all links */
1398
int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
1399
{
1400
	struct smc_link_group *lgr = smc_get_lgr(lnk);
1401
	u32 rkey = ntohl(nw_rkey);
1402
	int i, j;
1403 1404

	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1405
		if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1406
		    test_bit(i, lgr->rtokens_used_mask)) {
1407 1408 1409 1410
			for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
				lgr->rtokens[i][j].rkey = 0;
				lgr->rtokens[i][j].dma_addr = 0;
			}
1411
			clear_bit(i, lgr->rtokens_used_mask);
1412 1413 1414
			return 0;
		}
	}
1415 1416 1417 1418 1419
	return -ENOENT;
}

/* save rkey and dma_addr received from peer during clc handshake */
int smc_rmb_rtoken_handling(struct smc_connection *conn,
1420
			    struct smc_link *lnk,
1421 1422
			    struct smc_clc_msg_accept_confirm *clc)
{
1423
	conn->rtoken_idx = smc_rtoken_add(lnk, clc->rmb_dma_addr,
1424
					  clc->rmb_rkey);
1425 1426 1427 1428
	if (conn->rtoken_idx < 0)
		return conn->rtoken_idx;
	return 0;
}
1429

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
static void smc_core_going_away(void)
{
	struct smc_ib_device *smcibdev;
	struct smcd_dev *smcd;

	spin_lock(&smc_ib_devices.lock);
	list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
		int i;

		for (i = 0; i < SMC_MAX_PORTS; i++)
			set_bit(i, smcibdev->ports_going_away);
	}
	spin_unlock(&smc_ib_devices.lock);

	spin_lock(&smcd_dev_list.lock);
	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
		smcd->going_away = 1;
	}
	spin_unlock(&smcd_dev_list.lock);
}

1451 1452
/* Clean up all SMC link groups */
static void smc_lgrs_shutdown(void)
1453
{
1454
	struct smcd_dev *smcd;
1455

1456 1457
	smc_core_going_away();

1458
	smc_smcr_terminate_all(NULL);
1459 1460 1461

	spin_lock(&smcd_dev_list.lock);
	list_for_each_entry(smcd, &smcd_dev_list.list, list)
1462
		smc_smcd_terminate_all(smcd);
1463
	spin_unlock(&smcd_dev_list.lock);
1464
}
1465

1466 1467 1468 1469
static int smc_core_reboot_event(struct notifier_block *this,
				 unsigned long event, void *ptr)
{
	smc_lgrs_shutdown();
1470
	smc_ib_unregister_client();
1471 1472 1473 1474 1475 1476 1477
	return 0;
}

static struct notifier_block smc_reboot_notifier = {
	.notifier_call = smc_core_reboot_event,
};

1478 1479
int __init smc_core_init(void)
{
1480
	return register_reboot_notifier(&smc_reboot_notifier);
1481 1482
}

1483 1484 1485
/* Called (from smc_exit) when module is removed */
void smc_core_exit(void)
{
1486
	unregister_reboot_notifier(&smc_reboot_notifier);
1487 1488
	smc_lgrs_shutdown();
}