summaryrefslogtreecommitdiff
path: root/drivers/infiniband/sw/siw/siw_main.c
blob: 1ab62982df74cb4f6a006ac3f9d2ed16c3c217dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause

/* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
/* Copyright (c) 2008-2019, IBM Corporation */

#include <linux/init.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <net/net_namespace.h>
#include <linux/rtnetlink.h>
#include <linux/if_arp.h>
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/dma-mapping.h>

#include <net/addrconf.h>
#include <rdma/ib_verbs.h>
#include <rdma/ib_user_verbs.h>
#include <rdma/rdma_netlink.h>
#include <linux/kthread.h>

#include "siw.h"
#include "siw_verbs.h"

MODULE_AUTHOR("Bernard Metzler");
MODULE_DESCRIPTION("Software iWARP Driver");
MODULE_LICENSE("Dual BSD/GPL");

/* transmit from user buffer, if possible */
const bool zcopy_tx = true;

/* Restrict usage of GSO, if hardware peer iwarp is unable to process
 * large packets. try_gso = true lets siw try to use local GSO,
 * if peer agrees.  Not using GSO severly limits siw maximum tx bandwidth.
 */
const bool try_gso;

/* Attach siw also with loopback devices */
const bool loopback_enabled = true;

/* We try to negotiate CRC on, if true */
const bool mpa_crc_required;

/* MPA CRC on/off enforced */
const bool mpa_crc_strict;

/* Control TCP_NODELAY socket option */
const bool siw_tcp_nagle;

/* Select MPA version to be used during connection setup */
u_char mpa_version = MPA_REVISION_2;

/* Selects MPA P2P mode (additional handshake during connection
 * setup, if true.
 */
const bool peer_to_peer;

struct task_struct *siw_tx_thread[NR_CPUS];
struct crypto_shash *siw_crypto_shash;

static int siw_device_register(struct siw_device *sdev, const char *name)
{
	struct ib_device *base_dev = &sdev->base_dev;
	static int dev_id = 1;
	int rv;

	sdev->vendor_part_id = dev_id++;

	rv = ib_register_device(base_dev, name, NULL);
	if (rv) {
		pr_warn("siw: device registration error %d\n", rv);
		return rv;
	}

	siw_dbg(base_dev, "HWaddr=%pM\n", sdev->raw_gid);
	return 0;
}

static void siw_device_cleanup(struct ib_device *base_dev)
{
	struct siw_device *sdev = to_siw_dev(base_dev);

	xa_destroy(&sdev->qp_xa);
	xa_destroy(&sdev->mem_xa);
}

static int siw_dev_qualified(struct net_device *netdev)
{
	/*
	 * Additional hardware support can be added here
	 * (e.g. ARPHRD_FDDI, ARPHRD_ATM, ...) - see
	 * <linux/if_arp.h> for type identifiers.
	 */
	if (netdev->type == ARPHRD_ETHER || netdev->type == ARPHRD_IEEE802 ||
	    netdev->type == ARPHRD_NONE ||
	    (netdev->type == ARPHRD_LOOPBACK && loopback_enabled))
		return 1;

	return 0;
}

static DEFINE_PER_CPU(atomic_t, siw_use_cnt);

static struct {
	struct cpumask **tx_valid_cpus;
	int num_nodes;
} siw_cpu_info;

