|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Netronome Systems, Inc. |
| 3 | + * |
| 4 | + * This software is dual licensed under the GNU General License Version 2, |
| 5 | + * June 1991 as shown in the file COPYING in the top-level directory of this |
| 6 | + * source tree or the BSD 2-Clause License provided below. You have the |
| 7 | + * option to license this software under the complete terms of either license. |
| 8 | + * |
| 9 | + * The BSD 2-Clause License: |
| 10 | + * |
| 11 | + * Redistribution and use in source and binary forms, with or |
| 12 | + * without modification, are permitted provided that the following |
| 13 | + * conditions are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above |
| 16 | + * copyright notice, this list of conditions and the following |
| 17 | + * disclaimer. |
| 18 | + * |
| 19 | + * 2. Redistributions in binary form must reproduce the above |
| 20 | + * copyright notice, this list of conditions and the following |
| 21 | + * disclaimer in the documentation and/or other materials |
| 22 | + * provided with the distribution. |
| 23 | + * |
| 24 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | + * SOFTWARE. |
| 32 | + */ |
| 33 | + |
| 34 | +#include <linux/etherdevice.h> |
| 35 | +#include <linux/lockdep.h> |
| 36 | +#include <net/dst_metadata.h> |
| 37 | + |
| 38 | +#include "nfpcore/nfp_cpp.h" |
| 39 | +#include "nfp_app.h" |
| 40 | +#include "nfp_main.h" |
| 41 | +#include "nfp_net_repr.h" |
| 42 | +#include "nfp_port.h" |
| 43 | + |
| 44 | +static void nfp_repr_clean(struct nfp_repr *repr) |
| 45 | +{ |
| 46 | + unregister_netdev(repr->netdev); |
| 47 | + dst_release((struct dst_entry *)repr->dst); |
| 48 | + nfp_port_free(repr->port); |
| 49 | +} |
| 50 | + |
| 51 | +static struct lock_class_key nfp_repr_netdev_xmit_lock_key; |
| 52 | +static struct lock_class_key nfp_repr_netdev_addr_lock_key; |
| 53 | + |
| 54 | +static void nfp_repr_set_lockdep_class_one(struct net_device *dev, |
| 55 | + struct netdev_queue *txq, |
| 56 | + void *_unused) |
| 57 | +{ |
| 58 | + lockdep_set_class(&txq->_xmit_lock, &nfp_repr_netdev_xmit_lock_key); |
| 59 | +} |
| 60 | + |
| 61 | +static void nfp_repr_set_lockdep_class(struct net_device *dev) |
| 62 | +{ |
| 63 | + lockdep_set_class(&dev->addr_list_lock, &nfp_repr_netdev_addr_lock_key); |
| 64 | + netdev_for_each_tx_queue(dev, nfp_repr_set_lockdep_class_one, NULL); |
| 65 | +} |
| 66 | + |
| 67 | +int nfp_repr_init(struct nfp_app *app, struct net_device *netdev, |
| 68 | + const struct net_device_ops *netdev_ops, u32 cmsg_port_id, |
| 69 | + struct nfp_port *port, struct net_device *pf_netdev) |
| 70 | +{ |
| 71 | + struct nfp_repr *repr = netdev_priv(netdev); |
| 72 | + int err; |
| 73 | + |
| 74 | + nfp_repr_set_lockdep_class(netdev); |
| 75 | + |
| 76 | + repr->port = port; |
| 77 | + repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, GFP_KERNEL); |
| 78 | + if (!repr->dst) |
| 79 | + return -ENOMEM; |
| 80 | + repr->dst->u.port_info.port_id = cmsg_port_id; |
| 81 | + repr->dst->u.port_info.lower_dev = pf_netdev; |
| 82 | + |
| 83 | + netdev->netdev_ops = netdev_ops; |
| 84 | + |
| 85 | + err = register_netdev(netdev); |
| 86 | + if (err) |
| 87 | + goto err_clean; |
| 88 | + |
| 89 | + return 0; |
| 90 | + |
| 91 | +err_clean: |
| 92 | + dst_release((struct dst_entry *)repr->dst); |
| 93 | + return err; |
| 94 | +} |
| 95 | + |
| 96 | +struct net_device *nfp_repr_alloc(struct nfp_app *app) |
| 97 | +{ |
| 98 | + struct net_device *netdev; |
| 99 | + struct nfp_repr *repr; |
| 100 | + |
| 101 | + netdev = alloc_etherdev(sizeof(*repr)); |
| 102 | + if (!netdev) |
| 103 | + return NULL; |
| 104 | + |
| 105 | + repr = netdev_priv(netdev); |
| 106 | + repr->netdev = netdev; |
| 107 | + repr->app = app; |
| 108 | + |
| 109 | + return netdev; |
| 110 | +} |
| 111 | + |
| 112 | +static void nfp_repr_clean_and_free(struct nfp_repr *repr) |
| 113 | +{ |
| 114 | + nfp_info(repr->app->cpp, "Destroying Representor(%s)\n", |
| 115 | + repr->netdev->name); |
| 116 | + nfp_repr_clean(repr); |
| 117 | + free_netdev(repr->netdev); |
| 118 | +} |
| 119 | + |
| 120 | +void nfp_reprs_clean_and_free(struct nfp_reprs *reprs) |
| 121 | +{ |
| 122 | + unsigned int i; |
| 123 | + |
| 124 | + for (i = 0; i < reprs->num_reprs; i++) |
| 125 | + if (reprs->reprs[i]) |
| 126 | + nfp_repr_clean_and_free(netdev_priv(reprs->reprs[i])); |
| 127 | + |
| 128 | + kfree(reprs); |
| 129 | +} |
| 130 | + |
| 131 | +void |
| 132 | +nfp_reprs_clean_and_free_by_type(struct nfp_app *app, |
| 133 | + enum nfp_repr_type type) |
| 134 | +{ |
| 135 | + struct nfp_reprs *reprs; |
| 136 | + |
| 137 | + reprs = nfp_app_reprs_set(app, type, NULL); |
| 138 | + if (!reprs) |
| 139 | + return; |
| 140 | + |
| 141 | + synchronize_rcu(); |
| 142 | + nfp_reprs_clean_and_free(reprs); |
| 143 | +} |
| 144 | + |
| 145 | +struct nfp_reprs *nfp_reprs_alloc(unsigned int num_reprs) |
| 146 | +{ |
| 147 | + struct nfp_reprs *reprs; |
| 148 | + |
| 149 | + reprs = kzalloc(sizeof(*reprs) + |
| 150 | + num_reprs * sizeof(struct net_device *), GFP_KERNEL); |
| 151 | + if (!reprs) |
| 152 | + return NULL; |
| 153 | + reprs->num_reprs = num_reprs; |
| 154 | + |
| 155 | + return reprs; |
| 156 | +} |
0 commit comments