9855d0e12e37502b04ae0a2733e86ed15384284a
[dpdk.git] / drivers / net / qede / qede_eth_if.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 static int
12 qed_start_vport(struct ecore_dev *edev, struct qed_start_vport_params *p_params)
13 {
14         int rc, i;
15
16         for_each_hwfn(edev, i) {
17                 struct ecore_hwfn *p_hwfn = &edev->hwfns[i];
18                 u8 tx_switching = 0;
19                 struct ecore_sp_vport_start_params start = { 0 };
20
21                 start.tpa_mode = p_params->gro_enable ? ECORE_TPA_MODE_GRO :
22                     ECORE_TPA_MODE_NONE;
23                 start.remove_inner_vlan = p_params->remove_inner_vlan;
24                 start.tx_switching = tx_switching;
25                 start.only_untagged = false;    /* untagged only */
26                 start.drop_ttl0 = p_params->drop_ttl0;
27                 start.concrete_fid = p_hwfn->hw_info.concrete_fid;
28                 start.opaque_fid = p_hwfn->hw_info.opaque_fid;
29                 start.concrete_fid = p_hwfn->hw_info.concrete_fid;
30                 start.handle_ptp_pkts = p_params->handle_ptp_pkts;
31                 start.vport_id = p_params->vport_id;
32                 start.max_buffers_per_cqe = 16; /* TODO-is this right */
33                 start.mtu = p_params->mtu;
34                 /* @DPDK - Disable FW placement */
35                 start.zero_placement_offset = 1;
36
37                 rc = ecore_sp_vport_start(p_hwfn, &start);
38                 if (rc) {
39                         DP_ERR(edev, "Failed to start VPORT\n");
40                         return rc;
41                 }
42
43                 DP_VERBOSE(edev, ECORE_MSG_SPQ,
44                            "Started V-PORT %d with MTU %d\n",
45                            p_params->vport_id, p_params->mtu);
46         }
47
48         ecore_reset_vport_stats(edev);
49
50         return 0;
51 }
52
53 static int qed_stop_vport(struct ecore_dev *edev, uint8_t vport_id)
54 {
55         int rc, i;
56
57         for_each_hwfn(edev, i) {
58                 struct ecore_hwfn *p_hwfn = &edev->hwfns[i];
59                 rc = ecore_sp_vport_stop(p_hwfn,
60                                          p_hwfn->hw_info.opaque_fid, vport_id);
61
62                 if (rc) {
63                         DP_ERR(edev, "Failed to stop VPORT\n");
64                         return rc;
65                 }
66         }
67
68         return 0;
69 }
70
71 static int
72 qed_update_vport(struct ecore_dev *edev, struct qed_update_vport_params *params)
73 {
74         struct ecore_sp_vport_update_params sp_params;
75         struct ecore_rss_params sp_rss_params;
76         int rc, i;
77
78         memset(&sp_params, 0, sizeof(sp_params));
79         memset(&sp_rss_params, 0, sizeof(sp_rss_params));
80
81         /* Translate protocol params into sp params */
82         sp_params.vport_id = params->vport_id;
83         sp_params.update_vport_active_rx_flg = params->update_vport_active_flg;
84         sp_params.update_vport_active_tx_flg = params->update_vport_active_flg;
85         sp_params.vport_active_rx_flg = params->vport_active_flg;
86         sp_params.vport_active_tx_flg = params->vport_active_flg;
87         sp_params.update_inner_vlan_removal_flg =
88             params->update_inner_vlan_removal_flg;
89         sp_params.inner_vlan_removal_flg = params->inner_vlan_removal_flg;
90         sp_params.update_tx_switching_flg = params->update_tx_switching_flg;
91         sp_params.tx_switching_flg = params->tx_switching_flg;
92         sp_params.accept_any_vlan = params->accept_any_vlan;
93         sp_params.update_accept_any_vlan_flg =
94             params->update_accept_any_vlan_flg;
95         sp_params.mtu = params->mtu;
96
97         /* RSS - is a bit tricky, since upper-layer isn't familiar with hwfns.
98          * We need to re-fix the rss values per engine for CMT.
99          */
100
101         if (edev->num_hwfns > 1 && params->update_rss_flg) {
102                 struct qed_update_vport_rss_params *rss = &params->rss_params;
103                 int k, max = 0;
104
105                 /* Find largest entry, since it's possible RSS needs to
106                  * be disabled [in case only 1 queue per-hwfn]
107                  */
108                 for (k = 0; k < ECORE_RSS_IND_TABLE_SIZE; k++)
109                         max = (max > rss->rss_ind_table[k]) ?
110                             max : rss->rss_ind_table[k];
111
112                 /* Either fix RSS values or disable RSS */
113                 if (edev->num_hwfns < max + 1) {
114                         int divisor = (max + edev->num_hwfns - 1) /
115                             edev->num_hwfns;
116
117                         DP_VERBOSE(edev, ECORE_MSG_SPQ,
118                                    "CMT - fixing RSS values (modulo %02x)\n",
119                                    divisor);
120
121                         for (k = 0; k < ECORE_RSS_IND_TABLE_SIZE; k++)
122                                 rss->rss_ind_table[k] =
123                                     rss->rss_ind_table[k] % divisor;
124                 } else {
125                         DP_VERBOSE(edev, ECORE_MSG_SPQ,
126                                    "CMT - 1 queue per-hwfn; Disabling RSS\n");
127                         params->update_rss_flg = 0;
128                 }
129         }
130
131         /* Now, update the RSS configuration for actual configuration */
132         if (params->update_rss_flg) {
133                 sp_rss_params.update_rss_config = 1;
134                 sp_rss_params.rss_enable = 1;
135                 sp_rss_params.update_rss_capabilities = 1;
136                 sp_rss_params.update_rss_ind_table = 1;
137                 sp_rss_params.update_rss_key = 1;
138                 sp_rss_params.rss_caps = ECORE_RSS_IPV4 | ECORE_RSS_IPV6 |
139                     ECORE_RSS_IPV4_TCP | ECORE_RSS_IPV6_TCP;
140                 sp_rss_params.rss_table_size_log = 7;   /* 2^7 = 128 */
141                 rte_memcpy(sp_rss_params.rss_ind_table,
142                        params->rss_params.rss_ind_table,
143                        ECORE_RSS_IND_TABLE_SIZE * sizeof(uint16_t));
144                 rte_memcpy(sp_rss_params.rss_key, params->rss_params.rss_key,
145                        ECORE_RSS_KEY_SIZE * sizeof(uint32_t));
146         }
147         sp_params.rss_params = &sp_rss_params;
148
149         for_each_hwfn(edev, i) {
150                 struct ecore_hwfn *p_hwfn = &edev->hwfns[i];
151
152                 sp_params.opaque_fid = p_hwfn->hw_info.opaque_fid;
153                 rc = ecore_sp_vport_update(p_hwfn, &sp_params,
154                                            ECORE_SPQ_MODE_EBLOCK, NULL);
155                 if (rc) {
156                         DP_ERR(edev, "Failed to update VPORT\n");
157                         return rc;
158                 }
159
160                 DP_VERBOSE(edev, ECORE_MSG_SPQ,
161                            "Updated V-PORT %d: active_flag %d [update %d]\n",
162                            params->vport_id, params->vport_active_flg,
163                            params->update_vport_active_flg);
164         }
165
166         return 0;
167 }
168
169 static int
170 qed_start_rxq(struct ecore_dev *edev,
171               uint8_t rss_id, uint8_t rx_queue_id,
172               uint8_t vport_id, uint16_t sb,
173               uint8_t sb_index, uint16_t bd_max_bytes,
174               dma_addr_t bd_chain_phys_addr,
175               dma_addr_t cqe_pbl_addr,
176               uint16_t cqe_pbl_size, void OSAL_IOMEM * *pp_prod)
177 {
178         struct ecore_hwfn *p_hwfn;
179         int rc, hwfn_index;
180
181         hwfn_index = rss_id % edev->num_hwfns;
182         p_hwfn = &edev->hwfns[hwfn_index];
183
184         rc = ecore_sp_eth_rx_queue_start(p_hwfn,
185                                          p_hwfn->hw_info.opaque_fid,
186                                          rx_queue_id / edev->num_hwfns,
187                                          vport_id,
188                                          vport_id,
189                                          sb,
190                                          sb_index,
191                                          bd_max_bytes,
192                                          bd_chain_phys_addr,
193                                          cqe_pbl_addr, cqe_pbl_size, pp_prod);
194
195         if (rc) {
196                 DP_ERR(edev, "Failed to start RXQ#%d\n", rx_queue_id);
197                 return rc;
198         }
199
200         DP_VERBOSE(edev, ECORE_MSG_SPQ,
201                    "Started RX-Q %d [rss %d] on V-PORT %d and SB %d\n",
202                    rx_queue_id, rss_id, vport_id, sb);
203
204         return 0;
205 }
206
207 static int
208 qed_stop_rxq(struct ecore_dev *edev, struct qed_stop_rxq_params *params)
209 {
210         int rc, hwfn_index;
211         struct ecore_hwfn *p_hwfn;
212
213         hwfn_index = params->rss_id % edev->num_hwfns;
214         p_hwfn = &edev->hwfns[hwfn_index];
215
216         rc = ecore_sp_eth_rx_queue_stop(p_hwfn,
217                                         params->rx_queue_id / edev->num_hwfns,
218                                         params->eq_completion_only, false);
219         if (rc) {
220                 DP_ERR(edev, "Failed to stop RXQ#%d\n", params->rx_queue_id);
221                 return rc;
222         }
223
224         return 0;
225 }
226
227 static int
228 qed_start_txq(struct ecore_dev *edev,
229               uint8_t rss_id, uint16_t tx_queue_id,
230               uint8_t vport_id, uint16_t sb,
231               uint8_t sb_index,
232               dma_addr_t pbl_addr,
233               uint16_t pbl_size, void OSAL_IOMEM * *pp_doorbell)
234 {
235         struct ecore_hwfn *p_hwfn;
236         int rc, hwfn_index;
237
238         hwfn_index = rss_id % edev->num_hwfns;
239         p_hwfn = &edev->hwfns[hwfn_index];
240
241         rc = ecore_sp_eth_tx_queue_start(p_hwfn,
242                                          p_hwfn->hw_info.opaque_fid,
243                                          tx_queue_id / edev->num_hwfns,
244                                          vport_id,
245                                          vport_id,
246                                          sb,
247                                          sb_index,
248                                          0 /* tc */,
249                                          pbl_addr, pbl_size, pp_doorbell);
250
251         if (rc) {
252                 DP_ERR(edev, "Failed to start TXQ#%d\n", tx_queue_id);
253                 return rc;
254         }
255
256         DP_VERBOSE(edev, ECORE_MSG_SPQ,
257                    "Started TX-Q %d [rss %d] on V-PORT %d and SB %d\n",
258                    tx_queue_id, rss_id, vport_id, sb);
259
260         return 0;
261 }
262
263 static int
264 qed_stop_txq(struct ecore_dev *edev, struct qed_stop_txq_params *params)
265 {
266         struct ecore_hwfn *p_hwfn;
267         int rc, hwfn_index;
268
269         hwfn_index = params->rss_id % edev->num_hwfns;
270         p_hwfn = &edev->hwfns[hwfn_index];
271
272         rc = ecore_sp_eth_tx_queue_stop(p_hwfn,
273                                         params->tx_queue_id / edev->num_hwfns);
274         if (rc) {
275                 DP_ERR(edev, "Failed to stop TXQ#%d\n", params->tx_queue_id);
276                 return rc;
277         }
278
279         return 0;
280 }
281
282 static int
283 qed_fp_cqe_completion(struct ecore_dev *edev,
284                       uint8_t rss_id, struct eth_slow_path_rx_cqe *cqe)
285 {
286         return ecore_eth_cqe_completion(&edev->hwfns[rss_id % edev->num_hwfns],
287                                         cqe);
288 }
289
290 static int qed_fastpath_stop(struct ecore_dev *edev)
291 {
292         ecore_hw_stop_fastpath(edev);
293
294         return 0;
295 }
296
297 static void qed_fastpath_start(struct ecore_dev *edev)
298 {
299         struct ecore_hwfn *p_hwfn;
300         int i;
301
302         for_each_hwfn(edev, i) {
303                 p_hwfn = &edev->hwfns[i];
304                 ecore_hw_start_fastpath(p_hwfn);
305         }
306 }
307
308 static void
309 qed_get_vport_stats(struct ecore_dev *edev, struct ecore_eth_stats *stats)
310 {
311         ecore_get_vport_stats(edev, stats);
312 }
313
314 static int
315 qed_configure_filter_ucast(struct ecore_dev *edev,
316                            struct qed_filter_ucast_params *params)
317 {
318         struct ecore_filter_ucast ucast;
319
320         if (!params->vlan_valid && !params->mac_valid) {
321                 DP_NOTICE(edev, true,
322                           "Tried configuring a unicast filter,"
323                           "but both MAC and VLAN are not set\n");
324                 return -EINVAL;
325         }
326
327         memset(&ucast, 0, sizeof(ucast));
328         switch (params->type) {
329         case QED_FILTER_XCAST_TYPE_ADD:
330                 ucast.opcode = ECORE_FILTER_ADD;
331                 break;
332         case QED_FILTER_XCAST_TYPE_DEL:
333                 ucast.opcode = ECORE_FILTER_REMOVE;
334                 break;
335         case QED_FILTER_XCAST_TYPE_REPLACE:
336                 ucast.opcode = ECORE_FILTER_REPLACE;
337                 break;
338         default:
339                 DP_NOTICE(edev, true, "Unknown unicast filter type %d\n",
340                           params->type);
341         }
342
343         if (params->vlan_valid && params->mac_valid) {
344                 ucast.type = ECORE_FILTER_MAC_VLAN;
345                 ether_addr_copy((struct ether_addr *)&params->mac,
346                                 (struct ether_addr *)&ucast.mac);
347                 ucast.vlan = params->vlan;
348         } else if (params->mac_valid) {
349                 ucast.type = ECORE_FILTER_MAC;
350                 ether_addr_copy((struct ether_addr *)&params->mac,
351                                 (struct ether_addr *)&ucast.mac);
352         } else {
353                 ucast.type = ECORE_FILTER_VLAN;
354                 ucast.vlan = params->vlan;
355         }
356
357         ucast.is_rx_filter = true;
358         ucast.is_tx_filter = true;
359
360         return ecore_filter_ucast_cmd(edev, &ucast, ECORE_SPQ_MODE_CB, NULL);
361 }
362
363 static int
364 qed_configure_filter_mcast(struct ecore_dev *edev,
365                            struct qed_filter_mcast_params *params)
366 {
367         struct ecore_filter_mcast mcast;
368         int i;
369
370         memset(&mcast, 0, sizeof(mcast));
371         switch (params->type) {
372         case QED_FILTER_XCAST_TYPE_ADD:
373                 mcast.opcode = ECORE_FILTER_ADD;
374                 break;
375         case QED_FILTER_XCAST_TYPE_DEL:
376                 mcast.opcode = ECORE_FILTER_REMOVE;
377                 break;
378         default:
379                 DP_NOTICE(edev, true, "Unknown multicast filter type %d\n",
380                           params->type);
381         }
382
383         mcast.num_mc_addrs = params->num;
384         for (i = 0; i < mcast.num_mc_addrs; i++)
385                 ether_addr_copy((struct ether_addr *)&params->mac[i],
386                                 (struct ether_addr *)&mcast.mac[i]);
387
388         return ecore_filter_mcast_cmd(edev, &mcast, ECORE_SPQ_MODE_CB, NULL);
389 }
390
391 int qed_configure_filter_rx_mode(struct ecore_dev *edev,
392                                  enum qed_filter_rx_mode_type type)
393 {
394         struct ecore_filter_accept_flags flags;
395
396         memset(&flags, 0, sizeof(flags));
397
398         flags.update_rx_mode_config = 1;
399         flags.update_tx_mode_config = 1;
400         flags.rx_accept_filter = ECORE_ACCEPT_UCAST_MATCHED |
401                                         ECORE_ACCEPT_MCAST_MATCHED |
402                                         ECORE_ACCEPT_BCAST;
403
404         flags.tx_accept_filter = ECORE_ACCEPT_UCAST_MATCHED |
405                                  ECORE_ACCEPT_MCAST_MATCHED |
406                                  ECORE_ACCEPT_BCAST;
407
408         if (type == QED_FILTER_RX_MODE_TYPE_PROMISC) {
409                 flags.rx_accept_filter |= ECORE_ACCEPT_UCAST_UNMATCHED;
410                 if (IS_VF(edev)) {
411                         flags.tx_accept_filter |= ECORE_ACCEPT_UCAST_UNMATCHED;
412                         DP_INFO(edev, "Enabling Tx unmatched flag for VF\n");
413                 }
414         } else if (type == QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC) {
415                 flags.rx_accept_filter |= ECORE_ACCEPT_MCAST_UNMATCHED;
416         } else if (type == (QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC |
417                             QED_FILTER_RX_MODE_TYPE_PROMISC)) {
418                 flags.rx_accept_filter |= ECORE_ACCEPT_UCAST_UNMATCHED |
419                                           ECORE_ACCEPT_MCAST_UNMATCHED;
420         }
421
422         return ecore_filter_accept_cmd(edev, 0, flags, false, false,
423                                        ECORE_SPQ_MODE_CB, NULL);
424 }
425
426 static int
427 qed_configure_filter(struct ecore_dev *edev, struct qed_filter_params *params)
428 {
429         switch (params->type) {
430         case QED_FILTER_TYPE_UCAST:
431                 return qed_configure_filter_ucast(edev, &params->filter.ucast);
432         case QED_FILTER_TYPE_MCAST:
433                 return qed_configure_filter_mcast(edev, &params->filter.mcast);
434         case QED_FILTER_TYPE_RX_MODE:
435                 return qed_configure_filter_rx_mode(edev,
436                                                     params->filter.
437                                                     accept_flags);
438         default:
439                 DP_NOTICE(edev, true, "Unknown filter type %d\n",
440                           (int)params->type);
441                 return -EINVAL;
442         }
443 }
444
445 static const struct qed_eth_ops qed_eth_ops_pass = {
446         INIT_STRUCT_FIELD(common, &qed_common_ops_pass),
447         INIT_STRUCT_FIELD(fill_dev_info, &qed_fill_eth_dev_info),
448         INIT_STRUCT_FIELD(vport_start, &qed_start_vport),
449         INIT_STRUCT_FIELD(vport_stop, &qed_stop_vport),
450         INIT_STRUCT_FIELD(vport_update, &qed_update_vport),
451         INIT_STRUCT_FIELD(q_rx_start, &qed_start_rxq),
452         INIT_STRUCT_FIELD(q_tx_start, &qed_start_txq),
453         INIT_STRUCT_FIELD(q_rx_stop, &qed_stop_rxq),
454         INIT_STRUCT_FIELD(q_tx_stop, &qed_stop_txq),
455         INIT_STRUCT_FIELD(eth_cqe_completion, &qed_fp_cqe_completion),
456         INIT_STRUCT_FIELD(fastpath_stop, &qed_fastpath_stop),
457         INIT_STRUCT_FIELD(fastpath_start, &qed_fastpath_start),
458         INIT_STRUCT_FIELD(get_vport_stats, &qed_get_vport_stats),
459         INIT_STRUCT_FIELD(filter_config, &qed_configure_filter),
460 };
461
462 uint32_t qed_get_protocol_version(enum qed_protocol protocol)
463 {
464         switch (protocol) {
465         case QED_PROTOCOL_ETH:
466                 return QED_ETH_INTERFACE_VERSION;
467         default:
468                 return 0;
469         }
470 }
471
472 const struct qed_eth_ops *qed_get_eth_ops(void)
473 {
474         return &qed_eth_ops_pass;
475 }