static int siw_init_cpulist(void)
{
	int i, num_nodes = nr_node_ids;

	memset(siw_tx_thread, 0, sizeof(siw_tx_thread));

	siw_cpu_info.num_nodes = num_nodes;

	siw_cpu_info.tx_valid_cpus =
		kcalloc(num_nodes, sizeof(struct cpumask *), GFP_KERNEL);
	if (!siw_cpu_info.tx_valid_cpus) {
		siw_cpu_info.num_nodes = 0;
		return -ENOMEM;
	}
	for (i = 0; i < siw_cpu_info.num_nodes; i++) {
		siw_cpu_info.tx_valid_cpus[i] =
			kzalloc(sizeof(struct cpumask), GFP_KERNEL);
		if (!siw_cpu_info.tx_valid_cpus[i])
			goto out_err;

		cpumask_clear(siw_cpu_info.tx_valid_cpus[i]);
	}
	for_each_possible_cpu(i)
		cpumask_set_cpu(i, siw_cpu_info.tx_valid_cpus[cpu_to_node(i)]);

	return 0;

out_err:
	siw_cpu_info.num_nodes = 0;
	while (--i >= 0)
		kfree(siw_cpu_info.tx_valid_cpus[i]);
	kfree(siw_cpu_info.tx_valid_cpus);
	siw_cpu_info.tx_valid_cpus = NULL;

	return -ENOMEM;
}

static void siw_destroy_cpulist(void)
{
	int i = 0;

	while (i < siw_cpu_info.num_nodes)
		kfree(siw_cpu_info.tx_valid_cpus[i++]);

	kfree(siw_cpu_info.tx_valid_cpus);
}

/*
 * Choose CPU with least number of active QP's from NUMA node of
 * TX interface.
 */
int siw_get_tx_cpu(struct siw_device *sdev)
{
	const struct cpumask *tx_cpumask;
	int i, num_cpus, cpu, min_use, node = sdev->numa_node, tx_cpu = -1;

	if (node < 0)
		tx_cpumask = cpu_online_mask;
	else
		tx_cpumask = siw_cpu_info.tx_valid_cpus[node];

	num_cpus = cpumask_weight(tx_cpumask);
	if (!num_cpus) {
		/* no CPU on this NUMA node */
		tx_cpumask = cpu_online_mask;
		num_cpus = cpumask_weight(tx_cpumask);
	}
	if (!num_cpus)
		goto out;

	cpu = cpumask_first(tx_cpumask);

	for (i = 0, min_use = SIW_MAX_QP; i < num_cpus;
	     i++, cpu = cpumask_next(cpu, tx_cpumask)) {
		int usage;

		/* Skip any cores which have no TX thread */
		if (!siw_tx_thread[cpu])
			continue;

		usage = atomic_read(&per_cpu(siw_use_cnt, cpu));
		if (usage <= min_use) {
			tx_cpu = cpu;
			min_use = usage;
		}
	}
	siw_dbg(&sdev->base_dev,
		"tx cpu %d, node %d, %d qp's\n", tx_cpu, node, min_use);

out:
	if (tx_cpu >= 0)
		atomic_inc(&per_cpu(siw_use_cnt, tx_cpu));
	else
		pr_warn("siw: no tx cpu found\n");

	return tx_cpu;
}

void siw_put_tx_cpu(int cpu)
{
	atomic_dec(&per_cpu(siw_use_cnt, cpu));
}

static struct ib_qp *siw_get_base_qp(struct ib_device *base_dev, int id)
{
	struct siw_qp *qp = siw_qp_id2obj(to_siw_dev(base_dev), id);

	if (qp) {
		/*
		 * siw_qp_id2obj() increments object reference count
		 */
		siw_qp_put(qp);
		return &qp->base_qp;
	}
	return NULL;
}

