]> Git Repo - J-linux.git/blob - drivers/infiniband/sw/siw/siw_main.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / drivers / infiniband / sw / siw / siw_main.c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2
3 /* Authors: Bernard Metzler <[email protected]> */
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 <net/addrconf.h>
20 #include <rdma/ib_verbs.h>
21 #include <rdma/ib_user_verbs.h>
22 #include <rdma/rdma_netlink.h>
23 #include <linux/kthread.h>
24
25 #include "siw.h"
26 #include "siw_verbs.h"
27
28 MODULE_AUTHOR("Bernard Metzler");
29 MODULE_DESCRIPTION("Software iWARP Driver");
30 MODULE_LICENSE("Dual BSD/GPL");
31
32 /* transmit from user buffer, if possible */
33 const bool zcopy_tx = true;
34
35 /* Restrict usage of GSO, if hardware peer iwarp is unable to process
36  * large packets. try_gso = true lets siw try to use local GSO,
37  * if peer agrees.  Not using GSO severly limits siw maximum tx bandwidth.
38  */
39 const bool try_gso;
40
41 /* Attach siw also with loopback devices */
42 const bool loopback_enabled = true;
43
44 /* We try to negotiate CRC on, if true */
45 const bool mpa_crc_required;
46
47 /* MPA CRC on/off enforced */
48 const bool mpa_crc_strict;
49
50 /* Control TCP_NODELAY socket option */
51 const bool siw_tcp_nagle;
52
53 /* Select MPA version to be used during connection setup */
54 u_char mpa_version = MPA_REVISION_2;
55
56 /* Selects MPA P2P mode (additional handshake during connection
57  * setup, if true.
58  */
59 const bool peer_to_peer;
60
61 struct task_struct *siw_tx_thread[NR_CPUS];
62 struct crypto_shash *siw_crypto_shash;
63
64 static int siw_device_register(struct siw_device *sdev, const char *name)
65 {
66         struct ib_device *base_dev = &sdev->base_dev;
67         static int dev_id = 1;
68         int rv;
69
70         sdev->vendor_part_id = dev_id++;
71
72         rv = ib_register_device(base_dev, name, NULL);
73         if (rv) {
74                 pr_warn("siw: device registration error %d\n", rv);
75                 return rv;
76         }
77
78         siw_dbg(base_dev, "HWaddr=%pM\n", sdev->raw_gid);
79         return 0;
80 }
81
82 static 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
90 static int siw_dev_qualified(struct net_device *netdev)
91 {
92         /*
93          * Additional hardware support can be added here
94          * (e.g. ARPHRD_FDDI, ARPHRD_ATM, ...) - see
95          * <linux/if_arp.h> for type identifiers.
96          */
97         if (netdev->type == ARPHRD_ETHER || netdev->type == ARPHRD_IEEE802 ||
98             netdev->type == ARPHRD_NONE ||
99             (netdev->type == ARPHRD_LOOPBACK && loopback_enabled))
100                 return 1;
101
102         return 0;
103 }
104
105 static DEFINE_PER_CPU(atomic_t, siw_use_cnt);
106
107 static struct {
108         struct cpumask **tx_valid_cpus;
109         int num_nodes;
110 } siw_cpu_info;
111
112 static void siw_destroy_cpulist(int number)
113 {
114         int i = 0;
115
116         while (i < number)
117                 kfree(siw_cpu_info.tx_valid_cpus[i++]);
118
119         kfree(siw_cpu_info.tx_valid_cpus);
120         siw_cpu_info.tx_valid_cpus = NULL;
121 }
122
123 static int siw_init_cpulist(void)
124 {
125         int i, num_nodes = nr_node_ids;
126
127         memset(siw_tx_thread, 0, sizeof(siw_tx_thread));
128
129         siw_cpu_info.num_nodes = num_nodes;
130
131         siw_cpu_info.tx_valid_cpus =
132                 kcalloc(num_nodes, sizeof(struct cpumask *), GFP_KERNEL);
133         if (!siw_cpu_info.tx_valid_cpus) {
134                 siw_cpu_info.num_nodes = 0;
135                 return -ENOMEM;
136         }
137         for (i = 0; i < siw_cpu_info.num_nodes; i++) {
138                 siw_cpu_info.tx_valid_cpus[i] =
139                         kzalloc(sizeof(struct cpumask), GFP_KERNEL);
140                 if (!siw_cpu_info.tx_valid_cpus[i])
141                         goto out_err;
142
143                 cpumask_clear(siw_cpu_info.tx_valid_cpus[i]);
144         }
145         for_each_possible_cpu(i)
146                 cpumask_set_cpu(i, siw_cpu_info.tx_valid_cpus[cpu_to_node(i)]);
147
148         return 0;
149
150 out_err:
151         siw_cpu_info.num_nodes = 0;
152         siw_destroy_cpulist(i);
153
154         return -ENOMEM;
155 }
156
157 /*
158  * Choose CPU with least number of active QP's from NUMA node of
159  * TX interface.
160  */
161 int siw_get_tx_cpu(struct siw_device *sdev)
162 {
163         const struct cpumask *tx_cpumask;
164         int i, num_cpus, cpu, min_use, node = sdev->numa_node, tx_cpu = -1;
165
166         if (node < 0)
167                 tx_cpumask = cpu_online_mask;
168         else
169                 tx_cpumask = siw_cpu_info.tx_valid_cpus[node];
170
171         num_cpus = cpumask_weight(tx_cpumask);
172         if (!num_cpus) {
173                 /* no CPU on this NUMA node */
174                 tx_cpumask = cpu_online_mask;
175                 num_cpus = cpumask_weight(tx_cpumask);
176         }
177         if (!num_cpus)
178                 goto out;
179
180         cpu = cpumask_first(tx_cpumask);
181
182         for (i = 0, min_use = SIW_MAX_QP; i < num_cpus;
183              i++, cpu = cpumask_next(cpu, tx_cpumask)) {
184                 int usage;
185
186                 /* Skip any cores which have no TX thread */
187                 if (!siw_tx_thread[cpu])
188                         continue;
189
190                 usage = atomic_read(&per_cpu(siw_use_cnt, cpu));
191                 if (usage <= min_use) {
192                         tx_cpu = cpu;
193                         min_use = usage;
194                 }
195         }
196         siw_dbg(&sdev->base_dev,
197                 "tx cpu %d, node %d, %d qp's\n", tx_cpu, node, min_use);
198
199 out:
200         if (tx_cpu >= 0)
201                 atomic_inc(&per_cpu(siw_use_cnt, tx_cpu));
202         else
203                 pr_warn("siw: no tx cpu found\n");
204
205         return tx_cpu;
206 }
207
208 void siw_put_tx_cpu(int cpu)
209 {
210         atomic_dec(&per_cpu(siw_use_cnt, cpu));
211 }
212
213 static struct ib_qp *siw_get_base_qp(struct ib_device *base_dev, int id)
214 {
215         struct siw_qp *qp = siw_qp_id2obj(to_siw_dev(base_dev), id);
216
217         if (qp) {
218                 /*
219                  * siw_qp_id2obj() increments object reference count
220                  */
221                 siw_qp_put(qp);
222                 return &qp->base_qp;
223         }
224         return NULL;
225 }
226
227 static const struct ib_device_ops siw_device_ops = {
228         .owner = THIS_MODULE,
229         .uverbs_abi_ver = SIW_ABI_VERSION,
230         .driver_id = RDMA_DRIVER_SIW,
231
232         .alloc_mr = siw_alloc_mr,
233         .alloc_pd = siw_alloc_pd,
234         .alloc_ucontext = siw_alloc_ucontext,
235         .create_cq = siw_create_cq,
236         .create_qp = siw_create_qp,
237         .create_srq = siw_create_srq,
238         .dealloc_driver = siw_device_cleanup,
239         .dealloc_pd = siw_dealloc_pd,
240         .dealloc_ucontext = siw_dealloc_ucontext,
241         .dereg_mr = siw_dereg_mr,
242         .destroy_cq = siw_destroy_cq,
243         .destroy_qp = siw_destroy_qp,
244         .destroy_srq = siw_destroy_srq,
245         .get_dma_mr = siw_get_dma_mr,
246         .get_port_immutable = siw_get_port_immutable,
247         .iw_accept = siw_accept,
248         .iw_add_ref = siw_qp_get_ref,
249         .iw_connect = siw_connect,
250         .iw_create_listen = siw_create_listen,
251         .iw_destroy_listen = siw_destroy_listen,
252         .iw_get_qp = siw_get_base_qp,
253         .iw_reject = siw_reject,
254         .iw_rem_ref = siw_qp_put_ref,
255         .map_mr_sg = siw_map_mr_sg,
256         .mmap = siw_mmap,
257         .mmap_free = siw_mmap_free,
258         .modify_qp = siw_verbs_modify_qp,
259         .modify_srq = siw_modify_srq,
260         .poll_cq = siw_poll_cq,
261         .post_recv = siw_post_receive,
262         .post_send = siw_post_send,
263         .post_srq_recv = siw_post_srq_recv,
264         .query_device = siw_query_device,
265         .query_gid = siw_query_gid,
266         .query_port = siw_query_port,
267         .query_qp = siw_query_qp,
268         .query_srq = siw_query_srq,
269         .req_notify_cq = siw_req_notify_cq,
270         .reg_user_mr = siw_reg_user_mr,
271
272         INIT_RDMA_OBJ_SIZE(ib_cq, siw_cq, base_cq),
273         INIT_RDMA_OBJ_SIZE(ib_pd, siw_pd, base_pd),
274         INIT_RDMA_OBJ_SIZE(ib_qp, siw_qp, base_qp),
275         INIT_RDMA_OBJ_SIZE(ib_srq, siw_srq, base_srq),
276         INIT_RDMA_OBJ_SIZE(ib_ucontext, siw_ucontext, base_ucontext),
277 };
278
279 static struct siw_device *siw_device_create(struct net_device *netdev)
280 {
281         struct siw_device *sdev = NULL;
282         struct ib_device *base_dev;
283         int rv;
284
285         sdev = ib_alloc_device(siw_device, base_dev);
286         if (!sdev)
287                 return NULL;
288
289         base_dev = &sdev->base_dev;
290
291         if (netdev->addr_len) {
292                 memcpy(sdev->raw_gid, netdev->dev_addr,
293                        min_t(unsigned int, netdev->addr_len, ETH_ALEN));
294         } else {
295                 /*
296                  * This device does not have a HW address, but
297                  * connection mangagement requires a unique gid.
298                  */
299                 eth_random_addr(sdev->raw_gid);
300         }
301         addrconf_addr_eui48((u8 *)&base_dev->node_guid, sdev->raw_gid);
302
303         base_dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND);
304
305         base_dev->node_type = RDMA_NODE_RNIC;
306         memcpy(base_dev->node_desc, SIW_NODE_DESC_COMMON,
307                sizeof(SIW_NODE_DESC_COMMON));
308
309         /*
310          * Current model (one-to-one device association):
311          * One Softiwarp device per net_device or, equivalently,
312          * per physical port.
313          */
314         base_dev->phys_port_cnt = 1;
315         base_dev->num_comp_vectors = num_possible_cpus();
316
317         xa_init_flags(&sdev->qp_xa, XA_FLAGS_ALLOC1);
318         xa_init_flags(&sdev->mem_xa, XA_FLAGS_ALLOC1);
319
320         ib_set_device_ops(base_dev, &siw_device_ops);
321         rv = ib_device_set_netdev(base_dev, netdev, 1);
322         if (rv)
323                 goto error;
324
325         memcpy(base_dev->iw_ifname, netdev->name,
326                sizeof(base_dev->iw_ifname));
327
328         /* Disable TCP port mapping */
329         base_dev->iw_driver_flags = IW_F_NO_PORT_MAP;
330
331         sdev->attrs.max_qp = SIW_MAX_QP;
332         sdev->attrs.max_qp_wr = SIW_MAX_QP_WR;
333         sdev->attrs.max_ord = SIW_MAX_ORD_QP;
334         sdev->attrs.max_ird = SIW_MAX_IRD_QP;
335         sdev->attrs.max_sge = SIW_MAX_SGE;
336         sdev->attrs.max_sge_rd = SIW_MAX_SGE_RD;
337         sdev->attrs.max_cq = SIW_MAX_CQ;
338         sdev->attrs.max_cqe = SIW_MAX_CQE;
339         sdev->attrs.max_mr = SIW_MAX_MR;
340         sdev->attrs.max_pd = SIW_MAX_PD;
341         sdev->attrs.max_mw = SIW_MAX_MW;
342         sdev->attrs.max_srq = SIW_MAX_SRQ;
343         sdev->attrs.max_srq_wr = SIW_MAX_SRQ_WR;
344         sdev->attrs.max_srq_sge = SIW_MAX_SGE;
345
346         INIT_LIST_HEAD(&sdev->cep_list);
347         INIT_LIST_HEAD(&sdev->qp_list);
348
349         atomic_set(&sdev->num_ctx, 0);
350         atomic_set(&sdev->num_srq, 0);
351         atomic_set(&sdev->num_qp, 0);
352         atomic_set(&sdev->num_cq, 0);
353         atomic_set(&sdev->num_mr, 0);
354         atomic_set(&sdev->num_pd, 0);
355
356         sdev->numa_node = dev_to_node(&netdev->dev);
357         spin_lock_init(&sdev->lock);
358
359         return sdev;
360 error:
361         ib_dealloc_device(base_dev);
362
363         return NULL;
364 }
365
366 static int siw_netdev_event(struct notifier_block *nb, unsigned long event,
367                             void *arg)
368 {
369         struct net_device *netdev = netdev_notifier_info_to_dev(arg);
370         struct ib_device *base_dev;
371         struct siw_device *sdev;
372
373         dev_dbg(&netdev->dev, "siw: event %lu\n", event);
374
375         base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
376         if (!base_dev)
377                 return NOTIFY_OK;
378
379         sdev = to_siw_dev(base_dev);
380
381         switch (event) {
382         case NETDEV_UP:
383                 siw_port_event(sdev, 1, IB_EVENT_PORT_ACTIVE);
384                 break;
385
386         case NETDEV_DOWN:
387                 siw_port_event(sdev, 1, IB_EVENT_PORT_ERR);
388                 break;
389
390         case NETDEV_REGISTER:
391                 /*
392                  * Device registration now handled only by
393                  * rdma netlink commands. So it shall be impossible
394                  * to end up here with a valid siw device.
395                  */
396                 siw_dbg(base_dev, "unexpected NETDEV_REGISTER event\n");
397                 break;
398
399         case NETDEV_UNREGISTER:
400                 ib_unregister_device_queued(&sdev->base_dev);
401                 break;
402
403         case NETDEV_CHANGEADDR:
404                 siw_port_event(sdev, 1, IB_EVENT_LID_CHANGE);
405                 break;
406         /*
407          * All other events are not handled
408          */
409         default:
410                 break;
411         }
412         ib_device_put(&sdev->base_dev);
413
414         return NOTIFY_OK;
415 }
416
417 static struct notifier_block siw_netdev_nb = {
418         .notifier_call = siw_netdev_event,
419 };
420
421 static int siw_newlink(const char *basedev_name, struct net_device *netdev)
422 {
423         struct ib_device *base_dev;
424         struct siw_device *sdev = NULL;
425         int rv = -ENOMEM;
426
427         if (!siw_dev_qualified(netdev))
428                 return -EINVAL;
429
430         base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
431         if (base_dev) {
432                 ib_device_put(base_dev);
433                 return -EEXIST;
434         }
435         sdev = siw_device_create(netdev);
436         if (sdev) {
437                 dev_dbg(&netdev->dev, "siw: new device\n");
438                 ib_mark_name_assigned_by_user(&sdev->base_dev);
439                 rv = siw_device_register(sdev, basedev_name);
440                 if (rv)
441                         ib_dealloc_device(&sdev->base_dev);
442         }
443         return rv;
444 }
445
446 static struct rdma_link_ops siw_link_ops = {
447         .type = "siw",
448         .newlink = siw_newlink,
449 };
450
451 /*
452  * siw_init_module - Initialize Softiwarp module and register with netdev
453  *                   subsystem.
454  */
455 static __init int siw_init_module(void)
456 {
457         int rv;
458
459         if (SENDPAGE_THRESH < SIW_MAX_INLINE) {
460                 pr_info("siw: sendpage threshold too small: %u\n",
461                         (int)SENDPAGE_THRESH);
462                 rv = -EINVAL;
463                 goto out_error;
464         }
465         rv = siw_init_cpulist();
466         if (rv)
467                 goto out_error;
468
469         rv = siw_cm_init();
470         if (rv)
471                 goto out_error;
472
473         if (!siw_create_tx_threads()) {
474                 pr_info("siw: Could not start any TX thread\n");
475                 rv = -ENOMEM;
476                 goto out_error;
477         }
478         /*
479          * Locate CRC32 algorithm. If unsuccessful, fail
480          * loading siw only, if CRC is required.
481          */
482         siw_crypto_shash = crypto_alloc_shash("crc32c", 0, 0);
483         if (IS_ERR(siw_crypto_shash)) {
484                 pr_info("siw: Loading CRC32c failed: %ld\n",
485                         PTR_ERR(siw_crypto_shash));
486                 siw_crypto_shash = NULL;
487                 if (mpa_crc_required) {
488                         rv = -EOPNOTSUPP;
489                         goto out_error;
490                 }
491         }
492         rv = register_netdevice_notifier(&siw_netdev_nb);
493         if (rv)
494                 goto out_error;
495
496         rdma_link_register(&siw_link_ops);
497
498         pr_info("SoftiWARP attached\n");
499         return 0;
500
501 out_error:
502         siw_stop_tx_threads();
503
504         if (siw_crypto_shash)
505                 crypto_free_shash(siw_crypto_shash);
506
507         pr_info("SoftIWARP attach failed. Error: %d\n", rv);
508
509         siw_cm_exit();
510         siw_destroy_cpulist(siw_cpu_info.num_nodes);
511
512         return rv;
513 }
514
515 static void __exit siw_exit_module(void)
516 {
517         siw_stop_tx_threads();
518
519         unregister_netdevice_notifier(&siw_netdev_nb);
520         rdma_link_unregister(&siw_link_ops);
521         ib_unregister_driver(RDMA_DRIVER_SIW);
522
523         siw_cm_exit();
524
525         siw_destroy_cpulist(siw_cpu_info.num_nodes);
526
527         if (siw_crypto_shash)
528                 crypto_free_shash(siw_crypto_shash);
529
530         pr_info("SoftiWARP detached\n");
531 }
532
533 module_init(siw_init_module);
534 module_exit(siw_exit_module);
535
536 MODULE_ALIAS_RDMA_LINK("siw");
This page took 0.055989 seconds and 4 git commands to generate.