1 /*
2  * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3  *
4  * This program is free software; you may redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15  * SOFTWARE.
16  *
17  */
18 
19 #ifndef USNIC_FWD_H_
20 #define USNIC_FWD_H_
21 
22 #include <linux/if.h>
23 #include <linux/netdevice.h>
24 #include <linux/pci.h>
25 #include <linux/in.h>
26 
27 #include "usnic_abi.h"
28 #include "usnic_common_pkt_hdr.h"
29 #include "vnic_devcmd.h"
30 
31 struct usnic_fwd_dev {
32 	struct pci_dev			*pdev;
33 	struct net_device		*netdev;
34 	spinlock_t			lock;
35 	/*
36 	 * The following fields can be read directly off the device.
37 	 * However, they should be set by a accessor function, except name,
38 	 * which cannot be changed.
39 	 */
40 	bool				link_up;
41 	char				mac[ETH_ALEN];
42 	unsigned int			mtu;
43 	__be32				inaddr;
44 	char				name[IFNAMSIZ+1];
45 };
46 
47 struct usnic_fwd_flow {
48 	uint32_t			flow_id;
49 	struct usnic_fwd_dev		*ufdev;
50 	unsigned int			vnic_idx;
51 };
52 
53 struct usnic_filter_action {
54 	int				vnic_idx;
55 	struct filter_action		action;
56 };
57 
58 struct usnic_fwd_dev *usnic_fwd_dev_alloc(struct pci_dev *pdev);
59 void usnic_fwd_dev_free(struct usnic_fwd_dev *ufdev);
60 
61 void usnic_fwd_set_mac(struct usnic_fwd_dev *ufdev, char mac[ETH_ALEN]);
62 int usnic_fwd_add_ipaddr(struct usnic_fwd_dev *ufdev, __be32 inaddr);
63 void usnic_fwd_del_ipaddr(struct usnic_fwd_dev *ufdev);
64 void usnic_fwd_carrier_up(struct usnic_fwd_dev *ufdev);
65 void usnic_fwd_carrier_down(struct usnic_fwd_dev *ufdev);
66 void usnic_fwd_set_mtu(struct usnic_fwd_dev *ufdev, unsigned int mtu);
67 
68 /*
69  * Allocate a flow on this forwarding device. Whoever calls this function,
70  * must monitor netdev events on ufdev's netdevice. If NETDEV_REBOOT or
71  * NETDEV_DOWN is seen, flow will no longer function and must be
72  * immediately freed by calling usnic_dealloc_flow.
73  */
74 struct usnic_fwd_flow*
75 usnic_fwd_alloc_flow(struct usnic_fwd_dev *ufdev, struct filter *filter,
76 				struct usnic_filter_action *action);
77 int usnic_fwd_dealloc_flow(struct usnic_fwd_flow *flow);
78 int usnic_fwd_enable_qp(struct usnic_fwd_dev *ufdev, int vnic_idx, int qp_idx);
79 int usnic_fwd_disable_qp(struct usnic_fwd_dev *ufdev, int vnic_idx, int qp_idx);
80 
81 static inline void usnic_fwd_init_usnic_filter(struct filter *filter,
82 						uint32_t usnic_id)
83 {
84 	filter->type = FILTER_USNIC_ID;
85 	filter->u.usnic.ethtype = USNIC_ROCE_ETHERTYPE;
86 	filter->u.usnic.flags = FILTER_FIELD_USNIC_ETHTYPE |
87 				FILTER_FIELD_USNIC_ID |
88 				FILTER_FIELD_USNIC_PROTO;
89 	filter->u.usnic.proto_version = (USNIC_ROCE_GRH_VER <<
90 					 USNIC_ROCE_GRH_VER_SHIFT) |
91 					 USNIC_PROTO_VER;
92 	filter->u.usnic.usnic_id = usnic_id;
93 }
94 
95 static inline void usnic_fwd_init_udp_filter(struct filter *filter,
96 						uint32_t daddr, uint16_t dport)
97 {
98 	filter->type = FILTER_IPV4_5TUPLE;
99 	filter->u.ipv4.flags = FILTER_FIELD_5TUP_PROTO;
100 	filter->u.ipv4.protocol = PROTO_UDP;
101 
102 	if (daddr) {
103 		filter->u.ipv4.flags |= FILTER_FIELD_5TUP_DST_AD;
104 		filter->u.ipv4.dst_addr = daddr;
105 	}
106 
107 	if (dport) {
108 		filter->u.ipv4.flags |= FILTER_FIELD_5TUP_DST_PT;
109 		filter->u.ipv4.dst_port = dport;
110 	}
111 }
112 
113 #endif /* !USNIC_FWD_H_ */
114