static const struct ib_device_ops siw_device_ops = {
	.owner = THIS_MODULE,
	.uverbs_abi_ver = SIW_ABI_VERSION,
	.driver_id = RDMA_DRIVER_SIW,

	.alloc_mr = siw_alloc_mr,
	.alloc_pd = siw_alloc_pd,
	.alloc_ucontext = siw_alloc_ucontext,
	.create_cq = siw_create_cq,
	.create_qp = siw_create_qp,
	.create_srq = siw_create_srq,
	.dealloc_driver = siw_device_cleanup,
	.dealloc_pd = siw_dealloc_pd,
	.dealloc_ucontext = siw_dealloc_ucontext,
	.dereg_mr = siw_dereg_mr,
	.destroy_cq = siw_destroy_cq,
	.destroy_qp = siw_destroy_qp,
	.destroy_srq = siw_destroy_srq,
	.get_dma_mr = siw_get_dma_mr,
	.get_port_immutable = siw_get_port_immutable,
	.iw_accept = siw_accept,
	.iw_add_ref = siw_qp_get_ref,
	.iw_connect = siw_connect,
	.iw_create_listen = siw_create_listen,
	.iw_destroy_listen = siw_destroy_listen,
	.iw_get_qp = siw_get_base_qp,
	.iw_reject = siw_reject,
	.iw_rem_ref = siw_qp_put_ref,
	.map_mr_sg = siw_map_mr_sg,
	.mmap = siw_mmap,
	.mmap_free = siw_mmap_free,
	.modify_qp = siw_verbs_modify_qp,
	.modify_srq = siw_modify_srq,
	.poll_cq = siw_poll_cq,
	.post_recv = siw_post_receive,
	.post_send = siw_post_send,
	.post_srq_recv = siw_post_srq_recv,
	.query_device = siw_query_device,
	.query_gid = siw_query_gid,
	.query_port = siw_query_port,
	.query_qp = siw_query_qp,
	.query_srq = siw_query_srq,
	.req_notify_cq = siw_req_notify_cq,
	.reg_user_mr = siw_reg_user_mr,

	INIT_RDMA_OBJ_SIZE(ib_cq, siw_cq, base_cq),
	INIT_RDMA_OBJ_SIZE(ib_pd, siw_pd, base_pd),
	INIT_RDMA_OBJ_SIZE(ib_qp, siw_qp, base_qp),
	INIT_RDMA_OBJ_SIZE(ib_srq, siw_srq, base_srq),
	INIT_RDMA_OBJ_SIZE(ib_ucontext, siw_ucontext, base_ucontext),
};

static struct siw_device *siw_device_create(struct net_device *netdev)
{
	struct siw_device *sdev = NULL;
	struct ib_device *base_dev;
	int rv;

	sdev = ib_alloc_device(siw_device, base_dev);
	if (!sdev)
		return NULL;

	base_dev = &sdev->base_dev;
	sdev->netdev = netdev;

	if (netdev->addr_len) {
		memcpy(sdev->raw_gid, netdev->dev_addr,
		       min_t(unsigned int, netdev->addr_len, ETH_ALEN));
	} else {
		/*
		 * This device does not have a HW address, but
		 * connection mangagement requires a unique gid.
		 */
		eth_random_addr(sdev->raw_gid);
	}
	addrconf_addr_eui48((u8 *)&base_dev->node_guid, sdev->raw_gid);

