blob: a462c2fc6f31137e9483bbcd81f6bc6f9e7d0f72 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2
3/* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4/* Copyright (c) 2008-2019, IBM Corporation */
5
6#include <linux/init.h>
7#include <linux/errno.h>
8#include <linux/netdevice.h>
9#include <linux/inetdevice.h>
10#include <net/net_namespace.h>
11#include <linux/rtnetlink.h>
12#include <linux/if_arp.h>
13#include <linux/list.h>
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/module.h>
17#include <linux/dma-mapping.h>
18
19#include <rdma/ib_verbs.h>
20#include <rdma/ib_user_verbs.h>
21#include <rdma/rdma_netlink.h>
22#include <linux/kthread.h>
23
24#include "siw.h"
25#include "siw_verbs.h"
26
27MODULE_AUTHOR("Bernard Metzler");
28MODULE_DESCRIPTION("Software iWARP Driver");
29MODULE_LICENSE("Dual BSD/GPL");
30
31/* transmit from user buffer, if possible */
32const bool zcopy_tx = true;
33
34/* Restrict usage of GSO, if hardware peer iwarp is unable to process
35 * large packets. try_gso = true lets siw try to use local GSO,
36 * if peer agrees. Not using GSO severly limits siw maximum tx bandwidth.
37 */
38const bool try_gso;
39
40/* Attach siw also with loopback devices */
41const bool loopback_enabled = true;
42
43/* We try to negotiate CRC on, if true */
44const bool mpa_crc_required;
45
46/* MPA CRC on/off enforced */
47const bool mpa_crc_strict;
48
49/* Control TCP_NODELAY socket option */
50const bool siw_tcp_nagle;
51
52/* Select MPA version to be used during connection setup */
53u_char mpa_version = MPA_REVISION_2;
54
55/* Selects MPA P2P mode (additional handshake during connection
56 * setup, if true.
57 */
58const bool peer_to_peer;
59
60struct task_struct *siw_tx_thread[NR_CPUS];
61struct crypto_shash *siw_crypto_shash;
62
63static int siw_device_register(struct siw_device *sdev, const char *name)
64{
65 struct ib_device *base_dev = &sdev->base_dev;
66 static int dev_id = 1;
67 int rv;
68
69 sdev->vendor_part_id = dev_id++;
70
71 rv = ib_register_device(base_dev, name);
72 if (rv) {
73 pr_warn("siw: device registration error %d\n", rv);
74 return rv;
75 }
76
77 siw_dbg(base_dev, "HWaddr=%pM\n", sdev->netdev->dev_addr);
78
79 return 0;
80}
81
82static void siw_device_cleanup(struct ib_device *base_dev)
83{
84 struct siw_device *sdev = to_siw_dev(base_dev);
85
86 xa_destroy(&sdev->qp_xa);
87 xa_destroy(&sdev->mem_xa);
88}
89
90static int siw_create_tx_threads(void)
91{
92 int cpu, assigned = 0;
93
94 for_each_online_cpu(cpu) {
95 /* Skip HT cores */
96 if (cpu % cpumask_weight(topology_sibling_cpumask(cpu)))
97 continue;
98
99 siw_tx_thread[cpu] =
100 kthread_create(siw_run_sq, (unsigned long *)(long)cpu,
101 "siw_tx/%d", cpu);
102 if (IS_ERR(siw_tx_thread[cpu])) {
103 siw_tx_thread[cpu] = NULL;
104 continue;
105 }
106 kthread_bind(siw_tx_thread[cpu], cpu);
107
108 wake_up_process(siw_tx_thread[cpu]);
109 assigned++;
110 }
111 return assigned;
112}
113
114static int siw_dev_qualified(struct net_device *netdev)
115{
116 /*
117 * Additional hardware support can be added here
118 * (e.g. ARPHRD_FDDI, ARPHRD_ATM, ...) - see
119 * <linux/if_arp.h> for type identifiers.
120 */
121 if (netdev->type == ARPHRD_ETHER || netdev->type == ARPHRD_IEEE802 ||
122 (netdev->type == ARPHRD_LOOPBACK && loopback_enabled))
123 return 1;
124
125 return 0;
126}
127
128static DEFINE_PER_CPU(atomic_t, siw_use_cnt);
129
130static struct {
131 struct cpumask **tx_valid_cpus;
132 int num_nodes;
133} siw_cpu_info;
134
135static int siw_init_cpulist(void)
136{
137 int i, num_nodes = nr_node_ids;
138
139 memset(siw_tx_thread, 0, sizeof(siw_tx_thread));
140
141 siw_cpu_info.num_nodes = num_nodes;
142
143 siw_cpu_info.tx_valid_cpus =
144 kcalloc(num_nodes, sizeof(struct cpumask *), GFP_KERNEL);
145 if (!siw_cpu_info.tx_valid_cpus) {
146 siw_cpu_info.num_nodes = 0;
147 return -ENOMEM;
148 }
149 for (i = 0; i < siw_cpu_info.num_nodes; i++) {
150 siw_cpu_info.tx_valid_cpus[i] =
151 kzalloc(sizeof(struct cpumask), GFP_KERNEL);
152 if (!siw_cpu_info.tx_valid_cpus[i])
153 goto out_err;
154
155 cpumask_clear(siw_cpu_info.tx_valid_cpus[i]);
156 }
157 for_each_possible_cpu(i)
158 cpumask_set_cpu(i, siw_cpu_info.tx_valid_cpus[cpu_to_node(i)]);
159
160 return 0;
161
162out_err:
163 siw_cpu_info.num_nodes = 0;
164 while (--i >= 0)
165 kfree(siw_cpu_info.tx_valid_cpus[i]);
166 kfree(siw_cpu_info.tx_valid_cpus);
167 siw_cpu_info.tx_valid_cpus = NULL;
168
169 return -ENOMEM;
170}
171
172static void siw_destroy_cpulist(void)
173{
174 int i = 0;
175
176 while (i < siw_cpu_info.num_nodes)
177 kfree(siw_cpu_info.tx_valid_cpus[i++]);
178
179 kfree(siw_cpu_info.tx_valid_cpus);
180}
181
182/*
183 * Choose CPU with least number of active QP's from NUMA node of
184 * TX interface.
185 */
186int siw_get_tx_cpu(struct siw_device *sdev)
187{
188 const struct cpumask *tx_cpumask;
189 int i, num_cpus, cpu, min_use, node = sdev->numa_node, tx_cpu = -1;
190
191 if (node < 0)
192 tx_cpumask = cpu_online_mask;
193 else
194 tx_cpumask = siw_cpu_info.tx_valid_cpus[node];
195
196 num_cpus = cpumask_weight(tx_cpumask);
197 if (!num_cpus) {
198 /* no CPU on this NUMA node */
199 tx_cpumask = cpu_online_mask;
200 num_cpus = cpumask_weight(tx_cpumask);
201 }
202 if (!num_cpus)
203 goto out;
204
205 cpu = cpumask_first(tx_cpumask);
206
207 for (i = 0, min_use = SIW_MAX_QP; i < num_cpus;
208 i++, cpu = cpumask_next(cpu, tx_cpumask)) {
209 int usage;
210
211 /* Skip any cores which have no TX thread */
212 if (!siw_tx_thread[cpu])
213 continue;
214
215 usage = atomic_read(&per_cpu(siw_use_cnt, cpu));
216 if (usage <= min_use) {
217 tx_cpu = cpu;
218 min_use = usage;
219 }
220 }
221 siw_dbg(&sdev->base_dev,
222 "tx cpu %d, node %d, %d qp's\n", tx_cpu, node, min_use);
223
224out:
225 if (tx_cpu >= 0)
226 atomic_inc(&per_cpu(siw_use_cnt, tx_cpu));
227 else
228 pr_warn("siw: no tx cpu found\n");
229
230 return tx_cpu;
231}
232
233void siw_put_tx_cpu(int cpu)
234{
235 atomic_dec(&per_cpu(siw_use_cnt, cpu));
236}
237
238static struct ib_qp *siw_get_base_qp(struct ib_device *base_dev, int id)
239{
240 struct siw_qp *qp = siw_qp_id2obj(to_siw_dev(base_dev), id);
241
242 if (qp) {
243 /*
244 * siw_qp_id2obj() increments object reference count
245 */
246 siw_qp_put(qp);
247 return qp->ib_qp;
248 }
249 return NULL;
250}
251
252static const struct ib_device_ops siw_device_ops = {
253 .owner = THIS_MODULE,
254 .uverbs_abi_ver = SIW_ABI_VERSION,
255 .driver_id = RDMA_DRIVER_SIW,
256
257 .alloc_mr = siw_alloc_mr,
258 .alloc_pd = siw_alloc_pd,
259 .alloc_ucontext = siw_alloc_ucontext,
260 .create_cq = siw_create_cq,
261 .create_qp = siw_create_qp,
262 .create_srq = siw_create_srq,
263 .dealloc_driver = siw_device_cleanup,
264 .dealloc_pd = siw_dealloc_pd,
265 .dealloc_ucontext = siw_dealloc_ucontext,
266 .dereg_mr = siw_dereg_mr,
267 .destroy_cq = siw_destroy_cq,
268 .destroy_qp = siw_destroy_qp,
269 .destroy_srq = siw_destroy_srq,
270 .get_dma_mr = siw_get_dma_mr,
271 .get_port_immutable = siw_get_port_immutable,
272 .iw_accept = siw_accept,
273 .iw_add_ref = siw_qp_get_ref,
274 .iw_connect = siw_connect,
275 .iw_create_listen = siw_create_listen,
276 .iw_destroy_listen = siw_destroy_listen,
277 .iw_get_qp = siw_get_base_qp,
278 .iw_reject = siw_reject,
279 .iw_rem_ref = siw_qp_put_ref,
280 .map_mr_sg = siw_map_mr_sg,
281 .mmap = siw_mmap,
282 .modify_qp = siw_verbs_modify_qp,
283 .modify_srq = siw_modify_srq,
284 .poll_cq = siw_poll_cq,
285 .post_recv = siw_post_receive,
286 .post_send = siw_post_send,
287 .post_srq_recv = siw_post_srq_recv,
288 .query_device = siw_query_device,
289 .query_gid = siw_query_gid,
290 .query_pkey = siw_query_pkey,
291 .query_port = siw_query_port,
292 .query_qp = siw_query_qp,
293 .query_srq = siw_query_srq,
294 .req_notify_cq = siw_req_notify_cq,
295 .reg_user_mr = siw_reg_user_mr,
296
297 INIT_RDMA_OBJ_SIZE(ib_cq, siw_cq, base_cq),
298 INIT_RDMA_OBJ_SIZE(ib_pd, siw_pd, base_pd),
299 INIT_RDMA_OBJ_SIZE(ib_srq, siw_srq, base_srq),
300 INIT_RDMA_OBJ_SIZE(ib_ucontext, siw_ucontext, base_ucontext),
301};
302
303static struct siw_device *siw_device_create(struct net_device *netdev)
304{
305 struct siw_device *sdev = NULL;
306 struct ib_device *base_dev;
307 struct device *parent = netdev->dev.parent;
308 int rv;
309
310 if (!parent) {
311 /*
312 * The loopback device has no parent device,
313 * so it appears as a top-level device. To support
314 * loopback device connectivity, take this device
315 * as the parent device. Skip all other devices
316 * w/o parent device.
317 */
318 if (netdev->type != ARPHRD_LOOPBACK) {
319 pr_warn("siw: device %s error: no parent device\n",
320 netdev->name);
321 return NULL;
322 }
323 parent = &netdev->dev;
324 }
325 sdev = ib_alloc_device(siw_device, base_dev);
326 if (!sdev)
327 return NULL;
328
329 base_dev = &sdev->base_dev;
330
331 sdev->netdev = netdev;
332
333 if (netdev->type != ARPHRD_LOOPBACK) {
334 memcpy(&base_dev->node_guid, netdev->dev_addr, 6);
335 } else {
336 /*
337 * The loopback device does not have a HW address,
338 * but connection mangagement lib expects gid != 0
339 */
340 size_t gidlen = min_t(size_t, strlen(base_dev->name), 6);
341
342 memcpy(&base_dev->node_guid, base_dev->name, gidlen);
343 }
344 base_dev->uverbs_cmd_mask =
345 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
346 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
347 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
348 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
349 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
350 (1ull << IB_USER_VERBS_CMD_REG_MR) |
351 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
352 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
353 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
354 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
355 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
356 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
357 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
358 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
359 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
360 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
361 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
362 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
363 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
364 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV) |
365 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
366 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
367 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
368
369 base_dev->node_type = RDMA_NODE_RNIC;
370 memcpy(base_dev->node_desc, SIW_NODE_DESC_COMMON,
371 sizeof(SIW_NODE_DESC_COMMON));
372
373 /*
374 * Current model (one-to-one device association):
375 * One Softiwarp device per net_device or, equivalently,
376 * per physical port.
377 */
378 base_dev->phys_port_cnt = 1;
379 base_dev->dev.parent = parent;
380 base_dev->dev.dma_ops = &dma_virt_ops;
381 base_dev->num_comp_vectors = num_possible_cpus();
382
383 xa_init_flags(&sdev->qp_xa, XA_FLAGS_ALLOC1);
384 xa_init_flags(&sdev->mem_xa, XA_FLAGS_ALLOC1);
385
386 ib_set_device_ops(base_dev, &siw_device_ops);
387 rv = ib_device_set_netdev(base_dev, netdev, 1);
388 if (rv)
389 goto error;
390
391 memcpy(base_dev->iw_ifname, netdev->name,
392 sizeof(base_dev->iw_ifname));
393
394 /* Disable TCP port mapping */
395 base_dev->iw_driver_flags = IW_F_NO_PORT_MAP,
396
397 sdev->attrs.max_qp = SIW_MAX_QP;
398 sdev->attrs.max_qp_wr = SIW_MAX_QP_WR;
399 sdev->attrs.max_ord = SIW_MAX_ORD_QP;
400 sdev->attrs.max_ird = SIW_MAX_IRD_QP;
401 sdev->attrs.max_sge = SIW_MAX_SGE;
402 sdev->attrs.max_sge_rd = SIW_MAX_SGE_RD;
403 sdev->attrs.max_cq = SIW_MAX_CQ;
404 sdev->attrs.max_cqe = SIW_MAX_CQE;
405 sdev->attrs.max_mr = SIW_MAX_MR;
406 sdev->attrs.max_pd = SIW_MAX_PD;
407 sdev->attrs.max_mw = SIW_MAX_MW;
408 sdev->attrs.max_fmr = SIW_MAX_FMR;
409 sdev->attrs.max_srq = SIW_MAX_SRQ;
410 sdev->attrs.max_srq_wr = SIW_MAX_SRQ_WR;
411 sdev->attrs.max_srq_sge = SIW_MAX_SGE;
412
413 INIT_LIST_HEAD(&sdev->cep_list);
414 INIT_LIST_HEAD(&sdev->qp_list);
415
416 atomic_set(&sdev->num_ctx, 0);
417 atomic_set(&sdev->num_srq, 0);
418 atomic_set(&sdev->num_qp, 0);
419 atomic_set(&sdev->num_cq, 0);
420 atomic_set(&sdev->num_mr, 0);
421 atomic_set(&sdev->num_pd, 0);
422
423 sdev->numa_node = dev_to_node(parent);
424 spin_lock_init(&sdev->lock);
425
426 return sdev;
427error:
428 ib_dealloc_device(base_dev);
429
430 return NULL;
431}
432
433/*
434 * Network link becomes unavailable. Mark all
435 * affected QP's accordingly.
436 */
437static void siw_netdev_down(struct work_struct *work)
438{
439 struct siw_device *sdev =
440 container_of(work, struct siw_device, netdev_down);
441
442 struct siw_qp_attrs qp_attrs;
443 struct list_head *pos, *tmp;
444
445 memset(&qp_attrs, 0, sizeof(qp_attrs));
446 qp_attrs.state = SIW_QP_STATE_ERROR;
447
448 list_for_each_safe(pos, tmp, &sdev->qp_list) {
449 struct siw_qp *qp = list_entry(pos, struct siw_qp, devq);
450
451 down_write(&qp->state_lock);
452 WARN_ON(siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE));
453 up_write(&qp->state_lock);
454 }
455 ib_device_put(&sdev->base_dev);
456}
457
458static void siw_device_goes_down(struct siw_device *sdev)
459{
460 if (ib_device_try_get(&sdev->base_dev)) {
461 INIT_WORK(&sdev->netdev_down, siw_netdev_down);
462 schedule_work(&sdev->netdev_down);
463 }
464}
465
466static int siw_netdev_event(struct notifier_block *nb, unsigned long event,
467 void *arg)
468{
469 struct net_device *netdev = netdev_notifier_info_to_dev(arg);
470 struct ib_device *base_dev;
471 struct siw_device *sdev;
472
473 dev_dbg(&netdev->dev, "siw: event %lu\n", event);
474
475 base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
476 if (!base_dev)
477 return NOTIFY_OK;
478
479 sdev = to_siw_dev(base_dev);
480
481 switch (event) {
482 case NETDEV_UP:
483 sdev->state = IB_PORT_ACTIVE;
484 siw_port_event(sdev, 1, IB_EVENT_PORT_ACTIVE);
485 break;
486
487 case NETDEV_GOING_DOWN:
488 siw_device_goes_down(sdev);
489 break;
490
491 case NETDEV_DOWN:
492 sdev->state = IB_PORT_DOWN;
493 siw_port_event(sdev, 1, IB_EVENT_PORT_ERR);
494 break;
495
496 case NETDEV_REGISTER:
497 /*
498 * Device registration now handled only by
499 * rdma netlink commands. So it shall be impossible
500 * to end up here with a valid siw device.
501 */
502 siw_dbg(base_dev, "unexpected NETDEV_REGISTER event\n");
503 break;
504
505 case NETDEV_UNREGISTER:
506 ib_unregister_device_queued(&sdev->base_dev);
507 break;
508
509 case NETDEV_CHANGEADDR:
510 siw_port_event(sdev, 1, IB_EVENT_LID_CHANGE);
511 break;
512 /*
513 * Todo: Below netdev events are currently not handled.
514 */
515 case NETDEV_CHANGEMTU:
516 case NETDEV_CHANGE:
517 break;
518
519 default:
520 break;
521 }
522 ib_device_put(&sdev->base_dev);
523
524 return NOTIFY_OK;
525}
526
527static struct notifier_block siw_netdev_nb = {
528 .notifier_call = siw_netdev_event,
529};
530
531static int siw_newlink(const char *basedev_name, struct net_device *netdev)
532{
533 struct ib_device *base_dev;
534 struct siw_device *sdev = NULL;
535 int rv = -ENOMEM;
536
537 if (!siw_dev_qualified(netdev))
538 return -EINVAL;
539
540 base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
541 if (base_dev) {
542 ib_device_put(base_dev);
543 return -EEXIST;
544 }
545 sdev = siw_device_create(netdev);
546 if (sdev) {
547 dev_dbg(&netdev->dev, "siw: new device\n");
548
549 if (netif_running(netdev) && netif_carrier_ok(netdev))
550 sdev->state = IB_PORT_ACTIVE;
551 else
552 sdev->state = IB_PORT_DOWN;
553
554 rv = siw_device_register(sdev, basedev_name);
555 if (rv)
556 ib_dealloc_device(&sdev->base_dev);
557 }
558 return rv;
559}
560
561static struct rdma_link_ops siw_link_ops = {
562 .type = "siw",
563 .newlink = siw_newlink,
564};
565
566/*
567 * siw_init_module - Initialize Softiwarp module and register with netdev
568 * subsystem.
569 */
570static __init int siw_init_module(void)
571{
572 int rv;
573 int nr_cpu;
574
575 if (SENDPAGE_THRESH < SIW_MAX_INLINE) {
576 pr_info("siw: sendpage threshold too small: %u\n",
577 (int)SENDPAGE_THRESH);
578 rv = -EINVAL;
579 goto out_error;
580 }
581 rv = siw_init_cpulist();
582 if (rv)
583 goto out_error;
584
585 rv = siw_cm_init();
586 if (rv)
587 goto out_error;
588
589 if (!siw_create_tx_threads()) {
590 pr_info("siw: Could not start any TX thread\n");
591 rv = -ENOMEM;
592 goto out_error;
593 }
594 /*
595 * Locate CRC32 algorithm. If unsuccessful, fail
596 * loading siw only, if CRC is required.
597 */
598 siw_crypto_shash = crypto_alloc_shash("crc32c", 0, 0);
599 if (IS_ERR(siw_crypto_shash)) {
600 pr_info("siw: Loading CRC32c failed: %ld\n",
601 PTR_ERR(siw_crypto_shash));
602 siw_crypto_shash = NULL;
603 if (mpa_crc_required) {
604 rv = -EOPNOTSUPP;
605 goto out_error;
606 }
607 }
608 rv = register_netdevice_notifier(&siw_netdev_nb);
609 if (rv)
610 goto out_error;
611
612 rdma_link_register(&siw_link_ops);
613
614 pr_info("SoftiWARP attached\n");
615 return 0;
616
617out_error:
618 for (nr_cpu = 0; nr_cpu < nr_cpu_ids; nr_cpu++) {
619 if (siw_tx_thread[nr_cpu]) {
620 siw_stop_tx_thread(nr_cpu);
621 siw_tx_thread[nr_cpu] = NULL;
622 }
623 }
624 if (siw_crypto_shash)
625 crypto_free_shash(siw_crypto_shash);
626
627 pr_info("SoftIWARP attach failed. Error: %d\n", rv);
628
629 siw_cm_exit();
630 siw_destroy_cpulist();
631
632 return rv;
633}
634
635static void __exit siw_exit_module(void)
636{
637 int cpu;
638
639 for_each_possible_cpu(cpu) {
640 if (siw_tx_thread[cpu]) {
641 siw_stop_tx_thread(cpu);
642 siw_tx_thread[cpu] = NULL;
643 }
644 }
645 unregister_netdevice_notifier(&siw_netdev_nb);
646 rdma_link_unregister(&siw_link_ops);
647 ib_unregister_driver(RDMA_DRIVER_SIW);
648
649 siw_cm_exit();
650
651 siw_destroy_cpulist();
652
653 if (siw_crypto_shash)
654 crypto_free_shash(siw_crypto_shash);
655
656 pr_info("SoftiWARP detached\n");
657}
658
659module_init(siw_init_module);
660module_exit(siw_exit_module);
661
662MODULE_ALIAS_RDMA_LINK("siw");