net/atlantic: implement Rx path
[dpdk.git] / drivers / net / atlantic / atl_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Aquantia Corporation
3  */
4
5 #include <rte_ethdev_pci.h>
6
7 #include "atl_ethdev.h"
8 #include "atl_common.h"
9 #include "atl_hw_regs.h"
10 #include "atl_logs.h"
11 #include "hw_atl/hw_atl_llh.h"
12 #include "hw_atl/hw_atl_b0.h"
13 #include "hw_atl/hw_atl_b0_internal.h"
14
15 static int eth_atl_dev_init(struct rte_eth_dev *eth_dev);
16 static int eth_atl_dev_uninit(struct rte_eth_dev *eth_dev);
17
18 static int  atl_dev_configure(struct rte_eth_dev *dev);
19 static int  atl_dev_start(struct rte_eth_dev *dev);
20 static void atl_dev_stop(struct rte_eth_dev *dev);
21 static void atl_dev_close(struct rte_eth_dev *dev);
22 static int  atl_dev_reset(struct rte_eth_dev *dev);
23
24 static int atl_fw_version_get(struct rte_eth_dev *dev, char *fw_version,
25                               size_t fw_size);
26
27 static void atl_dev_info_get(struct rte_eth_dev *dev,
28                                struct rte_eth_dev_info *dev_info);
29
30 static const uint32_t *atl_dev_supported_ptypes_get(struct rte_eth_dev *dev);
31
32 static int eth_atl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
33         struct rte_pci_device *pci_dev);
34 static int eth_atl_pci_remove(struct rte_pci_device *pci_dev);
35
36 static void atl_dev_info_get(struct rte_eth_dev *dev,
37                                 struct rte_eth_dev_info *dev_info);
38
39 int atl_logtype_init;
40 int atl_logtype_driver;
41
42 /*
43  * The set of PCI devices this driver supports
44  */
45 static const struct rte_pci_id pci_id_atl_map[] = {
46         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_0001) },
47         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D100) },
48         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D107) },
49         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D108) },
50         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D109) },
51
52         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC100) },
53         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC107) },
54         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC108) },
55         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC109) },
56         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111) },
57         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112) },
58
59         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC100S) },
60         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC107S) },
61         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC108S) },
62         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC109S) },
63         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111S) },
64         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112S) },
65
66         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111E) },
67         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112E) },
68         { .vendor_id = 0, /* sentinel */ },
69 };
70
71 static struct rte_pci_driver rte_atl_pmd = {
72         .id_table = pci_id_atl_map,
73         .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
74                      RTE_PCI_DRV_IOVA_AS_VA,
75         .probe = eth_atl_pci_probe,
76         .remove = eth_atl_pci_remove,
77 };
78
79 #define ATL_RX_OFFLOADS (DEV_RX_OFFLOAD_VLAN_STRIP \
80                         | DEV_RX_OFFLOAD_IPV4_CKSUM \
81                         | DEV_RX_OFFLOAD_UDP_CKSUM \
82                         | DEV_RX_OFFLOAD_TCP_CKSUM \
83                         | DEV_RX_OFFLOAD_JUMBO_FRAME)
84
85 static const struct rte_eth_desc_lim rx_desc_lim = {
86         .nb_max = ATL_MAX_RING_DESC,
87         .nb_min = ATL_MIN_RING_DESC,
88         .nb_align = ATL_RXD_ALIGN,
89 };
90
91 static const struct eth_dev_ops atl_eth_dev_ops = {
92         .dev_configure        = atl_dev_configure,
93         .dev_start            = atl_dev_start,
94         .dev_stop             = atl_dev_stop,
95         .dev_close            = atl_dev_close,
96         .dev_reset            = atl_dev_reset,
97
98         .fw_version_get       = atl_fw_version_get,
99         .dev_infos_get        = atl_dev_info_get,
100         .dev_supported_ptypes_get = atl_dev_supported_ptypes_get,
101
102         /* Queue Control */
103         .rx_queue_start       = atl_rx_queue_start,
104         .rx_queue_stop        = atl_rx_queue_stop,
105         .rx_queue_setup       = atl_rx_queue_setup,
106         .rx_queue_release     = atl_rx_queue_release,
107 };
108
109 static inline int32_t
110 atl_reset_hw(struct aq_hw_s *hw)
111 {
112         return hw_atl_b0_hw_reset(hw);
113 }
114
115 static int
116 eth_atl_dev_init(struct rte_eth_dev *eth_dev)
117 {
118         struct atl_adapter *adapter =
119                 (struct atl_adapter *)eth_dev->data->dev_private;
120         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
121         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
122         int err = 0;
123
124         PMD_INIT_FUNC_TRACE();
125
126         eth_dev->dev_ops = &atl_eth_dev_ops;
127         eth_dev->rx_pkt_burst = &atl_recv_pkts;
128         eth_dev->tx_pkt_burst = &atl_xmit_pkts;
129         eth_dev->tx_pkt_prepare = &atl_prep_pkts;
130
131         /* For secondary processes, the primary process has done all the work */
132         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
133                 return 0;
134
135         /* Vendor and Device ID need to be set before init of shared code */
136         hw->device_id = pci_dev->id.device_id;
137         hw->vendor_id = pci_dev->id.vendor_id;
138         hw->mmio = (void *)pci_dev->mem_resource[0].addr;
139
140         /* Hardware configuration - hardcode */
141         adapter->hw_cfg.is_lro = false;
142         adapter->hw_cfg.wol = false;
143
144         hw->aq_nic_cfg = &adapter->hw_cfg;
145
146         /* Allocate memory for storing MAC addresses */
147         eth_dev->data->mac_addrs = rte_zmalloc("atlantic", ETHER_ADDR_LEN, 0);
148         if (eth_dev->data->mac_addrs == NULL) {
149                 PMD_INIT_LOG(ERR, "MAC Malloc failed");
150                 return -ENOMEM;
151         }
152
153         err = hw_atl_utils_initfw(hw, &hw->aq_fw_ops);
154         if (err)
155                 return err;
156
157         /* Copy the permanent MAC address */
158         if (hw->aq_fw_ops->get_mac_permanent(hw,
159                         eth_dev->data->mac_addrs->addr_bytes) != 0)
160                 return -EINVAL;
161
162         return err;
163 }
164
165 static int
166 eth_atl_dev_uninit(struct rte_eth_dev *eth_dev)
167 {
168         struct aq_hw_s *hw;
169
170         PMD_INIT_FUNC_TRACE();
171
172         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
173                 return -EPERM;
174
175         hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
176
177         if (hw->adapter_stopped == 0)
178                 atl_dev_close(eth_dev);
179
180         eth_dev->dev_ops = NULL;
181         eth_dev->rx_pkt_burst = NULL;
182         eth_dev->tx_pkt_burst = NULL;
183
184         rte_free(eth_dev->data->mac_addrs);
185         eth_dev->data->mac_addrs = NULL;
186
187         return 0;
188 }
189
190 static int
191 eth_atl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
192         struct rte_pci_device *pci_dev)
193 {
194         return rte_eth_dev_pci_generic_probe(pci_dev,
195                 sizeof(struct atl_adapter), eth_atl_dev_init);
196 }
197
198 static int
199 eth_atl_pci_remove(struct rte_pci_device *pci_dev)
200 {
201         return rte_eth_dev_pci_generic_remove(pci_dev, eth_atl_dev_uninit);
202 }
203
204 static int
205 atl_dev_configure(struct rte_eth_dev *dev __rte_unused)
206 {
207         return 0;
208 }
209
210 /*
211  * Configure device link speed and setup link.
212  * It returns 0 on success.
213  */
214 static int
215 atl_dev_start(struct rte_eth_dev *dev)
216 {
217         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
218         int status;
219         int err;
220
221         PMD_INIT_FUNC_TRACE();
222
223         /* set adapter started */
224         hw->adapter_stopped = 0;
225
226         /* reinitialize adapter
227          * this calls reset and start
228          */
229         status = atl_reset_hw(hw);
230         if (status != 0)
231                 return -EIO;
232
233         err = hw_atl_b0_hw_init(hw, dev->data->mac_addrs->addr_bytes);
234
235         hw_atl_b0_hw_start(hw);
236         /* initialize transmission unit */
237         atl_tx_init(dev);
238
239         /* This can fail when allocating mbufs for descriptor rings */
240         err = atl_rx_init(dev);
241         if (err) {
242                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
243                 goto error;
244         }
245
246         PMD_INIT_LOG(DEBUG, "FW version: %u.%u.%u",
247                 hw->fw_ver_actual >> 24,
248                 (hw->fw_ver_actual >> 16) & 0xFF,
249                 hw->fw_ver_actual & 0xFFFF);
250         PMD_INIT_LOG(DEBUG, "Driver version: %s", ATL_PMD_DRIVER_VERSION);
251
252         err = atl_start_queues(dev);
253         if (err < 0) {
254                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
255                 goto error;
256         }
257
258         return 0;
259
260 error:
261         atl_stop_queues(dev);
262         return -EIO;
263 }
264
265 /*
266  * Stop device: disable rx and tx functions to allow for reconfiguring.
267  */
268 static void
269 atl_dev_stop(struct rte_eth_dev *dev)
270 {
271         struct aq_hw_s *hw =
272                 ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
273
274         /* reset the NIC */
275         atl_reset_hw(hw);
276         hw->adapter_stopped = 1;
277
278         atl_stop_queues(dev);
279
280         /* Clear stored conf */
281         dev->data->scattered_rx = 0;
282         dev->data->lro = 0;
283 }
284
285 /*
286  * Reset and stop device.
287  */
288 static void
289 atl_dev_close(struct rte_eth_dev *dev)
290 {
291         PMD_INIT_FUNC_TRACE();
292
293         atl_dev_stop(dev);
294
295         atl_free_queues(dev);
296 }
297
298 static int
299 atl_dev_reset(struct rte_eth_dev *dev)
300 {
301         int ret;
302
303         ret = eth_atl_dev_uninit(dev);
304         if (ret)
305                 return ret;
306
307         ret = eth_atl_dev_init(dev);
308
309         return ret;
310 }
311
312 static int
313 atl_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
314 {
315         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
316         uint32_t fw_ver = 0;
317         unsigned int ret = 0;
318
319         ret = hw_atl_utils_get_fw_version(hw, &fw_ver);
320         if (ret)
321                 return -EIO;
322
323         ret = snprintf(fw_version, fw_size, "%u.%u.%u", fw_ver >> 24,
324                        (fw_ver >> 16) & 0xFFU, fw_ver & 0xFFFFU);
325
326         ret += 1; /* add string null-terminator */
327
328         if (fw_size < ret)
329                 return ret;
330
331         return 0;
332 }
333
334 static void
335 atl_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
336 {
337         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
338
339         dev_info->max_rx_queues = AQ_HW_MAX_RX_QUEUES;
340         dev_info->max_tx_queues = AQ_HW_MAX_TX_QUEUES;
341
342         dev_info->min_rx_bufsize = 1024;
343         dev_info->max_rx_pktlen = HW_ATL_B0_MTU_JUMBO;
344         dev_info->max_mac_addrs = HW_ATL_B0_MAC_MAX;
345         dev_info->max_vfs = pci_dev->max_vfs;
346
347         dev_info->max_hash_mac_addrs = 0;
348         dev_info->max_vmdq_pools = 0;
349         dev_info->vmdq_queue_num = 0;
350
351         dev_info->rx_offload_capa = ATL_RX_OFFLOADS;
352
353         dev_info->default_rxconf = (struct rte_eth_rxconf) {
354                 .rx_free_thresh = ATL_DEFAULT_RX_FREE_THRESH,
355         };
356
357         dev_info->rx_desc_lim = rx_desc_lim;
358 }
359
360 static const uint32_t *
361 atl_dev_supported_ptypes_get(struct rte_eth_dev *dev)
362 {
363         static const uint32_t ptypes[] = {
364                 RTE_PTYPE_L2_ETHER,
365                 RTE_PTYPE_L2_ETHER_ARP,
366                 RTE_PTYPE_L2_ETHER_VLAN,
367                 RTE_PTYPE_L3_IPV4,
368                 RTE_PTYPE_L3_IPV6,
369                 RTE_PTYPE_L4_TCP,
370                 RTE_PTYPE_L4_UDP,
371                 RTE_PTYPE_L4_SCTP,
372                 RTE_PTYPE_L4_ICMP,
373                 RTE_PTYPE_UNKNOWN
374         };
375
376         if (dev->rx_pkt_burst == atl_recv_pkts)
377                 return ptypes;
378
379         return NULL;
380 }
381
382 RTE_PMD_REGISTER_PCI(net_atlantic, rte_atl_pmd);
383 RTE_PMD_REGISTER_PCI_TABLE(net_atlantic, pci_id_atl_map);
384 RTE_PMD_REGISTER_KMOD_DEP(net_atlantic, "* igb_uio | uio_pci_generic");
385
386 RTE_INIT(atl_init_log)
387 {
388         atl_logtype_init = rte_log_register("pmd.net.atlantic.init");
389         if (atl_logtype_init >= 0)
390                 rte_log_set_level(atl_logtype_init, RTE_LOG_NOTICE);
391         atl_logtype_driver = rte_log_register("pmd.net.atlantic.driver");
392         if (atl_logtype_driver >= 0)
393                 rte_log_set_level(atl_logtype_driver, RTE_LOG_NOTICE);
394 }
395