]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/netronome/nfp/flower/cmsg.c
Merge tag 'mlx5-updates-2017-06-27' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / netronome / nfp / flower / cmsg.c
CommitLineData
948faa46
SH
1/*
2 * Copyright (C) 2015-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/bitfield.h>
35#include <linux/netdevice.h>
36#include <linux/skbuff.h>
37#include <net/dst_metadata.h>
38
39#include "../nfpcore/nfp_cpp.h"
40#include "../nfp_net_repr.h"
41#include "./cmsg.h"
42
43#define nfp_flower_cmsg_warn(app, fmt, args...) \
44 do { \
45 if (net_ratelimit()) \
46 nfp_warn((app)->cpp, fmt, ## args); \
47 } while (0)
48
49static struct nfp_flower_cmsg_hdr *
50nfp_flower_cmsg_get_hdr(struct sk_buff *skb)
51{
52 return (struct nfp_flower_cmsg_hdr *)skb->data;
53}
54
55static void *nfp_flower_cmsg_get_data(struct sk_buff *skb)
56{
57 return (unsigned char *)skb->data + NFP_FLOWER_CMSG_HLEN;
58}
59
60static struct sk_buff *
61nfp_flower_cmsg_alloc(struct nfp_app *app, unsigned int size,
62 enum nfp_flower_cmsg_type_port type)
63{
64 struct nfp_flower_cmsg_hdr *ch;
65 struct sk_buff *skb;
66
67 size += NFP_FLOWER_CMSG_HLEN;
68
69 skb = nfp_app_ctrl_msg_alloc(app, size, GFP_KERNEL);
70 if (!skb)
71 return NULL;
72
73 ch = nfp_flower_cmsg_get_hdr(skb);
74 ch->pad = 0;
75 ch->version = NFP_FLOWER_CMSG_VER1;
76 ch->type = type;
77 skb_put(skb, size);
78
79 return skb;
80}
81
5d7c64a7 82int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok)
948faa46 83{
948faa46
SH
84 struct nfp_flower_cmsg_portmod *msg;
85 struct sk_buff *skb;
86
87 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
88 NFP_FLOWER_CMSG_TYPE_PORT_MOD);
89 if (!skb)
90 return -ENOMEM;
91
92 msg = nfp_flower_cmsg_get_data(skb);
93 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
94 msg->reserved = 0;
95 msg->info = carrier_ok;
5d7c64a7 96 msg->mtu = cpu_to_be16(repr->netdev->mtu);
948faa46
SH
97
98 nfp_ctrl_tx(repr->app->ctrl, skb);
99
100 return 0;
101}
102
103static void
104nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb)
105{
106 struct nfp_flower_cmsg_portmod *msg;
107 struct net_device *netdev;
108 bool link;
109
110 msg = nfp_flower_cmsg_get_data(skb);
111 link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK;
112
113 rcu_read_lock();
114 netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
115 if (!netdev) {
116 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
117 be32_to_cpu(msg->portnum));
118 rcu_read_unlock();
119 return;
120 }
121
122 if (link) {
123 netif_carrier_on(netdev);
124 rtnl_lock();
125 dev_set_mtu(netdev, be16_to_cpu(msg->mtu));
126 rtnl_unlock();
127 } else {
128 netif_carrier_off(netdev);
129 }
130 rcu_read_unlock();
131}
132
133void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb)
134{
135 struct nfp_flower_cmsg_hdr *cmsg_hdr;
136 enum nfp_flower_cmsg_type_port type;
137
138 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
139
140 if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) {
141 nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n",
142 cmsg_hdr->version);
143 goto out;
144 }
145
146 type = cmsg_hdr->type;
147 switch (type) {
148 case NFP_FLOWER_CMSG_TYPE_PORT_MOD:
149 nfp_flower_cmsg_portmod_rx(app, skb);
150 break;
151 default:
152 nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n",
153 type);
154 }
155
156out:
157 dev_kfree_skb_any(skb);
158}