	base_dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND);

	base_dev->node_type = RDMA_NODE_RNIC;
	memcpy(base_dev->node_desc, SIW_NODE_DESC_COMMON,
	       sizeof(SIW_NODE_DESC_COMMON));

	/*
	 * Current model (one-to-one device association):
	 * One Softiwarp device per net_device or, equivalently,
	 * per physical port.
	 */
	base_dev->phys_port_cnt = 1;
	base_dev->num_comp_vectors = num_possible_cpus();

	xa_init_flags(&sdev->qp_xa, XA_FLAGS_ALLOC1);
	xa_init_flags(&sdev->mem_xa, XA_FLAGS_ALLOC1);

	ib_set_device_ops(base_dev, &siw_device_ops);
	rv = ib_device_set_netdev(base_dev, netdev, 1);
	if (rv)
		goto error;

	memcpy(base_dev->iw_ifname, netdev->name,
	       sizeof(base_dev->iw_ifname));

	/* Disable TCP port mapping */
	base_dev->iw_driver_flags = IW_F_NO_PORT_MAP;

	sdev->attrs.max_qp = SIW_MAX_QP;
	sdev->attrs.max_qp_wr = SIW_MAX_QP_WR;
	sdev->attrs.max_ord = SIW_MAX_ORD_QP;
	sdev->attrs.max_ird = SIW_MAX_IRD_QP;
	sdev->attrs.max_sge = SIW_MAX_SGE;
	sdev->attrs.max_sge_rd = SIW_MAX_SGE_RD;
	sdev->attrs.max_cq = SIW_MAX_CQ;
	sdev->attrs.max_cqe = SIW_MAX_CQE;
	sdev->attrs.max_mr = SIW_MAX_MR;
	sdev->attrs.max_pd = SIW_MAX_PD;
	sdev->attrs.max_mw = SIW_MAX_MW;
	sdev->attrs.max_srq = SIW_MAX_SRQ;
	sdev->attrs.max_srq_wr = SIW_MAX_SRQ_WR;
	sdev->attrs.max_srq_sge = SIW_MAX_SGE;

	INIT_LIST_HEAD(&sdev->cep_list);
	INIT_LIST_HEAD(&sdev->qp_list);

	atomic_set(&sdev->num_ctx, 0);
	atomic_set(&sdev->num_srq, 0);
	atomic_set(&sdev->num_qp, 0);
	atomic_set(&sdev->num_cq, 0);
	atomic_set(&sdev->num_mr, 0);
	atomic_set(&sdev->num_pd, 0);

	sdev->numa_node = dev_to_node(&netdev->dev);
	spin_lock_init(&sdev->lock);

	return sdev;
error:
	ib_dealloc_device(base_dev);

	return NULL;
}

/*
 * Network link becomes unavailable. Mark all
 * affected QP's accordingly.
 */
static void siw_netdev_down(struct work_struct *work)
{
	struct siw_device *sdev =
		container_of(work, struct siw_device, netdev_down);

	struct siw_qp_attrs qp_attrs;
	struct list_head *pos, *tmp;

	memset(&qp_attrs, 0, sizeof(qp_attrs));
	qp_attrs.state = SIW_QP_STATE_ERROR;

	list_for_each_safe(pos, tmp, &sdev->qp_list) {
		struct siw_qp *qp = list_entry(pos, struct siw_qp, devq);

		down_write(&qp->state_lock);
		WARN_ON(siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE));
		up_write(&qp->state_lock);
	}
	ib_device_put(&sdev->base_dev);
}

static void siw_device_goes_down(struct siw_device *sdev)
{
	if (ib_device_try_get(&sdev->base_dev)) {
		INIT_WORK(&sdev->netdev_down, siw_netdev_down);
		schedule_work(&sdev->netdev_down);
	}
}

static int siw_netdev_event(struct notifier_block *nb, unsigned long event,
			    void *arg)
{
	struct net_device *netdev = netdev_notifier_info_to_dev(arg);
	struct ib_device *base_dev;
	struct siw_device *sdev;

	dev_dbg(&netdev->dev, "siw: event %lu\n", event);

	base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
	if (!base_dev)
		return NOTIFY_OK;

	sdev = to_siw_dev(base_dev);

	switch (event) {
	case NETDEV_UP:
		sdev->state = IB_PORT_ACTIVE;
		siw_port_event(sdev, 1, IB_EVENT_PORT_ACTIVE);
		break;

	case NETDEV_GOING_DOWN:
		siw_device_goes_down(sdev);
		break;

	case NETDEV_DOWN:
		sdev->state = IB_PORT_DOWN;
		siw_port_event(sdev, 1, IB_EVENT_PORT_ERR);
		break;

	case NETDEV_REGISTER:
		/*
		 * Device registration now handled only by
		 * rdma netlink commands. So it shall be impossible
		 * to end up here with a valid siw device.
		 */
		siw_dbg(base_dev, "unexpected NETDEV_REGISTER event\n");
		break;

	case NETDEV_UNREGISTER:
		ib_unregister_device_queued(&sdev->base_dev);
		break;

	case NETDEV_CHANGEADDR:
		siw_port_event(sdev, 1, IB_EVENT_LID_CHANGE);
		break;
	/*
	 * Todo: Below netdev events are currently not handled.
	 */
	case NETDEV_CHANGEMTU:
	case NETDEV_CHANGE:
		break;

	default:
		break;
	}
	ib_device_put(&sdev->base_dev);

	return NOTIFY_OK;
}

