qede: add L2 support
[dpdk.git] / drivers / net / qede / qede_ethdev.c
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #include "qede_ethdev.h"
10
11 /* Globals */
12 static const struct qed_eth_ops *qed_ops;
13 static const char *drivername = "qede pmd";
14
15 static void qede_interrupt_action(struct ecore_hwfn *p_hwfn)
16 {
17         ecore_int_sp_dpc((osal_int_ptr_t)(p_hwfn));
18 }
19
20 static void
21 qede_interrupt_handler(__rte_unused struct rte_intr_handle *handle, void *param)
22 {
23         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
24         struct qede_dev *qdev = eth_dev->data->dev_private;
25         struct ecore_dev *edev = &qdev->edev;
26
27         qede_interrupt_action(ECORE_LEADING_HWFN(edev));
28         if (rte_intr_enable(&eth_dev->pci_dev->intr_handle))
29                 DP_ERR(edev, "rte_intr_enable failed\n");
30 }
31
32 static void
33 qede_alloc_etherdev(struct qede_dev *qdev, struct qed_dev_eth_info *info)
34 {
35         rte_memcpy(&qdev->dev_info, info, sizeof(*info));
36         qdev->num_tc = qdev->dev_info.num_tc;
37         qdev->ops = qed_ops;
38 }
39
40 static void qede_print_adapter_info(struct qede_dev *qdev)
41 {
42         struct ecore_dev *edev = &qdev->edev;
43         struct qed_dev_info *info = &qdev->dev_info.common;
44         static char ver_str[QED_DRV_VER_STR_SIZE];
45
46         DP_INFO(edev, "*********************************\n");
47         DP_INFO(edev, " Chip details : %s%d\n",
48                 ECORE_IS_BB(edev) ? "BB" : "AH",
49                 CHIP_REV_IS_A0(edev) ? 0 : 1);
50
51         sprintf(ver_str, "%s %s_%d.%d.%d.%d", QEDE_PMD_VER_PREFIX,
52                 edev->ver_str, QEDE_PMD_VERSION_MAJOR, QEDE_PMD_VERSION_MINOR,
53                 QEDE_PMD_VERSION_REVISION, QEDE_PMD_VERSION_PATCH);
54         strcpy(qdev->drv_ver, ver_str);
55         DP_INFO(edev, " Driver version : %s\n", ver_str);
56
57         sprintf(ver_str, "%d.%d.%d.%d", info->fw_major, info->fw_minor,
58                 info->fw_rev, info->fw_eng);
59         DP_INFO(edev, " Firmware version : %s\n", ver_str);
60
61         sprintf(ver_str, "%d.%d.%d.%d",
62                 (info->mfw_rev >> 24) & 0xff,
63                 (info->mfw_rev >> 16) & 0xff,
64                 (info->mfw_rev >> 8) & 0xff, (info->mfw_rev) & 0xff);
65         DP_INFO(edev, " Management firmware version : %s\n", ver_str);
66
67         DP_INFO(edev, " Firmware file : %s\n", fw_file);
68
69         DP_INFO(edev, "*********************************\n");
70 }
71
72 static int
73 qede_set_ucast_rx_mac(struct qede_dev *qdev,
74                       enum qed_filter_xcast_params_type opcode,
75                       uint8_t mac[ETHER_ADDR_LEN])
76 {
77         struct ecore_dev *edev = &qdev->edev;
78         struct qed_filter_params filter_cmd;
79
80         memset(&filter_cmd, 0, sizeof(filter_cmd));
81         filter_cmd.type = QED_FILTER_TYPE_UCAST;
82         filter_cmd.filter.ucast.type = opcode;
83         filter_cmd.filter.ucast.mac_valid = 1;
84         rte_memcpy(&filter_cmd.filter.ucast.mac[0], &mac[0], ETHER_ADDR_LEN);
85         return qdev->ops->filter_config(edev, &filter_cmd);
86 }
87
88 static void
89 qede_mac_addr_add(struct rte_eth_dev *eth_dev, struct ether_addr *mac_addr,
90                   uint32_t index, __rte_unused uint32_t pool)
91 {
92         struct qede_dev *qdev = eth_dev->data->dev_private;
93         struct ecore_dev *edev = &qdev->edev;
94         int rc;
95
96         PMD_INIT_FUNC_TRACE(edev);
97
98         if (index >= qdev->dev_info.num_mac_addrs) {
99                 DP_ERR(edev, "Index %u is above MAC filter limit %u\n",
100                        index, qdev->dev_info.num_mac_addrs);
101                 return;
102         }
103
104         /* Adding macaddr even though promiscuous mode is set */
105         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1)
106                 DP_INFO(edev, "Port is in promisc mode, yet adding it\n");
107
108         /* Add MAC filters according to the unicast secondary macs */
109         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_ADD,
110                                    mac_addr->addr_bytes);
111         if (rc)
112                 DP_ERR(edev, "Unable to add macaddr rc=%d\n", rc);
113 }
114
115 static void
116 qede_mac_addr_remove(struct rte_eth_dev *eth_dev, uint32_t index)
117 {
118         struct qede_dev *qdev = eth_dev->data->dev_private;
119         struct ecore_dev *edev = &qdev->edev;
120         struct ether_addr mac_addr;
121         int rc;
122
123         PMD_INIT_FUNC_TRACE(edev);
124
125         if (index >= qdev->dev_info.num_mac_addrs) {
126                 DP_ERR(edev, "Index %u is above MAC filter limit %u\n",
127                        index, qdev->dev_info.num_mac_addrs);
128                 return;
129         }
130
131         /* Use the index maintained by rte */
132         ether_addr_copy(&eth_dev->data->mac_addrs[index], &mac_addr);
133         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_DEL,
134                                    mac_addr.addr_bytes);
135         if (rc)
136                 DP_ERR(edev, "Unable to remove macaddr rc=%d\n", rc);
137 }
138
139 static void
140 qede_mac_addr_set(struct rte_eth_dev *eth_dev, struct ether_addr *mac_addr)
141 {
142         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
143         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
144         int rc;
145
146         /* First remove the primary mac */
147         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_DEL,
148                                    qdev->primary_mac.addr_bytes);
149
150         if (rc) {
151                 DP_ERR(edev, "Unable to remove current macaddr"
152                              " Reverting to previous default mac\n");
153                 ether_addr_copy(&qdev->primary_mac,
154                                 &eth_dev->data->mac_addrs[0]);
155                 return;
156         }
157
158         /* Add new MAC */
159         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_ADD,
160                                    mac_addr->addr_bytes);
161
162         if (rc)
163                 DP_ERR(edev, "Unable to add new default mac\n");
164         else
165                 ether_addr_copy(mac_addr, &qdev->primary_mac);
166 }
167
168
169
170
171 static void qede_config_accept_any_vlan(struct qede_dev *qdev, bool action)
172 {
173         struct ecore_dev *edev = &qdev->edev;
174         struct qed_update_vport_params params = {
175                 .vport_id = 0,
176                 .accept_any_vlan = action,
177                 .update_accept_any_vlan_flg = 1,
178         };
179         int rc;
180
181         /* Proceed only if action actually needs to be performed */
182         if (qdev->accept_any_vlan == action)
183                 return;
184
185         rc = qdev->ops->vport_update(edev, &params);
186         if (rc) {
187                 DP_ERR(edev, "Failed to %s accept-any-vlan\n",
188                        action ? "enable" : "disable");
189         } else {
190                 DP_INFO(edev, "%s accept-any-vlan\n",
191                         action ? "enabled" : "disabled");
192                 qdev->accept_any_vlan = action;
193         }
194 }
195
196 void qede_config_rx_mode(struct rte_eth_dev *eth_dev)
197 {
198         struct qede_dev *qdev = eth_dev->data->dev_private;
199         struct ecore_dev *edev = &qdev->edev;
200         /* TODO: - QED_FILTER_TYPE_UCAST */
201         enum qed_filter_rx_mode_type accept_flags =
202                         QED_FILTER_RX_MODE_TYPE_REGULAR;
203         struct qed_filter_params rx_mode;
204         int rc;
205
206         /* Configure the struct for the Rx mode */
207         memset(&rx_mode, 0, sizeof(struct qed_filter_params));
208         rx_mode.type = QED_FILTER_TYPE_RX_MODE;
209
210         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_REPLACE,
211                                    eth_dev->data->mac_addrs[0].addr_bytes);
212         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1) {
213                 accept_flags = QED_FILTER_RX_MODE_TYPE_PROMISC;
214         } else {
215                 rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_ADD,
216                                            eth_dev->data->
217                                            mac_addrs[0].addr_bytes);
218                 if (rc) {
219                         DP_ERR(edev, "Unable to add filter\n");
220                         return;
221                 }
222         }
223
224         /* take care of VLAN mode */
225         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1) {
226                 qede_config_accept_any_vlan(qdev, true);
227         } else if (!qdev->non_configured_vlans) {
228                 /* If we dont have non-configured VLANs and promisc
229                  * is not set, then check if we need to disable
230                  * accept_any_vlan mode.
231                  * Because in this case, accept_any_vlan mode is set
232                  * as part of IFF_RPOMISC flag handling.
233                  */
234                 qede_config_accept_any_vlan(qdev, false);
235         }
236         rx_mode.filter.accept_flags = accept_flags;
237         rc = qdev->ops->filter_config(edev, &rx_mode);
238         if (rc)
239                 DP_ERR(edev, "Filter config failed rc=%d\n", rc);
240 }
241
242 static int qede_vlan_stripping(struct rte_eth_dev *eth_dev, bool set_stripping)
243 {
244         struct qed_update_vport_params vport_update_params;
245         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
246         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
247         int rc;
248
249         memset(&vport_update_params, 0, sizeof(vport_update_params));
250         vport_update_params.vport_id = 0;
251         vport_update_params.update_inner_vlan_removal_flg = 1;
252         vport_update_params.inner_vlan_removal_flg = set_stripping;
253         rc = qdev->ops->vport_update(edev, &vport_update_params);
254         if (rc) {
255                 DP_ERR(edev, "Update V-PORT failed %d\n", rc);
256                 return rc;
257         }
258
259         return 0;
260 }
261
262 static void qede_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
263 {
264         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
265         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
266
267         if (mask & ETH_VLAN_STRIP_MASK) {
268                 if (eth_dev->data->dev_conf.rxmode.hw_vlan_strip)
269                         (void)qede_vlan_stripping(eth_dev, 1);
270                 else
271                         (void)qede_vlan_stripping(eth_dev, 0);
272         }
273
274         DP_INFO(edev, "vlan offload mask %d vlan-strip %d\n",
275                 mask, eth_dev->data->dev_conf.rxmode.hw_vlan_strip);
276 }
277
278 static int qede_set_ucast_rx_vlan(struct qede_dev *qdev,
279                                   enum qed_filter_xcast_params_type opcode,
280                                   uint16_t vid)
281 {
282         struct qed_filter_params filter_cmd;
283         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
284
285         memset(&filter_cmd, 0, sizeof(filter_cmd));
286         filter_cmd.type = QED_FILTER_TYPE_UCAST;
287         filter_cmd.filter.ucast.type = opcode;
288         filter_cmd.filter.ucast.vlan_valid = 1;
289         filter_cmd.filter.ucast.vlan = vid;
290
291         return qdev->ops->filter_config(edev, &filter_cmd);
292 }
293
294 static int qede_vlan_filter_set(struct rte_eth_dev *eth_dev,
295                                 uint16_t vlan_id, int on)
296 {
297         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
298         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
299         struct qed_dev_eth_info *dev_info = &qdev->dev_info;
300         int rc;
301
302         if (vlan_id != 0 &&
303             qdev->configured_vlans == dev_info->num_vlan_filters) {
304                 DP_NOTICE(edev, false, "Reached max VLAN filter limit"
305                                      " enabling accept_any_vlan\n");
306                 qede_config_accept_any_vlan(qdev, true);
307                 return 0;
308         }
309
310         if (on) {
311                 rc = qede_set_ucast_rx_vlan(qdev, QED_FILTER_XCAST_TYPE_ADD,
312                                             vlan_id);
313                 if (rc)
314                         DP_ERR(edev, "Failed to add VLAN %u rc %d\n", vlan_id,
315                                rc);
316                 else
317                         if (vlan_id != 0)
318                                 qdev->configured_vlans++;
319         } else {
320                 rc = qede_set_ucast_rx_vlan(qdev, QED_FILTER_XCAST_TYPE_DEL,
321                                             vlan_id);
322                 if (rc)
323                         DP_ERR(edev, "Failed to delete VLAN %u rc %d\n",
324                                vlan_id, rc);
325                 else
326                         if (vlan_id != 0)
327                                 qdev->configured_vlans--;
328         }
329
330         DP_INFO(edev, "vlan_id %u on %u rc %d configured_vlans %u\n",
331                         vlan_id, on, rc, qdev->configured_vlans);
332
333         return rc;
334 }
335
336 static int qede_dev_configure(struct rte_eth_dev *eth_dev)
337 {
338         struct qede_dev *qdev = eth_dev->data->dev_private;
339         struct ecore_dev *edev = &qdev->edev;
340         struct rte_eth_rxmode *rxmode = &eth_dev->data->dev_conf.rxmode;
341
342         PMD_INIT_FUNC_TRACE(edev);
343
344         if (eth_dev->data->nb_rx_queues != eth_dev->data->nb_tx_queues) {
345                 DP_NOTICE(edev, false,
346                           "Unequal number of rx/tx queues "
347                           "is not supported RX=%u TX=%u\n",
348                           eth_dev->data->nb_rx_queues,
349                           eth_dev->data->nb_tx_queues);
350                 return -EINVAL;
351         }
352
353         qdev->num_rss = eth_dev->data->nb_rx_queues;
354
355         /* Initial state */
356         qdev->state = QEDE_CLOSE;
357
358         /* Sanity checks and throw warnings */
359
360         if (rxmode->enable_scatter == 1) {
361                 DP_ERR(edev, "RX scatter packets is not supported\n");
362                 return -EINVAL;
363         }
364
365         if (rxmode->enable_lro == 1) {
366                 DP_INFO(edev, "LRO is not supported\n");
367                 return -EINVAL;
368         }
369
370         if (!rxmode->hw_strip_crc)
371                 DP_INFO(edev, "L2 CRC stripping is always enabled in hw\n");
372
373         if (!rxmode->hw_ip_checksum)
374                 DP_INFO(edev, "IP/UDP/TCP checksum offload is always enabled "
375                               "in hw\n");
376
377
378         DP_INFO(edev, "Allocated %d RSS queues on %d TC/s\n",
379                 QEDE_RSS_CNT(qdev), qdev->num_tc);
380
381         DP_INFO(edev, "my_id %u rel_pf_id %u abs_pf_id %u"
382                 " port %u first_on_engine %d\n",
383                 edev->hwfns[0].my_id,
384                 edev->hwfns[0].rel_pf_id,
385                 edev->hwfns[0].abs_pf_id,
386                 edev->hwfns[0].port_id, edev->hwfns[0].first_on_engine);
387
388         return 0;
389 }
390
391 /* Info about HW descriptor ring limitations */
392 static const struct rte_eth_desc_lim qede_rx_desc_lim = {
393         .nb_max = NUM_RX_BDS_MAX,
394         .nb_min = 128,
395         .nb_align = 128 /* lowest common multiple */
396 };
397
398 static const struct rte_eth_desc_lim qede_tx_desc_lim = {
399         .nb_max = NUM_TX_BDS_MAX,
400         .nb_min = 256,
401         .nb_align = 256
402 };
403
404 static void
405 qede_dev_info_get(struct rte_eth_dev *eth_dev,
406                   struct rte_eth_dev_info *dev_info)
407 {
408         struct qede_dev *qdev = eth_dev->data->dev_private;
409         struct ecore_dev *edev = &qdev->edev;
410
411         PMD_INIT_FUNC_TRACE(edev);
412
413         dev_info->min_rx_bufsize = (uint32_t)(ETHER_MIN_MTU +
414                                               QEDE_ETH_OVERHEAD);
415         dev_info->max_rx_pktlen = (uint32_t)ETH_TX_MAX_NON_LSO_PKT_LEN;
416         dev_info->rx_desc_lim = qede_rx_desc_lim;
417         dev_info->tx_desc_lim = qede_tx_desc_lim;
418         dev_info->max_rx_queues = (uint16_t)QEDE_MAX_RSS_CNT(qdev);
419         dev_info->max_tx_queues = dev_info->max_rx_queues;
420         dev_info->max_mac_addrs = qdev->dev_info.num_mac_addrs;
421         dev_info->max_vfs = (uint16_t)NUM_OF_VFS(&qdev->edev);
422         dev_info->driver_name = qdev->drv_ver;
423         dev_info->reta_size = ECORE_RSS_IND_TABLE_SIZE;
424         dev_info->flow_type_rss_offloads = (uint64_t)QEDE_RSS_OFFLOAD_ALL;
425
426         dev_info->default_txconf = (struct rte_eth_txconf) {
427                 .txq_flags = QEDE_TXQ_FLAGS,
428         };
429
430         dev_info->rx_offload_capa = (DEV_RX_OFFLOAD_VLAN_STRIP |
431                                      DEV_RX_OFFLOAD_IPV4_CKSUM |
432                                      DEV_RX_OFFLOAD_UDP_CKSUM |
433                                      DEV_RX_OFFLOAD_TCP_CKSUM);
434         dev_info->tx_offload_capa = (DEV_TX_OFFLOAD_VLAN_INSERT |
435                                      DEV_TX_OFFLOAD_IPV4_CKSUM |
436                                      DEV_TX_OFFLOAD_UDP_CKSUM |
437                                      DEV_TX_OFFLOAD_TCP_CKSUM);
438
439         dev_info->speed_capa = ETH_LINK_SPEED_25G | ETH_LINK_SPEED_40G;
440 }
441
442 /* return 0 means link status changed, -1 means not changed */
443 static int
444 qede_link_update(struct rte_eth_dev *eth_dev, __rte_unused int wait_to_complete)
445 {
446         struct qede_dev *qdev = eth_dev->data->dev_private;
447         struct ecore_dev *edev = &qdev->edev;
448         uint16_t link_duplex;
449         struct qed_link_output link;
450         struct rte_eth_link *curr = &eth_dev->data->dev_link;
451
452         memset(&link, 0, sizeof(struct qed_link_output));
453         qdev->ops->common->get_link(edev, &link);
454
455         /* Link Speed */
456         curr->link_speed = link.speed;
457
458         /* Link Mode */
459         switch (link.duplex) {
460         case QEDE_DUPLEX_HALF:
461                 link_duplex = ETH_LINK_HALF_DUPLEX;
462                 break;
463         case QEDE_DUPLEX_FULL:
464                 link_duplex = ETH_LINK_FULL_DUPLEX;
465                 break;
466         case QEDE_DUPLEX_UNKNOWN:
467         default:
468                 link_duplex = -1;
469         }
470         curr->link_duplex = link_duplex;
471
472         /* Link Status */
473         curr->link_status = (link.link_up) ? ETH_LINK_UP : ETH_LINK_DOWN;
474
475         /* AN */
476         curr->link_autoneg = (link.supported_caps & QEDE_SUPPORTED_AUTONEG) ?
477                              ETH_LINK_AUTONEG : ETH_LINK_FIXED;
478
479         DP_INFO(edev, "Link - Speed %u Mode %u AN %u Status %u\n",
480                 curr->link_speed, curr->link_duplex,
481                 curr->link_autoneg, curr->link_status);
482
483         /* return 0 means link status changed, -1 means not changed */
484         return ((curr->link_status == link.link_up) ? -1 : 0);
485 }
486
487 static void
488 qede_rx_mode_setting(struct rte_eth_dev *eth_dev,
489                      enum qed_filter_rx_mode_type accept_flags)
490 {
491         struct qede_dev *qdev = eth_dev->data->dev_private;
492         struct ecore_dev *edev = &qdev->edev;
493         struct qed_filter_params rx_mode;
494
495         DP_INFO(edev, "%s mode %u\n", __func__, accept_flags);
496
497         memset(&rx_mode, 0, sizeof(struct qed_filter_params));
498         rx_mode.type = QED_FILTER_TYPE_RX_MODE;
499         rx_mode.filter.accept_flags = accept_flags;
500         qdev->ops->filter_config(edev, &rx_mode);
501 }
502
503 static void qede_promiscuous_enable(struct rte_eth_dev *eth_dev)
504 {
505         struct qede_dev *qdev = eth_dev->data->dev_private;
506         struct ecore_dev *edev = &qdev->edev;
507
508         PMD_INIT_FUNC_TRACE(edev);
509
510         enum qed_filter_rx_mode_type type = QED_FILTER_RX_MODE_TYPE_PROMISC;
511
512         if (rte_eth_allmulticast_get(eth_dev->data->port_id) == 1)
513                 type |= QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC;
514
515         qede_rx_mode_setting(eth_dev, type);
516 }
517
518 static void qede_promiscuous_disable(struct rte_eth_dev *eth_dev)
519 {
520         struct qede_dev *qdev = eth_dev->data->dev_private;
521         struct ecore_dev *edev = &qdev->edev;
522
523         PMD_INIT_FUNC_TRACE(edev);
524
525         if (rte_eth_allmulticast_get(eth_dev->data->port_id) == 1)
526                 qede_rx_mode_setting(eth_dev,
527                                      QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC);
528         else
529                 qede_rx_mode_setting(eth_dev, QED_FILTER_RX_MODE_TYPE_REGULAR);
530 }
531
532 static void qede_dev_close(struct rte_eth_dev *eth_dev)
533 {
534         struct qede_dev *qdev = eth_dev->data->dev_private;
535         struct ecore_dev *edev = &qdev->edev;
536
537         PMD_INIT_FUNC_TRACE(edev);
538
539         /* dev_stop() shall cleanup fp resources in hw but without releasing
540          * dma memories and sw structures so that dev_start() can be called
541          * by the app without reconfiguration. However, in dev_close() we
542          * can release all the resources and device can be brought up newly
543          */
544         if (qdev->state != QEDE_STOP)
545                 qede_dev_stop(eth_dev);
546         else
547                 DP_INFO(edev, "Device is already stopped\n");
548
549         qede_free_mem_load(qdev);
550
551         qede_free_fp_arrays(qdev);
552
553         qede_dev_set_link_state(eth_dev, false);
554
555         qdev->ops->common->slowpath_stop(edev);
556
557         qdev->ops->common->remove(edev);
558
559         rte_intr_disable(&eth_dev->pci_dev->intr_handle);
560
561         rte_intr_callback_unregister(&eth_dev->pci_dev->intr_handle,
562                                      qede_interrupt_handler, (void *)eth_dev);
563
564         qdev->state = QEDE_CLOSE;
565 }
566
567 static void
568 qede_get_stats(struct rte_eth_dev *eth_dev, struct rte_eth_stats *eth_stats)
569 {
570         struct qede_dev *qdev = eth_dev->data->dev_private;
571         struct ecore_dev *edev = &qdev->edev;
572         struct ecore_eth_stats stats;
573
574         qdev->ops->get_vport_stats(edev, &stats);
575
576         /* RX Stats */
577         eth_stats->ipackets = stats.rx_ucast_pkts +
578             stats.rx_mcast_pkts + stats.rx_bcast_pkts;
579
580         eth_stats->ibytes = stats.rx_ucast_bytes +
581             stats.rx_mcast_bytes + stats.rx_bcast_bytes;
582
583         eth_stats->ierrors = stats.rx_crc_errors +
584             stats.rx_align_errors +
585             stats.rx_carrier_errors +
586             stats.rx_oversize_packets +
587             stats.rx_jabbers + stats.rx_undersize_packets;
588
589         eth_stats->rx_nombuf = stats.no_buff_discards;
590
591         eth_stats->imissed = stats.mftag_filter_discards +
592             stats.mac_filter_discards +
593             stats.no_buff_discards + stats.brb_truncates + stats.brb_discards;
594
595         /* TX stats */
596         eth_stats->opackets = stats.tx_ucast_pkts +
597             stats.tx_mcast_pkts + stats.tx_bcast_pkts;
598
599         eth_stats->obytes = stats.tx_ucast_bytes +
600             stats.tx_mcast_bytes + stats.tx_bcast_bytes;
601
602         eth_stats->oerrors = stats.tx_err_drop_pkts;
603
604         DP_INFO(edev,
605                 "no_buff_discards=%" PRIu64 ""
606                 " mac_filter_discards=%" PRIu64 ""
607                 " brb_truncates=%" PRIu64 ""
608                 " brb_discards=%" PRIu64 "\n",
609                 stats.no_buff_discards,
610                 stats.mac_filter_discards,
611                 stats.brb_truncates, stats.brb_discards);
612 }
613
614 int qede_dev_set_link_state(struct rte_eth_dev *eth_dev, bool link_up)
615 {
616         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
617         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
618         struct qed_link_params link_params;
619         int rc;
620
621         DP_INFO(edev, "setting link state %d\n", link_up);
622         memset(&link_params, 0, sizeof(link_params));
623         link_params.link_up = link_up;
624         rc = qdev->ops->common->set_link(edev, &link_params);
625         if (rc != ECORE_SUCCESS)
626                 DP_ERR(edev, "Unable to set link state %d\n", link_up);
627
628         return rc;
629 }
630
631 static int qede_dev_set_link_up(struct rte_eth_dev *eth_dev)
632 {
633         return qede_dev_set_link_state(eth_dev, true);
634 }
635
636 static int qede_dev_set_link_down(struct rte_eth_dev *eth_dev)
637 {
638         return qede_dev_set_link_state(eth_dev, false);
639 }
640
641 static void qede_reset_stats(struct rte_eth_dev *eth_dev)
642 {
643         struct qede_dev *qdev = eth_dev->data->dev_private;
644         struct ecore_dev *edev = &qdev->edev;
645
646         ecore_reset_vport_stats(edev);
647 }
648
649 static void qede_allmulticast_enable(struct rte_eth_dev *eth_dev)
650 {
651         enum qed_filter_rx_mode_type type =
652             QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC;
653
654         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1)
655                 type |= QED_FILTER_RX_MODE_TYPE_PROMISC;
656
657         qede_rx_mode_setting(eth_dev, type);
658 }
659
660 static void qede_allmulticast_disable(struct rte_eth_dev *eth_dev)
661 {
662         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1)
663                 qede_rx_mode_setting(eth_dev, QED_FILTER_RX_MODE_TYPE_PROMISC);
664         else
665                 qede_rx_mode_setting(eth_dev, QED_FILTER_RX_MODE_TYPE_REGULAR);
666 }
667
668 static int qede_flow_ctrl_set(struct rte_eth_dev *eth_dev,
669                               struct rte_eth_fc_conf *fc_conf)
670 {
671         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
672         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
673         struct qed_link_output current_link;
674         struct qed_link_params params;
675
676         memset(&current_link, 0, sizeof(current_link));
677         qdev->ops->common->get_link(edev, &current_link);
678
679         memset(&params, 0, sizeof(params));
680         params.override_flags |= QED_LINK_OVERRIDE_PAUSE_CONFIG;
681         if (fc_conf->autoneg) {
682                 if (!(current_link.supported_caps & QEDE_SUPPORTED_AUTONEG)) {
683                         DP_ERR(edev, "Autoneg not supported\n");
684                         return -EINVAL;
685                 }
686                 params.pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
687         }
688
689         /* Pause is assumed to be supported (SUPPORTED_Pause) */
690         if (fc_conf->mode == RTE_FC_FULL)
691                 params.pause_config |= (QED_LINK_PAUSE_TX_ENABLE |
692                                         QED_LINK_PAUSE_RX_ENABLE);
693         if (fc_conf->mode == RTE_FC_TX_PAUSE)
694                 params.pause_config |= QED_LINK_PAUSE_TX_ENABLE;
695         if (fc_conf->mode == RTE_FC_RX_PAUSE)
696                 params.pause_config |= QED_LINK_PAUSE_RX_ENABLE;
697
698         params.link_up = true;
699         (void)qdev->ops->common->set_link(edev, &params);
700
701         return 0;
702 }
703
704 static int qede_flow_ctrl_get(struct rte_eth_dev *eth_dev,
705                               struct rte_eth_fc_conf *fc_conf)
706 {
707         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
708         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
709         struct qed_link_output current_link;
710
711         memset(&current_link, 0, sizeof(current_link));
712         qdev->ops->common->get_link(edev, &current_link);
713
714         if (current_link.pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
715                 fc_conf->autoneg = true;
716
717         if (current_link.pause_config & (QED_LINK_PAUSE_RX_ENABLE |
718                                          QED_LINK_PAUSE_TX_ENABLE))
719                 fc_conf->mode = RTE_FC_FULL;
720         else if (current_link.pause_config & QED_LINK_PAUSE_RX_ENABLE)
721                 fc_conf->mode = RTE_FC_RX_PAUSE;
722         else if (current_link.pause_config & QED_LINK_PAUSE_TX_ENABLE)
723                 fc_conf->mode = RTE_FC_TX_PAUSE;
724         else
725                 fc_conf->mode = RTE_FC_NONE;
726
727         return 0;
728 }
729
730 static const uint32_t *
731 qede_dev_supported_ptypes_get(struct rte_eth_dev *eth_dev)
732 {
733         static const uint32_t ptypes[] = {
734                 RTE_PTYPE_L3_IPV4,
735                 RTE_PTYPE_L3_IPV6,
736                 RTE_PTYPE_UNKNOWN
737         };
738
739         if (eth_dev->rx_pkt_burst == qede_recv_pkts)
740                 return ptypes;
741
742         return NULL;
743 }
744
745 static const struct eth_dev_ops qede_eth_dev_ops = {
746         .dev_configure = qede_dev_configure,
747         .dev_infos_get = qede_dev_info_get,
748         .rx_queue_setup = qede_rx_queue_setup,
749         .rx_queue_release = qede_rx_queue_release,
750         .tx_queue_setup = qede_tx_queue_setup,
751         .tx_queue_release = qede_tx_queue_release,
752         .dev_start = qede_dev_start,
753         .dev_set_link_up = qede_dev_set_link_up,
754         .dev_set_link_down = qede_dev_set_link_down,
755         .link_update = qede_link_update,
756         .promiscuous_enable = qede_promiscuous_enable,
757         .promiscuous_disable = qede_promiscuous_disable,
758         .allmulticast_enable = qede_allmulticast_enable,
759         .allmulticast_disable = qede_allmulticast_disable,
760         .dev_stop = qede_dev_stop,
761         .dev_close = qede_dev_close,
762         .stats_get = qede_get_stats,
763         .stats_reset = qede_reset_stats,
764         .mac_addr_add = qede_mac_addr_add,
765         .mac_addr_remove = qede_mac_addr_remove,
766         .mac_addr_set = qede_mac_addr_set,
767         .vlan_offload_set = qede_vlan_offload_set,
768         .vlan_filter_set = qede_vlan_filter_set,
769         .flow_ctrl_set = qede_flow_ctrl_set,
770         .flow_ctrl_get = qede_flow_ctrl_get,
771         .dev_supported_ptypes_get = qede_dev_supported_ptypes_get,
772 };
773
774 static void qede_update_pf_params(struct ecore_dev *edev)
775 {
776         struct ecore_pf_params pf_params;
777         /* 32 rx + 32 tx */
778         memset(&pf_params, 0, sizeof(struct ecore_pf_params));
779         pf_params.eth_pf_params.num_cons = 64;
780         qed_ops->common->update_pf_params(edev, &pf_params);
781 }
782
783 static int qede_common_dev_init(struct rte_eth_dev *eth_dev, bool is_vf)
784 {
785         struct rte_pci_device *pci_dev;
786         struct rte_pci_addr pci_addr;
787         struct qede_dev *adapter;
788         struct ecore_dev *edev;
789         struct qed_dev_eth_info dev_info;
790         struct qed_slowpath_params params;
791         uint32_t qed_ver;
792         static bool do_once = true;
793         uint8_t bulletin_change;
794         uint8_t vf_mac[ETHER_ADDR_LEN];
795         uint8_t is_mac_forced;
796         bool is_mac_exist;
797         /* Fix up ecore debug level */
798         uint32_t dp_module = ~0 & ~ECORE_MSG_HW;
799         uint8_t dp_level = ECORE_LEVEL_VERBOSE;
800         uint32_t max_mac_addrs;
801         int rc;
802
803         /* Extract key data structures */
804         adapter = eth_dev->data->dev_private;
805         edev = &adapter->edev;
806         pci_addr = eth_dev->pci_dev->addr;
807
808         PMD_INIT_FUNC_TRACE(edev);
809
810         snprintf(edev->name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
811                  pci_addr.bus, pci_addr.devid, pci_addr.function,
812                  eth_dev->data->port_id);
813
814         eth_dev->rx_pkt_burst = qede_recv_pkts;
815         eth_dev->tx_pkt_burst = qede_xmit_pkts;
816
817         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
818                 DP_NOTICE(edev, false,
819                           "Skipping device init from secondary process\n");
820                 return 0;
821         }
822
823         pci_dev = eth_dev->pci_dev;
824
825         rte_eth_copy_pci_info(eth_dev, pci_dev);
826
827         qed_ver = qed_get_protocol_version(QED_PROTOCOL_ETH);
828
829         qed_ops = qed_get_eth_ops();
830         if (!qed_ops) {
831                 DP_ERR(edev, "Failed to get qed_eth_ops_pass\n");
832                 return -EINVAL;
833         }
834
835         DP_INFO(edev, "Starting qede probe\n");
836
837         rc = qed_ops->common->probe(edev, pci_dev, QED_PROTOCOL_ETH,
838                                     dp_module, dp_level, is_vf);
839
840         if (rc != 0) {
841                 DP_ERR(edev, "qede probe failed rc %d\n", rc);
842                 return -ENODEV;
843         }
844
845         qede_update_pf_params(edev);
846
847         rte_intr_callback_register(&eth_dev->pci_dev->intr_handle,
848                                    qede_interrupt_handler, (void *)eth_dev);
849
850         if (rte_intr_enable(&eth_dev->pci_dev->intr_handle)) {
851                 DP_ERR(edev, "rte_intr_enable() failed\n");
852                 return -ENODEV;
853         }
854
855         /* Start the Slowpath-process */
856         memset(&params, 0, sizeof(struct qed_slowpath_params));
857         params.int_mode = ECORE_INT_MODE_MSIX;
858         params.drv_major = QEDE_MAJOR_VERSION;
859         params.drv_minor = QEDE_MINOR_VERSION;
860         params.drv_rev = QEDE_REVISION_VERSION;
861         params.drv_eng = QEDE_ENGINEERING_VERSION;
862         strncpy((char *)params.name, "qede LAN", QED_DRV_VER_STR_SIZE);
863
864         rc = qed_ops->common->slowpath_start(edev, &params);
865         if (rc) {
866                 DP_ERR(edev, "Cannot start slowpath rc = %d\n", rc);
867                 return -ENODEV;
868         }
869
870         rc = qed_ops->fill_dev_info(edev, &dev_info);
871         if (rc) {
872                 DP_ERR(edev, "Cannot get device_info rc %d\n", rc);
873                 qed_ops->common->slowpath_stop(edev);
874                 qed_ops->common->remove(edev);
875                 return -ENODEV;
876         }
877
878         qede_alloc_etherdev(adapter, &dev_info);
879
880         adapter->ops->common->set_id(edev, edev->name, QEDE_DRV_MODULE_VERSION);
881
882         if (!is_vf)
883                 adapter->dev_info.num_mac_addrs =
884                         (uint32_t)RESC_NUM(ECORE_LEADING_HWFN(edev),
885                                             ECORE_MAC);
886         else
887                 adapter->dev_info.num_mac_addrs = 1;
888
889         /* Allocate memory for storing MAC addr */
890         eth_dev->data->mac_addrs = rte_zmalloc(edev->name,
891                                         (ETHER_ADDR_LEN *
892                                         adapter->dev_info.num_mac_addrs),
893                                         RTE_CACHE_LINE_SIZE);
894
895         if (eth_dev->data->mac_addrs == NULL) {
896                 DP_ERR(edev, "Failed to allocate MAC address\n");
897                 qed_ops->common->slowpath_stop(edev);
898                 qed_ops->common->remove(edev);
899                 return -ENOMEM;
900         }
901
902         ether_addr_copy((struct ether_addr *)edev->hwfns[0].
903                                 hw_info.hw_mac_addr,
904                                 &eth_dev->data->mac_addrs[0]);
905
906         eth_dev->dev_ops = &qede_eth_dev_ops;
907
908         if (do_once) {
909                 qede_print_adapter_info(adapter);
910                 do_once = false;
911         }
912
913         DP_NOTICE(edev, false, "MAC address : %02x:%02x:%02x:%02x:%02x:%02x\n",
914                   adapter->primary_mac.addr_bytes[0],
915                   adapter->primary_mac.addr_bytes[1],
916                   adapter->primary_mac.addr_bytes[2],
917                   adapter->primary_mac.addr_bytes[3],
918                   adapter->primary_mac.addr_bytes[4],
919                   adapter->primary_mac.addr_bytes[5]);
920
921         return rc;
922 }
923
924 static int qedevf_eth_dev_init(struct rte_eth_dev *eth_dev)
925 {
926         return qede_common_dev_init(eth_dev, 1);
927 }
928
929 static int qede_eth_dev_init(struct rte_eth_dev *eth_dev)
930 {
931         return qede_common_dev_init(eth_dev, 0);
932 }
933
934 static int qede_dev_common_uninit(struct rte_eth_dev *eth_dev)
935 {
936         /* only uninitialize in the primary process */
937         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
938                 return 0;
939
940         /* safe to close dev here */
941         qede_dev_close(eth_dev);
942
943         eth_dev->dev_ops = NULL;
944         eth_dev->rx_pkt_burst = NULL;
945         eth_dev->tx_pkt_burst = NULL;
946
947         if (eth_dev->data->mac_addrs)
948                 rte_free(eth_dev->data->mac_addrs);
949
950         eth_dev->data->mac_addrs = NULL;
951
952         return 0;
953 }
954
955 static int qede_eth_dev_uninit(struct rte_eth_dev *eth_dev)
956 {
957         return qede_dev_common_uninit(eth_dev);
958 }
959
960 static int qedevf_eth_dev_uninit(struct rte_eth_dev *eth_dev)
961 {
962         return qede_dev_common_uninit(eth_dev);
963 }
964
965 static struct rte_pci_id pci_id_qedevf_map[] = {
966 #define QEDEVF_RTE_PCI_DEVICE(dev) RTE_PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, dev)
967         {
968                 QEDEVF_RTE_PCI_DEVICE(PCI_DEVICE_ID_NX2_VF)
969         },
970         {
971                 QEDEVF_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_IOV)
972         },
973         {.vendor_id = 0,}
974 };
975
976 static struct rte_pci_id pci_id_qede_map[] = {
977 #define QEDE_RTE_PCI_DEVICE(dev) RTE_PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, dev)
978         {
979                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_NX2_57980E)
980         },
981         {
982                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_NX2_57980S)
983         },
984         {
985                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_40)
986         },
987         {
988                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_25)
989         },
990         {.vendor_id = 0,}
991 };
992
993 static struct eth_driver rte_qedevf_pmd = {
994         .pci_drv = {
995                     .name = "rte_qedevf_pmd",
996                     .id_table = pci_id_qedevf_map,
997                     .drv_flags =
998                     RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
999                     },
1000         .eth_dev_init = qedevf_eth_dev_init,
1001         .eth_dev_uninit = qedevf_eth_dev_uninit,
1002         .dev_private_size = sizeof(struct qede_dev),
1003 };
1004
1005 static struct eth_driver rte_qede_pmd = {
1006         .pci_drv = {
1007                     .name = "rte_qede_pmd",
1008                     .id_table = pci_id_qede_map,
1009                     .drv_flags =
1010                     RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1011                     },
1012         .eth_dev_init = qede_eth_dev_init,
1013         .eth_dev_uninit = qede_eth_dev_uninit,
1014         .dev_private_size = sizeof(struct qede_dev),
1015 };
1016
1017 static int
1018 rte_qedevf_pmd_init(const char *name __rte_unused,
1019                     const char *params __rte_unused)
1020 {
1021         rte_eth_driver_register(&rte_qedevf_pmd);
1022
1023         return 0;
1024 }
1025
1026 static int
1027 rte_qede_pmd_init(const char *name __rte_unused,
1028                   const char *params __rte_unused)
1029 {
1030         rte_eth_driver_register(&rte_qede_pmd);
1031
1032         return 0;
1033 }
1034
1035 static struct rte_driver rte_qedevf_driver = {
1036         .type = PMD_PDEV,
1037         .init = rte_qede_pmd_init
1038 };
1039
1040 static struct rte_driver rte_qede_driver = {
1041         .type = PMD_PDEV,
1042         .init = rte_qedevf_pmd_init
1043 };
1044
1045 PMD_REGISTER_DRIVER(rte_qede_driver);
1046 PMD_REGISTER_DRIVER(rte_qedevf_driver);