net/qede: skip slow path polling for 100G VF device
[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 #include <rte_alarm.h>
11
12 /* Globals */
13 static const struct qed_eth_ops *qed_ops;
14 static const char *drivername = "qede pmd";
15 static int64_t timer_period = 1;
16
17 struct rte_qede_xstats_name_off {
18         char name[RTE_ETH_XSTATS_NAME_SIZE];
19         uint64_t offset;
20 };
21
22 static const struct rte_qede_xstats_name_off qede_xstats_strings[] = {
23         {"rx_unicast_bytes", offsetof(struct ecore_eth_stats, rx_ucast_bytes)},
24         {"rx_multicast_bytes",
25                 offsetof(struct ecore_eth_stats, rx_mcast_bytes)},
26         {"rx_broadcast_bytes",
27                 offsetof(struct ecore_eth_stats, rx_bcast_bytes)},
28         {"rx_unicast_packets", offsetof(struct ecore_eth_stats, rx_ucast_pkts)},
29         {"rx_multicast_packets",
30                 offsetof(struct ecore_eth_stats, rx_mcast_pkts)},
31         {"rx_broadcast_packets",
32                 offsetof(struct ecore_eth_stats, rx_bcast_pkts)},
33
34         {"tx_unicast_bytes", offsetof(struct ecore_eth_stats, tx_ucast_bytes)},
35         {"tx_multicast_bytes",
36                 offsetof(struct ecore_eth_stats, tx_mcast_bytes)},
37         {"tx_broadcast_bytes",
38                 offsetof(struct ecore_eth_stats, tx_bcast_bytes)},
39         {"tx_unicast_packets", offsetof(struct ecore_eth_stats, tx_ucast_pkts)},
40         {"tx_multicast_packets",
41                 offsetof(struct ecore_eth_stats, tx_mcast_pkts)},
42         {"tx_broadcast_packets",
43                 offsetof(struct ecore_eth_stats, tx_bcast_pkts)},
44
45         {"rx_64_byte_packets",
46                 offsetof(struct ecore_eth_stats, rx_64_byte_packets)},
47         {"rx_65_to_127_byte_packets",
48                 offsetof(struct ecore_eth_stats, rx_65_to_127_byte_packets)},
49         {"rx_128_to_255_byte_packets",
50                 offsetof(struct ecore_eth_stats, rx_128_to_255_byte_packets)},
51         {"rx_256_to_511_byte_packets",
52                 offsetof(struct ecore_eth_stats, rx_256_to_511_byte_packets)},
53         {"rx_512_to_1023_byte_packets",
54                 offsetof(struct ecore_eth_stats, rx_512_to_1023_byte_packets)},
55         {"rx_1024_to_1518_byte_packets",
56                 offsetof(struct ecore_eth_stats, rx_1024_to_1518_byte_packets)},
57         {"rx_1519_to_1522_byte_packets",
58                 offsetof(struct ecore_eth_stats, rx_1519_to_1522_byte_packets)},
59         {"rx_1519_to_2047_byte_packets",
60                 offsetof(struct ecore_eth_stats, rx_1519_to_2047_byte_packets)},
61         {"rx_2048_to_4095_byte_packets",
62                 offsetof(struct ecore_eth_stats, rx_2048_to_4095_byte_packets)},
63         {"rx_4096_to_9216_byte_packets",
64                 offsetof(struct ecore_eth_stats, rx_4096_to_9216_byte_packets)},
65         {"rx_9217_to_16383_byte_packets",
66                 offsetof(struct ecore_eth_stats,
67                          rx_9217_to_16383_byte_packets)},
68         {"tx_64_byte_packets",
69                 offsetof(struct ecore_eth_stats, tx_64_byte_packets)},
70         {"tx_65_to_127_byte_packets",
71                 offsetof(struct ecore_eth_stats, tx_65_to_127_byte_packets)},
72         {"tx_128_to_255_byte_packets",
73                 offsetof(struct ecore_eth_stats, tx_128_to_255_byte_packets)},
74         {"tx_256_to_511_byte_packets",
75                 offsetof(struct ecore_eth_stats, tx_256_to_511_byte_packets)},
76         {"tx_512_to_1023_byte_packets",
77                 offsetof(struct ecore_eth_stats, tx_512_to_1023_byte_packets)},
78         {"tx_1024_to_1518_byte_packets",
79                 offsetof(struct ecore_eth_stats, tx_1024_to_1518_byte_packets)},
80         {"trx_1519_to_1522_byte_packets",
81                 offsetof(struct ecore_eth_stats, tx_1519_to_2047_byte_packets)},
82         {"tx_2048_to_4095_byte_packets",
83                 offsetof(struct ecore_eth_stats, tx_2048_to_4095_byte_packets)},
84         {"tx_4096_to_9216_byte_packets",
85                 offsetof(struct ecore_eth_stats, tx_4096_to_9216_byte_packets)},
86         {"tx_9217_to_16383_byte_packets",
87                 offsetof(struct ecore_eth_stats,
88                          tx_9217_to_16383_byte_packets)},
89
90         {"rx_mac_crtl_frames",
91                 offsetof(struct ecore_eth_stats, rx_mac_crtl_frames)},
92         {"tx_mac_control_frames",
93                 offsetof(struct ecore_eth_stats, tx_mac_ctrl_frames)},
94         {"rx_pause_frames", offsetof(struct ecore_eth_stats, rx_pause_frames)},
95         {"tx_pause_frames", offsetof(struct ecore_eth_stats, tx_pause_frames)},
96         {"rx_priority_flow_control_frames",
97                 offsetof(struct ecore_eth_stats, rx_pfc_frames)},
98         {"tx_priority_flow_control_frames",
99                 offsetof(struct ecore_eth_stats, tx_pfc_frames)},
100
101         {"rx_crc_errors", offsetof(struct ecore_eth_stats, rx_crc_errors)},
102         {"rx_align_errors", offsetof(struct ecore_eth_stats, rx_align_errors)},
103         {"rx_carrier_errors",
104                 offsetof(struct ecore_eth_stats, rx_carrier_errors)},
105         {"rx_oversize_packet_errors",
106                 offsetof(struct ecore_eth_stats, rx_oversize_packets)},
107         {"rx_jabber_errors", offsetof(struct ecore_eth_stats, rx_jabbers)},
108         {"rx_undersize_packet_errors",
109                 offsetof(struct ecore_eth_stats, rx_undersize_packets)},
110         {"rx_fragments", offsetof(struct ecore_eth_stats, rx_fragments)},
111         {"rx_host_buffer_not_available",
112                 offsetof(struct ecore_eth_stats, no_buff_discards)},
113         /* Number of packets discarded because they are bigger than MTU */
114         {"rx_packet_too_big_discards",
115                 offsetof(struct ecore_eth_stats, packet_too_big_discard)},
116         {"rx_ttl_zero_discards",
117                 offsetof(struct ecore_eth_stats, ttl0_discard)},
118         {"rx_multi_function_tag_filter_discards",
119                 offsetof(struct ecore_eth_stats, mftag_filter_discards)},
120         {"rx_mac_filter_discards",
121                 offsetof(struct ecore_eth_stats, mac_filter_discards)},
122         {"rx_hw_buffer_truncates",
123                 offsetof(struct ecore_eth_stats, brb_truncates)},
124         {"rx_hw_buffer_discards",
125                 offsetof(struct ecore_eth_stats, brb_discards)},
126         {"tx_lpi_entry_count",
127                 offsetof(struct ecore_eth_stats, tx_lpi_entry_count)},
128         {"tx_total_collisions",
129                 offsetof(struct ecore_eth_stats, tx_total_collisions)},
130         {"tx_error_drop_packets",
131                 offsetof(struct ecore_eth_stats, tx_err_drop_pkts)},
132
133         {"rx_mac_bytes", offsetof(struct ecore_eth_stats, rx_mac_bytes)},
134         {"rx_mac_unicast_packets",
135                 offsetof(struct ecore_eth_stats, rx_mac_uc_packets)},
136         {"rx_mac_multicast_packets",
137                 offsetof(struct ecore_eth_stats, rx_mac_mc_packets)},
138         {"rx_mac_broadcast_packets",
139                 offsetof(struct ecore_eth_stats, rx_mac_bc_packets)},
140         {"rx_mac_frames_ok",
141                 offsetof(struct ecore_eth_stats, rx_mac_frames_ok)},
142         {"tx_mac_bytes", offsetof(struct ecore_eth_stats, tx_mac_bytes)},
143         {"tx_mac_unicast_packets",
144                 offsetof(struct ecore_eth_stats, tx_mac_uc_packets)},
145         {"tx_mac_multicast_packets",
146                 offsetof(struct ecore_eth_stats, tx_mac_mc_packets)},
147         {"tx_mac_broadcast_packets",
148                 offsetof(struct ecore_eth_stats, tx_mac_bc_packets)},
149
150         {"lro_coalesced_packets",
151                 offsetof(struct ecore_eth_stats, tpa_coalesced_pkts)},
152         {"lro_coalesced_events",
153                 offsetof(struct ecore_eth_stats, tpa_coalesced_events)},
154         {"lro_aborts_num",
155                 offsetof(struct ecore_eth_stats, tpa_aborts_num)},
156         {"lro_not_coalesced_packets",
157                 offsetof(struct ecore_eth_stats, tpa_not_coalesced_pkts)},
158         {"lro_coalesced_bytes",
159                 offsetof(struct ecore_eth_stats, tpa_coalesced_bytes)},
160 };
161
162 static void qede_interrupt_action(struct ecore_hwfn *p_hwfn)
163 {
164         ecore_int_sp_dpc((osal_int_ptr_t)(p_hwfn));
165 }
166
167 static void
168 qede_interrupt_handler(__rte_unused struct rte_intr_handle *handle, void *param)
169 {
170         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
171         struct qede_dev *qdev = eth_dev->data->dev_private;
172         struct ecore_dev *edev = &qdev->edev;
173
174         qede_interrupt_action(ECORE_LEADING_HWFN(edev));
175         if (rte_intr_enable(&eth_dev->pci_dev->intr_handle))
176                 DP_ERR(edev, "rte_intr_enable failed\n");
177 }
178
179 static void
180 qede_alloc_etherdev(struct qede_dev *qdev, struct qed_dev_eth_info *info)
181 {
182         rte_memcpy(&qdev->dev_info, info, sizeof(*info));
183         qdev->num_tc = qdev->dev_info.num_tc;
184         qdev->ops = qed_ops;
185 }
186
187 static void qede_print_adapter_info(struct qede_dev *qdev)
188 {
189         struct ecore_dev *edev = &qdev->edev;
190         struct qed_dev_info *info = &qdev->dev_info.common;
191         static char ver_str[QED_DRV_VER_STR_SIZE];
192
193         DP_INFO(edev, "*********************************\n");
194         DP_INFO(edev, " Chip details : %s%d\n",
195                 ECORE_IS_BB(edev) ? "BB" : "AH",
196                 CHIP_REV_IS_A0(edev) ? 0 : 1);
197
198         sprintf(ver_str, "%s %s_%d.%d.%d.%d", QEDE_PMD_VER_PREFIX,
199                 edev->ver_str, QEDE_PMD_VERSION_MAJOR, QEDE_PMD_VERSION_MINOR,
200                 QEDE_PMD_VERSION_REVISION, QEDE_PMD_VERSION_PATCH);
201         strcpy(qdev->drv_ver, ver_str);
202         DP_INFO(edev, " Driver version : %s\n", ver_str);
203
204         sprintf(ver_str, "%d.%d.%d.%d", info->fw_major, info->fw_minor,
205                 info->fw_rev, info->fw_eng);
206         DP_INFO(edev, " Firmware version : %s\n", ver_str);
207
208         sprintf(ver_str, "%d.%d.%d.%d",
209                 (info->mfw_rev >> 24) & 0xff,
210                 (info->mfw_rev >> 16) & 0xff,
211                 (info->mfw_rev >> 8) & 0xff, (info->mfw_rev) & 0xff);
212         DP_INFO(edev, " Management firmware version : %s\n", ver_str);
213
214         DP_INFO(edev, " Firmware file : %s\n", fw_file);
215
216         DP_INFO(edev, "*********************************\n");
217 }
218
219 static int
220 qede_set_ucast_rx_mac(struct qede_dev *qdev,
221                       enum qed_filter_xcast_params_type opcode,
222                       uint8_t mac[ETHER_ADDR_LEN])
223 {
224         struct ecore_dev *edev = &qdev->edev;
225         struct qed_filter_params filter_cmd;
226
227         memset(&filter_cmd, 0, sizeof(filter_cmd));
228         filter_cmd.type = QED_FILTER_TYPE_UCAST;
229         filter_cmd.filter.ucast.type = opcode;
230         filter_cmd.filter.ucast.mac_valid = 1;
231         rte_memcpy(&filter_cmd.filter.ucast.mac[0], &mac[0], ETHER_ADDR_LEN);
232         return qdev->ops->filter_config(edev, &filter_cmd);
233 }
234
235 static void
236 qede_mac_addr_add(struct rte_eth_dev *eth_dev, struct ether_addr *mac_addr,
237                   uint32_t index, __rte_unused uint32_t pool)
238 {
239         struct qede_dev *qdev = eth_dev->data->dev_private;
240         struct ecore_dev *edev = &qdev->edev;
241         int rc;
242
243         PMD_INIT_FUNC_TRACE(edev);
244
245         if (index >= qdev->dev_info.num_mac_addrs) {
246                 DP_ERR(edev, "Index %u is above MAC filter limit %u\n",
247                        index, qdev->dev_info.num_mac_addrs);
248                 return;
249         }
250
251         /* Adding macaddr even though promiscuous mode is set */
252         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1)
253                 DP_INFO(edev, "Port is in promisc mode, yet adding it\n");
254
255         /* Add MAC filters according to the unicast secondary macs */
256         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_ADD,
257                                    mac_addr->addr_bytes);
258         if (rc)
259                 DP_ERR(edev, "Unable to add macaddr rc=%d\n", rc);
260 }
261
262 static void
263 qede_mac_addr_remove(struct rte_eth_dev *eth_dev, uint32_t index)
264 {
265         struct qede_dev *qdev = eth_dev->data->dev_private;
266         struct ecore_dev *edev = &qdev->edev;
267         struct ether_addr mac_addr;
268         int rc;
269
270         PMD_INIT_FUNC_TRACE(edev);
271
272         if (index >= qdev->dev_info.num_mac_addrs) {
273                 DP_ERR(edev, "Index %u is above MAC filter limit %u\n",
274                        index, qdev->dev_info.num_mac_addrs);
275                 return;
276         }
277
278         /* Use the index maintained by rte */
279         ether_addr_copy(&eth_dev->data->mac_addrs[index], &mac_addr);
280         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_DEL,
281                                    mac_addr.addr_bytes);
282         if (rc)
283                 DP_ERR(edev, "Unable to remove macaddr rc=%d\n", rc);
284 }
285
286 static void
287 qede_mac_addr_set(struct rte_eth_dev *eth_dev, struct ether_addr *mac_addr)
288 {
289         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
290         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
291         int rc;
292
293         if (IS_VF(edev) && !ecore_vf_check_mac(ECORE_LEADING_HWFN(edev),
294                                                mac_addr->addr_bytes)) {
295                 DP_ERR(edev, "Setting MAC address is not allowed\n");
296                 ether_addr_copy(&qdev->primary_mac,
297                                 &eth_dev->data->mac_addrs[0]);
298                 return;
299         }
300
301         /* First remove the primary mac */
302         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_DEL,
303                                    qdev->primary_mac.addr_bytes);
304
305         if (rc) {
306                 DP_ERR(edev, "Unable to remove current macaddr"
307                              " Reverting to previous default mac\n");
308                 ether_addr_copy(&qdev->primary_mac,
309                                 &eth_dev->data->mac_addrs[0]);
310                 return;
311         }
312
313         /* Add new MAC */
314         rc = qede_set_ucast_rx_mac(qdev, QED_FILTER_XCAST_TYPE_ADD,
315                                    mac_addr->addr_bytes);
316
317         if (rc)
318                 DP_ERR(edev, "Unable to add new default mac\n");
319         else
320                 ether_addr_copy(mac_addr, &qdev->primary_mac);
321 }
322
323
324
325
326 static void qede_config_accept_any_vlan(struct qede_dev *qdev, bool action)
327 {
328         struct ecore_dev *edev = &qdev->edev;
329         struct qed_update_vport_params params = {
330                 .vport_id = 0,
331                 .accept_any_vlan = action,
332                 .update_accept_any_vlan_flg = 1,
333         };
334         int rc;
335
336         /* Proceed only if action actually needs to be performed */
337         if (qdev->accept_any_vlan == action)
338                 return;
339
340         rc = qdev->ops->vport_update(edev, &params);
341         if (rc) {
342                 DP_ERR(edev, "Failed to %s accept-any-vlan\n",
343                        action ? "enable" : "disable");
344         } else {
345                 DP_INFO(edev, "%s accept-any-vlan\n",
346                         action ? "enabled" : "disabled");
347                 qdev->accept_any_vlan = action;
348         }
349 }
350
351 static int qede_vlan_stripping(struct rte_eth_dev *eth_dev, bool set_stripping)
352 {
353         struct qed_update_vport_params vport_update_params;
354         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
355         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
356         int rc;
357
358         memset(&vport_update_params, 0, sizeof(vport_update_params));
359         vport_update_params.vport_id = 0;
360         vport_update_params.update_inner_vlan_removal_flg = 1;
361         vport_update_params.inner_vlan_removal_flg = set_stripping;
362         rc = qdev->ops->vport_update(edev, &vport_update_params);
363         if (rc) {
364                 DP_ERR(edev, "Update V-PORT failed %d\n", rc);
365                 return rc;
366         }
367
368         return 0;
369 }
370
371 static void qede_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
372 {
373         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
374         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
375         struct rte_eth_rxmode *rxmode = &eth_dev->data->dev_conf.rxmode;
376
377         if (mask & ETH_VLAN_STRIP_MASK) {
378                 if (rxmode->hw_vlan_strip)
379                         (void)qede_vlan_stripping(eth_dev, 1);
380                 else
381                         (void)qede_vlan_stripping(eth_dev, 0);
382         }
383
384         if (mask & ETH_VLAN_FILTER_MASK) {
385                 /* VLAN filtering kicks in when a VLAN is added */
386                 if (rxmode->hw_vlan_filter) {
387                         qede_vlan_filter_set(eth_dev, 0, 1);
388                 } else {
389                         if (qdev->configured_vlans > 1) { /* Excluding VLAN0 */
390                                 DP_NOTICE(edev, false,
391                                   " Please remove existing VLAN filters"
392                                   " before disabling VLAN filtering\n");
393                                 /* Signal app that VLAN filtering is still
394                                  * enabled
395                                  */
396                                 rxmode->hw_vlan_filter = true;
397                         } else {
398                                 qede_vlan_filter_set(eth_dev, 0, 0);
399                         }
400                 }
401         }
402
403         if (mask & ETH_VLAN_EXTEND_MASK)
404                 DP_INFO(edev, "No offloads are supported with VLAN Q-in-Q"
405                         " and classification is based on outer tag only\n");
406
407         DP_INFO(edev, "vlan offload mask %d vlan-strip %d vlan-filter %d\n",
408                 mask, rxmode->hw_vlan_strip, rxmode->hw_vlan_filter);
409 }
410
411 static int qede_set_ucast_rx_vlan(struct qede_dev *qdev,
412                                   enum qed_filter_xcast_params_type opcode,
413                                   uint16_t vid)
414 {
415         struct qed_filter_params filter_cmd;
416         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
417
418         memset(&filter_cmd, 0, sizeof(filter_cmd));
419         filter_cmd.type = QED_FILTER_TYPE_UCAST;
420         filter_cmd.filter.ucast.type = opcode;
421         filter_cmd.filter.ucast.vlan_valid = 1;
422         filter_cmd.filter.ucast.vlan = vid;
423
424         return qdev->ops->filter_config(edev, &filter_cmd);
425 }
426
427 static int qede_vlan_filter_set(struct rte_eth_dev *eth_dev,
428                                 uint16_t vlan_id, int on)
429 {
430         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
431         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
432         struct qed_dev_eth_info *dev_info = &qdev->dev_info;
433         struct qede_vlan_entry *tmp = NULL;
434         struct qede_vlan_entry *vlan;
435         int rc;
436
437         if (on) {
438                 if (qdev->configured_vlans == dev_info->num_vlan_filters) {
439                         DP_INFO(edev, "Reached max VLAN filter limit"
440                                       " enabling accept_any_vlan\n");
441                         qede_config_accept_any_vlan(qdev, true);
442                         return 0;
443                 }
444
445                 SLIST_FOREACH(tmp, &qdev->vlan_list_head, list) {
446                         if (tmp->vid == vlan_id) {
447                                 DP_ERR(edev, "VLAN %u already configured\n",
448                                        vlan_id);
449                                 return -EEXIST;
450                         }
451                 }
452
453                 vlan = rte_malloc(NULL, sizeof(struct qede_vlan_entry),
454                                   RTE_CACHE_LINE_SIZE);
455
456                 if (!vlan) {
457                         DP_ERR(edev, "Did not allocate memory for VLAN\n");
458                         return -ENOMEM;
459                 }
460
461                 rc = qede_set_ucast_rx_vlan(qdev, QED_FILTER_XCAST_TYPE_ADD,
462                                             vlan_id);
463                 if (rc) {
464                         DP_ERR(edev, "Failed to add VLAN %u rc %d\n", vlan_id,
465                                rc);
466                         rte_free(vlan);
467                 } else {
468                         vlan->vid = vlan_id;
469                         SLIST_INSERT_HEAD(&qdev->vlan_list_head, vlan, list);
470                         qdev->configured_vlans++;
471                         DP_INFO(edev, "VLAN %u added, configured_vlans %u\n",
472                                 vlan_id, qdev->configured_vlans);
473                 }
474         } else {
475                 SLIST_FOREACH(tmp, &qdev->vlan_list_head, list) {
476                         if (tmp->vid == vlan_id)
477                                 break;
478                 }
479
480                 if (!tmp) {
481                         if (qdev->configured_vlans == 0) {
482                                 DP_INFO(edev,
483                                         "No VLAN filters configured yet\n");
484                                 return 0;
485                         }
486
487                         DP_ERR(edev, "VLAN %u not configured\n", vlan_id);
488                         return -EINVAL;
489                 }
490
491                 SLIST_REMOVE(&qdev->vlan_list_head, tmp, qede_vlan_entry, list);
492
493                 rc = qede_set_ucast_rx_vlan(qdev, QED_FILTER_XCAST_TYPE_DEL,
494                                             vlan_id);
495                 if (rc) {
496                         DP_ERR(edev, "Failed to delete VLAN %u rc %d\n",
497                                vlan_id, rc);
498                 } else {
499                         qdev->configured_vlans--;
500                         DP_INFO(edev, "VLAN %u removed configured_vlans %u\n",
501                                 vlan_id, qdev->configured_vlans);
502                 }
503         }
504
505         return rc;
506 }
507
508 static int qede_init_vport(struct qede_dev *qdev)
509 {
510         struct ecore_dev *edev = &qdev->edev;
511         struct qed_start_vport_params start = {0};
512         int rc;
513
514         start.remove_inner_vlan = 1;
515         start.gro_enable = 0;
516         start.mtu = ETHER_MTU + QEDE_ETH_OVERHEAD;
517         start.vport_id = 0;
518         start.drop_ttl0 = false;
519         start.clear_stats = 1;
520         start.handle_ptp_pkts = 0;
521
522         rc = qdev->ops->vport_start(edev, &start);
523         if (rc) {
524                 DP_ERR(edev, "Start V-PORT failed %d\n", rc);
525                 return rc;
526         }
527
528         DP_INFO(edev,
529                 "Start vport ramrod passed, vport_id = %d, MTU = %u\n",
530                 start.vport_id, ETHER_MTU);
531
532         return 0;
533 }
534
535 static int qede_dev_configure(struct rte_eth_dev *eth_dev)
536 {
537         struct qede_dev *qdev = eth_dev->data->dev_private;
538         struct ecore_dev *edev = &qdev->edev;
539         struct rte_eth_rxmode *rxmode = &eth_dev->data->dev_conf.rxmode;
540         int rc, i, j;
541
542         PMD_INIT_FUNC_TRACE(edev);
543
544         /* Check requirements for 100G mode */
545         if (edev->num_hwfns > 1) {
546                 if (eth_dev->data->nb_rx_queues < 2 ||
547                     eth_dev->data->nb_tx_queues < 2) {
548                         DP_NOTICE(edev, false,
549                                   "100G mode needs min. 2 RX/TX queues\n");
550                         return -EINVAL;
551                 }
552
553                 if ((eth_dev->data->nb_rx_queues % 2 != 0) ||
554                     (eth_dev->data->nb_tx_queues % 2 != 0)) {
555                         DP_NOTICE(edev, false,
556                                   "100G mode needs even no. of RX/TX queues\n");
557                         return -EINVAL;
558                 }
559         }
560
561         /* Sanity checks and throw warnings */
562         if (rxmode->enable_scatter == 1)
563                 eth_dev->data->scattered_rx = 1;
564
565         if (rxmode->enable_lro == 1) {
566                 DP_INFO(edev, "LRO is not supported\n");
567                 return -EINVAL;
568         }
569
570         if (!rxmode->hw_strip_crc)
571                 DP_INFO(edev, "L2 CRC stripping is always enabled in hw\n");
572
573         if (!rxmode->hw_ip_checksum)
574                 DP_INFO(edev, "IP/UDP/TCP checksum offload is always enabled "
575                               "in hw\n");
576
577         /* Check for the port restart case */
578         if (qdev->state != QEDE_DEV_INIT) {
579                 rc = qdev->ops->vport_stop(edev, 0);
580                 if (rc != 0)
581                         return rc;
582                 qede_dealloc_fp_resc(eth_dev);
583         }
584
585         qdev->fp_num_tx = eth_dev->data->nb_tx_queues;
586         qdev->fp_num_rx = eth_dev->data->nb_rx_queues;
587         qdev->num_queues = qdev->fp_num_tx + qdev->fp_num_rx;
588
589         /* Fastpath status block should be initialized before sending
590          * VPORT-START in the case of VF. Anyway, do it for both VF/PF.
591          */
592         rc = qede_alloc_fp_resc(qdev);
593         if (rc != 0)
594                 return rc;
595
596         /* Issue VPORT-START with default config values to allow
597          * other port configurations early on.
598          */
599         rc = qede_init_vport(qdev);
600         if (rc != 0)
601                 return rc;
602
603         SLIST_INIT(&qdev->vlan_list_head);
604
605         /* Add primary mac for PF */
606         if (IS_PF(edev))
607                 qede_mac_addr_set(eth_dev, &qdev->primary_mac);
608
609         /* Enable VLAN offloads by default */
610         qede_vlan_offload_set(eth_dev, ETH_VLAN_STRIP_MASK  |
611                                        ETH_VLAN_FILTER_MASK |
612                                        ETH_VLAN_EXTEND_MASK);
613
614         qdev->state = QEDE_DEV_CONFIG;
615
616         DP_INFO(edev, "Allocated RSS=%d TSS=%d (with CoS=%d)\n",
617                 (int)QEDE_RSS_COUNT(qdev), (int)QEDE_TSS_COUNT(qdev),
618                 qdev->num_tc);
619
620         return 0;
621 }
622
623 /* Info about HW descriptor ring limitations */
624 static const struct rte_eth_desc_lim qede_rx_desc_lim = {
625         .nb_max = NUM_RX_BDS_MAX,
626         .nb_min = 128,
627         .nb_align = 128 /* lowest common multiple */
628 };
629
630 static const struct rte_eth_desc_lim qede_tx_desc_lim = {
631         .nb_max = NUM_TX_BDS_MAX,
632         .nb_min = 256,
633         .nb_align = 256
634 };
635
636 static void
637 qede_dev_info_get(struct rte_eth_dev *eth_dev,
638                   struct rte_eth_dev_info *dev_info)
639 {
640         struct qede_dev *qdev = eth_dev->data->dev_private;
641         struct ecore_dev *edev = &qdev->edev;
642
643         PMD_INIT_FUNC_TRACE(edev);
644
645         dev_info->min_rx_bufsize = (uint32_t)(ETHER_MIN_MTU +
646                                               QEDE_ETH_OVERHEAD);
647         dev_info->max_rx_pktlen = (uint32_t)ETH_TX_MAX_NON_LSO_PKT_LEN;
648         dev_info->rx_desc_lim = qede_rx_desc_lim;
649         dev_info->tx_desc_lim = qede_tx_desc_lim;
650         dev_info->max_rx_queues = (uint16_t)QEDE_MAX_RSS_CNT(qdev);
651         dev_info->max_tx_queues = dev_info->max_rx_queues;
652         dev_info->max_mac_addrs = qdev->dev_info.num_mac_addrs;
653         if (IS_VF(edev))
654                 dev_info->max_vfs = 0;
655         else
656                 dev_info->max_vfs = (uint16_t)NUM_OF_VFS(&qdev->edev);
657         dev_info->driver_name = qdev->drv_ver;
658         dev_info->reta_size = ECORE_RSS_IND_TABLE_SIZE;
659         dev_info->flow_type_rss_offloads = (uint64_t)QEDE_RSS_OFFLOAD_ALL;
660
661         dev_info->default_txconf = (struct rte_eth_txconf) {
662                 .txq_flags = QEDE_TXQ_FLAGS,
663         };
664
665         dev_info->rx_offload_capa = (DEV_RX_OFFLOAD_VLAN_STRIP |
666                                      DEV_RX_OFFLOAD_IPV4_CKSUM |
667                                      DEV_RX_OFFLOAD_UDP_CKSUM |
668                                      DEV_RX_OFFLOAD_TCP_CKSUM);
669         dev_info->tx_offload_capa = (DEV_TX_OFFLOAD_VLAN_INSERT |
670                                      DEV_TX_OFFLOAD_IPV4_CKSUM |
671                                      DEV_TX_OFFLOAD_UDP_CKSUM |
672                                      DEV_TX_OFFLOAD_TCP_CKSUM);
673
674         dev_info->speed_capa = ETH_LINK_SPEED_25G | ETH_LINK_SPEED_40G |
675                                ETH_LINK_SPEED_100G;
676 }
677
678 /* return 0 means link status changed, -1 means not changed */
679 static int
680 qede_link_update(struct rte_eth_dev *eth_dev, __rte_unused int wait_to_complete)
681 {
682         struct qede_dev *qdev = eth_dev->data->dev_private;
683         struct ecore_dev *edev = &qdev->edev;
684         uint16_t link_duplex;
685         struct qed_link_output link;
686         struct rte_eth_link *curr = &eth_dev->data->dev_link;
687
688         memset(&link, 0, sizeof(struct qed_link_output));
689         qdev->ops->common->get_link(edev, &link);
690
691         /* Link Speed */
692         curr->link_speed = link.speed;
693
694         /* Link Mode */
695         switch (link.duplex) {
696         case QEDE_DUPLEX_HALF:
697                 link_duplex = ETH_LINK_HALF_DUPLEX;
698                 break;
699         case QEDE_DUPLEX_FULL:
700                 link_duplex = ETH_LINK_FULL_DUPLEX;
701                 break;
702         case QEDE_DUPLEX_UNKNOWN:
703         default:
704                 link_duplex = -1;
705         }
706         curr->link_duplex = link_duplex;
707
708         /* Link Status */
709         curr->link_status = (link.link_up) ? ETH_LINK_UP : ETH_LINK_DOWN;
710
711         /* AN */
712         curr->link_autoneg = (link.supported_caps & QEDE_SUPPORTED_AUTONEG) ?
713                              ETH_LINK_AUTONEG : ETH_LINK_FIXED;
714
715         DP_INFO(edev, "Link - Speed %u Mode %u AN %u Status %u\n",
716                 curr->link_speed, curr->link_duplex,
717                 curr->link_autoneg, curr->link_status);
718
719         /* return 0 means link status changed, -1 means not changed */
720         return ((curr->link_status == link.link_up) ? -1 : 0);
721 }
722
723 static void
724 qede_rx_mode_setting(struct rte_eth_dev *eth_dev,
725                      enum qed_filter_rx_mode_type accept_flags)
726 {
727         struct qede_dev *qdev = eth_dev->data->dev_private;
728         struct ecore_dev *edev = &qdev->edev;
729         struct qed_filter_params rx_mode;
730
731         DP_INFO(edev, "%s mode %u\n", __func__, accept_flags);
732
733         memset(&rx_mode, 0, sizeof(struct qed_filter_params));
734         rx_mode.type = QED_FILTER_TYPE_RX_MODE;
735         rx_mode.filter.accept_flags = accept_flags;
736         qdev->ops->filter_config(edev, &rx_mode);
737 }
738
739 static void qede_promiscuous_enable(struct rte_eth_dev *eth_dev)
740 {
741         struct qede_dev *qdev = eth_dev->data->dev_private;
742         struct ecore_dev *edev = &qdev->edev;
743
744         PMD_INIT_FUNC_TRACE(edev);
745
746         enum qed_filter_rx_mode_type type = QED_FILTER_RX_MODE_TYPE_PROMISC;
747
748         if (rte_eth_allmulticast_get(eth_dev->data->port_id) == 1)
749                 type |= QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC;
750
751         qede_rx_mode_setting(eth_dev, type);
752 }
753
754 static void qede_promiscuous_disable(struct rte_eth_dev *eth_dev)
755 {
756         struct qede_dev *qdev = eth_dev->data->dev_private;
757         struct ecore_dev *edev = &qdev->edev;
758
759         PMD_INIT_FUNC_TRACE(edev);
760
761         if (rte_eth_allmulticast_get(eth_dev->data->port_id) == 1)
762                 qede_rx_mode_setting(eth_dev,
763                                      QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC);
764         else
765                 qede_rx_mode_setting(eth_dev, QED_FILTER_RX_MODE_TYPE_REGULAR);
766 }
767
768 static void qede_poll_sp_sb_cb(void *param)
769 {
770         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
771         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
772         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
773         int rc;
774
775         qede_interrupt_action(ECORE_LEADING_HWFN(edev));
776         qede_interrupt_action(&edev->hwfns[1]);
777
778         rc = rte_eal_alarm_set(timer_period * US_PER_S,
779                                qede_poll_sp_sb_cb,
780                                (void *)eth_dev);
781         if (rc != 0) {
782                 DP_ERR(edev, "Unable to start periodic"
783                              " timer rc %d\n", rc);
784                 assert(false && "Unable to start periodic timer");
785         }
786 }
787
788 static void qede_dev_close(struct rte_eth_dev *eth_dev)
789 {
790         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
791         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
792         int rc;
793
794         PMD_INIT_FUNC_TRACE(edev);
795
796         /* dev_stop() shall cleanup fp resources in hw but without releasing
797          * dma memories and sw structures so that dev_start() can be called
798          * by the app without reconfiguration. However, in dev_close() we
799          * can release all the resources and device can be brought up newly
800          */
801         if (qdev->state != QEDE_DEV_STOP)
802                 qede_dev_stop(eth_dev);
803         else
804                 DP_INFO(edev, "Device is already stopped\n");
805
806         rc = qdev->ops->vport_stop(edev, 0);
807         if (rc != 0)
808                 DP_ERR(edev, "Failed to stop VPORT\n");
809
810         qede_dealloc_fp_resc(eth_dev);
811
812         qdev->ops->common->slowpath_stop(edev);
813
814         qdev->ops->common->remove(edev);
815
816         rte_intr_disable(&eth_dev->pci_dev->intr_handle);
817
818         rte_intr_callback_unregister(&eth_dev->pci_dev->intr_handle,
819                                      qede_interrupt_handler, (void *)eth_dev);
820
821         if (edev->num_hwfns > 1)
822                 rte_eal_alarm_cancel(qede_poll_sp_sb_cb, (void *)eth_dev);
823
824         qdev->state = QEDE_DEV_INIT; /* Go back to init state */
825 }
826
827 static void
828 qede_get_stats(struct rte_eth_dev *eth_dev, struct rte_eth_stats *eth_stats)
829 {
830         struct qede_dev *qdev = eth_dev->data->dev_private;
831         struct ecore_dev *edev = &qdev->edev;
832         struct ecore_eth_stats stats;
833
834         qdev->ops->get_vport_stats(edev, &stats);
835
836         /* RX Stats */
837         eth_stats->ipackets = stats.rx_ucast_pkts +
838             stats.rx_mcast_pkts + stats.rx_bcast_pkts;
839
840         eth_stats->ibytes = stats.rx_ucast_bytes +
841             stats.rx_mcast_bytes + stats.rx_bcast_bytes;
842
843         eth_stats->ierrors = stats.rx_crc_errors +
844             stats.rx_align_errors +
845             stats.rx_carrier_errors +
846             stats.rx_oversize_packets +
847             stats.rx_jabbers + stats.rx_undersize_packets;
848
849         eth_stats->rx_nombuf = stats.no_buff_discards;
850
851         eth_stats->imissed = stats.mftag_filter_discards +
852             stats.mac_filter_discards +
853             stats.no_buff_discards + stats.brb_truncates + stats.brb_discards;
854
855         /* TX stats */
856         eth_stats->opackets = stats.tx_ucast_pkts +
857             stats.tx_mcast_pkts + stats.tx_bcast_pkts;
858
859         eth_stats->obytes = stats.tx_ucast_bytes +
860             stats.tx_mcast_bytes + stats.tx_bcast_bytes;
861
862         eth_stats->oerrors = stats.tx_err_drop_pkts;
863 }
864
865 static int
866 qede_get_xstats_names(__rte_unused struct rte_eth_dev *dev,
867                       struct rte_eth_xstat_name *xstats_names, unsigned limit)
868 {
869         unsigned int i, stat_cnt = RTE_DIM(qede_xstats_strings);
870
871         if (xstats_names != NULL)
872                 for (i = 0; i < stat_cnt; i++)
873                         snprintf(xstats_names[i].name,
874                                 sizeof(xstats_names[i].name),
875                                 "%s",
876                                 qede_xstats_strings[i].name);
877
878         return stat_cnt;
879 }
880
881 static int
882 qede_get_xstats(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
883                 unsigned int n)
884 {
885         struct qede_dev *qdev = dev->data->dev_private;
886         struct ecore_dev *edev = &qdev->edev;
887         struct ecore_eth_stats stats;
888         unsigned int num = RTE_DIM(qede_xstats_strings);
889
890         if (n < num)
891                 return num;
892
893         qdev->ops->get_vport_stats(edev, &stats);
894
895         for (num = 0; num < n; num++)
896                 xstats[num].value = *(u64 *)(((char *)&stats) +
897                                              qede_xstats_strings[num].offset);
898
899         return num;
900 }
901
902 static void
903 qede_reset_xstats(struct rte_eth_dev *dev)
904 {
905         struct qede_dev *qdev = dev->data->dev_private;
906         struct ecore_dev *edev = &qdev->edev;
907
908         ecore_reset_vport_stats(edev);
909 }
910
911 int qede_dev_set_link_state(struct rte_eth_dev *eth_dev, bool link_up)
912 {
913         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
914         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
915         struct qed_link_params link_params;
916         int rc;
917
918         DP_INFO(edev, "setting link state %d\n", link_up);
919         memset(&link_params, 0, sizeof(link_params));
920         link_params.link_up = link_up;
921         rc = qdev->ops->common->set_link(edev, &link_params);
922         if (rc != ECORE_SUCCESS)
923                 DP_ERR(edev, "Unable to set link state %d\n", link_up);
924
925         return rc;
926 }
927
928 static int qede_dev_set_link_up(struct rte_eth_dev *eth_dev)
929 {
930         return qede_dev_set_link_state(eth_dev, true);
931 }
932
933 static int qede_dev_set_link_down(struct rte_eth_dev *eth_dev)
934 {
935         return qede_dev_set_link_state(eth_dev, false);
936 }
937
938 static void qede_reset_stats(struct rte_eth_dev *eth_dev)
939 {
940         struct qede_dev *qdev = eth_dev->data->dev_private;
941         struct ecore_dev *edev = &qdev->edev;
942
943         ecore_reset_vport_stats(edev);
944 }
945
946 static void qede_allmulticast_enable(struct rte_eth_dev *eth_dev)
947 {
948         enum qed_filter_rx_mode_type type =
949             QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC;
950
951         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1)
952                 type |= QED_FILTER_RX_MODE_TYPE_PROMISC;
953
954         qede_rx_mode_setting(eth_dev, type);
955 }
956
957 static void qede_allmulticast_disable(struct rte_eth_dev *eth_dev)
958 {
959         if (rte_eth_promiscuous_get(eth_dev->data->port_id) == 1)
960                 qede_rx_mode_setting(eth_dev, QED_FILTER_RX_MODE_TYPE_PROMISC);
961         else
962                 qede_rx_mode_setting(eth_dev, QED_FILTER_RX_MODE_TYPE_REGULAR);
963 }
964
965 static int qede_flow_ctrl_set(struct rte_eth_dev *eth_dev,
966                               struct rte_eth_fc_conf *fc_conf)
967 {
968         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
969         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
970         struct qed_link_output current_link;
971         struct qed_link_params params;
972
973         memset(&current_link, 0, sizeof(current_link));
974         qdev->ops->common->get_link(edev, &current_link);
975
976         memset(&params, 0, sizeof(params));
977         params.override_flags |= QED_LINK_OVERRIDE_PAUSE_CONFIG;
978         if (fc_conf->autoneg) {
979                 if (!(current_link.supported_caps & QEDE_SUPPORTED_AUTONEG)) {
980                         DP_ERR(edev, "Autoneg not supported\n");
981                         return -EINVAL;
982                 }
983                 params.pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
984         }
985
986         /* Pause is assumed to be supported (SUPPORTED_Pause) */
987         if (fc_conf->mode == RTE_FC_FULL)
988                 params.pause_config |= (QED_LINK_PAUSE_TX_ENABLE |
989                                         QED_LINK_PAUSE_RX_ENABLE);
990         if (fc_conf->mode == RTE_FC_TX_PAUSE)
991                 params.pause_config |= QED_LINK_PAUSE_TX_ENABLE;
992         if (fc_conf->mode == RTE_FC_RX_PAUSE)
993                 params.pause_config |= QED_LINK_PAUSE_RX_ENABLE;
994
995         params.link_up = true;
996         (void)qdev->ops->common->set_link(edev, &params);
997
998         return 0;
999 }
1000
1001 static int qede_flow_ctrl_get(struct rte_eth_dev *eth_dev,
1002                               struct rte_eth_fc_conf *fc_conf)
1003 {
1004         struct qede_dev *qdev = QEDE_INIT_QDEV(eth_dev);
1005         struct ecore_dev *edev = QEDE_INIT_EDEV(qdev);
1006         struct qed_link_output current_link;
1007
1008         memset(&current_link, 0, sizeof(current_link));
1009         qdev->ops->common->get_link(edev, &current_link);
1010
1011         if (current_link.pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
1012                 fc_conf->autoneg = true;
1013
1014         if (current_link.pause_config & (QED_LINK_PAUSE_RX_ENABLE |
1015                                          QED_LINK_PAUSE_TX_ENABLE))
1016                 fc_conf->mode = RTE_FC_FULL;
1017         else if (current_link.pause_config & QED_LINK_PAUSE_RX_ENABLE)
1018                 fc_conf->mode = RTE_FC_RX_PAUSE;
1019         else if (current_link.pause_config & QED_LINK_PAUSE_TX_ENABLE)
1020                 fc_conf->mode = RTE_FC_TX_PAUSE;
1021         else
1022                 fc_conf->mode = RTE_FC_NONE;
1023
1024         return 0;
1025 }
1026
1027 static const uint32_t *
1028 qede_dev_supported_ptypes_get(struct rte_eth_dev *eth_dev)
1029 {
1030         static const uint32_t ptypes[] = {
1031                 RTE_PTYPE_L3_IPV4,
1032                 RTE_PTYPE_L3_IPV6,
1033                 RTE_PTYPE_UNKNOWN
1034         };
1035
1036         if (eth_dev->rx_pkt_burst == qede_recv_pkts)
1037                 return ptypes;
1038
1039         return NULL;
1040 }
1041
1042 void qede_init_rss_caps(uint8_t *rss_caps, uint64_t hf)
1043 {
1044         *rss_caps = 0;
1045         *rss_caps |= (hf & ETH_RSS_IPV4)              ? ECORE_RSS_IPV4 : 0;
1046         *rss_caps |= (hf & ETH_RSS_IPV6)              ? ECORE_RSS_IPV6 : 0;
1047         *rss_caps |= (hf & ETH_RSS_IPV6_EX)           ? ECORE_RSS_IPV6 : 0;
1048         *rss_caps |= (hf & ETH_RSS_NONFRAG_IPV4_TCP)  ? ECORE_RSS_IPV4_TCP : 0;
1049         *rss_caps |= (hf & ETH_RSS_NONFRAG_IPV6_TCP)  ? ECORE_RSS_IPV6_TCP : 0;
1050         *rss_caps |= (hf & ETH_RSS_IPV6_TCP_EX)       ? ECORE_RSS_IPV6_TCP : 0;
1051 }
1052
1053 static int qede_rss_hash_update(struct rte_eth_dev *eth_dev,
1054                                 struct rte_eth_rss_conf *rss_conf)
1055 {
1056         struct qed_update_vport_params vport_update_params;
1057         struct qede_dev *qdev = eth_dev->data->dev_private;
1058         struct ecore_dev *edev = &qdev->edev;
1059         uint32_t *key = (uint32_t *)rss_conf->rss_key;
1060         uint64_t hf = rss_conf->rss_hf;
1061         int i;
1062
1063         memset(&vport_update_params, 0, sizeof(vport_update_params));
1064
1065         if (hf != 0) {
1066                 /* Enable RSS */
1067                 qede_init_rss_caps(&qdev->rss_params.rss_caps, hf);
1068                 memcpy(&vport_update_params.rss_params, &qdev->rss_params,
1069                        sizeof(vport_update_params.rss_params));
1070                 if (key)
1071                         memcpy(qdev->rss_params.rss_key, rss_conf->rss_key,
1072                                rss_conf->rss_key_len);
1073                 vport_update_params.update_rss_flg = 1;
1074                 qdev->rss_enabled = 1;
1075         } else {
1076                 /* Disable RSS */
1077                 qdev->rss_enabled = 0;
1078         }
1079
1080         /* If the mapping doesn't fit any supported, return */
1081         if (qdev->rss_params.rss_caps == 0 && hf != 0)
1082                 return -EINVAL;
1083
1084         DP_INFO(edev, "%s\n", (vport_update_params.update_rss_flg) ?
1085                                 "Enabling RSS" : "Disabling RSS");
1086
1087         vport_update_params.vport_id = 0;
1088
1089         return qdev->ops->vport_update(edev, &vport_update_params);
1090 }
1091
1092 int qede_rss_hash_conf_get(struct rte_eth_dev *eth_dev,
1093                            struct rte_eth_rss_conf *rss_conf)
1094 {
1095         struct qede_dev *qdev = eth_dev->data->dev_private;
1096         uint64_t hf;
1097
1098         if (rss_conf->rss_key_len < sizeof(qdev->rss_params.rss_key))
1099                 return -EINVAL;
1100
1101         if (rss_conf->rss_key)
1102                 memcpy(rss_conf->rss_key, qdev->rss_params.rss_key,
1103                        sizeof(qdev->rss_params.rss_key));
1104
1105         hf = 0;
1106         hf |= (qdev->rss_params.rss_caps & ECORE_RSS_IPV4)     ?
1107                         ETH_RSS_IPV4 : 0;
1108         hf |= (qdev->rss_params.rss_caps & ECORE_RSS_IPV6)     ?
1109                         ETH_RSS_IPV6 : 0;
1110         hf |= (qdev->rss_params.rss_caps & ECORE_RSS_IPV6)     ?
1111                         ETH_RSS_IPV6_EX : 0;
1112         hf |= (qdev->rss_params.rss_caps & ECORE_RSS_IPV4_TCP) ?
1113                         ETH_RSS_NONFRAG_IPV4_TCP : 0;
1114         hf |= (qdev->rss_params.rss_caps & ECORE_RSS_IPV6_TCP) ?
1115                         ETH_RSS_NONFRAG_IPV6_TCP : 0;
1116         hf |= (qdev->rss_params.rss_caps & ECORE_RSS_IPV6_TCP) ?
1117                         ETH_RSS_IPV6_TCP_EX : 0;
1118
1119         rss_conf->rss_hf = hf;
1120
1121         return 0;
1122 }
1123
1124 static int qede_rss_reta_update(struct rte_eth_dev *eth_dev,
1125                                 struct rte_eth_rss_reta_entry64 *reta_conf,
1126                                 uint16_t reta_size)
1127 {
1128         struct qed_update_vport_params vport_update_params;
1129         struct qede_dev *qdev = eth_dev->data->dev_private;
1130         struct ecore_dev *edev = &qdev->edev;
1131         uint16_t i, idx, shift;
1132
1133         if (reta_size > ETH_RSS_RETA_SIZE_128) {
1134                 DP_ERR(edev, "reta_size %d is not supported by hardware\n",
1135                        reta_size);
1136                 return -EINVAL;
1137         }
1138
1139         memset(&vport_update_params, 0, sizeof(vport_update_params));
1140         memcpy(&vport_update_params.rss_params, &qdev->rss_params,
1141                sizeof(vport_update_params.rss_params));
1142
1143         for (i = 0; i < reta_size; i++) {
1144                 idx = i / RTE_RETA_GROUP_SIZE;
1145                 shift = i % RTE_RETA_GROUP_SIZE;
1146                 if (reta_conf[idx].mask & (1ULL << shift)) {
1147                         uint8_t entry = reta_conf[idx].reta[shift];
1148                         qdev->rss_params.rss_ind_table[i] = entry;
1149                 }
1150         }
1151
1152         vport_update_params.update_rss_flg = 1;
1153         vport_update_params.vport_id = 0;
1154
1155         return qdev->ops->vport_update(edev, &vport_update_params);
1156 }
1157
1158 int qede_rss_reta_query(struct rte_eth_dev *eth_dev,
1159                         struct rte_eth_rss_reta_entry64 *reta_conf,
1160                         uint16_t reta_size)
1161 {
1162         struct qede_dev *qdev = eth_dev->data->dev_private;
1163         uint16_t i, idx, shift;
1164
1165         if (reta_size > ETH_RSS_RETA_SIZE_128) {
1166                 struct ecore_dev *edev = &qdev->edev;
1167                 DP_ERR(edev, "reta_size %d is not supported\n",
1168                        reta_size);
1169         }
1170
1171         for (i = 0; i < reta_size; i++) {
1172                 idx = i / RTE_RETA_GROUP_SIZE;
1173                 shift = i % RTE_RETA_GROUP_SIZE;
1174                 if (reta_conf[idx].mask & (1ULL << shift)) {
1175                         uint8_t entry = qdev->rss_params.rss_ind_table[i];
1176                         reta_conf[idx].reta[shift] = entry;
1177                 }
1178         }
1179
1180         return 0;
1181 }
1182
1183 int qede_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1184 {
1185         uint32_t frame_size;
1186         struct qede_dev *qdev = dev->data->dev_private;
1187         struct rte_eth_dev_info dev_info = {0};
1188
1189         qede_dev_info_get(dev, &dev_info);
1190
1191         /* VLAN_TAG = 4 */
1192         frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 4;
1193
1194         if ((mtu < ETHER_MIN_MTU) || (frame_size > dev_info.max_rx_pktlen))
1195                 return -EINVAL;
1196
1197         if (!dev->data->scattered_rx &&
1198             frame_size > dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM)
1199                 return -EINVAL;
1200
1201         if (frame_size > ETHER_MAX_LEN)
1202                 dev->data->dev_conf.rxmode.jumbo_frame = 1;
1203         else
1204                 dev->data->dev_conf.rxmode.jumbo_frame = 0;
1205
1206         /* update max frame size */
1207         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1208         qdev->mtu = mtu;
1209         qede_dev_stop(dev);
1210         qede_dev_start(dev);
1211
1212         return 0;
1213 }
1214
1215 static const struct eth_dev_ops qede_eth_dev_ops = {
1216         .dev_configure = qede_dev_configure,
1217         .dev_infos_get = qede_dev_info_get,
1218         .rx_queue_setup = qede_rx_queue_setup,
1219         .rx_queue_release = qede_rx_queue_release,
1220         .tx_queue_setup = qede_tx_queue_setup,
1221         .tx_queue_release = qede_tx_queue_release,
1222         .dev_start = qede_dev_start,
1223         .dev_set_link_up = qede_dev_set_link_up,
1224         .dev_set_link_down = qede_dev_set_link_down,
1225         .link_update = qede_link_update,
1226         .promiscuous_enable = qede_promiscuous_enable,
1227         .promiscuous_disable = qede_promiscuous_disable,
1228         .allmulticast_enable = qede_allmulticast_enable,
1229         .allmulticast_disable = qede_allmulticast_disable,
1230         .dev_stop = qede_dev_stop,
1231         .dev_close = qede_dev_close,
1232         .stats_get = qede_get_stats,
1233         .stats_reset = qede_reset_stats,
1234         .xstats_get = qede_get_xstats,
1235         .xstats_reset = qede_reset_xstats,
1236         .xstats_get_names = qede_get_xstats_names,
1237         .mac_addr_add = qede_mac_addr_add,
1238         .mac_addr_remove = qede_mac_addr_remove,
1239         .mac_addr_set = qede_mac_addr_set,
1240         .vlan_offload_set = qede_vlan_offload_set,
1241         .vlan_filter_set = qede_vlan_filter_set,
1242         .flow_ctrl_set = qede_flow_ctrl_set,
1243         .flow_ctrl_get = qede_flow_ctrl_get,
1244         .dev_supported_ptypes_get = qede_dev_supported_ptypes_get,
1245         .rss_hash_update = qede_rss_hash_update,
1246         .rss_hash_conf_get = qede_rss_hash_conf_get,
1247         .reta_update  = qede_rss_reta_update,
1248         .reta_query  = qede_rss_reta_query,
1249         .mtu_set = qede_set_mtu,
1250 };
1251
1252 static const struct eth_dev_ops qede_eth_vf_dev_ops = {
1253         .dev_configure = qede_dev_configure,
1254         .dev_infos_get = qede_dev_info_get,
1255         .rx_queue_setup = qede_rx_queue_setup,
1256         .rx_queue_release = qede_rx_queue_release,
1257         .tx_queue_setup = qede_tx_queue_setup,
1258         .tx_queue_release = qede_tx_queue_release,
1259         .dev_start = qede_dev_start,
1260         .dev_set_link_up = qede_dev_set_link_up,
1261         .dev_set_link_down = qede_dev_set_link_down,
1262         .link_update = qede_link_update,
1263         .promiscuous_enable = qede_promiscuous_enable,
1264         .promiscuous_disable = qede_promiscuous_disable,
1265         .allmulticast_enable = qede_allmulticast_enable,
1266         .allmulticast_disable = qede_allmulticast_disable,
1267         .dev_stop = qede_dev_stop,
1268         .dev_close = qede_dev_close,
1269         .stats_get = qede_get_stats,
1270         .stats_reset = qede_reset_stats,
1271         .xstats_get = qede_get_xstats,
1272         .xstats_reset = qede_reset_xstats,
1273         .xstats_get_names = qede_get_xstats_names,
1274         .vlan_offload_set = qede_vlan_offload_set,
1275         .vlan_filter_set = qede_vlan_filter_set,
1276         .dev_supported_ptypes_get = qede_dev_supported_ptypes_get,
1277         .rss_hash_update = qede_rss_hash_update,
1278         .rss_hash_conf_get = qede_rss_hash_conf_get,
1279         .reta_update  = qede_rss_reta_update,
1280         .reta_query  = qede_rss_reta_query,
1281         .mtu_set = qede_set_mtu,
1282 };
1283
1284 static void qede_update_pf_params(struct ecore_dev *edev)
1285 {
1286         struct ecore_pf_params pf_params;
1287         /* 32 rx + 32 tx */
1288         memset(&pf_params, 0, sizeof(struct ecore_pf_params));
1289         pf_params.eth_pf_params.num_cons = 64;
1290         qed_ops->common->update_pf_params(edev, &pf_params);
1291 }
1292
1293 static int qede_common_dev_init(struct rte_eth_dev *eth_dev, bool is_vf)
1294 {
1295         struct rte_pci_device *pci_dev;
1296         struct rte_pci_addr pci_addr;
1297         struct qede_dev *adapter;
1298         struct ecore_dev *edev;
1299         struct qed_dev_eth_info dev_info;
1300         struct qed_slowpath_params params;
1301         static bool do_once = true;
1302         uint8_t bulletin_change;
1303         uint8_t vf_mac[ETHER_ADDR_LEN];
1304         uint8_t is_mac_forced;
1305         bool is_mac_exist;
1306         /* Fix up ecore debug level */
1307         uint32_t dp_module = ~0 & ~ECORE_MSG_HW;
1308         uint8_t dp_level = ECORE_LEVEL_VERBOSE;
1309         uint32_t max_mac_addrs;
1310         int rc;
1311
1312         /* Extract key data structures */
1313         adapter = eth_dev->data->dev_private;
1314         edev = &adapter->edev;
1315         pci_addr = eth_dev->pci_dev->addr;
1316
1317         PMD_INIT_FUNC_TRACE(edev);
1318
1319         snprintf(edev->name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
1320                  pci_addr.bus, pci_addr.devid, pci_addr.function,
1321                  eth_dev->data->port_id);
1322
1323         eth_dev->rx_pkt_burst = qede_recv_pkts;
1324         eth_dev->tx_pkt_burst = qede_xmit_pkts;
1325
1326         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1327                 DP_NOTICE(edev, false,
1328                           "Skipping device init from secondary process\n");
1329                 return 0;
1330         }
1331
1332         pci_dev = eth_dev->pci_dev;
1333
1334         rte_eth_copy_pci_info(eth_dev, pci_dev);
1335
1336         qed_ops = qed_get_eth_ops();
1337         if (!qed_ops) {
1338                 DP_ERR(edev, "Failed to get qed_eth_ops_pass\n");
1339                 return -EINVAL;
1340         }
1341
1342         DP_INFO(edev, "Starting qede probe\n");
1343
1344         rc = qed_ops->common->probe(edev, pci_dev, QED_PROTOCOL_ETH,
1345                                     dp_module, dp_level, is_vf);
1346
1347         if (rc != 0) {
1348                 DP_ERR(edev, "qede probe failed rc %d\n", rc);
1349                 return -ENODEV;
1350         }
1351
1352         qede_update_pf_params(edev);
1353
1354         rte_intr_callback_register(&eth_dev->pci_dev->intr_handle,
1355                                    qede_interrupt_handler, (void *)eth_dev);
1356
1357         if (rte_intr_enable(&eth_dev->pci_dev->intr_handle)) {
1358                 DP_ERR(edev, "rte_intr_enable() failed\n");
1359                 return -ENODEV;
1360         }
1361
1362         /* Start the Slowpath-process */
1363         memset(&params, 0, sizeof(struct qed_slowpath_params));
1364         params.int_mode = ECORE_INT_MODE_MSIX;
1365         params.drv_major = QEDE_MAJOR_VERSION;
1366         params.drv_minor = QEDE_MINOR_VERSION;
1367         params.drv_rev = QEDE_REVISION_VERSION;
1368         params.drv_eng = QEDE_ENGINEERING_VERSION;
1369         strncpy((char *)params.name, "qede LAN", QED_DRV_VER_STR_SIZE);
1370
1371         /* For CMT mode device do periodic polling for slowpath events.
1372          * This is required since uio device uses only one MSI-x
1373          * interrupt vector but we need one for each engine.
1374          */
1375         if (edev->num_hwfns > 1 && IS_PF(edev)) {
1376                 rc = rte_eal_alarm_set(timer_period * US_PER_S,
1377                                        qede_poll_sp_sb_cb,
1378                                        (void *)eth_dev);
1379                 if (rc != 0) {
1380                         DP_ERR(edev, "Unable to start periodic"
1381                                      " timer rc %d\n", rc);
1382                         return -EINVAL;
1383                 }
1384         }
1385
1386         rc = qed_ops->common->slowpath_start(edev, &params);
1387         if (rc) {
1388                 DP_ERR(edev, "Cannot start slowpath rc = %d\n", rc);
1389                 rte_eal_alarm_cancel(qede_poll_sp_sb_cb,
1390                                      (void *)eth_dev);
1391                 return -ENODEV;
1392         }
1393
1394         rc = qed_ops->fill_dev_info(edev, &dev_info);
1395         if (rc) {
1396                 DP_ERR(edev, "Cannot get device_info rc %d\n", rc);
1397                 qed_ops->common->slowpath_stop(edev);
1398                 qed_ops->common->remove(edev);
1399                 rte_eal_alarm_cancel(qede_poll_sp_sb_cb,
1400                                      (void *)eth_dev);
1401                 return -ENODEV;
1402         }
1403
1404         qede_alloc_etherdev(adapter, &dev_info);
1405
1406         adapter->ops->common->set_id(edev, edev->name, QEDE_DRV_MODULE_VERSION);
1407
1408         if (!is_vf)
1409                 adapter->dev_info.num_mac_addrs =
1410                         (uint32_t)RESC_NUM(ECORE_LEADING_HWFN(edev),
1411                                             ECORE_MAC);
1412         else
1413                 ecore_vf_get_num_mac_filters(ECORE_LEADING_HWFN(edev),
1414                                              &adapter->dev_info.num_mac_addrs);
1415
1416         /* Allocate memory for storing MAC addr */
1417         eth_dev->data->mac_addrs = rte_zmalloc(edev->name,
1418                                         (ETHER_ADDR_LEN *
1419                                         adapter->dev_info.num_mac_addrs),
1420                                         RTE_CACHE_LINE_SIZE);
1421
1422         if (eth_dev->data->mac_addrs == NULL) {
1423                 DP_ERR(edev, "Failed to allocate MAC address\n");
1424                 qed_ops->common->slowpath_stop(edev);
1425                 qed_ops->common->remove(edev);
1426                 rte_eal_alarm_cancel(qede_poll_sp_sb_cb,
1427                                      (void *)eth_dev);
1428                 return -ENOMEM;
1429         }
1430
1431         if (!is_vf) {
1432                 ether_addr_copy((struct ether_addr *)edev->hwfns[0].
1433                                 hw_info.hw_mac_addr,
1434                                 &eth_dev->data->mac_addrs[0]);
1435                 ether_addr_copy(&eth_dev->data->mac_addrs[0],
1436                                 &adapter->primary_mac);
1437         } else {
1438                 ecore_vf_read_bulletin(ECORE_LEADING_HWFN(edev),
1439                                        &bulletin_change);
1440                 if (bulletin_change) {
1441                         is_mac_exist =
1442                             ecore_vf_bulletin_get_forced_mac(
1443                                                 ECORE_LEADING_HWFN(edev),
1444                                                 vf_mac,
1445                                                 &is_mac_forced);
1446                         if (is_mac_exist && is_mac_forced) {
1447                                 DP_INFO(edev, "VF macaddr received from PF\n");
1448                                 ether_addr_copy((struct ether_addr *)&vf_mac,
1449                                                 &eth_dev->data->mac_addrs[0]);
1450                                 ether_addr_copy(&eth_dev->data->mac_addrs[0],
1451                                                 &adapter->primary_mac);
1452                         } else {
1453                                 DP_NOTICE(edev, false,
1454                                           "No VF macaddr assigned\n");
1455                         }
1456                 }
1457         }
1458
1459         eth_dev->dev_ops = (is_vf) ? &qede_eth_vf_dev_ops : &qede_eth_dev_ops;
1460
1461         if (do_once) {
1462                 qede_print_adapter_info(adapter);
1463                 do_once = false;
1464         }
1465
1466         adapter->state = QEDE_DEV_INIT;
1467
1468         DP_NOTICE(edev, false, "MAC address : %02x:%02x:%02x:%02x:%02x:%02x\n",
1469                   adapter->primary_mac.addr_bytes[0],
1470                   adapter->primary_mac.addr_bytes[1],
1471                   adapter->primary_mac.addr_bytes[2],
1472                   adapter->primary_mac.addr_bytes[3],
1473                   adapter->primary_mac.addr_bytes[4],
1474                   adapter->primary_mac.addr_bytes[5]);
1475
1476         return rc;
1477 }
1478
1479 static int qedevf_eth_dev_init(struct rte_eth_dev *eth_dev)
1480 {
1481         return qede_common_dev_init(eth_dev, 1);
1482 }
1483
1484 static int qede_eth_dev_init(struct rte_eth_dev *eth_dev)
1485 {
1486         return qede_common_dev_init(eth_dev, 0);
1487 }
1488
1489 static int qede_dev_common_uninit(struct rte_eth_dev *eth_dev)
1490 {
1491         /* only uninitialize in the primary process */
1492         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1493                 return 0;
1494
1495         /* safe to close dev here */
1496         qede_dev_close(eth_dev);
1497
1498         eth_dev->dev_ops = NULL;
1499         eth_dev->rx_pkt_burst = NULL;
1500         eth_dev->tx_pkt_burst = NULL;
1501
1502         if (eth_dev->data->mac_addrs)
1503                 rte_free(eth_dev->data->mac_addrs);
1504
1505         eth_dev->data->mac_addrs = NULL;
1506
1507         return 0;
1508 }
1509
1510 static int qede_eth_dev_uninit(struct rte_eth_dev *eth_dev)
1511 {
1512         return qede_dev_common_uninit(eth_dev);
1513 }
1514
1515 static int qedevf_eth_dev_uninit(struct rte_eth_dev *eth_dev)
1516 {
1517         return qede_dev_common_uninit(eth_dev);
1518 }
1519
1520 static struct rte_pci_id pci_id_qedevf_map[] = {
1521 #define QEDEVF_RTE_PCI_DEVICE(dev) RTE_PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, dev)
1522         {
1523                 QEDEVF_RTE_PCI_DEVICE(PCI_DEVICE_ID_NX2_VF)
1524         },
1525         {
1526                 QEDEVF_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_IOV)
1527         },
1528         {.vendor_id = 0,}
1529 };
1530
1531 static struct rte_pci_id pci_id_qede_map[] = {
1532 #define QEDE_RTE_PCI_DEVICE(dev) RTE_PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, dev)
1533         {
1534                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_NX2_57980E)
1535         },
1536         {
1537                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_NX2_57980S)
1538         },
1539         {
1540                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_40)
1541         },
1542         {
1543                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_25)
1544         },
1545         {
1546                 QEDE_RTE_PCI_DEVICE(PCI_DEVICE_ID_57980S_100)
1547         },
1548         {.vendor_id = 0,}
1549 };
1550
1551 static struct eth_driver rte_qedevf_pmd = {
1552         .pci_drv = {
1553                     .id_table = pci_id_qedevf_map,
1554                     .drv_flags =
1555                     RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1556                     .probe = rte_eth_dev_pci_probe,
1557                     .remove = rte_eth_dev_pci_remove,
1558                    },
1559         .eth_dev_init = qedevf_eth_dev_init,
1560         .eth_dev_uninit = qedevf_eth_dev_uninit,
1561         .dev_private_size = sizeof(struct qede_dev),
1562 };
1563
1564 static struct eth_driver rte_qede_pmd = {
1565         .pci_drv = {
1566                     .id_table = pci_id_qede_map,
1567                     .drv_flags =
1568                     RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1569                     .probe = rte_eth_dev_pci_probe,
1570                     .remove = rte_eth_dev_pci_remove,
1571                    },
1572         .eth_dev_init = qede_eth_dev_init,
1573         .eth_dev_uninit = qede_eth_dev_uninit,
1574         .dev_private_size = sizeof(struct qede_dev),
1575 };
1576
1577 RTE_PMD_REGISTER_PCI(net_qede, rte_qede_pmd.pci_drv);
1578 RTE_PMD_REGISTER_PCI_TABLE(net_qede, pci_id_qede_map);
1579 RTE_PMD_REGISTER_PCI(net_qede_vf, rte_qedevf_pmd.pci_drv);
1580 RTE_PMD_REGISTER_PCI_TABLE(net_qede_vf, pci_id_qedevf_map);