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