2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/netdevice.h>
34 #include <linux/pci.h>
37 #include "usnic_common_pkt_hdr.h"
38 #include "usnic_fwd.h"
39 #include "usnic_log.h"
41 static int usnic_fwd_devcmd_locked(struct usnic_fwd_dev
*ufdev
, int vnic_idx
,
42 enum vnic_devcmd_cmd cmd
, u64
*a0
,
46 struct net_device
*netdev
= ufdev
->netdev
;
48 lockdep_assert_held(&ufdev
->lock
);
50 status
= enic_api_devcmd_proxy_by_index(netdev
,
56 if (status
== ERR_EINVAL
&& cmd
== CMD_DEL_FILTER
) {
57 usnic_dbg("Dev %s vnic idx %u cmd %u already deleted",
58 ufdev
->name
, vnic_idx
, cmd
);
60 usnic_err("Dev %s vnic idx %u cmd %u failed with status %d\n",
61 ufdev
->name
, vnic_idx
, cmd
,
65 usnic_dbg("Dev %s vnic idx %u cmd %u success",
66 ufdev
->name
, vnic_idx
, cmd
);
72 static int usnic_fwd_devcmd(struct usnic_fwd_dev
*ufdev
, int vnic_idx
,
73 enum vnic_devcmd_cmd cmd
, u64
*a0
, u64
*a1
)
77 spin_lock(&ufdev
->lock
);
78 status
= usnic_fwd_devcmd_locked(ufdev
, vnic_idx
, cmd
, a0
, a1
);
79 spin_unlock(&ufdev
->lock
);
84 struct usnic_fwd_dev
*usnic_fwd_dev_alloc(struct pci_dev
*pdev
)
86 struct usnic_fwd_dev
*ufdev
;
88 ufdev
= kzalloc(sizeof(*ufdev
), GFP_KERNEL
);
93 ufdev
->netdev
= pci_get_drvdata(pdev
);
94 spin_lock_init(&ufdev
->lock
);
95 BUILD_BUG_ON(sizeof(ufdev
->name
) != sizeof(ufdev
->netdev
->name
));
96 strcpy(ufdev
->name
, ufdev
->netdev
->name
);
101 void usnic_fwd_dev_free(struct usnic_fwd_dev
*ufdev
)
106 void usnic_fwd_set_mac(struct usnic_fwd_dev
*ufdev
, char mac
[ETH_ALEN
])
108 spin_lock(&ufdev
->lock
);
109 memcpy(&ufdev
->mac
, mac
, sizeof(ufdev
->mac
));
110 spin_unlock(&ufdev
->lock
);
113 void usnic_fwd_add_ipaddr(struct usnic_fwd_dev
*ufdev
, __be32 inaddr
)
115 spin_lock(&ufdev
->lock
);
117 ufdev
->inaddr
= inaddr
;
118 spin_unlock(&ufdev
->lock
);
121 void usnic_fwd_del_ipaddr(struct usnic_fwd_dev
*ufdev
)
123 spin_lock(&ufdev
->lock
);
125 spin_unlock(&ufdev
->lock
);
128 void usnic_fwd_carrier_up(struct usnic_fwd_dev
*ufdev
)
130 spin_lock(&ufdev
->lock
);
132 spin_unlock(&ufdev
->lock
);
135 void usnic_fwd_carrier_down(struct usnic_fwd_dev
*ufdev
)
137 spin_lock(&ufdev
->lock
);
139 spin_unlock(&ufdev
->lock
);
142 void usnic_fwd_set_mtu(struct usnic_fwd_dev
*ufdev
, unsigned int mtu
)
144 spin_lock(&ufdev
->lock
);
146 spin_unlock(&ufdev
->lock
);
149 static int usnic_fwd_dev_ready_locked(struct usnic_fwd_dev
*ufdev
)
151 lockdep_assert_held(&ufdev
->lock
);
159 static int validate_filter_locked(struct usnic_fwd_dev
*ufdev
,
160 struct filter
*filter
)
163 lockdep_assert_held(&ufdev
->lock
);
165 if (filter
->type
== FILTER_IPV4_5TUPLE
) {
166 if (!(filter
->u
.ipv4
.flags
& FILTER_FIELD_5TUP_DST_AD
))
168 if (!(filter
->u
.ipv4
.flags
& FILTER_FIELD_5TUP_DST_PT
))
170 else if (ufdev
->inaddr
== 0)
172 else if (filter
->u
.ipv4
.dst_port
== 0)
174 else if (ntohl(ufdev
->inaddr
) != filter
->u
.ipv4
.dst_addr
)
183 static void fill_tlv(struct filter_tlv
*tlv
, struct filter
*filter
,
184 struct filter_action
*action
)
186 tlv
->type
= CLSF_TLV_FILTER
;
187 tlv
->length
= sizeof(struct filter
);
188 *((struct filter
*)&tlv
->val
) = *filter
;
190 tlv
= (struct filter_tlv
*)((char *)tlv
+ sizeof(struct filter_tlv
) +
191 sizeof(struct filter
));
192 tlv
->type
= CLSF_TLV_ACTION
;
193 tlv
->length
= sizeof(struct filter_action
);
194 *((struct filter_action
*)&tlv
->val
) = *action
;
197 struct usnic_fwd_flow
*
198 usnic_fwd_alloc_flow(struct usnic_fwd_dev
*ufdev
, struct filter
*filter
,
199 struct usnic_filter_action
*uaction
)
201 struct filter_tlv
*tlv
;
202 struct pci_dev
*pdev
;
203 struct usnic_fwd_flow
*flow
;
210 tlv_size
= (2*sizeof(struct filter_tlv
) + sizeof(struct filter
) +
211 sizeof(struct filter_action
));
213 flow
= kzalloc(sizeof(*flow
), GFP_ATOMIC
);
215 return ERR_PTR(-ENOMEM
);
217 tlv
= dma_alloc_coherent(&pdev
->dev
, tlv_size
, &tlv_pa
, GFP_ATOMIC
);
219 usnic_err("Failed to allocate memory\n");
224 fill_tlv(tlv
, filter
, &uaction
->action
);
226 spin_lock(&ufdev
->lock
);
227 status
= usnic_fwd_dev_ready_locked(ufdev
);
229 usnic_err("Forwarding dev %s not ready with status %d\n",
230 ufdev
->name
, status
);
234 status
= validate_filter_locked(ufdev
, filter
);
236 usnic_err("Failed to validate filter with status %d\n",
244 status
= usnic_fwd_devcmd_locked(ufdev
, uaction
->vnic_idx
,
245 CMD_ADD_FILTER
, &a0
, &a1
);
247 usnic_err("VF %s Filter add failed with status:%d",
248 ufdev
->name
, status
);
252 usnic_dbg("VF %s FILTER ID:%llu", ufdev
->name
, a0
);
255 flow
->flow_id
= (uint32_t) a0
;
256 flow
->vnic_idx
= uaction
->vnic_idx
;
260 spin_unlock(&ufdev
->lock
);
261 dma_free_coherent(&pdev
->dev
, tlv_size
, tlv
, tlv_pa
);
266 return ERR_PTR(status
);
269 int usnic_fwd_dealloc_flow(struct usnic_fwd_flow
*flow
)
276 status
= usnic_fwd_devcmd(flow
->ufdev
, flow
->vnic_idx
,
277 CMD_DEL_FILTER
, &a0
, &a1
);
279 if (status
== ERR_EINVAL
) {
280 usnic_dbg("Filter %u already deleted for VF Idx %u pf: %s status: %d",
281 flow
->flow_id
, flow
->vnic_idx
,
282 flow
->ufdev
->name
, status
);
284 usnic_err("PF %s VF Idx %u Filter: %u FILTER DELETE failed with status %d",
285 flow
->ufdev
->name
, flow
->vnic_idx
,
286 flow
->flow_id
, status
);
290 * Log the error and fake success to the caller because if
291 * a flow fails to be deleted in the firmware, it is an
292 * unrecoverable error.
295 usnic_dbg("PF %s VF Idx %u Filter: %u FILTER DELETED",
296 flow
->ufdev
->name
, flow
->vnic_idx
,
304 int usnic_fwd_enable_qp(struct usnic_fwd_dev
*ufdev
, int vnic_idx
, int qp_idx
)
307 struct net_device
*pf_netdev
;
310 pf_netdev
= ufdev
->netdev
;
314 status
= usnic_fwd_devcmd(ufdev
, vnic_idx
, CMD_QP_ENABLE
,
317 usnic_err("PF %s VNIC Index %u RQ Index: %u ENABLE Failed with status %d",
318 netdev_name(pf_netdev
),
323 usnic_dbg("PF %s VNIC Index %u RQ Index: %u ENABLED",
324 netdev_name(pf_netdev
),
331 int usnic_fwd_disable_qp(struct usnic_fwd_dev
*ufdev
, int vnic_idx
, int qp_idx
)
335 struct net_device
*pf_netdev
;
337 pf_netdev
= ufdev
->netdev
;
341 status
= usnic_fwd_devcmd(ufdev
, vnic_idx
, CMD_QP_DISABLE
,
344 usnic_err("PF %s VNIC Index %u RQ Index: %u DISABLE Failed with status %d",
345 netdev_name(pf_netdev
),
350 usnic_dbg("PF %s VNIC Index %u RQ Index: %u DISABLED",
351 netdev_name(pf_netdev
),