GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / aquantia / atlantic / aq_main.c
1 /*
2  * aQuantia Corporation Network Driver
3  * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  */
9
10 /* File aq_main.c: Main file for aQuantia Linux driver. */
11
12 #include "aq_main.h"
13 #include "aq_nic.h"
14 #include "aq_pci_func.h"
15 #include "aq_ethtool.h"
16 #include "hw_atl/hw_atl_a0.h"
17 #include "hw_atl/hw_atl_b0.h"
18
19 #include <linux/netdevice.h>
20 #include <linux/module.h>
21
22 static const struct pci_device_id aq_pci_tbl[] = {
23         { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_0001), },
24         { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D100), },
25         { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D107), },
26         { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D108), },
27         { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D109), },
28         {}
29 };
30
31 MODULE_DEVICE_TABLE(pci, aq_pci_tbl);
32
33 MODULE_LICENSE("GPL v2");
34 MODULE_VERSION(AQ_CFG_DRV_VERSION);
35 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR);
36 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC);
37
38 static struct aq_hw_ops *aq_pci_probe_get_hw_ops_by_id(struct pci_dev *pdev)
39 {
40         struct aq_hw_ops *ops = NULL;
41
42         ops = hw_atl_a0_get_ops_by_id(pdev);
43         if (!ops)
44                 ops = hw_atl_b0_get_ops_by_id(pdev);
45
46         return ops;
47 }
48
49 static int aq_ndev_open(struct net_device *ndev)
50 {
51         struct aq_nic_s *aq_nic = NULL;
52         int err = 0;
53
54         aq_nic = aq_nic_alloc_hot(ndev);
55         if (!aq_nic) {
56                 err = -ENOMEM;
57                 goto err_exit;
58         }
59         err = aq_nic_init(aq_nic);
60         if (err < 0)
61                 goto err_exit;
62         err = aq_nic_start(aq_nic);
63         if (err < 0) {
64                 aq_nic_stop(aq_nic);
65                 goto err_exit;
66         }
67
68 err_exit:
69         if (err < 0)
70                 aq_nic_deinit(aq_nic);
71         return err;
72 }
73
74 static int aq_ndev_close(struct net_device *ndev)
75 {
76         int err = 0;
77         struct aq_nic_s *aq_nic = netdev_priv(ndev);
78
79         err = aq_nic_stop(aq_nic);
80         if (err < 0)
81                 goto err_exit;
82         aq_nic_deinit(aq_nic);
83         aq_nic_free_hot_resources(aq_nic);
84
85 err_exit:
86         return err;
87 }
88
89 static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
90 {
91         struct aq_nic_s *aq_nic = netdev_priv(ndev);
92
93         return aq_nic_xmit(aq_nic, skb);
94 }
95
96 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
97 {
98         struct aq_nic_s *aq_nic = netdev_priv(ndev);
99         int err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
100
101         if (err < 0)
102                 goto err_exit;
103         ndev->mtu = new_mtu;
104
105 err_exit:
106         return err;
107 }
108
109 static int aq_ndev_set_features(struct net_device *ndev,
110                                 netdev_features_t features)
111 {
112         struct aq_nic_s *aq_nic = netdev_priv(ndev);
113         struct aq_nic_cfg_s *aq_cfg = aq_nic_get_cfg(aq_nic);
114         bool is_lro = false;
115
116         if (aq_cfg->hw_features & NETIF_F_LRO) {
117                 is_lro = features & NETIF_F_LRO;
118
119                 if (aq_cfg->is_lro != is_lro) {
120                         aq_cfg->is_lro = is_lro;
121
122                         if (netif_running(ndev)) {
123                                 aq_ndev_close(ndev);
124                                 aq_ndev_open(ndev);
125                         }
126                 }
127         }
128
129         return 0;
130 }
131
132 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
133 {
134         struct aq_nic_s *aq_nic = netdev_priv(ndev);
135         int err = 0;
136
137         err = eth_mac_addr(ndev, addr);
138         if (err < 0)
139                 goto err_exit;
140         err = aq_nic_set_mac(aq_nic, ndev);
141         if (err < 0)
142                 goto err_exit;
143
144 err_exit:
145         return err;
146 }
147
148 static void aq_ndev_set_multicast_settings(struct net_device *ndev)
149 {
150         struct aq_nic_s *aq_nic = netdev_priv(ndev);
151         int err = 0;
152
153         err = aq_nic_set_packet_filter(aq_nic, ndev->flags);
154         if (err < 0)
155                 goto err_exit;
156
157         if (netdev_mc_count(ndev)) {
158                 err = aq_nic_set_multicast_list(aq_nic, ndev);
159                 if (err < 0)
160                         goto err_exit;
161         }
162
163 err_exit:;
164 }
165
166 static const struct net_device_ops aq_ndev_ops = {
167         .ndo_open = aq_ndev_open,
168         .ndo_stop = aq_ndev_close,
169         .ndo_start_xmit = aq_ndev_start_xmit,
170         .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
171         .ndo_change_mtu = aq_ndev_change_mtu,
172         .ndo_set_mac_address = aq_ndev_set_mac_address,
173         .ndo_set_features = aq_ndev_set_features
174 };
175
176 static int aq_pci_probe(struct pci_dev *pdev,
177                         const struct pci_device_id *pci_id)
178 {
179         struct aq_hw_ops *aq_hw_ops = NULL;
180         struct aq_pci_func_s *aq_pci_func = NULL;
181         int err = 0;
182
183         err = pci_enable_device(pdev);
184         if (err < 0)
185                 goto err_exit;
186         aq_hw_ops = aq_pci_probe_get_hw_ops_by_id(pdev);
187         aq_pci_func = aq_pci_func_alloc(aq_hw_ops, pdev,
188                                         &aq_ndev_ops, &aq_ethtool_ops);
189         if (!aq_pci_func) {
190                 err = -ENOMEM;
191                 goto err_exit;
192         }
193         err = aq_pci_func_init(aq_pci_func);
194         if (err < 0)
195                 goto err_exit;
196
197 err_exit:
198         if (err < 0) {
199                 if (aq_pci_func)
200                         aq_pci_func_free(aq_pci_func);
201         }
202         return err;
203 }
204
205 static void aq_pci_remove(struct pci_dev *pdev)
206 {
207         struct aq_pci_func_s *aq_pci_func = pci_get_drvdata(pdev);
208
209         aq_pci_func_deinit(aq_pci_func);
210         aq_pci_func_free(aq_pci_func);
211 }
212
213 static int aq_pci_suspend(struct pci_dev *pdev, pm_message_t pm_msg)
214 {
215         struct aq_pci_func_s *aq_pci_func = pci_get_drvdata(pdev);
216
217         return aq_pci_func_change_pm_state(aq_pci_func, &pm_msg);
218 }
219
220 static int aq_pci_resume(struct pci_dev *pdev)
221 {
222         struct aq_pci_func_s *aq_pci_func = pci_get_drvdata(pdev);
223         pm_message_t pm_msg = PMSG_RESTORE;
224
225         return aq_pci_func_change_pm_state(aq_pci_func, &pm_msg);
226 }
227
228 static struct pci_driver aq_pci_ops = {
229         .name = AQ_CFG_DRV_NAME,
230         .id_table = aq_pci_tbl,
231         .probe = aq_pci_probe,
232         .remove = aq_pci_remove,
233         .suspend = aq_pci_suspend,
234         .resume = aq_pci_resume,
235 };
236
237 module_pci_driver(aq_pci_ops);