Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
0002 
0003 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
0004 /* Copyright (c) 2008-2019, IBM Corporation */
0005 
0006 #include <linux/init.h>
0007 #include <linux/errno.h>
0008 #include <linux/netdevice.h>
0009 #include <linux/inetdevice.h>
0010 #include <net/net_namespace.h>
0011 #include <linux/rtnetlink.h>
0012 #include <linux/if_arp.h>
0013 #include <linux/list.h>
0014 #include <linux/kernel.h>
0015 #include <linux/sched.h>
0016 #include <linux/module.h>
0017 #include <linux/dma-mapping.h>
0018 
0019 #include <net/addrconf.h>
0020 #include <rdma/ib_verbs.h>
0021 #include <rdma/ib_user_verbs.h>
0022 #include <rdma/rdma_netlink.h>
0023 #include <linux/kthread.h>
0024 
0025 #include "siw.h"
0026 #include "siw_verbs.h"
0027 
0028 MODULE_AUTHOR("Bernard Metzler");
0029 MODULE_DESCRIPTION("Software iWARP Driver");
0030 MODULE_LICENSE("Dual BSD/GPL");
0031 
0032 /* transmit from user buffer, if possible */
0033 const bool zcopy_tx = true;
0034 
0035 /* Restrict usage of GSO, if hardware peer iwarp is unable to process
0036  * large packets. try_gso = true lets siw try to use local GSO,
0037  * if peer agrees.  Not using GSO severly limits siw maximum tx bandwidth.
0038  */
0039 const bool try_gso;
0040 
0041 /* Attach siw also with loopback devices */
0042 const bool loopback_enabled = true;
0043 
0044 /* We try to negotiate CRC on, if true */
0045 const bool mpa_crc_required;
0046 
0047 /* MPA CRC on/off enforced */
0048 const bool mpa_crc_strict;
0049 
0050 /* Control TCP_NODELAY socket option */
0051 const bool siw_tcp_nagle;
0052 
0053 /* Select MPA version to be used during connection setup */
0054 u_char mpa_version = MPA_REVISION_2;
0055 
0056 /* Selects MPA P2P mode (additional handshake during connection
0057  * setup, if true.
0058  */
0059 const bool peer_to_peer;
0060 
0061 struct task_struct *siw_tx_thread[NR_CPUS];
0062 struct crypto_shash *siw_crypto_shash;
0063 
0064 static int siw_device_register(struct siw_device *sdev, const char *name)
0065 {
0066     struct ib_device *base_dev = &sdev->base_dev;
0067     static int dev_id = 1;
0068     int rv;
0069 
0070     sdev->vendor_part_id = dev_id++;
0071 
0072     rv = ib_register_device(base_dev, name, NULL);
0073     if (rv) {
0074         pr_warn("siw: device registration error %d\n", rv);
0075         return rv;
0076     }
0077 
0078     siw_dbg(base_dev, "HWaddr=%pM\n", sdev->netdev->dev_addr);
0079 
0080     return 0;
0081 }
0082 
0083 static void siw_device_cleanup(struct ib_device *base_dev)
0084 {
0085     struct siw_device *sdev = to_siw_dev(base_dev);
0086 
0087     xa_destroy(&sdev->qp_xa);
0088     xa_destroy(&sdev->mem_xa);
0089 }
0090 
0091 static int siw_create_tx_threads(void)
0092 {
0093     int cpu, assigned = 0;
0094 
0095     for_each_online_cpu(cpu) {
0096         /* Skip HT cores */
0097         if (cpu % cpumask_weight(topology_sibling_cpumask(cpu)))
0098             continue;
0099 
0100         siw_tx_thread[cpu] =
0101             kthread_run_on_cpu(siw_run_sq,
0102                        (unsigned long *)(long)cpu,
0103                        cpu, "siw_tx/%u");
0104         if (IS_ERR(siw_tx_thread[cpu])) {
0105             siw_tx_thread[cpu] = NULL;
0106             continue;
0107         }
0108 
0109         assigned++;
0110     }
0111     return assigned;
0112 }
0113 
0114 static int siw_dev_qualified(struct net_device *netdev)
0115 {
0116     /*
0117      * Additional hardware support can be added here
0118      * (e.g. ARPHRD_FDDI, ARPHRD_ATM, ...) - see
0119      * <linux/if_arp.h> for type identifiers.
0120      */
0121     if (netdev->type == ARPHRD_ETHER || netdev->type == ARPHRD_IEEE802 ||
0122         netdev->type == ARPHRD_NONE ||
0123         (netdev->type == ARPHRD_LOOPBACK && loopback_enabled))
0124         return 1;
0125 
0126     return 0;
0127 }
0128 
0129 static DEFINE_PER_CPU(atomic_t, siw_use_cnt);
0130 
0131 static struct {
0132     struct cpumask **tx_valid_cpus;
0133     int num_nodes;
0134 } siw_cpu_info;
0135 
0136 static int siw_init_cpulist(void)
0137 {
0138     int i, num_nodes = nr_node_ids;
0139 
0140     memset(siw_tx_thread, 0, sizeof(siw_tx_thread));
0141 
0142     siw_cpu_info.num_nodes = num_nodes;
0143 
0144     siw_cpu_info.tx_valid_cpus =
0145         kcalloc(num_nodes, sizeof(struct cpumask *), GFP_KERNEL);
0146     if (!siw_cpu_info.tx_valid_cpus) {
0147         siw_cpu_info.num_nodes = 0;
0148         return -ENOMEM;
0149     }
0150     for (i = 0; i < siw_cpu_info.num_nodes; i++) {
0151         siw_cpu_info.tx_valid_cpus[i] =
0152             kzalloc(sizeof(struct cpumask), GFP_KERNEL);
0153         if (!siw_cpu_info.tx_valid_cpus[i])
0154             goto out_err;
0155 
0156         cpumask_clear(siw_cpu_info.tx_valid_cpus[i]);
0157     }
0158     for_each_possible_cpu(i)
0159         cpumask_set_cpu(i, siw_cpu_info.tx_valid_cpus[cpu_to_node(i)]);
0160 
0161     return 0;
0162 
0163 out_err:
0164     siw_cpu_info.num_nodes = 0;
0165     while (--i >= 0)
0166         kfree(siw_cpu_info.tx_valid_cpus[i]);
0167     kfree(siw_cpu_info.tx_valid_cpus);
0168     siw_cpu_info.tx_valid_cpus = NULL;
0169 
0170     return -ENOMEM;
0171 }
0172 
0173 static void siw_destroy_cpulist(void)
0174 {
0175     int i = 0;
0176 
0177     while (i < siw_cpu_info.num_nodes)
0178         kfree(siw_cpu_info.tx_valid_cpus[i++]);
0179 
0180     kfree(siw_cpu_info.tx_valid_cpus);
0181 }
0182 
0183 /*
0184  * Choose CPU with least number of active QP's from NUMA node of
0185  * TX interface.
0186  */
0187 int siw_get_tx_cpu(struct siw_device *sdev)
0188 {
0189     const struct cpumask *tx_cpumask;
0190     int i, num_cpus, cpu, min_use, node = sdev->numa_node, tx_cpu = -1;
0191 
0192     if (node < 0)
0193         tx_cpumask = cpu_online_mask;
0194     else
0195         tx_cpumask = siw_cpu_info.tx_valid_cpus[node];
0196 
0197     num_cpus = cpumask_weight(tx_cpumask);
0198     if (!num_cpus) {
0199         /* no CPU on this NUMA node */
0200         tx_cpumask = cpu_online_mask;
0201         num_cpus = cpumask_weight(tx_cpumask);
0202     }
0203     if (!num_cpus)
0204         goto out;
0205 
0206     cpu = cpumask_first(tx_cpumask);
0207 
0208     for (i = 0, min_use = SIW_MAX_QP; i < num_cpus;
0209          i++, cpu = cpumask_next(cpu, tx_cpumask)) {
0210         int usage;
0211 
0212         /* Skip any cores which have no TX thread */
0213         if (!siw_tx_thread[cpu])
0214             continue;
0215 
0216         usage = atomic_read(&per_cpu(siw_use_cnt, cpu));
0217         if (usage <= min_use) {
0218             tx_cpu = cpu;
0219             min_use = usage;
0220         }
0221     }
0222     siw_dbg(&sdev->base_dev,
0223         "tx cpu %d, node %d, %d qp's\n", tx_cpu, node, min_use);
0224 
0225 out:
0226     if (tx_cpu >= 0)
0227         atomic_inc(&per_cpu(siw_use_cnt, tx_cpu));
0228     else
0229         pr_warn("siw: no tx cpu found\n");
0230 
0231     return tx_cpu;
0232 }
0233 
0234 void siw_put_tx_cpu(int cpu)
0235 {
0236     atomic_dec(&per_cpu(siw_use_cnt, cpu));
0237 }
0238 
0239 static struct ib_qp *siw_get_base_qp(struct ib_device *base_dev, int id)
0240 {
0241     struct siw_qp *qp = siw_qp_id2obj(to_siw_dev(base_dev), id);
0242 
0243     if (qp) {
0244         /*
0245          * siw_qp_id2obj() increments object reference count
0246          */
0247         siw_qp_put(qp);
0248         return &qp->base_qp;
0249     }
0250     return NULL;
0251 }
0252 
0253 static const struct ib_device_ops siw_device_ops = {
0254     .owner = THIS_MODULE,
0255     .uverbs_abi_ver = SIW_ABI_VERSION,
0256     .driver_id = RDMA_DRIVER_SIW,
0257 
0258     .alloc_mr = siw_alloc_mr,
0259     .alloc_pd = siw_alloc_pd,
0260     .alloc_ucontext = siw_alloc_ucontext,
0261     .create_cq = siw_create_cq,
0262     .create_qp = siw_create_qp,
0263     .create_srq = siw_create_srq,
0264     .dealloc_driver = siw_device_cleanup,
0265     .dealloc_pd = siw_dealloc_pd,
0266     .dealloc_ucontext = siw_dealloc_ucontext,
0267     .dereg_mr = siw_dereg_mr,
0268     .destroy_cq = siw_destroy_cq,
0269     .destroy_qp = siw_destroy_qp,
0270     .destroy_srq = siw_destroy_srq,
0271     .get_dma_mr = siw_get_dma_mr,
0272     .get_port_immutable = siw_get_port_immutable,
0273     .iw_accept = siw_accept,
0274     .iw_add_ref = siw_qp_get_ref,
0275     .iw_connect = siw_connect,
0276     .iw_create_listen = siw_create_listen,
0277     .iw_destroy_listen = siw_destroy_listen,
0278     .iw_get_qp = siw_get_base_qp,
0279     .iw_reject = siw_reject,
0280     .iw_rem_ref = siw_qp_put_ref,
0281     .map_mr_sg = siw_map_mr_sg,
0282     .mmap = siw_mmap,
0283     .mmap_free = siw_mmap_free,
0284     .modify_qp = siw_verbs_modify_qp,
0285     .modify_srq = siw_modify_srq,
0286     .poll_cq = siw_poll_cq,
0287     .post_recv = siw_post_receive,
0288     .post_send = siw_post_send,
0289     .post_srq_recv = siw_post_srq_recv,
0290     .query_device = siw_query_device,
0291     .query_gid = siw_query_gid,
0292     .query_port = siw_query_port,
0293     .query_qp = siw_query_qp,
0294     .query_srq = siw_query_srq,
0295     .req_notify_cq = siw_req_notify_cq,
0296     .reg_user_mr = siw_reg_user_mr,
0297 
0298     INIT_RDMA_OBJ_SIZE(ib_cq, siw_cq, base_cq),
0299     INIT_RDMA_OBJ_SIZE(ib_pd, siw_pd, base_pd),
0300     INIT_RDMA_OBJ_SIZE(ib_qp, siw_qp, base_qp),
0301     INIT_RDMA_OBJ_SIZE(ib_srq, siw_srq, base_srq),
0302     INIT_RDMA_OBJ_SIZE(ib_ucontext, siw_ucontext, base_ucontext),
0303 };
0304 
0305 static struct siw_device *siw_device_create(struct net_device *netdev)
0306 {
0307     struct siw_device *sdev = NULL;
0308     struct ib_device *base_dev;
0309     int rv;
0310 
0311     sdev = ib_alloc_device(siw_device, base_dev);
0312     if (!sdev)
0313         return NULL;
0314 
0315     base_dev = &sdev->base_dev;
0316 
0317     sdev->netdev = netdev;
0318 
0319     if (netdev->type != ARPHRD_LOOPBACK && netdev->type != ARPHRD_NONE) {
0320         addrconf_addr_eui48((unsigned char *)&base_dev->node_guid,
0321                     netdev->dev_addr);
0322     } else {
0323         /*
0324          * This device does not have a HW address,
0325          * but connection mangagement lib expects gid != 0
0326          */
0327         size_t len = min_t(size_t, strlen(base_dev->name), 6);
0328         char addr[6] = { };
0329 
0330         memcpy(addr, base_dev->name, len);
0331         addrconf_addr_eui48((unsigned char *)&base_dev->node_guid,
0332                     addr);
0333     }
0334 
0335     base_dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND);
0336 
0337     base_dev->node_type = RDMA_NODE_RNIC;
0338     memcpy(base_dev->node_desc, SIW_NODE_DESC_COMMON,
0339            sizeof(SIW_NODE_DESC_COMMON));
0340 
0341     /*
0342      * Current model (one-to-one device association):
0343      * One Softiwarp device per net_device or, equivalently,
0344      * per physical port.
0345      */
0346     base_dev->phys_port_cnt = 1;
0347     base_dev->num_comp_vectors = num_possible_cpus();
0348 
0349     xa_init_flags(&sdev->qp_xa, XA_FLAGS_ALLOC1);
0350     xa_init_flags(&sdev->mem_xa, XA_FLAGS_ALLOC1);
0351 
0352     ib_set_device_ops(base_dev, &siw_device_ops);
0353     rv = ib_device_set_netdev(base_dev, netdev, 1);
0354     if (rv)
0355         goto error;
0356 
0357     memcpy(base_dev->iw_ifname, netdev->name,
0358            sizeof(base_dev->iw_ifname));
0359 
0360     /* Disable TCP port mapping */
0361     base_dev->iw_driver_flags = IW_F_NO_PORT_MAP;
0362 
0363     sdev->attrs.max_qp = SIW_MAX_QP;
0364     sdev->attrs.max_qp_wr = SIW_MAX_QP_WR;
0365     sdev->attrs.max_ord = SIW_MAX_ORD_QP;
0366     sdev->attrs.max_ird = SIW_MAX_IRD_QP;
0367     sdev->attrs.max_sge = SIW_MAX_SGE;
0368     sdev->attrs.max_sge_rd = SIW_MAX_SGE_RD;
0369     sdev->attrs.max_cq = SIW_MAX_CQ;
0370     sdev->attrs.max_cqe = SIW_MAX_CQE;
0371     sdev->attrs.max_mr = SIW_MAX_MR;
0372     sdev->attrs.max_pd = SIW_MAX_PD;
0373     sdev->attrs.max_mw = SIW_MAX_MW;
0374     sdev->attrs.max_srq = SIW_MAX_SRQ;
0375     sdev->attrs.max_srq_wr = SIW_MAX_SRQ_WR;
0376     sdev->attrs.max_srq_sge = SIW_MAX_SGE;
0377 
0378     INIT_LIST_HEAD(&sdev->cep_list);
0379     INIT_LIST_HEAD(&sdev->qp_list);
0380 
0381     atomic_set(&sdev->num_ctx, 0);
0382     atomic_set(&sdev->num_srq, 0);
0383     atomic_set(&sdev->num_qp, 0);
0384     atomic_set(&sdev->num_cq, 0);
0385     atomic_set(&sdev->num_mr, 0);
0386     atomic_set(&sdev->num_pd, 0);
0387 
0388     sdev->numa_node = dev_to_node(&netdev->dev);
0389     spin_lock_init(&sdev->lock);
0390 
0391     return sdev;
0392 error:
0393     ib_dealloc_device(base_dev);
0394 
0395     return NULL;
0396 }
0397 
0398 /*
0399  * Network link becomes unavailable. Mark all
0400  * affected QP's accordingly.
0401  */
0402 static void siw_netdev_down(struct work_struct *work)
0403 {
0404     struct siw_device *sdev =
0405         container_of(work, struct siw_device, netdev_down);
0406 
0407     struct siw_qp_attrs qp_attrs;
0408     struct list_head *pos, *tmp;
0409 
0410     memset(&qp_attrs, 0, sizeof(qp_attrs));
0411     qp_attrs.state = SIW_QP_STATE_ERROR;
0412 
0413     list_for_each_safe(pos, tmp, &sdev->qp_list) {
0414         struct siw_qp *qp = list_entry(pos, struct siw_qp, devq);
0415 
0416         down_write(&qp->state_lock);
0417         WARN_ON(siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE));
0418         up_write(&qp->state_lock);
0419     }
0420     ib_device_put(&sdev->base_dev);
0421 }
0422 
0423 static void siw_device_goes_down(struct siw_device *sdev)
0424 {
0425     if (ib_device_try_get(&sdev->base_dev)) {
0426         INIT_WORK(&sdev->netdev_down, siw_netdev_down);
0427         schedule_work(&sdev->netdev_down);
0428     }
0429 }
0430 
0431 static int siw_netdev_event(struct notifier_block *nb, unsigned long event,
0432                 void *arg)
0433 {
0434     struct net_device *netdev = netdev_notifier_info_to_dev(arg);
0435     struct ib_device *base_dev;
0436     struct siw_device *sdev;
0437 
0438     dev_dbg(&netdev->dev, "siw: event %lu\n", event);
0439 
0440     if (dev_net(netdev) != &init_net)
0441         return NOTIFY_OK;
0442 
0443     base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
0444     if (!base_dev)
0445         return NOTIFY_OK;
0446 
0447     sdev = to_siw_dev(base_dev);
0448 
0449     switch (event) {
0450     case NETDEV_UP:
0451         sdev->state = IB_PORT_ACTIVE;
0452         siw_port_event(sdev, 1, IB_EVENT_PORT_ACTIVE);
0453         break;
0454 
0455     case NETDEV_GOING_DOWN:
0456         siw_device_goes_down(sdev);
0457         break;
0458 
0459     case NETDEV_DOWN:
0460         sdev->state = IB_PORT_DOWN;
0461         siw_port_event(sdev, 1, IB_EVENT_PORT_ERR);
0462         break;
0463 
0464     case NETDEV_REGISTER:
0465         /*
0466          * Device registration now handled only by
0467          * rdma netlink commands. So it shall be impossible
0468          * to end up here with a valid siw device.
0469          */
0470         siw_dbg(base_dev, "unexpected NETDEV_REGISTER event\n");
0471         break;
0472 
0473     case NETDEV_UNREGISTER:
0474         ib_unregister_device_queued(&sdev->base_dev);
0475         break;
0476 
0477     case NETDEV_CHANGEADDR:
0478         siw_port_event(sdev, 1, IB_EVENT_LID_CHANGE);
0479         break;
0480     /*
0481      * Todo: Below netdev events are currently not handled.
0482      */
0483     case NETDEV_CHANGEMTU:
0484     case NETDEV_CHANGE:
0485         break;
0486 
0487     default:
0488         break;
0489     }
0490     ib_device_put(&sdev->base_dev);
0491 
0492     return NOTIFY_OK;
0493 }
0494 
0495 static struct notifier_block siw_netdev_nb = {
0496     .notifier_call = siw_netdev_event,
0497 };
0498 
0499 static int siw_newlink(const char *basedev_name, struct net_device *netdev)
0500 {
0501     struct ib_device *base_dev;
0502     struct siw_device *sdev = NULL;
0503     int rv = -ENOMEM;
0504 
0505     if (!siw_dev_qualified(netdev))
0506         return -EINVAL;
0507 
0508     base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW);
0509     if (base_dev) {
0510         ib_device_put(base_dev);
0511         return -EEXIST;
0512     }
0513     sdev = siw_device_create(netdev);
0514     if (sdev) {
0515         dev_dbg(&netdev->dev, "siw: new device\n");
0516 
0517         if (netif_running(netdev) && netif_carrier_ok(netdev))
0518             sdev->state = IB_PORT_ACTIVE;
0519         else
0520             sdev->state = IB_PORT_DOWN;
0521 
0522         rv = siw_device_register(sdev, basedev_name);
0523         if (rv)
0524             ib_dealloc_device(&sdev->base_dev);
0525     }
0526     return rv;
0527 }
0528 
0529 static struct rdma_link_ops siw_link_ops = {
0530     .type = "siw",
0531     .newlink = siw_newlink,
0532 };
0533 
0534 /*
0535  * siw_init_module - Initialize Softiwarp module and register with netdev
0536  *                   subsystem.
0537  */
0538 static __init int siw_init_module(void)
0539 {
0540     int rv;
0541     int nr_cpu;
0542 
0543     if (SENDPAGE_THRESH < SIW_MAX_INLINE) {
0544         pr_info("siw: sendpage threshold too small: %u\n",
0545             (int)SENDPAGE_THRESH);
0546         rv = -EINVAL;
0547         goto out_error;
0548     }
0549     rv = siw_init_cpulist();
0550     if (rv)
0551         goto out_error;
0552 
0553     rv = siw_cm_init();
0554     if (rv)
0555         goto out_error;
0556 
0557     if (!siw_create_tx_threads()) {
0558         pr_info("siw: Could not start any TX thread\n");
0559         rv = -ENOMEM;
0560         goto out_error;
0561     }
0562     /*
0563      * Locate CRC32 algorithm. If unsuccessful, fail
0564      * loading siw only, if CRC is required.
0565      */
0566     siw_crypto_shash = crypto_alloc_shash("crc32c", 0, 0);
0567     if (IS_ERR(siw_crypto_shash)) {
0568         pr_info("siw: Loading CRC32c failed: %ld\n",
0569             PTR_ERR(siw_crypto_shash));
0570         siw_crypto_shash = NULL;
0571         if (mpa_crc_required) {
0572             rv = -EOPNOTSUPP;
0573             goto out_error;
0574         }
0575     }
0576     rv = register_netdevice_notifier(&siw_netdev_nb);
0577     if (rv)
0578         goto out_error;
0579 
0580     rdma_link_register(&siw_link_ops);
0581 
0582     pr_info("SoftiWARP attached\n");
0583     return 0;
0584 
0585 out_error:
0586     for (nr_cpu = 0; nr_cpu < nr_cpu_ids; nr_cpu++) {
0587         if (siw_tx_thread[nr_cpu]) {
0588             siw_stop_tx_thread(nr_cpu);
0589             siw_tx_thread[nr_cpu] = NULL;
0590         }
0591     }
0592     if (siw_crypto_shash)
0593         crypto_free_shash(siw_crypto_shash);
0594 
0595     pr_info("SoftIWARP attach failed. Error: %d\n", rv);
0596 
0597     siw_cm_exit();
0598     siw_destroy_cpulist();
0599 
0600     return rv;
0601 }
0602 
0603 static void __exit siw_exit_module(void)
0604 {
0605     int cpu;
0606 
0607     for_each_possible_cpu(cpu) {
0608         if (siw_tx_thread[cpu]) {
0609             siw_stop_tx_thread(cpu);
0610             siw_tx_thread[cpu] = NULL;
0611         }
0612     }
0613     unregister_netdevice_notifier(&siw_netdev_nb);
0614     rdma_link_unregister(&siw_link_ops);
0615     ib_unregister_driver(RDMA_DRIVER_SIW);
0616 
0617     siw_cm_exit();
0618 
0619     siw_destroy_cpulist();
0620 
0621     if (siw_crypto_shash)
0622         crypto_free_shash(siw_crypto_shash);
0623 
0624     pr_info("SoftiWARP detached\n");
0625 }
0626 
0627 module_init(siw_init_module);
0628 module_exit(siw_exit_module);
0629 
0630 MODULE_ALIAS_RDMA_LINK("siw");