gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / aquantia / atlantic / aq_main.c
blob9fcab646cbd5af0b40756c4810c8c0dba9170613
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * aQuantia Corporation Network Driver
4 * Copyright (C) 2014-2019 aQuantia Corporation. All rights reserved
5 */
7 /* File aq_main.c: Main file for aQuantia Linux driver. */
9 #include "aq_main.h"
10 #include "aq_nic.h"
11 #include "aq_pci_func.h"
12 #include "aq_ethtool.h"
13 #include "aq_ptp.h"
14 #include "aq_filters.h"
16 #include <linux/netdevice.h>
17 #include <linux/module.h>
18 #include <linux/ip.h>
19 #include <linux/udp.h>
21 MODULE_LICENSE("GPL v2");
22 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR);
23 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC);
25 static const char aq_ndev_driver_name[] = AQ_CFG_DRV_NAME;
27 static const struct net_device_ops aq_ndev_ops;
29 static struct workqueue_struct *aq_ndev_wq;
31 void aq_ndev_schedule_work(struct work_struct *work)
33 queue_work(aq_ndev_wq, work);
36 struct net_device *aq_ndev_alloc(void)
38 struct net_device *ndev = NULL;
39 struct aq_nic_s *aq_nic = NULL;
41 ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_CFG_VECS_MAX);
42 if (!ndev)
43 return NULL;
45 aq_nic = netdev_priv(ndev);
46 aq_nic->ndev = ndev;
47 ndev->netdev_ops = &aq_ndev_ops;
48 ndev->ethtool_ops = &aq_ethtool_ops;
50 return ndev;
53 static int aq_ndev_open(struct net_device *ndev)
55 struct aq_nic_s *aq_nic = netdev_priv(ndev);
56 int err = 0;
58 err = aq_nic_init(aq_nic);
59 if (err < 0)
60 goto err_exit;
62 err = aq_reapply_rxnfc_all_rules(aq_nic);
63 if (err < 0)
64 goto err_exit;
66 err = aq_filters_vlans_update(aq_nic);
67 if (err < 0)
68 goto err_exit;
70 err = aq_nic_start(aq_nic);
71 if (err < 0)
72 goto err_exit;
74 err_exit:
75 if (err < 0)
76 aq_nic_deinit(aq_nic, true);
78 return err;
81 static int aq_ndev_close(struct net_device *ndev)
83 struct aq_nic_s *aq_nic = netdev_priv(ndev);
84 int err = 0;
86 err = aq_nic_stop(aq_nic);
87 if (err < 0)
88 goto err_exit;
89 aq_nic_deinit(aq_nic, true);
91 err_exit:
92 return err;
95 static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
97 struct aq_nic_s *aq_nic = netdev_priv(ndev);
99 if (unlikely(aq_utils_obj_test(&aq_nic->flags, AQ_NIC_PTP_DPATH_UP))) {
100 /* Hardware adds the Timestamp for PTPv2 802.AS1
101 * and PTPv2 IPv4 UDP.
102 * We have to push even general 320 port messages to the ptp
103 * queue explicitly. This is a limitation of current firmware
104 * and hardware PTP design of the chip. Otherwise ptp stream
105 * will fail to sync
107 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
108 unlikely((ip_hdr(skb)->version == 4) &&
109 (ip_hdr(skb)->protocol == IPPROTO_UDP) &&
110 ((udp_hdr(skb)->dest == htons(319)) ||
111 (udp_hdr(skb)->dest == htons(320)))) ||
112 unlikely(eth_hdr(skb)->h_proto == htons(ETH_P_1588)))
113 return aq_ptp_xmit(aq_nic, skb);
116 skb_tx_timestamp(skb);
117 return aq_nic_xmit(aq_nic, skb);
120 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
122 struct aq_nic_s *aq_nic = netdev_priv(ndev);
123 int err;
125 err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
127 if (err < 0)
128 goto err_exit;
129 ndev->mtu = new_mtu;
131 err_exit:
132 return err;
135 static int aq_ndev_set_features(struct net_device *ndev,
136 netdev_features_t features)
138 bool is_vlan_tx_insert = !!(features & NETIF_F_HW_VLAN_CTAG_TX);
139 bool is_vlan_rx_strip = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
140 struct aq_nic_s *aq_nic = netdev_priv(ndev);
141 bool need_ndev_restart = false;
142 struct aq_nic_cfg_s *aq_cfg;
143 bool is_lro = false;
144 int err = 0;
146 aq_cfg = aq_nic_get_cfg(aq_nic);
148 if (!(features & NETIF_F_NTUPLE)) {
149 if (aq_nic->ndev->features & NETIF_F_NTUPLE) {
150 err = aq_clear_rxnfc_all_rules(aq_nic);
151 if (unlikely(err))
152 goto err_exit;
155 if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
156 if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) {
157 err = aq_filters_vlan_offload_off(aq_nic);
158 if (unlikely(err))
159 goto err_exit;
163 aq_cfg->features = features;
165 if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) {
166 is_lro = features & NETIF_F_LRO;
168 if (aq_cfg->is_lro != is_lro) {
169 aq_cfg->is_lro = is_lro;
170 need_ndev_restart = true;
174 if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) {
175 err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw,
176 aq_cfg);
178 if (unlikely(err))
179 goto err_exit;
182 if (aq_cfg->is_vlan_rx_strip != is_vlan_rx_strip) {
183 aq_cfg->is_vlan_rx_strip = is_vlan_rx_strip;
184 need_ndev_restart = true;
186 if (aq_cfg->is_vlan_tx_insert != is_vlan_tx_insert) {
187 aq_cfg->is_vlan_tx_insert = is_vlan_tx_insert;
188 need_ndev_restart = true;
191 if (need_ndev_restart && netif_running(ndev)) {
192 aq_ndev_close(ndev);
193 aq_ndev_open(ndev);
196 err_exit:
197 return err;
200 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
202 struct aq_nic_s *aq_nic = netdev_priv(ndev);
203 int err = 0;
205 err = eth_mac_addr(ndev, addr);
206 if (err < 0)
207 goto err_exit;
208 err = aq_nic_set_mac(aq_nic, ndev);
209 if (err < 0)
210 goto err_exit;
212 err_exit:
213 return err;
216 static void aq_ndev_set_multicast_settings(struct net_device *ndev)
218 struct aq_nic_s *aq_nic = netdev_priv(ndev);
220 (void)aq_nic_set_multicast_list(aq_nic, ndev);
223 static int aq_ndev_config_hwtstamp(struct aq_nic_s *aq_nic,
224 struct hwtstamp_config *config)
226 if (config->flags)
227 return -EINVAL;
229 switch (config->tx_type) {
230 case HWTSTAMP_TX_OFF:
231 case HWTSTAMP_TX_ON:
232 break;
233 default:
234 return -ERANGE;
237 switch (config->rx_filter) {
238 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
239 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
240 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
241 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
242 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
243 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
244 case HWTSTAMP_FILTER_PTP_V2_SYNC:
245 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
246 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
247 break;
248 case HWTSTAMP_FILTER_PTP_V2_EVENT:
249 case HWTSTAMP_FILTER_NONE:
250 break;
251 default:
252 return -ERANGE;
255 return aq_ptp_hwtstamp_config_set(aq_nic->aq_ptp, config);
258 static int aq_ndev_hwtstamp_set(struct aq_nic_s *aq_nic, struct ifreq *ifr)
260 struct hwtstamp_config config;
261 int ret_val;
263 if (!aq_nic->aq_ptp)
264 return -EOPNOTSUPP;
266 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
267 return -EFAULT;
269 ret_val = aq_ndev_config_hwtstamp(aq_nic, &config);
270 if (ret_val)
271 return ret_val;
273 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
274 -EFAULT : 0;
277 static int aq_ndev_hwtstamp_get(struct aq_nic_s *aq_nic, struct ifreq *ifr)
279 struct hwtstamp_config config;
281 if (!aq_nic->aq_ptp)
282 return -EOPNOTSUPP;
284 aq_ptp_hwtstamp_config_get(aq_nic->aq_ptp, &config);
285 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
286 -EFAULT : 0;
289 static int aq_ndev_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
291 struct aq_nic_s *aq_nic = netdev_priv(netdev);
293 switch (cmd) {
294 case SIOCSHWTSTAMP:
295 return aq_ndev_hwtstamp_set(aq_nic, ifr);
297 case SIOCGHWTSTAMP:
298 return aq_ndev_hwtstamp_get(aq_nic, ifr);
301 return -EOPNOTSUPP;
304 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto,
305 u16 vid)
307 struct aq_nic_s *aq_nic = netdev_priv(ndev);
309 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
310 return -EOPNOTSUPP;
312 set_bit(vid, aq_nic->active_vlans);
314 return aq_filters_vlans_update(aq_nic);
317 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto,
318 u16 vid)
320 struct aq_nic_s *aq_nic = netdev_priv(ndev);
322 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
323 return -EOPNOTSUPP;
325 clear_bit(vid, aq_nic->active_vlans);
327 if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid))
328 return aq_filters_vlans_update(aq_nic);
330 return 0;
333 static const struct net_device_ops aq_ndev_ops = {
334 .ndo_open = aq_ndev_open,
335 .ndo_stop = aq_ndev_close,
336 .ndo_start_xmit = aq_ndev_start_xmit,
337 .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
338 .ndo_change_mtu = aq_ndev_change_mtu,
339 .ndo_set_mac_address = aq_ndev_set_mac_address,
340 .ndo_set_features = aq_ndev_set_features,
341 .ndo_do_ioctl = aq_ndev_ioctl,
342 .ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid,
343 .ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid,
346 static int __init aq_ndev_init_module(void)
348 int ret;
350 aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name);
351 if (!aq_ndev_wq) {
352 pr_err("Failed to create workqueue\n");
353 return -ENOMEM;
356 ret = aq_pci_func_register_driver();
357 if (ret) {
358 destroy_workqueue(aq_ndev_wq);
359 return ret;
362 return 0;
365 static void __exit aq_ndev_exit_module(void)
367 aq_pci_func_unregister_driver();
369 if (aq_ndev_wq) {
370 destroy_workqueue(aq_ndev_wq);
371 aq_ndev_wq = NULL;
375 module_init(aq_ndev_init_module);
376 module_exit(aq_ndev_exit_module);