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 strncpy(ufdev
->name
, netdev_name(ufdev
->netdev
),
96 sizeof(ufdev
->name
) - 1);
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 int usnic_fwd_add_ipaddr(struct usnic_fwd_dev
*ufdev
, __be32 inaddr
)
117 spin_lock(&ufdev
->lock
);
118 if (ufdev
->inaddr
== 0) {
119 ufdev
->inaddr
= inaddr
;
124 spin_unlock(&ufdev
->lock
);
129 void usnic_fwd_del_ipaddr(struct usnic_fwd_dev
*ufdev
)
131 spin_lock(&ufdev
->lock
);
133 spin_unlock(&ufdev
->lock
);
136 void usnic_fwd_carrier_up(struct usnic_fwd_dev
*ufdev
)
138 spin_lock(&ufdev
->lock
);
140 spin_unlock(&ufdev
->lock
);
143 void usnic_fwd_carrier_down(struct usnic_fwd_dev
*ufdev
)
145 spin_lock(&ufdev
->lock
);
147 spin_unlock(&ufdev
->lock
);
150 void usnic_fwd_set_mtu(struct usnic_fwd_dev
*ufdev
, unsigned int mtu
)
152 spin_lock(&ufdev
->lock
);
154 spin_unlock(&ufdev
->lock
);
157 static int usnic_fwd_dev_ready_locked(struct usnic_fwd_dev
*ufdev
)
159 lockdep_assert_held(&ufdev
->lock
);
167 static int validate_filter_locked(struct usnic_fwd_dev
*ufdev
,
168 struct filter
*filter
)
171 lockdep_assert_held(&ufdev
->lock
);
173 if (filter
->type
== FILTER_IPV4_5TUPLE
) {
174 if (!(filter
->u
.ipv4
.flags
& FILTER_FIELD_5TUP_DST_AD
))
176 if (!(filter
->u
.ipv4
.flags
& FILTER_FIELD_5TUP_DST_PT
))
178 else if (ufdev
->inaddr
== 0)
180 else if (filter
->u
.ipv4
.dst_port
== 0)
182 else if (ntohl(ufdev
->inaddr
) != filter
->u
.ipv4
.dst_addr
)
191 static void fill_tlv(struct filter_tlv
*tlv
, struct filter
*filter
,
192 struct filter_action
*action
)
194 tlv
->type
= CLSF_TLV_FILTER
;
195 tlv
->length
= sizeof(struct filter
);
196 *((struct filter
*)&tlv
->val
) = *filter
;
198 tlv
= (struct filter_tlv
*)((char *)tlv
+ sizeof(struct filter_tlv
) +
199 sizeof(struct filter
));
200 tlv
->type
= CLSF_TLV_ACTION
;
201 tlv
->length
= sizeof(struct filter_action
);
202 *((struct filter_action
*)&tlv
->val
) = *action
;
205 struct usnic_fwd_flow
*
206 usnic_fwd_alloc_flow(struct usnic_fwd_dev
*ufdev
, struct filter
*filter
,
207 struct usnic_filter_action
*uaction
)
209 struct filter_tlv
*tlv
;
210 struct pci_dev
*pdev
;
211 struct usnic_fwd_flow
*flow
;
218 tlv_size
= (2*sizeof(struct filter_tlv
) + sizeof(struct filter
) +
219 sizeof(struct filter_action
));
221 flow
= kzalloc(sizeof(*flow
), GFP_ATOMIC
);
223 return ERR_PTR(-ENOMEM
);
225 tlv
= pci_alloc_consistent(pdev
, tlv_size
, &tlv_pa
);
227 usnic_err("Failed to allocate memory\n");
232 fill_tlv(tlv
, filter
, &uaction
->action
);
234 spin_lock(&ufdev
->lock
);
235 status
= usnic_fwd_dev_ready_locked(ufdev
);
237 usnic_err("Forwarding dev %s not ready with status %d\n",
238 ufdev
->name
, status
);
242 status
= validate_filter_locked(ufdev
, filter
);
244 usnic_err("Failed to validate filter with status %d\n",
252 status
= usnic_fwd_devcmd_locked(ufdev
, uaction
->vnic_idx
,
253 CMD_ADD_FILTER
, &a0
, &a1
);
255 usnic_err("VF %s Filter add failed with status:%d",
256 ufdev
->name
, status
);
260 usnic_dbg("VF %s FILTER ID:%llu", ufdev
->name
, a0
);
263 flow
->flow_id
= (uint32_t) a0
;
264 flow
->vnic_idx
= uaction
->vnic_idx
;
268 spin_unlock(&ufdev
->lock
);
269 pci_free_consistent(pdev
, tlv_size
, tlv
, tlv_pa
);
274 return ERR_PTR(status
);
277 int usnic_fwd_dealloc_flow(struct usnic_fwd_flow
*flow
)
284 status
= usnic_fwd_devcmd(flow
->ufdev
, flow
->vnic_idx
,
285 CMD_DEL_FILTER
, &a0
, &a1
);
287 if (status
== ERR_EINVAL
) {
288 usnic_dbg("Filter %u already deleted for VF Idx %u pf: %s status: %d",
289 flow
->flow_id
, flow
->vnic_idx
,
290 flow
->ufdev
->name
, status
);
292 usnic_err("PF %s VF Idx %u Filter: %u FILTER DELETE failed with status %d",
293 flow
->ufdev
->name
, flow
->vnic_idx
,
294 flow
->flow_id
, status
);
298 * Log the error and fake success to the caller because if
299 * a flow fails to be deleted in the firmware, it is an
300 * unrecoverable error.
303 usnic_dbg("PF %s VF Idx %u Filter: %u FILTER DELETED",
304 flow
->ufdev
->name
, flow
->vnic_idx
,
312 int usnic_fwd_enable_qp(struct usnic_fwd_dev
*ufdev
, int vnic_idx
, int qp_idx
)
315 struct net_device
*pf_netdev
;
318 pf_netdev
= ufdev
->netdev
;
322 status
= usnic_fwd_devcmd(ufdev
, vnic_idx
, CMD_QP_ENABLE
,
325 usnic_err("PF %s VNIC Index %u RQ Index: %u ENABLE Failed with status %d",
326 netdev_name(pf_netdev
),
331 usnic_dbg("PF %s VNIC Index %u RQ Index: %u ENABLED",
332 netdev_name(pf_netdev
),
339 int usnic_fwd_disable_qp(struct usnic_fwd_dev
*ufdev
, int vnic_idx
, int qp_idx
)
343 struct net_device
*pf_netdev
;
345 pf_netdev
= ufdev
->netdev
;
349 status
= usnic_fwd_devcmd(ufdev
, vnic_idx
, CMD_QP_DISABLE
,
352 usnic_err("PF %s VNIC Index %u RQ Index: %u DISABLE Failed with status %d",
353 netdev_name(pf_netdev
),
358 usnic_dbg("PF %s VNIC Index %u RQ Index: %u DISABLED",
359 netdev_name(pf_netdev
),