1*e6550b3eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*e6550b3eSThomas Gleixner // Copyright 2011 Cisco Systems, Inc.  All rights reserved.
3a6a5580cSJeff Kirsher 
4a6a5580cSJeff Kirsher #include <linux/pci.h>
5a6a5580cSJeff Kirsher #include <linux/etherdevice.h>
6a6a5580cSJeff Kirsher 
7a6a5580cSJeff Kirsher #include "vnic_dev.h"
8a6a5580cSJeff Kirsher #include "vnic_vic.h"
9a6a5580cSJeff Kirsher #include "enic_res.h"
10a6a5580cSJeff Kirsher #include "enic.h"
11a6a5580cSJeff Kirsher #include "enic_dev.h"
12a6a5580cSJeff Kirsher 
enic_dev_fw_info(struct enic * enic,struct vnic_devcmd_fw_info ** fw_info)13a6a5580cSJeff Kirsher int enic_dev_fw_info(struct enic *enic, struct vnic_devcmd_fw_info **fw_info)
14a6a5580cSJeff Kirsher {
15a6a5580cSJeff Kirsher 	int err;
16a6a5580cSJeff Kirsher 
178e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
18a6a5580cSJeff Kirsher 	err = vnic_dev_fw_info(enic->vdev, fw_info);
198e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
20a6a5580cSJeff Kirsher 
21a6a5580cSJeff Kirsher 	return err;
22a6a5580cSJeff Kirsher }
23a6a5580cSJeff Kirsher 
enic_dev_stats_dump(struct enic * enic,struct vnic_stats ** vstats)24a6a5580cSJeff Kirsher int enic_dev_stats_dump(struct enic *enic, struct vnic_stats **vstats)
25a6a5580cSJeff Kirsher {
26a6a5580cSJeff Kirsher 	int err;
27a6a5580cSJeff Kirsher 
288e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
29a6a5580cSJeff Kirsher 	err = vnic_dev_stats_dump(enic->vdev, vstats);
308e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
31a6a5580cSJeff Kirsher 
32a6a5580cSJeff Kirsher 	return err;
33a6a5580cSJeff Kirsher }
34a6a5580cSJeff Kirsher 
enic_dev_add_station_addr(struct enic * enic)35a6a5580cSJeff Kirsher int enic_dev_add_station_addr(struct enic *enic)
36a6a5580cSJeff Kirsher {
37a6a5580cSJeff Kirsher 	int err;
38a6a5580cSJeff Kirsher 
39a6a5580cSJeff Kirsher 	if (!is_valid_ether_addr(enic->netdev->dev_addr))
40a6a5580cSJeff Kirsher 		return -EADDRNOTAVAIL;
41a6a5580cSJeff Kirsher 
428e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
43a6a5580cSJeff Kirsher 	err = vnic_dev_add_addr(enic->vdev, enic->netdev->dev_addr);
448e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
45a6a5580cSJeff Kirsher 
46a6a5580cSJeff Kirsher 	return err;
47a6a5580cSJeff Kirsher }
48a6a5580cSJeff Kirsher 
enic_dev_del_station_addr(struct enic * enic)49a6a5580cSJeff Kirsher int enic_dev_del_station_addr(struct enic *enic)
50a6a5580cSJeff Kirsher {
51a6a5580cSJeff Kirsher 	int err;
52a6a5580cSJeff Kirsher 
53a6a5580cSJeff Kirsher 	if (!is_valid_ether_addr(enic->netdev->dev_addr))
54a6a5580cSJeff Kirsher 		return -EADDRNOTAVAIL;
55a6a5580cSJeff Kirsher 
568e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
57a6a5580cSJeff Kirsher 	err = vnic_dev_del_addr(enic->vdev, enic->netdev->dev_addr);
588e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
59a6a5580cSJeff Kirsher 
60a6a5580cSJeff Kirsher 	return err;
61a6a5580cSJeff Kirsher }
62a6a5580cSJeff Kirsher 
enic_dev_packet_filter(struct enic * enic,int directed,int multicast,int broadcast,int promisc,int allmulti)63a6a5580cSJeff Kirsher int enic_dev_packet_filter(struct enic *enic, int directed, int multicast,
64a6a5580cSJeff Kirsher 	int broadcast, int promisc, int allmulti)
65a6a5580cSJeff Kirsher {
66a6a5580cSJeff Kirsher 	int err;
67a6a5580cSJeff Kirsher 
688e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
69a6a5580cSJeff Kirsher 	err = vnic_dev_packet_filter(enic->vdev, directed,
70a6a5580cSJeff Kirsher 		multicast, broadcast, promisc, allmulti);
718e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
72a6a5580cSJeff Kirsher 
73a6a5580cSJeff Kirsher 	return err;
74a6a5580cSJeff Kirsher }
75a6a5580cSJeff Kirsher 
enic_dev_add_addr(struct enic * enic,const u8 * addr)76f009618aSAlexander Duyck int enic_dev_add_addr(struct enic *enic, const u8 *addr)
77a6a5580cSJeff Kirsher {
78a6a5580cSJeff Kirsher 	int err;
79a6a5580cSJeff Kirsher 
808e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
81a6a5580cSJeff Kirsher 	err = vnic_dev_add_addr(enic->vdev, addr);
828e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
83a6a5580cSJeff Kirsher 
84a6a5580cSJeff Kirsher 	return err;
85a6a5580cSJeff Kirsher }
86a6a5580cSJeff Kirsher 
enic_dev_del_addr(struct enic * enic,const u8 * addr)87f009618aSAlexander Duyck int enic_dev_del_addr(struct enic *enic, const u8 *addr)
88a6a5580cSJeff Kirsher {
89a6a5580cSJeff Kirsher 	int err;
90a6a5580cSJeff Kirsher 
918e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
92a6a5580cSJeff Kirsher 	err = vnic_dev_del_addr(enic->vdev, addr);
938e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
94a6a5580cSJeff Kirsher 
95a6a5580cSJeff Kirsher 	return err;
96a6a5580cSJeff Kirsher }
97a6a5580cSJeff Kirsher 
enic_dev_notify_unset(struct enic * enic)98a6a5580cSJeff Kirsher int enic_dev_notify_unset(struct enic *enic)
99a6a5580cSJeff Kirsher {
100a6a5580cSJeff Kirsher 	int err;
101a6a5580cSJeff Kirsher 
1028e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
103a6a5580cSJeff Kirsher 	err = vnic_dev_notify_unset(enic->vdev);
1048e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
105a6a5580cSJeff Kirsher 
106a6a5580cSJeff Kirsher 	return err;
107a6a5580cSJeff Kirsher }
108a6a5580cSJeff Kirsher 
enic_dev_hang_notify(struct enic * enic)109a6a5580cSJeff Kirsher int enic_dev_hang_notify(struct enic *enic)
110a6a5580cSJeff Kirsher {
111a6a5580cSJeff Kirsher 	int err;
112a6a5580cSJeff Kirsher 
1138e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
114a6a5580cSJeff Kirsher 	err = vnic_dev_hang_notify(enic->vdev);
1158e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
116a6a5580cSJeff Kirsher 
117a6a5580cSJeff Kirsher 	return err;
118a6a5580cSJeff Kirsher }
119a6a5580cSJeff Kirsher 
enic_dev_set_ig_vlan_rewrite_mode(struct enic * enic)120a6a5580cSJeff Kirsher int enic_dev_set_ig_vlan_rewrite_mode(struct enic *enic)
121a6a5580cSJeff Kirsher {
122a6a5580cSJeff Kirsher 	int err;
123a6a5580cSJeff Kirsher 
1248e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
125a6a5580cSJeff Kirsher 	err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
126a6a5580cSJeff Kirsher 		IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
1278e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
128a6a5580cSJeff Kirsher 
129a6a5580cSJeff Kirsher 	return err;
130a6a5580cSJeff Kirsher }
131a6a5580cSJeff Kirsher 
enic_dev_enable(struct enic * enic)132a6a5580cSJeff Kirsher int enic_dev_enable(struct enic *enic)
133a6a5580cSJeff Kirsher {
134a6a5580cSJeff Kirsher 	int err;
135a6a5580cSJeff Kirsher 
1368e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
137a6a5580cSJeff Kirsher 	err = vnic_dev_enable_wait(enic->vdev);
1388e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
139a6a5580cSJeff Kirsher 
140a6a5580cSJeff Kirsher 	return err;
141a6a5580cSJeff Kirsher }
142a6a5580cSJeff Kirsher 
enic_dev_disable(struct enic * enic)143a6a5580cSJeff Kirsher int enic_dev_disable(struct enic *enic)
144a6a5580cSJeff Kirsher {
145a6a5580cSJeff Kirsher 	int err;
146a6a5580cSJeff Kirsher 
1478e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
148a6a5580cSJeff Kirsher 	err = vnic_dev_disable(enic->vdev);
1498e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
150a6a5580cSJeff Kirsher 
151a6a5580cSJeff Kirsher 	return err;
152a6a5580cSJeff Kirsher }
153a6a5580cSJeff Kirsher 
enic_dev_intr_coal_timer_info(struct enic * enic)154a6a5580cSJeff Kirsher int enic_dev_intr_coal_timer_info(struct enic *enic)
155a6a5580cSJeff Kirsher {
156a6a5580cSJeff Kirsher 	int err;
157a6a5580cSJeff Kirsher 
1588e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
159a6a5580cSJeff Kirsher 	err = vnic_dev_intr_coal_timer_info(enic->vdev);
1608e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
161a6a5580cSJeff Kirsher 
162a6a5580cSJeff Kirsher 	return err;
163a6a5580cSJeff Kirsher }
164a6a5580cSJeff Kirsher 
165a6a5580cSJeff Kirsher /* rtnl lock is held */
enic_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)16680d5c368SPatrick McHardy int enic_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
167a6a5580cSJeff Kirsher {
168a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1698e586137SJiri Pirko 	int err;
170a6a5580cSJeff Kirsher 
1718e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
1728e586137SJiri Pirko 	err = enic_add_vlan(enic, vid);
1738e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
1748e586137SJiri Pirko 
1758e586137SJiri Pirko 	return err;
176a6a5580cSJeff Kirsher }
177a6a5580cSJeff Kirsher 
178a6a5580cSJeff Kirsher /* rtnl lock is held */
enic_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)17980d5c368SPatrick McHardy int enic_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
180a6a5580cSJeff Kirsher {
181a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1828e586137SJiri Pirko 	int err;
183a6a5580cSJeff Kirsher 
1848e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
1858e586137SJiri Pirko 	err = enic_del_vlan(enic, vid);
1868e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
1878e586137SJiri Pirko 
1888e586137SJiri Pirko 	return err;
189a6a5580cSJeff Kirsher }
190a6a5580cSJeff Kirsher 
enic_dev_status_to_errno(int devcmd_status)191a6a5580cSJeff Kirsher int enic_dev_status_to_errno(int devcmd_status)
192a6a5580cSJeff Kirsher {
193a6a5580cSJeff Kirsher 	switch (devcmd_status) {
194a6a5580cSJeff Kirsher 	case ERR_SUCCESS:
195a6a5580cSJeff Kirsher 		return 0;
196a6a5580cSJeff Kirsher 	case ERR_EINVAL:
197a6a5580cSJeff Kirsher 		return -EINVAL;
198a6a5580cSJeff Kirsher 	case ERR_EFAULT:
199a6a5580cSJeff Kirsher 		return -EFAULT;
200a6a5580cSJeff Kirsher 	case ERR_EPERM:
201a6a5580cSJeff Kirsher 		return -EPERM;
202a6a5580cSJeff Kirsher 	case ERR_EBUSY:
203a6a5580cSJeff Kirsher 		return -EBUSY;
204a6a5580cSJeff Kirsher 	case ERR_ECMDUNKNOWN:
205a6a5580cSJeff Kirsher 	case ERR_ENOTSUPPORTED:
206a6a5580cSJeff Kirsher 		return -EOPNOTSUPP;
207a6a5580cSJeff Kirsher 	case ERR_EBADSTATE:
208a6a5580cSJeff Kirsher 		return -EINVAL;
209a6a5580cSJeff Kirsher 	case ERR_ENOMEM:
210a6a5580cSJeff Kirsher 		return -ENOMEM;
211a6a5580cSJeff Kirsher 	case ERR_ETIMEDOUT:
212a6a5580cSJeff Kirsher 		return -ETIMEDOUT;
213a6a5580cSJeff Kirsher 	case ERR_ELINKDOWN:
214a6a5580cSJeff Kirsher 		return -ENETDOWN;
215a6a5580cSJeff Kirsher 	case ERR_EINPROGRESS:
216a6a5580cSJeff Kirsher 		return -EINPROGRESS;
217a6a5580cSJeff Kirsher 	case ERR_EMAXRES:
218a6a5580cSJeff Kirsher 	default:
219a6a5580cSJeff Kirsher 		return (devcmd_status < 0) ? devcmd_status : -1;
220a6a5580cSJeff Kirsher 	}
221a6a5580cSJeff Kirsher }
222