static struct notifier_block siw_netdev_nb = {
	.notifier_call = siw_netdev_event,
};

static int siw_newlink(const char *basedev_name, struct net_device *netdev)
{
	struct ib_device *base_dev;
	struct siw_device *sdev = NULL;
	int rv = -ENOMEM;

	if (!siw_dev_qualified(netdev))
		return -EINVAL;

	base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
	if (base_dev) {
		ib_device_put(base_dev);
		return -EEXIST;
	}
	sdev = siw_device_create(netdev);
	if (sdev) {
		dev_dbg(&netdev->dev, "siw: new device\n");

		if (netif_running(netdev) && netif_carrier_ok(netdev))
			sdev->state = IB_PORT_ACTIVE;
		else
			sdev->state = IB_PORT_DOWN;

		rv = siw_device_register(sdev, basedev_name);
		if (rv)
			ib_dealloc_device(&sdev->base_dev);
	}
	return rv;
}

static struct rdma_link_ops siw_link_ops = {
	.type = "siw",
	.newlink = siw_newlink,
};

/*
 * siw_init_module - Initialize Softiwarp module and register with netdev
 *                   subsystem.
 */
static __init int siw_init_module(void)
{
	int rv;

	if (SENDPAGE_THRESH < SIW_MAX_INLINE) {
		pr_info("siw: sendpage threshold too small: %u\n",
			(int)SENDPAGE_THRESH);
		rv = -EINVAL;
		goto out_error;
	}
	rv = siw_init_cpulist();
	if (rv)
		goto out_error;

	rv = siw_cm_init();
	if (rv)
		goto out_error;

	if (!siw_create_tx_threads()) {
		pr_info("siw: Could not start any TX thread\n");
		rv = -ENOMEM;
		goto out_error;
	}
	/*
	 * Locate CRC32 algorithm. If unsuccessful, fail
	 * loading siw only, if CRC is required.
	 */
	siw_crypto_shash = crypto_alloc_shash("crc32c", 0, 0);
	if (IS_ERR(siw_crypto_shash)) {
		pr_info("siw: Loading CRC32c failed: %ld\n",
			PTR_ERR(siw_crypto_shash));
		siw_crypto_shash = NULL;
		if (mpa_crc_required) {
			rv = -EOPNOTSUPP;
			goto out_error;
		}
	}
	rv = register_netdevice_notifier(&siw_netdev_nb);
	if (rv)
		goto out_error;

	rdma_link_register(&siw_link_ops);

	pr_info("SoftiWARP attached\n");
	return 0;

out_error:
	siw_stop_tx_threads();

	if (siw_crypto_shash)
		crypto_free_shash(siw_crypto_shash);

	pr_info("SoftIWARP attach failed. Error: %d\n", rv);

	siw_cm_exit();
	siw_destroy_cpulist();

	return rv;
}

static void __exit siw_exit_module(void)
{
	siw_stop_tx_threads();

	unregister_netdevice_notifier(&siw_netdev_nb);
	rdma_link_unregister(&siw_link_ops);
	ib_unregister_driver(RDMA_DRIVER_SIW);

	siw_cm_exit();

	siw_destroy_cpulist();

	if (siw_crypto_shash)
		crypto_free_shash(siw_crypto_shash);

	pr_info("SoftiWARP detached\n");
}

module_init(siw_init_module);
module_exit(siw_exit_module);

MODULE_ALIAS_RDMA_LINK("siw");