]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/net/ethernet/sfc/ef10.c
sfc: Store unicast and multicast promisc flag with address cache
[mirror_ubuntu-focal-kernel.git] / drivers / net / ethernet / sfc / ef10.c
CommitLineData
8127d661
BH
1/****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2012-2013 Solarflare Communications Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation, incorporated herein by reference.
8 */
9
10#include "net_driver.h"
11#include "ef10_regs.h"
12#include "io.h"
13#include "mcdi.h"
14#include "mcdi_pcol.h"
15#include "nic.h"
16#include "workarounds.h"
74cd60a4 17#include "selftest.h"
7fa8d547 18#include "ef10_sriov.h"
8127d661
BH
19#include <linux/in.h>
20#include <linux/jhash.h>
21#include <linux/wait.h>
22#include <linux/workqueue.h>
23
24/* Hardware control for EF10 architecture including 'Huntington'. */
25
26#define EFX_EF10_DRVGEN_EV 7
27enum {
28 EFX_EF10_TEST = 1,
29 EFX_EF10_REFILL,
30};
31
32/* The reserved RSS context value */
33#define EFX_EF10_RSS_CONTEXT_INVALID 0xffffffff
267c0157
JC
34/* The maximum size of a shared RSS context */
35/* TODO: this should really be from the mcdi protocol export */
36#define EFX_EF10_MAX_SHARED_RSS_CONTEXT_SIZE 64UL
8127d661
BH
37
38/* The filter table(s) are managed by firmware and we have write-only
39 * access. When removing filters we must identify them to the
40 * firmware by a 64-bit handle, but this is too wide for Linux kernel
41 * interfaces (32-bit for RX NFC, 16-bit for RFS). Also, we need to
42 * be able to tell in advance whether a requested insertion will
43 * replace an existing filter. Therefore we maintain a software hash
44 * table, which should be at least as large as the hardware hash
45 * table.
46 *
47 * Huntington has a single 8K filter table shared between all filter
48 * types and both ports.
49 */
50#define HUNT_FILTER_TBL_ROWS 8192
51
12fb0da4 52#define EFX_EF10_FILTER_ID_INVALID 0xffff
dc3273e0
AR
53
54#define EFX_EF10_FILTER_DEV_UC_MAX 32
55#define EFX_EF10_FILTER_DEV_MC_MAX 256
56
57/* Per-VLAN filters information */
58struct efx_ef10_filter_vlan {
59 u16 uc[EFX_EF10_FILTER_DEV_UC_MAX];
60 u16 mc[EFX_EF10_FILTER_DEV_MC_MAX];
61 u16 ucdef;
62 u16 bcast;
63 u16 mcdef;
64};
65
822b96f8
DP
66struct efx_ef10_dev_addr {
67 u8 addr[ETH_ALEN];
822b96f8
DP
68};
69
8127d661
BH
70struct efx_ef10_filter_table {
71/* The RX match field masks supported by this fw & hw, in order of priority */
72 enum efx_filter_match_flags rx_match_flags[
73 MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_MAXNUM];
74 unsigned int rx_match_count;
75
76 struct {
77 unsigned long spec; /* pointer to spec plus flag bits */
b59e6ef8
BH
78/* BUSY flag indicates that an update is in progress. AUTO_OLD is
79 * used to mark and sweep MAC filters for the device address lists.
8127d661
BH
80 */
81#define EFX_EF10_FILTER_FLAG_BUSY 1UL
b59e6ef8 82#define EFX_EF10_FILTER_FLAG_AUTO_OLD 2UL
8127d661
BH
83#define EFX_EF10_FILTER_FLAGS 3UL
84 u64 handle; /* firmware handle */
85 } *entry;
86 wait_queue_head_t waitq;
87/* Shadow of net_device address lists, guarded by mac_lock */
822b96f8
DP
88 struct efx_ef10_dev_addr dev_uc_list[EFX_EF10_FILTER_DEV_UC_MAX];
89 struct efx_ef10_dev_addr dev_mc_list[EFX_EF10_FILTER_DEV_MC_MAX];
12fb0da4
EC
90 int dev_uc_count;
91 int dev_mc_count;
afa4ce12
AR
92 bool uc_promisc;
93 bool mc_promisc;
b071c3a2
AR
94/* Whether in multicast promiscuous mode when last changed */
95 bool mc_promisc_last;
dc3273e0 96 struct efx_ef10_filter_vlan vlan;
8127d661
BH
97};
98
99/* An arbitrary search limit for the software hash table */
100#define EFX_EF10_FILTER_SEARCH_LIMIT 200
101
8127d661
BH
102static void efx_ef10_rx_free_indir_table(struct efx_nic *efx);
103static void efx_ef10_filter_table_remove(struct efx_nic *efx);
104
105static int efx_ef10_get_warm_boot_count(struct efx_nic *efx)
106{
107 efx_dword_t reg;
108
109 efx_readd(efx, &reg, ER_DZ_BIU_MC_SFT_STATUS);
110 return EFX_DWORD_FIELD(reg, EFX_WORD_1) == 0xb007 ?
111 EFX_DWORD_FIELD(reg, EFX_WORD_0) : -EIO;
112}
113
114static unsigned int efx_ef10_mem_map_size(struct efx_nic *efx)
115{
02246a7f
SS
116 int bar;
117
118 bar = efx->type->mem_bar;
119 return resource_size(&efx->pci_dev->resource[bar]);
8127d661
BH
120}
121
7a186f47
DP
122static bool efx_ef10_is_vf(struct efx_nic *efx)
123{
124 return efx->type->is_vf;
125}
126
1cd9ecbb
DP
127static int efx_ef10_get_pf_index(struct efx_nic *efx)
128{
129 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_FUNCTION_INFO_OUT_LEN);
130 struct efx_ef10_nic_data *nic_data = efx->nic_data;
131 size_t outlen;
132 int rc;
133
134 rc = efx_mcdi_rpc(efx, MC_CMD_GET_FUNCTION_INFO, NULL, 0, outbuf,
135 sizeof(outbuf), &outlen);
136 if (rc)
137 return rc;
138 if (outlen < sizeof(outbuf))
139 return -EIO;
140
141 nic_data->pf_index = MCDI_DWORD(outbuf, GET_FUNCTION_INFO_OUT_PF);
142 return 0;
143}
144
88a37de6
SS
145#ifdef CONFIG_SFC_SRIOV
146static int efx_ef10_get_vf_index(struct efx_nic *efx)
147{
148 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_FUNCTION_INFO_OUT_LEN);
149 struct efx_ef10_nic_data *nic_data = efx->nic_data;
150 size_t outlen;
151 int rc;
152
153 rc = efx_mcdi_rpc(efx, MC_CMD_GET_FUNCTION_INFO, NULL, 0, outbuf,
154 sizeof(outbuf), &outlen);
155 if (rc)
156 return rc;
157 if (outlen < sizeof(outbuf))
158 return -EIO;
159
160 nic_data->vf_index = MCDI_DWORD(outbuf, GET_FUNCTION_INFO_OUT_VF);
161 return 0;
162}
163#endif
164
e5a2538a 165static int efx_ef10_init_datapath_caps(struct efx_nic *efx)
8127d661
BH
166{
167 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CAPABILITIES_OUT_LEN);
168 struct efx_ef10_nic_data *nic_data = efx->nic_data;
169 size_t outlen;
170 int rc;
171
172 BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
173
174 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
175 outbuf, sizeof(outbuf), &outlen);
176 if (rc)
177 return rc;
e5a2538a
BH
178 if (outlen < sizeof(outbuf)) {
179 netif_err(efx, drv, efx->net_dev,
180 "unable to read datapath firmware capabilities\n");
181 return -EIO;
182 }
183
184 nic_data->datapath_caps =
185 MCDI_DWORD(outbuf, GET_CAPABILITIES_OUT_FLAGS1);
8127d661 186
8d9f9dd4
DP
187 /* record the DPCPU firmware IDs to determine VEB vswitching support.
188 */
189 nic_data->rx_dpcpu_fw_id =
190 MCDI_WORD(outbuf, GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID);
191 nic_data->tx_dpcpu_fw_id =
192 MCDI_WORD(outbuf, GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID);
193
e5a2538a
BH
194 if (!(nic_data->datapath_caps &
195 (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_PREFIX_LEN_14_LBN))) {
196 netif_err(efx, probe, efx->net_dev,
197 "current firmware does not support an RX prefix\n");
198 return -ENODEV;
8127d661
BH
199 }
200
201 return 0;
202}
203
204static int efx_ef10_get_sysclk_freq(struct efx_nic *efx)
205{
206 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CLOCK_OUT_LEN);
207 int rc;
208
209 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CLOCK, NULL, 0,
210 outbuf, sizeof(outbuf), NULL);
211 if (rc)
212 return rc;
213 rc = MCDI_DWORD(outbuf, GET_CLOCK_OUT_SYS_FREQ);
214 return rc > 0 ? rc : -ERANGE;
215}
216
0d5e0fbb 217static int efx_ef10_get_mac_address_pf(struct efx_nic *efx, u8 *mac_address)
8127d661
BH
218{
219 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_MAC_ADDRESSES_OUT_LEN);
220 size_t outlen;
221 int rc;
222
223 BUILD_BUG_ON(MC_CMD_GET_MAC_ADDRESSES_IN_LEN != 0);
224
225 rc = efx_mcdi_rpc(efx, MC_CMD_GET_MAC_ADDRESSES, NULL, 0,
226 outbuf, sizeof(outbuf), &outlen);
227 if (rc)
228 return rc;
229 if (outlen < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)
230 return -EIO;
231
cd84ff4d
EC
232 ether_addr_copy(mac_address,
233 MCDI_PTR(outbuf, GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE));
8127d661
BH
234 return 0;
235}
236
0d5e0fbb
DP
237static int efx_ef10_get_mac_address_vf(struct efx_nic *efx, u8 *mac_address)
238{
239 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN);
240 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX);
241 size_t outlen;
242 int num_addrs, rc;
243
244 MCDI_SET_DWORD(inbuf, VPORT_GET_MAC_ADDRESSES_IN_VPORT_ID,
245 EVB_PORT_ID_ASSIGNED);
246 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_GET_MAC_ADDRESSES, inbuf,
247 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
248
249 if (rc)
250 return rc;
251 if (outlen < MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMIN)
252 return -EIO;
253
254 num_addrs = MCDI_DWORD(outbuf,
255 VPORT_GET_MAC_ADDRESSES_OUT_MACADDR_COUNT);
256
257 WARN_ON(num_addrs != 1);
258
259 ether_addr_copy(mac_address,
260 MCDI_PTR(outbuf, VPORT_GET_MAC_ADDRESSES_OUT_MACADDR));
261
262 return 0;
263}
264
0f5c0845
SS
265static ssize_t efx_ef10_show_link_control_flag(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
268{
269 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
270
271 return sprintf(buf, "%d\n",
272 ((efx->mcdi->fn_flags) &
273 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL))
274 ? 1 : 0);
275}
276
277static ssize_t efx_ef10_show_primary_flag(struct device *dev,
278 struct device_attribute *attr,
279 char *buf)
280{
281 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
282
283 return sprintf(buf, "%d\n",
284 ((efx->mcdi->fn_flags) &
285 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
286 ? 1 : 0);
287}
288
289static DEVICE_ATTR(link_control_flag, 0444, efx_ef10_show_link_control_flag,
290 NULL);
291static DEVICE_ATTR(primary_flag, 0444, efx_ef10_show_primary_flag, NULL);
292
8127d661
BH
293static int efx_ef10_probe(struct efx_nic *efx)
294{
295 struct efx_ef10_nic_data *nic_data;
8be41320 296 struct net_device *net_dev = efx->net_dev;
8127d661
BH
297 int i, rc;
298
aa3930ee
BH
299 /* We can have one VI for each 8K region. However, until we
300 * use TX option descriptors we need two TX queues per channel.
8127d661 301 */
b0fbdae1
SS
302 efx->max_channels = min_t(unsigned int,
303 EFX_MAX_CHANNELS,
304 efx_ef10_mem_map_size(efx) /
305 (EFX_VI_PAGE_SIZE * EFX_TXQ_TYPES));
306 efx->max_tx_channels = efx->max_channels;
9fd3d3a4
EC
307 if (WARN_ON(efx->max_channels == 0))
308 return -EIO;
8127d661
BH
309
310 nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
311 if (!nic_data)
312 return -ENOMEM;
313 efx->nic_data = nic_data;
314
75aba2a5
EC
315 /* we assume later that we can copy from this buffer in dwords */
316 BUILD_BUG_ON(MCDI_CTL_SDU_LEN_MAX_V2 % 4);
317
8127d661
BH
318 rc = efx_nic_alloc_buffer(efx, &nic_data->mcdi_buf,
319 8 + MCDI_CTL_SDU_LEN_MAX_V2, GFP_KERNEL);
320 if (rc)
321 goto fail1;
322
323 /* Get the MC's warm boot count. In case it's rebooting right
324 * now, be prepared to retry.
325 */
326 i = 0;
327 for (;;) {
328 rc = efx_ef10_get_warm_boot_count(efx);
329 if (rc >= 0)
330 break;
331 if (++i == 5)
332 goto fail2;
333 ssleep(1);
334 }
335 nic_data->warm_boot_count = rc;
336
337 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
338
45b2449e
DP
339 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
340
8127d661
BH
341 /* In case we're recovering from a crash (kexec), we want to
342 * cancel any outstanding request by the previous user of this
343 * function. We send a special message using the least
344 * significant bits of the 'high' (doorbell) register.
345 */
346 _efx_writed(efx, cpu_to_le32(1), ER_DZ_MC_DB_HWRD);
347
348 rc = efx_mcdi_init(efx);
349 if (rc)
350 goto fail2;
351
352 /* Reset (most) configuration for this function */
353 rc = efx_mcdi_reset(efx, RESET_TYPE_ALL);
354 if (rc)
355 goto fail3;
356
357 /* Enable event logging */
358 rc = efx_mcdi_log_ctrl(efx, true, false, 0);
359 if (rc)
360 goto fail3;
361
0f5c0845
SS
362 rc = device_create_file(&efx->pci_dev->dev,
363 &dev_attr_link_control_flag);
1cd9ecbb
DP
364 if (rc)
365 goto fail3;
366
0f5c0845
SS
367 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_primary_flag);
368 if (rc)
369 goto fail4;
370
371 rc = efx_ef10_get_pf_index(efx);
372 if (rc)
373 goto fail5;
374
e5a2538a 375 rc = efx_ef10_init_datapath_caps(efx);
8127d661 376 if (rc < 0)
0f5c0845 377 goto fail5;
8127d661
BH
378
379 efx->rx_packet_len_offset =
380 ES_DZ_RX_PREFIX_PKTLEN_OFST - ES_DZ_RX_PREFIX_SIZE;
381
8127d661
BH
382 rc = efx_mcdi_port_get_number(efx);
383 if (rc < 0)
0f5c0845 384 goto fail5;
8127d661 385 efx->port_num = rc;
8be41320 386 net_dev->dev_port = rc;
8127d661 387
0d5e0fbb 388 rc = efx->type->get_mac_address(efx, efx->net_dev->perm_addr);
8127d661 389 if (rc)
0f5c0845 390 goto fail5;
8127d661
BH
391
392 rc = efx_ef10_get_sysclk_freq(efx);
393 if (rc < 0)
0f5c0845 394 goto fail5;
8127d661
BH
395 efx->timer_quantum_ns = 1536000 / rc; /* 1536 cycles */
396
267d9d73
EC
397 /* Check whether firmware supports bug 35388 workaround.
398 * First try to enable it, then if we get EPERM, just
399 * ask if it's already enabled
400 */
34ccfe6f 401 rc = efx_mcdi_set_workaround(efx, MC_CMD_WORKAROUND_BUG35388, true, NULL);
c9012e00 402 if (rc == 0) {
8127d661 403 nic_data->workaround_35388 = true;
c9012e00 404 } else if (rc == -EPERM) {
267d9d73
EC
405 unsigned int enabled;
406
407 rc = efx_mcdi_get_workarounds(efx, NULL, &enabled);
408 if (rc)
409 goto fail3;
410 nic_data->workaround_35388 = enabled &
411 MC_CMD_GET_WORKAROUNDS_OUT_BUG35388;
c9012e00 412 } else if (rc != -ENOSYS && rc != -ENOENT) {
0f5c0845 413 goto fail5;
c9012e00 414 }
8127d661
BH
415 netif_dbg(efx, probe, efx->net_dev,
416 "workaround for bug 35388 is %sabled\n",
417 nic_data->workaround_35388 ? "en" : "dis");
418
419 rc = efx_mcdi_mon_probe(efx);
267d9d73 420 if (rc && rc != -EPERM)
0f5c0845 421 goto fail5;
8127d661 422
9aecda95
BH
423 efx_ptp_probe(efx, NULL);
424
1d051e00
SS
425#ifdef CONFIG_SFC_SRIOV
426 if ((efx->pci_dev->physfn) && (!efx->pci_dev->is_physfn)) {
427 struct pci_dev *pci_dev_pf = efx->pci_dev->physfn;
428 struct efx_nic *efx_pf = pci_get_drvdata(pci_dev_pf);
429
430 efx_pf->type->get_mac_address(efx_pf, nic_data->port_id);
431 } else
432#endif
433 ether_addr_copy(nic_data->port_id, efx->net_dev->perm_addr);
434
8127d661
BH
435 return 0;
436
0f5c0845
SS
437fail5:
438 device_remove_file(&efx->pci_dev->dev, &dev_attr_primary_flag);
439fail4:
440 device_remove_file(&efx->pci_dev->dev, &dev_attr_link_control_flag);
8127d661
BH
441fail3:
442 efx_mcdi_fini(efx);
443fail2:
444 efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
445fail1:
446 kfree(nic_data);
447 efx->nic_data = NULL;
448 return rc;
449}
450
451static int efx_ef10_free_vis(struct efx_nic *efx)
452{
aa09a3da 453 MCDI_DECLARE_BUF_ERR(outbuf);
1e0b8120
EC
454 size_t outlen;
455 int rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FREE_VIS, NULL, 0,
456 outbuf, sizeof(outbuf), &outlen);
8127d661
BH
457
458 /* -EALREADY means nothing to free, so ignore */
459 if (rc == -EALREADY)
460 rc = 0;
1e0b8120
EC
461 if (rc)
462 efx_mcdi_display_error(efx, MC_CMD_FREE_VIS, 0, outbuf, outlen,
463 rc);
8127d661
BH
464 return rc;
465}
466
183233be
BH
467#ifdef EFX_USE_PIO
468
469static void efx_ef10_free_piobufs(struct efx_nic *efx)
470{
471 struct efx_ef10_nic_data *nic_data = efx->nic_data;
472 MCDI_DECLARE_BUF(inbuf, MC_CMD_FREE_PIOBUF_IN_LEN);
473 unsigned int i;
474 int rc;
475
476 BUILD_BUG_ON(MC_CMD_FREE_PIOBUF_OUT_LEN != 0);
477
478 for (i = 0; i < nic_data->n_piobufs; i++) {
479 MCDI_SET_DWORD(inbuf, FREE_PIOBUF_IN_PIOBUF_HANDLE,
480 nic_data->piobuf_handle[i]);
481 rc = efx_mcdi_rpc(efx, MC_CMD_FREE_PIOBUF, inbuf, sizeof(inbuf),
482 NULL, 0, NULL);
483 WARN_ON(rc);
484 }
485
486 nic_data->n_piobufs = 0;
487}
488
489static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
490{
491 struct efx_ef10_nic_data *nic_data = efx->nic_data;
492 MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_PIOBUF_OUT_LEN);
493 unsigned int i;
494 size_t outlen;
495 int rc = 0;
496
497 BUILD_BUG_ON(MC_CMD_ALLOC_PIOBUF_IN_LEN != 0);
498
499 for (i = 0; i < n; i++) {
09a04204
BK
500 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_ALLOC_PIOBUF, NULL, 0,
501 outbuf, sizeof(outbuf), &outlen);
502 if (rc) {
503 /* Don't display the MC error if we didn't have space
504 * for a VF.
505 */
506 if (!(efx_ef10_is_vf(efx) && rc == -ENOSPC))
507 efx_mcdi_display_error(efx, MC_CMD_ALLOC_PIOBUF,
508 0, outbuf, outlen, rc);
183233be 509 break;
09a04204 510 }
183233be
BH
511 if (outlen < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
512 rc = -EIO;
513 break;
514 }
515 nic_data->piobuf_handle[i] =
516 MCDI_DWORD(outbuf, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
517 netif_dbg(efx, probe, efx->net_dev,
518 "allocated PIO buffer %u handle %x\n", i,
519 nic_data->piobuf_handle[i]);
520 }
521
522 nic_data->n_piobufs = i;
523 if (rc)
524 efx_ef10_free_piobufs(efx);
525 return rc;
526}
527
528static int efx_ef10_link_piobufs(struct efx_nic *efx)
529{
530 struct efx_ef10_nic_data *nic_data = efx->nic_data;
aa09a3da
JC
531 _MCDI_DECLARE_BUF(inbuf,
532 max(MC_CMD_LINK_PIOBUF_IN_LEN,
533 MC_CMD_UNLINK_PIOBUF_IN_LEN));
183233be
BH
534 struct efx_channel *channel;
535 struct efx_tx_queue *tx_queue;
536 unsigned int offset, index;
537 int rc;
538
539 BUILD_BUG_ON(MC_CMD_LINK_PIOBUF_OUT_LEN != 0);
540 BUILD_BUG_ON(MC_CMD_UNLINK_PIOBUF_OUT_LEN != 0);
541
aa09a3da
JC
542 memset(inbuf, 0, sizeof(inbuf));
543
183233be
BH
544 /* Link a buffer to each VI in the write-combining mapping */
545 for (index = 0; index < nic_data->n_piobufs; ++index) {
546 MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_PIOBUF_HANDLE,
547 nic_data->piobuf_handle[index]);
548 MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_TXQ_INSTANCE,
549 nic_data->pio_write_vi_base + index);
550 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
551 inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
552 NULL, 0, NULL);
553 if (rc) {
554 netif_err(efx, drv, efx->net_dev,
555 "failed to link VI %u to PIO buffer %u (%d)\n",
556 nic_data->pio_write_vi_base + index, index,
557 rc);
558 goto fail;
559 }
560 netif_dbg(efx, probe, efx->net_dev,
561 "linked VI %u to PIO buffer %u\n",
562 nic_data->pio_write_vi_base + index, index);
563 }
564
565 /* Link a buffer to each TX queue */
566 efx_for_each_channel(channel, efx) {
567 efx_for_each_channel_tx_queue(tx_queue, channel) {
568 /* We assign the PIO buffers to queues in
569 * reverse order to allow for the following
570 * special case.
571 */
572 offset = ((efx->tx_channel_offset + efx->n_tx_channels -
573 tx_queue->channel->channel - 1) *
574 efx_piobuf_size);
575 index = offset / ER_DZ_TX_PIOBUF_SIZE;
576 offset = offset % ER_DZ_TX_PIOBUF_SIZE;
577
578 /* When the host page size is 4K, the first
579 * host page in the WC mapping may be within
580 * the same VI page as the last TX queue. We
581 * can only link one buffer to each VI.
582 */
583 if (tx_queue->queue == nic_data->pio_write_vi_base) {
584 BUG_ON(index != 0);
585 rc = 0;
586 } else {
587 MCDI_SET_DWORD(inbuf,
588 LINK_PIOBUF_IN_PIOBUF_HANDLE,
589 nic_data->piobuf_handle[index]);
590 MCDI_SET_DWORD(inbuf,
591 LINK_PIOBUF_IN_TXQ_INSTANCE,
592 tx_queue->queue);
593 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
594 inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
595 NULL, 0, NULL);
596 }
597
598 if (rc) {
599 /* This is non-fatal; the TX path just
600 * won't use PIO for this queue
601 */
602 netif_err(efx, drv, efx->net_dev,
603 "failed to link VI %u to PIO buffer %u (%d)\n",
604 tx_queue->queue, index, rc);
605 tx_queue->piobuf = NULL;
606 } else {
607 tx_queue->piobuf =
608 nic_data->pio_write_base +
609 index * EFX_VI_PAGE_SIZE + offset;
610 tx_queue->piobuf_offset = offset;
611 netif_dbg(efx, probe, efx->net_dev,
612 "linked VI %u to PIO buffer %u offset %x addr %p\n",
613 tx_queue->queue, index,
614 tx_queue->piobuf_offset,
615 tx_queue->piobuf);
616 }
617 }
618 }
619
620 return 0;
621
622fail:
623 while (index--) {
624 MCDI_SET_DWORD(inbuf, UNLINK_PIOBUF_IN_TXQ_INSTANCE,
625 nic_data->pio_write_vi_base + index);
626 efx_mcdi_rpc(efx, MC_CMD_UNLINK_PIOBUF,
627 inbuf, MC_CMD_UNLINK_PIOBUF_IN_LEN,
628 NULL, 0, NULL);
629 }
630 return rc;
631}
632
c0795bf6
EC
633static void efx_ef10_forget_old_piobufs(struct efx_nic *efx)
634{
635 struct efx_channel *channel;
636 struct efx_tx_queue *tx_queue;
637
638 /* All our existing PIO buffers went away */
639 efx_for_each_channel(channel, efx)
640 efx_for_each_channel_tx_queue(tx_queue, channel)
641 tx_queue->piobuf = NULL;
642}
643
183233be
BH
644#else /* !EFX_USE_PIO */
645
646static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
647{
648 return n == 0 ? 0 : -ENOBUFS;
649}
650
651static int efx_ef10_link_piobufs(struct efx_nic *efx)
652{
653 return 0;
654}
655
656static void efx_ef10_free_piobufs(struct efx_nic *efx)
657{
658}
659
c0795bf6
EC
660static void efx_ef10_forget_old_piobufs(struct efx_nic *efx)
661{
662}
663
183233be
BH
664#endif /* EFX_USE_PIO */
665
8127d661
BH
666static void efx_ef10_remove(struct efx_nic *efx)
667{
668 struct efx_ef10_nic_data *nic_data = efx->nic_data;
669 int rc;
670
f1122a34
SS
671#ifdef CONFIG_SFC_SRIOV
672 struct efx_ef10_nic_data *nic_data_pf;
673 struct pci_dev *pci_dev_pf;
674 struct efx_nic *efx_pf;
675 struct ef10_vf *vf;
676
677 if (efx->pci_dev->is_virtfn) {
678 pci_dev_pf = efx->pci_dev->physfn;
679 if (pci_dev_pf) {
680 efx_pf = pci_get_drvdata(pci_dev_pf);
681 nic_data_pf = efx_pf->nic_data;
682 vf = nic_data_pf->vf + nic_data->vf_index;
683 vf->efx = NULL;
684 } else
685 netif_info(efx, drv, efx->net_dev,
686 "Could not get the PF id from VF\n");
687 }
688#endif
689
9aecda95
BH
690 efx_ptp_remove(efx);
691
8127d661
BH
692 efx_mcdi_mon_remove(efx);
693
8127d661
BH
694 efx_ef10_rx_free_indir_table(efx);
695
183233be
BH
696 if (nic_data->wc_membase)
697 iounmap(nic_data->wc_membase);
698
8127d661
BH
699 rc = efx_ef10_free_vis(efx);
700 WARN_ON(rc != 0);
701
183233be
BH
702 if (!nic_data->must_restore_piobufs)
703 efx_ef10_free_piobufs(efx);
704
0f5c0845
SS
705 device_remove_file(&efx->pci_dev->dev, &dev_attr_primary_flag);
706 device_remove_file(&efx->pci_dev->dev, &dev_attr_link_control_flag);
707
8127d661
BH
708 efx_mcdi_fini(efx);
709 efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
710 kfree(nic_data);
711}
712
88a37de6
SS
713static int efx_ef10_probe_pf(struct efx_nic *efx)
714{
715 return efx_ef10_probe(efx);
716}
717
7a186f47
DP
718int efx_ef10_vadaptor_alloc(struct efx_nic *efx, unsigned int port_id)
719{
720 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_ALLOC_IN_LEN);
721
722 MCDI_SET_DWORD(inbuf, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
723 return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_ALLOC, inbuf, sizeof(inbuf),
724 NULL, 0, NULL);
725}
726
727int efx_ef10_vadaptor_free(struct efx_nic *efx, unsigned int port_id)
728{
729 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_FREE_IN_LEN);
730
731 MCDI_SET_DWORD(inbuf, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
732 return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_FREE, inbuf, sizeof(inbuf),
733 NULL, 0, NULL);
734}
735
736int efx_ef10_vport_add_mac(struct efx_nic *efx,
737 unsigned int port_id, u8 *mac)
738{
739 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ADD_MAC_ADDRESS_IN_LEN);
740
741 MCDI_SET_DWORD(inbuf, VPORT_ADD_MAC_ADDRESS_IN_VPORT_ID, port_id);
742 ether_addr_copy(MCDI_PTR(inbuf, VPORT_ADD_MAC_ADDRESS_IN_MACADDR), mac);
743
744 return efx_mcdi_rpc(efx, MC_CMD_VPORT_ADD_MAC_ADDRESS, inbuf,
745 sizeof(inbuf), NULL, 0, NULL);
746}
747
748int efx_ef10_vport_del_mac(struct efx_nic *efx,
749 unsigned int port_id, u8 *mac)
750{
751 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
752
753 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
754 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
755
756 return efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
757 sizeof(inbuf), NULL, 0, NULL);
758}
759
88a37de6
SS
760#ifdef CONFIG_SFC_SRIOV
761static int efx_ef10_probe_vf(struct efx_nic *efx)
762{
763 int rc;
6598dad2
DP
764 struct pci_dev *pci_dev_pf;
765
766 /* If the parent PF has no VF data structure, it doesn't know about this
767 * VF so fail probe. The VF needs to be re-created. This can happen
768 * if the PF driver is unloaded while the VF is assigned to a guest.
769 */
770 pci_dev_pf = efx->pci_dev->physfn;
771 if (pci_dev_pf) {
772 struct efx_nic *efx_pf = pci_get_drvdata(pci_dev_pf);
773 struct efx_ef10_nic_data *nic_data_pf = efx_pf->nic_data;
774
775 if (!nic_data_pf->vf) {
776 netif_info(efx, drv, efx->net_dev,
777 "The VF cannot link to its parent PF; "
778 "please destroy and re-create the VF\n");
779 return -EBUSY;
780 }
781 }
88a37de6
SS
782
783 rc = efx_ef10_probe(efx);
784 if (rc)
785 return rc;
786
787 rc = efx_ef10_get_vf_index(efx);
788 if (rc)
789 goto fail;
790
f1122a34
SS
791 if (efx->pci_dev->is_virtfn) {
792 if (efx->pci_dev->physfn) {
793 struct efx_nic *efx_pf =
794 pci_get_drvdata(efx->pci_dev->physfn);
795 struct efx_ef10_nic_data *nic_data_p = efx_pf->nic_data;
796 struct efx_ef10_nic_data *nic_data = efx->nic_data;
797
798 nic_data_p->vf[nic_data->vf_index].efx = efx;
6598dad2
DP
799 nic_data_p->vf[nic_data->vf_index].pci_dev =
800 efx->pci_dev;
f1122a34
SS
801 } else
802 netif_info(efx, drv, efx->net_dev,
803 "Could not get the PF id from VF\n");
804 }
805
88a37de6
SS
806 return 0;
807
808fail:
809 efx_ef10_remove(efx);
810 return rc;
811}
812#else
813static int efx_ef10_probe_vf(struct efx_nic *efx __attribute__ ((unused)))
814{
815 return 0;
816}
817#endif
818
8127d661
BH
819static int efx_ef10_alloc_vis(struct efx_nic *efx,
820 unsigned int min_vis, unsigned int max_vis)
821{
822 MCDI_DECLARE_BUF(inbuf, MC_CMD_ALLOC_VIS_IN_LEN);
823 MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_VIS_OUT_LEN);
824 struct efx_ef10_nic_data *nic_data = efx->nic_data;
825 size_t outlen;
826 int rc;
827
828 MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MIN_VI_COUNT, min_vis);
829 MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MAX_VI_COUNT, max_vis);
830 rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_VIS, inbuf, sizeof(inbuf),
831 outbuf, sizeof(outbuf), &outlen);
832 if (rc != 0)
833 return rc;
834
835 if (outlen < MC_CMD_ALLOC_VIS_OUT_LEN)
836 return -EIO;
837
838 netif_dbg(efx, drv, efx->net_dev, "base VI is A0x%03x\n",
839 MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE));
840
841 nic_data->vi_base = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE);
842 nic_data->n_allocated_vis = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_COUNT);
843 return 0;
844}
845
183233be
BH
846/* Note that the failure path of this function does not free
847 * resources, as this will be done by efx_ef10_remove().
848 */
8127d661
BH
849static int efx_ef10_dimension_resources(struct efx_nic *efx)
850{
183233be
BH
851 struct efx_ef10_nic_data *nic_data = efx->nic_data;
852 unsigned int uc_mem_map_size, wc_mem_map_size;
b0fbdae1
SS
853 unsigned int min_vis = max(EFX_TXQ_TYPES,
854 efx_separate_tx_channels ? 2 : 1);
855 unsigned int channel_vis, pio_write_vi_base, max_vis;
183233be
BH
856 void __iomem *membase;
857 int rc;
858
b0fbdae1 859 channel_vis = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
8127d661 860
183233be
BH
861#ifdef EFX_USE_PIO
862 /* Try to allocate PIO buffers if wanted and if the full
863 * number of PIO buffers would be sufficient to allocate one
864 * copy-buffer per TX channel. Failure is non-fatal, as there
865 * are only a small number of PIO buffers shared between all
866 * functions of the controller.
867 */
868 if (efx_piobuf_size != 0 &&
869 ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size * EF10_TX_PIOBUF_COUNT >=
870 efx->n_tx_channels) {
871 unsigned int n_piobufs =
872 DIV_ROUND_UP(efx->n_tx_channels,
873 ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size);
874
875 rc = efx_ef10_alloc_piobufs(efx, n_piobufs);
876 if (rc)
877 netif_err(efx, probe, efx->net_dev,
878 "failed to allocate PIO buffers (%d)\n", rc);
879 else
880 netif_dbg(efx, probe, efx->net_dev,
881 "allocated %u PIO buffers\n", n_piobufs);
882 }
883#else
884 nic_data->n_piobufs = 0;
885#endif
886
887 /* PIO buffers should be mapped with write-combining enabled,
888 * and we want to make single UC and WC mappings rather than
889 * several of each (in fact that's the only option if host
890 * page size is >4K). So we may allocate some extra VIs just
891 * for writing PIO buffers through.
52ad762b 892 *
b0fbdae1 893 * The UC mapping contains (channel_vis - 1) complete VIs and the
52ad762b
DP
894 * first half of the next VI. Then the WC mapping begins with
895 * the second half of this last VI.
183233be 896 */
b0fbdae1 897 uc_mem_map_size = PAGE_ALIGN((channel_vis - 1) * EFX_VI_PAGE_SIZE +
183233be
BH
898 ER_DZ_TX_PIOBUF);
899 if (nic_data->n_piobufs) {
52ad762b
DP
900 /* pio_write_vi_base rounds down to give the number of complete
901 * VIs inside the UC mapping.
902 */
183233be
BH
903 pio_write_vi_base = uc_mem_map_size / EFX_VI_PAGE_SIZE;
904 wc_mem_map_size = (PAGE_ALIGN((pio_write_vi_base +
905 nic_data->n_piobufs) *
906 EFX_VI_PAGE_SIZE) -
907 uc_mem_map_size);
908 max_vis = pio_write_vi_base + nic_data->n_piobufs;
909 } else {
910 pio_write_vi_base = 0;
911 wc_mem_map_size = 0;
b0fbdae1 912 max_vis = channel_vis;
183233be
BH
913 }
914
915 /* In case the last attached driver failed to free VIs, do it now */
916 rc = efx_ef10_free_vis(efx);
917 if (rc != 0)
918 return rc;
919
920 rc = efx_ef10_alloc_vis(efx, min_vis, max_vis);
921 if (rc != 0)
922 return rc;
923
b0fbdae1
SS
924 if (nic_data->n_allocated_vis < channel_vis) {
925 netif_info(efx, drv, efx->net_dev,
926 "Could not allocate enough VIs to satisfy RSS"
927 " requirements. Performance may not be optimal.\n");
928 /* We didn't get the VIs to populate our channels.
929 * We could keep what we got but then we'd have more
930 * interrupts than we need.
931 * Instead calculate new max_channels and restart
932 */
933 efx->max_channels = nic_data->n_allocated_vis;
934 efx->max_tx_channels =
935 nic_data->n_allocated_vis / EFX_TXQ_TYPES;
936
937 efx_ef10_free_vis(efx);
938 return -EAGAIN;
939 }
940
183233be
BH
941 /* If we didn't get enough VIs to map all the PIO buffers, free the
942 * PIO buffers
943 */
944 if (nic_data->n_piobufs &&
945 nic_data->n_allocated_vis <
946 pio_write_vi_base + nic_data->n_piobufs) {
947 netif_dbg(efx, probe, efx->net_dev,
948 "%u VIs are not sufficient to map %u PIO buffers\n",
949 nic_data->n_allocated_vis, nic_data->n_piobufs);
950 efx_ef10_free_piobufs(efx);
951 }
952
953 /* Shrink the original UC mapping of the memory BAR */
954 membase = ioremap_nocache(efx->membase_phys, uc_mem_map_size);
955 if (!membase) {
956 netif_err(efx, probe, efx->net_dev,
957 "could not shrink memory BAR to %x\n",
958 uc_mem_map_size);
959 return -ENOMEM;
960 }
961 iounmap(efx->membase);
962 efx->membase = membase;
963
964 /* Set up the WC mapping if needed */
965 if (wc_mem_map_size) {
966 nic_data->wc_membase = ioremap_wc(efx->membase_phys +
967 uc_mem_map_size,
968 wc_mem_map_size);
969 if (!nic_data->wc_membase) {
970 netif_err(efx, probe, efx->net_dev,
971 "could not allocate WC mapping of size %x\n",
972 wc_mem_map_size);
973 return -ENOMEM;
974 }
975 nic_data->pio_write_vi_base = pio_write_vi_base;
976 nic_data->pio_write_base =
977 nic_data->wc_membase +
978 (pio_write_vi_base * EFX_VI_PAGE_SIZE + ER_DZ_TX_PIOBUF -
979 uc_mem_map_size);
980
981 rc = efx_ef10_link_piobufs(efx);
982 if (rc)
983 efx_ef10_free_piobufs(efx);
984 }
985
986 netif_dbg(efx, probe, efx->net_dev,
987 "memory BAR at %pa (virtual %p+%x UC, %p+%x WC)\n",
988 &efx->membase_phys, efx->membase, uc_mem_map_size,
989 nic_data->wc_membase, wc_mem_map_size);
990
991 return 0;
8127d661
BH
992}
993
994static int efx_ef10_init_nic(struct efx_nic *efx)
995{
996 struct efx_ef10_nic_data *nic_data = efx->nic_data;
997 int rc;
998
a915ccc9
BH
999 if (nic_data->must_check_datapath_caps) {
1000 rc = efx_ef10_init_datapath_caps(efx);
1001 if (rc)
1002 return rc;
1003 nic_data->must_check_datapath_caps = false;
1004 }
1005
8127d661
BH
1006 if (nic_data->must_realloc_vis) {
1007 /* We cannot let the number of VIs change now */
1008 rc = efx_ef10_alloc_vis(efx, nic_data->n_allocated_vis,
1009 nic_data->n_allocated_vis);
1010 if (rc)
1011 return rc;
1012 nic_data->must_realloc_vis = false;
1013 }
1014
183233be
BH
1015 if (nic_data->must_restore_piobufs && nic_data->n_piobufs) {
1016 rc = efx_ef10_alloc_piobufs(efx, nic_data->n_piobufs);
1017 if (rc == 0) {
1018 rc = efx_ef10_link_piobufs(efx);
1019 if (rc)
1020 efx_ef10_free_piobufs(efx);
1021 }
1022
1023 /* Log an error on failure, but this is non-fatal */
1024 if (rc)
1025 netif_err(efx, drv, efx->net_dev,
1026 "failed to restore PIO buffers (%d)\n", rc);
1027 nic_data->must_restore_piobufs = false;
1028 }
1029
267c0157
JC
1030 /* don't fail init if RSS setup doesn't work */
1031 efx->type->rx_push_rss_config(efx, false, efx->rx_indir_table);
1032
8127d661
BH
1033 return 0;
1034}
1035
3e336261
JC
1036static void efx_ef10_reset_mc_allocations(struct efx_nic *efx)
1037{
1038 struct efx_ef10_nic_data *nic_data = efx->nic_data;
774ad031
DP
1039#ifdef CONFIG_SFC_SRIOV
1040 unsigned int i;
1041#endif
3e336261
JC
1042
1043 /* All our allocations have been reset */
1044 nic_data->must_realloc_vis = true;
1045 nic_data->must_restore_filters = true;
1046 nic_data->must_restore_piobufs = true;
c0795bf6 1047 efx_ef10_forget_old_piobufs(efx);
3e336261 1048 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
774ad031
DP
1049
1050 /* Driver-created vswitches and vports must be re-created */
1051 nic_data->must_probe_vswitching = true;
1052 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
1053#ifdef CONFIG_SFC_SRIOV
1054 if (nic_data->vf)
1055 for (i = 0; i < efx->vf_count; i++)
1056 nic_data->vf[i].vport_id = 0;
1057#endif
3e336261
JC
1058}
1059
087e9025
JC
1060static enum reset_type efx_ef10_map_reset_reason(enum reset_type reason)
1061{
1062 if (reason == RESET_TYPE_MC_FAILURE)
1063 return RESET_TYPE_DATAPATH;
1064
1065 return efx_mcdi_map_reset_reason(reason);
1066}
1067
8127d661
BH
1068static int efx_ef10_map_reset_flags(u32 *flags)
1069{
1070 enum {
1071 EF10_RESET_PORT = ((ETH_RESET_MAC | ETH_RESET_PHY) <<
1072 ETH_RESET_SHARED_SHIFT),
1073 EF10_RESET_MC = ((ETH_RESET_DMA | ETH_RESET_FILTER |
1074 ETH_RESET_OFFLOAD | ETH_RESET_MAC |
1075 ETH_RESET_PHY | ETH_RESET_MGMT) <<
1076 ETH_RESET_SHARED_SHIFT)
1077 };
1078
1079 /* We assume for now that our PCI function is permitted to
1080 * reset everything.
1081 */
1082
1083 if ((*flags & EF10_RESET_MC) == EF10_RESET_MC) {
1084 *flags &= ~EF10_RESET_MC;
1085 return RESET_TYPE_WORLD;
1086 }
1087
1088 if ((*flags & EF10_RESET_PORT) == EF10_RESET_PORT) {
1089 *flags &= ~EF10_RESET_PORT;
1090 return RESET_TYPE_ALL;
1091 }
1092
1093 /* no invisible reset implemented */
1094
1095 return -EINVAL;
1096}
1097
3e336261
JC
1098static int efx_ef10_reset(struct efx_nic *efx, enum reset_type reset_type)
1099{
1100 int rc = efx_mcdi_reset(efx, reset_type);
1101
27324820
DP
1102 /* Unprivileged functions return -EPERM, but need to return success
1103 * here so that the datapath is brought back up.
1104 */
1105 if (reset_type == RESET_TYPE_WORLD && rc == -EPERM)
1106 rc = 0;
1107
3e336261
JC
1108 /* If it was a port reset, trigger reallocation of MC resources.
1109 * Note that on an MC reset nothing needs to be done now because we'll
1110 * detect the MC reset later and handle it then.
e283546c
EC
1111 * For an FLR, we never get an MC reset event, but the MC has reset all
1112 * resources assigned to us, so we have to trigger reallocation now.
3e336261 1113 */
e283546c
EC
1114 if ((reset_type == RESET_TYPE_ALL ||
1115 reset_type == RESET_TYPE_MCDI_TIMEOUT) && !rc)
3e336261
JC
1116 efx_ef10_reset_mc_allocations(efx);
1117 return rc;
1118}
1119
8127d661
BH
1120#define EF10_DMA_STAT(ext_name, mcdi_name) \
1121 [EF10_STAT_ ## ext_name] = \
1122 { #ext_name, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
1123#define EF10_DMA_INVIS_STAT(int_name, mcdi_name) \
1124 [EF10_STAT_ ## int_name] = \
1125 { NULL, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
1126#define EF10_OTHER_STAT(ext_name) \
1127 [EF10_STAT_ ## ext_name] = { #ext_name, 0, 0 }
e4d112e4
EC
1128#define GENERIC_SW_STAT(ext_name) \
1129 [GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
8127d661
BH
1130
1131static const struct efx_hw_stat_desc efx_ef10_stat_desc[EF10_STAT_COUNT] = {
e80ca013
DP
1132 EF10_DMA_STAT(port_tx_bytes, TX_BYTES),
1133 EF10_DMA_STAT(port_tx_packets, TX_PKTS),
1134 EF10_DMA_STAT(port_tx_pause, TX_PAUSE_PKTS),
1135 EF10_DMA_STAT(port_tx_control, TX_CONTROL_PKTS),
1136 EF10_DMA_STAT(port_tx_unicast, TX_UNICAST_PKTS),
1137 EF10_DMA_STAT(port_tx_multicast, TX_MULTICAST_PKTS),
1138 EF10_DMA_STAT(port_tx_broadcast, TX_BROADCAST_PKTS),
1139 EF10_DMA_STAT(port_tx_lt64, TX_LT64_PKTS),
1140 EF10_DMA_STAT(port_tx_64, TX_64_PKTS),
1141 EF10_DMA_STAT(port_tx_65_to_127, TX_65_TO_127_PKTS),
1142 EF10_DMA_STAT(port_tx_128_to_255, TX_128_TO_255_PKTS),
1143 EF10_DMA_STAT(port_tx_256_to_511, TX_256_TO_511_PKTS),
1144 EF10_DMA_STAT(port_tx_512_to_1023, TX_512_TO_1023_PKTS),
1145 EF10_DMA_STAT(port_tx_1024_to_15xx, TX_1024_TO_15XX_PKTS),
1146 EF10_DMA_STAT(port_tx_15xx_to_jumbo, TX_15XX_TO_JUMBO_PKTS),
1147 EF10_DMA_STAT(port_rx_bytes, RX_BYTES),
1148 EF10_DMA_INVIS_STAT(port_rx_bytes_minus_good_bytes, RX_BAD_BYTES),
1149 EF10_OTHER_STAT(port_rx_good_bytes),
1150 EF10_OTHER_STAT(port_rx_bad_bytes),
1151 EF10_DMA_STAT(port_rx_packets, RX_PKTS),
1152 EF10_DMA_STAT(port_rx_good, RX_GOOD_PKTS),
1153 EF10_DMA_STAT(port_rx_bad, RX_BAD_FCS_PKTS),
1154 EF10_DMA_STAT(port_rx_pause, RX_PAUSE_PKTS),
1155 EF10_DMA_STAT(port_rx_control, RX_CONTROL_PKTS),
1156 EF10_DMA_STAT(port_rx_unicast, RX_UNICAST_PKTS),
1157 EF10_DMA_STAT(port_rx_multicast, RX_MULTICAST_PKTS),
1158 EF10_DMA_STAT(port_rx_broadcast, RX_BROADCAST_PKTS),
1159 EF10_DMA_STAT(port_rx_lt64, RX_UNDERSIZE_PKTS),
1160 EF10_DMA_STAT(port_rx_64, RX_64_PKTS),
1161 EF10_DMA_STAT(port_rx_65_to_127, RX_65_TO_127_PKTS),
1162 EF10_DMA_STAT(port_rx_128_to_255, RX_128_TO_255_PKTS),
1163 EF10_DMA_STAT(port_rx_256_to_511, RX_256_TO_511_PKTS),
1164 EF10_DMA_STAT(port_rx_512_to_1023, RX_512_TO_1023_PKTS),
1165 EF10_DMA_STAT(port_rx_1024_to_15xx, RX_1024_TO_15XX_PKTS),
1166 EF10_DMA_STAT(port_rx_15xx_to_jumbo, RX_15XX_TO_JUMBO_PKTS),
1167 EF10_DMA_STAT(port_rx_gtjumbo, RX_GTJUMBO_PKTS),
1168 EF10_DMA_STAT(port_rx_bad_gtjumbo, RX_JABBER_PKTS),
1169 EF10_DMA_STAT(port_rx_overflow, RX_OVERFLOW_PKTS),
1170 EF10_DMA_STAT(port_rx_align_error, RX_ALIGN_ERROR_PKTS),
1171 EF10_DMA_STAT(port_rx_length_error, RX_LENGTH_ERROR_PKTS),
1172 EF10_DMA_STAT(port_rx_nodesc_drops, RX_NODESC_DROPS),
e4d112e4
EC
1173 GENERIC_SW_STAT(rx_nodesc_trunc),
1174 GENERIC_SW_STAT(rx_noskb_drops),
e80ca013
DP
1175 EF10_DMA_STAT(port_rx_pm_trunc_bb_overflow, PM_TRUNC_BB_OVERFLOW),
1176 EF10_DMA_STAT(port_rx_pm_discard_bb_overflow, PM_DISCARD_BB_OVERFLOW),
1177 EF10_DMA_STAT(port_rx_pm_trunc_vfifo_full, PM_TRUNC_VFIFO_FULL),
1178 EF10_DMA_STAT(port_rx_pm_discard_vfifo_full, PM_DISCARD_VFIFO_FULL),
1179 EF10_DMA_STAT(port_rx_pm_trunc_qbb, PM_TRUNC_QBB),
1180 EF10_DMA_STAT(port_rx_pm_discard_qbb, PM_DISCARD_QBB),
1181 EF10_DMA_STAT(port_rx_pm_discard_mapping, PM_DISCARD_MAPPING),
1182 EF10_DMA_STAT(port_rx_dp_q_disabled_packets, RXDP_Q_DISABLED_PKTS),
1183 EF10_DMA_STAT(port_rx_dp_di_dropped_packets, RXDP_DI_DROPPED_PKTS),
1184 EF10_DMA_STAT(port_rx_dp_streaming_packets, RXDP_STREAMING_PKTS),
1185 EF10_DMA_STAT(port_rx_dp_hlb_fetch, RXDP_HLB_FETCH_CONDITIONS),
1186 EF10_DMA_STAT(port_rx_dp_hlb_wait, RXDP_HLB_WAIT_CONDITIONS),
3c36a2ad
DP
1187 EF10_DMA_STAT(rx_unicast, VADAPTER_RX_UNICAST_PACKETS),
1188 EF10_DMA_STAT(rx_unicast_bytes, VADAPTER_RX_UNICAST_BYTES),
1189 EF10_DMA_STAT(rx_multicast, VADAPTER_RX_MULTICAST_PACKETS),
1190 EF10_DMA_STAT(rx_multicast_bytes, VADAPTER_RX_MULTICAST_BYTES),
1191 EF10_DMA_STAT(rx_broadcast, VADAPTER_RX_BROADCAST_PACKETS),
1192 EF10_DMA_STAT(rx_broadcast_bytes, VADAPTER_RX_BROADCAST_BYTES),
1193 EF10_DMA_STAT(rx_bad, VADAPTER_RX_BAD_PACKETS),
1194 EF10_DMA_STAT(rx_bad_bytes, VADAPTER_RX_BAD_BYTES),
1195 EF10_DMA_STAT(rx_overflow, VADAPTER_RX_OVERFLOW),
1196 EF10_DMA_STAT(tx_unicast, VADAPTER_TX_UNICAST_PACKETS),
1197 EF10_DMA_STAT(tx_unicast_bytes, VADAPTER_TX_UNICAST_BYTES),
1198 EF10_DMA_STAT(tx_multicast, VADAPTER_TX_MULTICAST_PACKETS),
1199 EF10_DMA_STAT(tx_multicast_bytes, VADAPTER_TX_MULTICAST_BYTES),
1200 EF10_DMA_STAT(tx_broadcast, VADAPTER_TX_BROADCAST_PACKETS),
1201 EF10_DMA_STAT(tx_broadcast_bytes, VADAPTER_TX_BROADCAST_BYTES),
1202 EF10_DMA_STAT(tx_bad, VADAPTER_TX_BAD_PACKETS),
1203 EF10_DMA_STAT(tx_bad_bytes, VADAPTER_TX_BAD_BYTES),
1204 EF10_DMA_STAT(tx_overflow, VADAPTER_TX_OVERFLOW),
8127d661
BH
1205};
1206
e80ca013
DP
1207#define HUNT_COMMON_STAT_MASK ((1ULL << EF10_STAT_port_tx_bytes) | \
1208 (1ULL << EF10_STAT_port_tx_packets) | \
1209 (1ULL << EF10_STAT_port_tx_pause) | \
1210 (1ULL << EF10_STAT_port_tx_unicast) | \
1211 (1ULL << EF10_STAT_port_tx_multicast) | \
1212 (1ULL << EF10_STAT_port_tx_broadcast) | \
1213 (1ULL << EF10_STAT_port_rx_bytes) | \
1214 (1ULL << \
1215 EF10_STAT_port_rx_bytes_minus_good_bytes) | \
1216 (1ULL << EF10_STAT_port_rx_good_bytes) | \
1217 (1ULL << EF10_STAT_port_rx_bad_bytes) | \
1218 (1ULL << EF10_STAT_port_rx_packets) | \
1219 (1ULL << EF10_STAT_port_rx_good) | \
1220 (1ULL << EF10_STAT_port_rx_bad) | \
1221 (1ULL << EF10_STAT_port_rx_pause) | \
1222 (1ULL << EF10_STAT_port_rx_control) | \
1223 (1ULL << EF10_STAT_port_rx_unicast) | \
1224 (1ULL << EF10_STAT_port_rx_multicast) | \
1225 (1ULL << EF10_STAT_port_rx_broadcast) | \
1226 (1ULL << EF10_STAT_port_rx_lt64) | \
1227 (1ULL << EF10_STAT_port_rx_64) | \
1228 (1ULL << EF10_STAT_port_rx_65_to_127) | \
1229 (1ULL << EF10_STAT_port_rx_128_to_255) | \
1230 (1ULL << EF10_STAT_port_rx_256_to_511) | \
1231 (1ULL << EF10_STAT_port_rx_512_to_1023) |\
1232 (1ULL << EF10_STAT_port_rx_1024_to_15xx) |\
1233 (1ULL << EF10_STAT_port_rx_15xx_to_jumbo) |\
1234 (1ULL << EF10_STAT_port_rx_gtjumbo) | \
1235 (1ULL << EF10_STAT_port_rx_bad_gtjumbo) |\
1236 (1ULL << EF10_STAT_port_rx_overflow) | \
1237 (1ULL << EF10_STAT_port_rx_nodesc_drops) |\
e4d112e4
EC
1238 (1ULL << GENERIC_STAT_rx_nodesc_trunc) | \
1239 (1ULL << GENERIC_STAT_rx_noskb_drops))
8127d661
BH
1240
1241/* These statistics are only provided by the 10G MAC. For a 10G/40G
1242 * switchable port we do not expose these because they might not
1243 * include all the packets they should.
1244 */
e80ca013
DP
1245#define HUNT_10G_ONLY_STAT_MASK ((1ULL << EF10_STAT_port_tx_control) | \
1246 (1ULL << EF10_STAT_port_tx_lt64) | \
1247 (1ULL << EF10_STAT_port_tx_64) | \
1248 (1ULL << EF10_STAT_port_tx_65_to_127) |\
1249 (1ULL << EF10_STAT_port_tx_128_to_255) |\
1250 (1ULL << EF10_STAT_port_tx_256_to_511) |\
1251 (1ULL << EF10_STAT_port_tx_512_to_1023) |\
1252 (1ULL << EF10_STAT_port_tx_1024_to_15xx) |\
1253 (1ULL << EF10_STAT_port_tx_15xx_to_jumbo))
8127d661
BH
1254
1255/* These statistics are only provided by the 40G MAC. For a 10G/40G
1256 * switchable port we do expose these because the errors will otherwise
1257 * be silent.
1258 */
e80ca013
DP
1259#define HUNT_40G_EXTRA_STAT_MASK ((1ULL << EF10_STAT_port_rx_align_error) |\
1260 (1ULL << EF10_STAT_port_rx_length_error))
8127d661 1261
568d7a00
EC
1262/* These statistics are only provided if the firmware supports the
1263 * capability PM_AND_RXDP_COUNTERS.
1264 */
1265#define HUNT_PM_AND_RXDP_STAT_MASK ( \
e80ca013
DP
1266 (1ULL << EF10_STAT_port_rx_pm_trunc_bb_overflow) | \
1267 (1ULL << EF10_STAT_port_rx_pm_discard_bb_overflow) | \
1268 (1ULL << EF10_STAT_port_rx_pm_trunc_vfifo_full) | \
1269 (1ULL << EF10_STAT_port_rx_pm_discard_vfifo_full) | \
1270 (1ULL << EF10_STAT_port_rx_pm_trunc_qbb) | \
1271 (1ULL << EF10_STAT_port_rx_pm_discard_qbb) | \
1272 (1ULL << EF10_STAT_port_rx_pm_discard_mapping) | \
1273 (1ULL << EF10_STAT_port_rx_dp_q_disabled_packets) | \
1274 (1ULL << EF10_STAT_port_rx_dp_di_dropped_packets) | \
1275 (1ULL << EF10_STAT_port_rx_dp_streaming_packets) | \
1276 (1ULL << EF10_STAT_port_rx_dp_hlb_fetch) | \
1277 (1ULL << EF10_STAT_port_rx_dp_hlb_wait))
568d7a00 1278
4bae913b 1279static u64 efx_ef10_raw_stat_mask(struct efx_nic *efx)
8127d661 1280{
4bae913b 1281 u64 raw_mask = HUNT_COMMON_STAT_MASK;
8127d661 1282 u32 port_caps = efx_mcdi_phy_get_caps(efx);
568d7a00 1283 struct efx_ef10_nic_data *nic_data = efx->nic_data;
8127d661 1284
3c36a2ad
DP
1285 if (!(efx->mcdi->fn_flags &
1286 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL))
1287 return 0;
1288
8127d661 1289 if (port_caps & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
4bae913b 1290 raw_mask |= HUNT_40G_EXTRA_STAT_MASK;
8127d661 1291 else
4bae913b 1292 raw_mask |= HUNT_10G_ONLY_STAT_MASK;
568d7a00
EC
1293
1294 if (nic_data->datapath_caps &
1295 (1 << MC_CMD_GET_CAPABILITIES_OUT_PM_AND_RXDP_COUNTERS_LBN))
1296 raw_mask |= HUNT_PM_AND_RXDP_STAT_MASK;
1297
4bae913b
EC
1298 return raw_mask;
1299}
1300
1301static void efx_ef10_get_stat_mask(struct efx_nic *efx, unsigned long *mask)
1302{
d94619cd 1303 struct efx_ef10_nic_data *nic_data = efx->nic_data;
3c36a2ad
DP
1304 u64 raw_mask[2];
1305
1306 raw_mask[0] = efx_ef10_raw_stat_mask(efx);
1307
d94619cd
DP
1308 /* Only show vadaptor stats when EVB capability is present */
1309 if (nic_data->datapath_caps &
1310 (1 << MC_CMD_GET_CAPABILITIES_OUT_EVB_LBN)) {
1311 raw_mask[0] |= ~((1ULL << EF10_STAT_rx_unicast) - 1);
1312 raw_mask[1] = (1ULL << (EF10_STAT_COUNT - 63)) - 1;
1313 } else {
1314 raw_mask[1] = 0;
1315 }
4bae913b
EC
1316
1317#if BITS_PER_LONG == 64
3c36a2ad
DP
1318 mask[0] = raw_mask[0];
1319 mask[1] = raw_mask[1];
4bae913b 1320#else
3c36a2ad
DP
1321 mask[0] = raw_mask[0] & 0xffffffff;
1322 mask[1] = raw_mask[0] >> 32;
1323 mask[2] = raw_mask[1] & 0xffffffff;
1324 mask[3] = raw_mask[1] >> 32;
4bae913b 1325#endif
8127d661
BH
1326}
1327
1328static size_t efx_ef10_describe_stats(struct efx_nic *efx, u8 *names)
1329{
4bae913b
EC
1330 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
1331
1332 efx_ef10_get_stat_mask(efx, mask);
8127d661 1333 return efx_nic_describe_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
4bae913b 1334 mask, names);
8127d661
BH
1335}
1336
d7788196
DP
1337static size_t efx_ef10_update_stats_common(struct efx_nic *efx, u64 *full_stats,
1338 struct rtnl_link_stats64 *core_stats)
1339{
1340 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
1341 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1342 u64 *stats = nic_data->stats;
1343 size_t stats_count = 0, index;
1344
1345 efx_ef10_get_stat_mask(efx, mask);
1346
1347 if (full_stats) {
1348 for_each_set_bit(index, mask, EF10_STAT_COUNT) {
1349 if (efx_ef10_stat_desc[index].name) {
1350 *full_stats++ = stats[index];
1351 ++stats_count;
1352 }
1353 }
1354 }
1355
fbe4307e
BK
1356 if (!core_stats)
1357 return stats_count;
1358
1359 if (nic_data->datapath_caps &
1360 1 << MC_CMD_GET_CAPABILITIES_OUT_EVB_LBN) {
1361 /* Use vadaptor stats. */
0fc95fca
DP
1362 core_stats->rx_packets = stats[EF10_STAT_rx_unicast] +
1363 stats[EF10_STAT_rx_multicast] +
1364 stats[EF10_STAT_rx_broadcast];
1365 core_stats->tx_packets = stats[EF10_STAT_tx_unicast] +
1366 stats[EF10_STAT_tx_multicast] +
1367 stats[EF10_STAT_tx_broadcast];
1368 core_stats->rx_bytes = stats[EF10_STAT_rx_unicast_bytes] +
1369 stats[EF10_STAT_rx_multicast_bytes] +
1370 stats[EF10_STAT_rx_broadcast_bytes];
1371 core_stats->tx_bytes = stats[EF10_STAT_tx_unicast_bytes] +
1372 stats[EF10_STAT_tx_multicast_bytes] +
1373 stats[EF10_STAT_tx_broadcast_bytes];
1374 core_stats->rx_dropped = stats[GENERIC_STAT_rx_nodesc_trunc] +
d7788196 1375 stats[GENERIC_STAT_rx_noskb_drops];
0fc95fca
DP
1376 core_stats->multicast = stats[EF10_STAT_rx_multicast];
1377 core_stats->rx_crc_errors = stats[EF10_STAT_rx_bad];
1378 core_stats->rx_fifo_errors = stats[EF10_STAT_rx_overflow];
1379 core_stats->rx_errors = core_stats->rx_crc_errors;
1380 core_stats->tx_errors = stats[EF10_STAT_tx_bad];
fbe4307e
BK
1381 } else {
1382 /* Use port stats. */
1383 core_stats->rx_packets = stats[EF10_STAT_port_rx_packets];
1384 core_stats->tx_packets = stats[EF10_STAT_port_tx_packets];
1385 core_stats->rx_bytes = stats[EF10_STAT_port_rx_bytes];
1386 core_stats->tx_bytes = stats[EF10_STAT_port_tx_bytes];
1387 core_stats->rx_dropped = stats[EF10_STAT_port_rx_nodesc_drops] +
1388 stats[GENERIC_STAT_rx_nodesc_trunc] +
1389 stats[GENERIC_STAT_rx_noskb_drops];
1390 core_stats->multicast = stats[EF10_STAT_port_rx_multicast];
1391 core_stats->rx_length_errors =
1392 stats[EF10_STAT_port_rx_gtjumbo] +
1393 stats[EF10_STAT_port_rx_length_error];
1394 core_stats->rx_crc_errors = stats[EF10_STAT_port_rx_bad];
1395 core_stats->rx_frame_errors =
1396 stats[EF10_STAT_port_rx_align_error];
1397 core_stats->rx_fifo_errors = stats[EF10_STAT_port_rx_overflow];
1398 core_stats->rx_errors = (core_stats->rx_length_errors +
1399 core_stats->rx_crc_errors +
1400 core_stats->rx_frame_errors);
d7788196
DP
1401 }
1402
1403 return stats_count;
1404}
1405
1406static int efx_ef10_try_update_nic_stats_pf(struct efx_nic *efx)
8127d661
BH
1407{
1408 struct efx_ef10_nic_data *nic_data = efx->nic_data;
4bae913b 1409 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
8127d661
BH
1410 __le64 generation_start, generation_end;
1411 u64 *stats = nic_data->stats;
1412 __le64 *dma_stats;
1413
4bae913b
EC
1414 efx_ef10_get_stat_mask(efx, mask);
1415
8127d661
BH
1416 dma_stats = efx->stats_buffer.addr;
1417 nic_data = efx->nic_data;
1418
1419 generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
1420 if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
1421 return 0;
1422 rmb();
4bae913b 1423 efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, mask,
8127d661 1424 stats, efx->stats_buffer.addr, false);
d546a893 1425 rmb();
8127d661
BH
1426 generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
1427 if (generation_end != generation_start)
1428 return -EAGAIN;
1429
1430 /* Update derived statistics */
e80ca013
DP
1431 efx_nic_fix_nodesc_drop_stat(efx,
1432 &stats[EF10_STAT_port_rx_nodesc_drops]);
1433 stats[EF10_STAT_port_rx_good_bytes] =
1434 stats[EF10_STAT_port_rx_bytes] -
1435 stats[EF10_STAT_port_rx_bytes_minus_good_bytes];
1436 efx_update_diff_stat(&stats[EF10_STAT_port_rx_bad_bytes],
1437 stats[EF10_STAT_port_rx_bytes_minus_good_bytes]);
e4d112e4 1438 efx_update_sw_stats(efx, stats);
8127d661
BH
1439 return 0;
1440}
1441
1442
d7788196
DP
1443static size_t efx_ef10_update_stats_pf(struct efx_nic *efx, u64 *full_stats,
1444 struct rtnl_link_stats64 *core_stats)
8127d661 1445{
8127d661
BH
1446 int retry;
1447
1448 /* If we're unlucky enough to read statistics during the DMA, wait
1449 * up to 10ms for it to finish (typically takes <500us)
1450 */
1451 for (retry = 0; retry < 100; ++retry) {
d7788196 1452 if (efx_ef10_try_update_nic_stats_pf(efx) == 0)
8127d661
BH
1453 break;
1454 udelay(100);
1455 }
1456
d7788196
DP
1457 return efx_ef10_update_stats_common(efx, full_stats, core_stats);
1458}
8127d661 1459
d7788196
DP
1460static int efx_ef10_try_update_nic_stats_vf(struct efx_nic *efx)
1461{
1462 MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
1463 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1464 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
1465 __le64 generation_start, generation_end;
1466 u64 *stats = nic_data->stats;
1467 u32 dma_len = MC_CMD_MAC_NSTATS * sizeof(u64);
1468 struct efx_buffer stats_buf;
1469 __le64 *dma_stats;
1470 int rc;
1471
f00bf230
DP
1472 spin_unlock_bh(&efx->stats_lock);
1473
1474 if (in_interrupt()) {
1475 /* If in atomic context, cannot update stats. Just update the
1476 * software stats and return so the caller can continue.
1477 */
1478 spin_lock_bh(&efx->stats_lock);
1479 efx_update_sw_stats(efx, stats);
1480 return 0;
1481 }
1482
d7788196
DP
1483 efx_ef10_get_stat_mask(efx, mask);
1484
1485 rc = efx_nic_alloc_buffer(efx, &stats_buf, dma_len, GFP_ATOMIC);
f00bf230
DP
1486 if (rc) {
1487 spin_lock_bh(&efx->stats_lock);
d7788196 1488 return rc;
f00bf230 1489 }
d7788196
DP
1490
1491 dma_stats = stats_buf.addr;
1492 dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
1493
1494 MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, stats_buf.dma_addr);
1495 MCDI_POPULATE_DWORD_1(inbuf, MAC_STATS_IN_CMD,
0fc95fca 1496 MAC_STATS_IN_DMA, 1);
d7788196
DP
1497 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
1498 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
1499
6dd4859b
DP
1500 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
1501 NULL, 0, NULL);
d7788196 1502 spin_lock_bh(&efx->stats_lock);
6dd4859b
DP
1503 if (rc) {
1504 /* Expect ENOENT if DMA queues have not been set up */
1505 if (rc != -ENOENT || atomic_read(&efx->active_queues))
1506 efx_mcdi_display_error(efx, MC_CMD_MAC_STATS,
1507 sizeof(inbuf), NULL, 0, rc);
d7788196 1508 goto out;
6dd4859b 1509 }
d7788196
DP
1510
1511 generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
0fc95fca
DP
1512 if (generation_end == EFX_MC_STATS_GENERATION_INVALID) {
1513 WARN_ON_ONCE(1);
d7788196 1514 goto out;
0fc95fca 1515 }
d7788196
DP
1516 rmb();
1517 efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, mask,
1518 stats, stats_buf.addr, false);
1519 rmb();
1520 generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
1521 if (generation_end != generation_start) {
1522 rc = -EAGAIN;
1523 goto out;
8127d661
BH
1524 }
1525
d7788196
DP
1526 efx_update_sw_stats(efx, stats);
1527out:
1528 efx_nic_free_buffer(efx, &stats_buf);
1529 return rc;
1530}
1531
1532static size_t efx_ef10_update_stats_vf(struct efx_nic *efx, u64 *full_stats,
1533 struct rtnl_link_stats64 *core_stats)
1534{
1535 if (efx_ef10_try_update_nic_stats_vf(efx))
1536 return 0;
1537
1538 return efx_ef10_update_stats_common(efx, full_stats, core_stats);
8127d661
BH
1539}
1540
1541static void efx_ef10_push_irq_moderation(struct efx_channel *channel)
1542{
1543 struct efx_nic *efx = channel->efx;
1544 unsigned int mode, value;
1545 efx_dword_t timer_cmd;
1546
1547 if (channel->irq_moderation) {
1548 mode = 3;
1549 value = channel->irq_moderation - 1;
1550 } else {
1551 mode = 0;
1552 value = 0;
1553 }
1554
1555 if (EFX_EF10_WORKAROUND_35388(efx)) {
1556 EFX_POPULATE_DWORD_3(timer_cmd, ERF_DD_EVQ_IND_TIMER_FLAGS,
1557 EFE_DD_EVQ_IND_TIMER_FLAGS,
1558 ERF_DD_EVQ_IND_TIMER_MODE, mode,
1559 ERF_DD_EVQ_IND_TIMER_VAL, value);
1560 efx_writed_page(efx, &timer_cmd, ER_DD_EVQ_INDIRECT,
1561 channel->channel);
1562 } else {
1563 EFX_POPULATE_DWORD_2(timer_cmd, ERF_DZ_TC_TIMER_MODE, mode,
1564 ERF_DZ_TC_TIMER_VAL, value);
1565 efx_writed_page(efx, &timer_cmd, ER_DZ_EVQ_TMR,
1566 channel->channel);
1567 }
1568}
1569
02246a7f
SS
1570static void efx_ef10_get_wol_vf(struct efx_nic *efx,
1571 struct ethtool_wolinfo *wol) {}
1572
1573static int efx_ef10_set_wol_vf(struct efx_nic *efx, u32 type)
1574{
1575 return -EOPNOTSUPP;
1576}
1577
8127d661
BH
1578static void efx_ef10_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
1579{
1580 wol->supported = 0;
1581 wol->wolopts = 0;
1582 memset(&wol->sopass, 0, sizeof(wol->sopass));
1583}
1584
1585static int efx_ef10_set_wol(struct efx_nic *efx, u32 type)
1586{
1587 if (type != 0)
1588 return -EINVAL;
1589 return 0;
1590}
1591
1592static void efx_ef10_mcdi_request(struct efx_nic *efx,
1593 const efx_dword_t *hdr, size_t hdr_len,
1594 const efx_dword_t *sdu, size_t sdu_len)
1595{
1596 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1597 u8 *pdu = nic_data->mcdi_buf.addr;
1598
1599 memcpy(pdu, hdr, hdr_len);
1600 memcpy(pdu + hdr_len, sdu, sdu_len);
1601 wmb();
1602
1603 /* The hardware provides 'low' and 'high' (doorbell) registers
1604 * for passing the 64-bit address of an MCDI request to
1605 * firmware. However the dwords are swapped by firmware. The
1606 * least significant bits of the doorbell are then 0 for all
1607 * MCDI requests due to alignment.
1608 */
1609 _efx_writed(efx, cpu_to_le32((u64)nic_data->mcdi_buf.dma_addr >> 32),
1610 ER_DZ_MC_DB_LWRD);
1611 _efx_writed(efx, cpu_to_le32((u32)nic_data->mcdi_buf.dma_addr),
1612 ER_DZ_MC_DB_HWRD);
1613}
1614
1615static bool efx_ef10_mcdi_poll_response(struct efx_nic *efx)
1616{
1617 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1618 const efx_dword_t hdr = *(const efx_dword_t *)nic_data->mcdi_buf.addr;
1619
1620 rmb();
1621 return EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE);
1622}
1623
1624static void
1625efx_ef10_mcdi_read_response(struct efx_nic *efx, efx_dword_t *outbuf,
1626 size_t offset, size_t outlen)
1627{
1628 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1629 const u8 *pdu = nic_data->mcdi_buf.addr;
1630
1631 memcpy(outbuf, pdu + offset, outlen);
1632}
1633
c577e59e
DP
1634static void efx_ef10_mcdi_reboot_detected(struct efx_nic *efx)
1635{
1636 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1637
1638 /* All our allocations have been reset */
1639 efx_ef10_reset_mc_allocations(efx);
1640
1641 /* The datapath firmware might have been changed */
1642 nic_data->must_check_datapath_caps = true;
1643
1644 /* MAC statistics have been cleared on the NIC; clear the local
1645 * statistic that we update with efx_update_diff_stat().
1646 */
1647 nic_data->stats[EF10_STAT_port_rx_bad_bytes] = 0;
1648}
1649
8127d661
BH
1650static int efx_ef10_mcdi_poll_reboot(struct efx_nic *efx)
1651{
1652 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1653 int rc;
1654
1655 rc = efx_ef10_get_warm_boot_count(efx);
1656 if (rc < 0) {
1657 /* The firmware is presumably in the process of
1658 * rebooting. However, we are supposed to report each
1659 * reboot just once, so we must only do that once we
1660 * can read and store the updated warm boot count.
1661 */
1662 return 0;
1663 }
1664
1665 if (rc == nic_data->warm_boot_count)
1666 return 0;
1667
1668 nic_data->warm_boot_count = rc;
c577e59e 1669 efx_ef10_mcdi_reboot_detected(efx);
869070c5 1670
8127d661
BH
1671 return -EIO;
1672}
1673
1674/* Handle an MSI interrupt
1675 *
1676 * Handle an MSI hardware interrupt. This routine schedules event
1677 * queue processing. No interrupt acknowledgement cycle is necessary.
1678 * Also, we never need to check that the interrupt is for us, since
1679 * MSI interrupts cannot be shared.
1680 */
1681static irqreturn_t efx_ef10_msi_interrupt(int irq, void *dev_id)
1682{
1683 struct efx_msi_context *context = dev_id;
1684 struct efx_nic *efx = context->efx;
1685
1686 netif_vdbg(efx, intr, efx->net_dev,
1687 "IRQ %d on CPU %d\n", irq, raw_smp_processor_id());
1688
1689 if (likely(ACCESS_ONCE(efx->irq_soft_enabled))) {
1690 /* Note test interrupts */
1691 if (context->index == efx->irq_level)
1692 efx->last_irq_cpu = raw_smp_processor_id();
1693
1694 /* Schedule processing of the channel */
1695 efx_schedule_channel_irq(efx->channel[context->index]);
1696 }
1697
1698 return IRQ_HANDLED;
1699}
1700
1701static irqreturn_t efx_ef10_legacy_interrupt(int irq, void *dev_id)
1702{
1703 struct efx_nic *efx = dev_id;
1704 bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
1705 struct efx_channel *channel;
1706 efx_dword_t reg;
1707 u32 queues;
1708
1709 /* Read the ISR which also ACKs the interrupts */
1710 efx_readd(efx, &reg, ER_DZ_BIU_INT_ISR);
1711 queues = EFX_DWORD_FIELD(reg, ERF_DZ_ISR_REG);
1712
1713 if (queues == 0)
1714 return IRQ_NONE;
1715
1716 if (likely(soft_enabled)) {
1717 /* Note test interrupts */
1718 if (queues & (1U << efx->irq_level))
1719 efx->last_irq_cpu = raw_smp_processor_id();
1720
1721 efx_for_each_channel(channel, efx) {
1722 if (queues & 1)
1723 efx_schedule_channel_irq(channel);
1724 queues >>= 1;
1725 }
1726 }
1727
1728 netif_vdbg(efx, intr, efx->net_dev,
1729 "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
1730 irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
1731
1732 return IRQ_HANDLED;
1733}
1734
1735static void efx_ef10_irq_test_generate(struct efx_nic *efx)
1736{
1737 MCDI_DECLARE_BUF(inbuf, MC_CMD_TRIGGER_INTERRUPT_IN_LEN);
1738
1739 BUILD_BUG_ON(MC_CMD_TRIGGER_INTERRUPT_OUT_LEN != 0);
1740
1741 MCDI_SET_DWORD(inbuf, TRIGGER_INTERRUPT_IN_INTR_LEVEL, efx->irq_level);
1742 (void) efx_mcdi_rpc(efx, MC_CMD_TRIGGER_INTERRUPT,
1743 inbuf, sizeof(inbuf), NULL, 0, NULL);
1744}
1745
1746static int efx_ef10_tx_probe(struct efx_tx_queue *tx_queue)
1747{
1748 return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
1749 (tx_queue->ptr_mask + 1) *
1750 sizeof(efx_qword_t),
1751 GFP_KERNEL);
1752}
1753
1754/* This writes to the TX_DESC_WPTR and also pushes data */
1755static inline void efx_ef10_push_tx_desc(struct efx_tx_queue *tx_queue,
1756 const efx_qword_t *txd)
1757{
1758 unsigned int write_ptr;
1759 efx_oword_t reg;
1760
1761 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
1762 EFX_POPULATE_OWORD_1(reg, ERF_DZ_TX_DESC_WPTR, write_ptr);
1763 reg.qword[0] = *txd;
1764 efx_writeo_page(tx_queue->efx, &reg,
1765 ER_DZ_TX_DESC_UPD, tx_queue->queue);
1766}
1767
1768static void efx_ef10_tx_init(struct efx_tx_queue *tx_queue)
1769{
1770 MCDI_DECLARE_BUF(inbuf, MC_CMD_INIT_TXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
1771 EFX_BUF_SIZE));
8127d661
BH
1772 bool csum_offload = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
1773 size_t entries = tx_queue->txd.buf.len / EFX_BUF_SIZE;
1774 struct efx_channel *channel = tx_queue->channel;
1775 struct efx_nic *efx = tx_queue->efx;
45b2449e 1776 struct efx_ef10_nic_data *nic_data = efx->nic_data;
aa09a3da 1777 size_t inlen;
8127d661
BH
1778 dma_addr_t dma_addr;
1779 efx_qword_t *txd;
1780 int rc;
1781 int i;
aa09a3da 1782 BUILD_BUG_ON(MC_CMD_INIT_TXQ_OUT_LEN != 0);
8127d661
BH
1783
1784 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_SIZE, tx_queue->ptr_mask + 1);
1785 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_TARGET_EVQ, channel->channel);
1786 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_LABEL, tx_queue->queue);
1787 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_INSTANCE, tx_queue->queue);
1788 MCDI_POPULATE_DWORD_2(inbuf, INIT_TXQ_IN_FLAGS,
1789 INIT_TXQ_IN_FLAG_IP_CSUM_DIS, !csum_offload,
1790 INIT_TXQ_IN_FLAG_TCP_CSUM_DIS, !csum_offload);
1791 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_OWNER_ID, 0);
45b2449e 1792 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_PORT_ID, nic_data->vport_id);
8127d661
BH
1793
1794 dma_addr = tx_queue->txd.buf.dma_addr;
1795
1796 netif_dbg(efx, hw, efx->net_dev, "pushing TXQ %d. %zu entries (%llx)\n",
1797 tx_queue->queue, entries, (u64)dma_addr);
1798
1799 for (i = 0; i < entries; ++i) {
1800 MCDI_SET_ARRAY_QWORD(inbuf, INIT_TXQ_IN_DMA_ADDR, i, dma_addr);
1801 dma_addr += EFX_BUF_SIZE;
1802 }
1803
1804 inlen = MC_CMD_INIT_TXQ_IN_LEN(entries);
1805
1806 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_TXQ, inbuf, inlen,
aa09a3da 1807 NULL, 0, NULL);
8127d661
BH
1808 if (rc)
1809 goto fail;
1810
1811 /* A previous user of this TX queue might have set us up the
1812 * bomb by writing a descriptor to the TX push collector but
1813 * not the doorbell. (Each collector belongs to a port, not a
1814 * queue or function, so cannot easily be reset.) We must
1815 * attempt to push a no-op descriptor in its place.
1816 */
1817 tx_queue->buffer[0].flags = EFX_TX_BUF_OPTION;
1818 tx_queue->insert_count = 1;
1819 txd = efx_tx_desc(tx_queue, 0);
1820 EFX_POPULATE_QWORD_4(*txd,
1821 ESF_DZ_TX_DESC_IS_OPT, true,
1822 ESF_DZ_TX_OPTION_TYPE,
1823 ESE_DZ_TX_OPTION_DESC_CRC_CSUM,
1824 ESF_DZ_TX_OPTION_UDP_TCP_CSUM, csum_offload,
1825 ESF_DZ_TX_OPTION_IP_CSUM, csum_offload);
1826 tx_queue->write_count = 1;
93171b14
BK
1827
1828 if (nic_data->datapath_caps &
1829 (1 << MC_CMD_GET_CAPABILITIES_OUT_TX_TSO_LBN)) {
1830 tx_queue->tso_version = 1;
1831 }
1832
8127d661
BH
1833 wmb();
1834 efx_ef10_push_tx_desc(tx_queue, txd);
1835
1836 return;
1837
1838fail:
48ce5634
BH
1839 netdev_WARN(efx->net_dev, "failed to initialise TXQ %d\n",
1840 tx_queue->queue);
8127d661
BH
1841}
1842
1843static void efx_ef10_tx_fini(struct efx_tx_queue *tx_queue)
1844{
1845 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_TXQ_IN_LEN);
aa09a3da 1846 MCDI_DECLARE_BUF_ERR(outbuf);
8127d661
BH
1847 struct efx_nic *efx = tx_queue->efx;
1848 size_t outlen;
1849 int rc;
1850
1851 MCDI_SET_DWORD(inbuf, FINI_TXQ_IN_INSTANCE,
1852 tx_queue->queue);
1853
1e0b8120 1854 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_TXQ, inbuf, sizeof(inbuf),
8127d661
BH
1855 outbuf, sizeof(outbuf), &outlen);
1856
1857 if (rc && rc != -EALREADY)
1858 goto fail;
1859
1860 return;
1861
1862fail:
1e0b8120
EC
1863 efx_mcdi_display_error(efx, MC_CMD_FINI_TXQ, MC_CMD_FINI_TXQ_IN_LEN,
1864 outbuf, outlen, rc);
8127d661
BH
1865}
1866
1867static void efx_ef10_tx_remove(struct efx_tx_queue *tx_queue)
1868{
1869 efx_nic_free_buffer(tx_queue->efx, &tx_queue->txd.buf);
1870}
1871
1872/* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
1873static inline void efx_ef10_notify_tx_desc(struct efx_tx_queue *tx_queue)
1874{
1875 unsigned int write_ptr;
1876 efx_dword_t reg;
1877
1878 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
1879 EFX_POPULATE_DWORD_1(reg, ERF_DZ_TX_DESC_WPTR_DWORD, write_ptr);
1880 efx_writed_page(tx_queue->efx, &reg,
1881 ER_DZ_TX_DESC_UPD_DWORD, tx_queue->queue);
1882}
1883
1884static void efx_ef10_tx_write(struct efx_tx_queue *tx_queue)
1885{
1886 unsigned int old_write_count = tx_queue->write_count;
1887 struct efx_tx_buffer *buffer;
1888 unsigned int write_ptr;
1889 efx_qword_t *txd;
1890
b2663a4f
MH
1891 tx_queue->xmit_more_available = false;
1892 if (unlikely(tx_queue->write_count == tx_queue->insert_count))
1893 return;
8127d661
BH
1894
1895 do {
1896 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
1897 buffer = &tx_queue->buffer[write_ptr];
1898 txd = efx_tx_desc(tx_queue, write_ptr);
1899 ++tx_queue->write_count;
1900
1901 /* Create TX descriptor ring entry */
1902 if (buffer->flags & EFX_TX_BUF_OPTION) {
1903 *txd = buffer->option;
1904 } else {
1905 BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
1906 EFX_POPULATE_QWORD_3(
1907 *txd,
1908 ESF_DZ_TX_KER_CONT,
1909 buffer->flags & EFX_TX_BUF_CONT,
1910 ESF_DZ_TX_KER_BYTE_CNT, buffer->len,
1911 ESF_DZ_TX_KER_BUF_ADDR, buffer->dma_addr);
1912 }
1913 } while (tx_queue->write_count != tx_queue->insert_count);
1914
1915 wmb(); /* Ensure descriptors are written before they are fetched */
1916
1917 if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
1918 txd = efx_tx_desc(tx_queue,
1919 old_write_count & tx_queue->ptr_mask);
1920 efx_ef10_push_tx_desc(tx_queue, txd);
1921 ++tx_queue->pushes;
1922 } else {
1923 efx_ef10_notify_tx_desc(tx_queue);
1924 }
1925}
1926
267c0157
JC
1927static int efx_ef10_alloc_rss_context(struct efx_nic *efx, u32 *context,
1928 bool exclusive, unsigned *context_size)
8127d661
BH
1929{
1930 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
1931 MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
45b2449e 1932 struct efx_ef10_nic_data *nic_data = efx->nic_data;
8127d661
BH
1933 size_t outlen;
1934 int rc;
267c0157
JC
1935 u32 alloc_type = exclusive ?
1936 MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE :
1937 MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_SHARED;
1938 unsigned rss_spread = exclusive ?
1939 efx->rss_spread :
1940 min(rounddown_pow_of_two(efx->rss_spread),
1941 EFX_EF10_MAX_SHARED_RSS_CONTEXT_SIZE);
1942
1943 if (!exclusive && rss_spread == 1) {
1944 *context = EFX_EF10_RSS_CONTEXT_INVALID;
1945 if (context_size)
1946 *context_size = 1;
1947 return 0;
1948 }
8127d661 1949
dcb4123c
JC
1950 if (nic_data->datapath_caps &
1951 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_RSS_LIMITED_LBN)
1952 return -EOPNOTSUPP;
1953
8127d661 1954 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
45b2449e 1955 nic_data->vport_id);
267c0157
JC
1956 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE, alloc_type);
1957 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES, rss_spread);
8127d661
BH
1958
1959 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
1960 outbuf, sizeof(outbuf), &outlen);
1961 if (rc != 0)
1962 return rc;
1963
1964 if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
1965 return -EIO;
1966
1967 *context = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
1968
267c0157
JC
1969 if (context_size)
1970 *context_size = rss_spread;
1971
8127d661
BH
1972 return 0;
1973}
1974
1975static void efx_ef10_free_rss_context(struct efx_nic *efx, u32 context)
1976{
1977 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
1978 int rc;
1979
1980 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
1981 context);
1982
1983 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
1984 NULL, 0, NULL);
1985 WARN_ON(rc != 0);
1986}
1987
267c0157
JC
1988static int efx_ef10_populate_rss_table(struct efx_nic *efx, u32 context,
1989 const u32 *rx_indir_table)
8127d661
BH
1990{
1991 MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
1992 MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
1993 int i, rc;
1994
1995 MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
1996 context);
1997 BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
1998 MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
1999
2000 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); ++i)
2001 MCDI_PTR(tablebuf,
2002 RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
267c0157 2003 (u8) rx_indir_table[i];
8127d661
BH
2004
2005 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
2006 sizeof(tablebuf), NULL, 0, NULL);
2007 if (rc != 0)
2008 return rc;
2009
2010 MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
2011 context);
2012 BUILD_BUG_ON(ARRAY_SIZE(efx->rx_hash_key) !=
2013 MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
2014 for (i = 0; i < ARRAY_SIZE(efx->rx_hash_key); ++i)
2015 MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] =
2016 efx->rx_hash_key[i];
2017
2018 return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
2019 sizeof(keybuf), NULL, 0, NULL);
2020}
2021
2022static void efx_ef10_rx_free_indir_table(struct efx_nic *efx)
2023{
2024 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2025
2026 if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
2027 efx_ef10_free_rss_context(efx, nic_data->rx_rss_context);
2028 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
2029}
2030
267c0157
JC
2031static int efx_ef10_rx_push_shared_rss_config(struct efx_nic *efx,
2032 unsigned *context_size)
8127d661 2033{
267c0157 2034 u32 new_rx_rss_context;
8127d661 2035 struct efx_ef10_nic_data *nic_data = efx->nic_data;
267c0157
JC
2036 int rc = efx_ef10_alloc_rss_context(efx, &new_rx_rss_context,
2037 false, context_size);
2038
2039 if (rc != 0)
2040 return rc;
8127d661 2041
267c0157
JC
2042 nic_data->rx_rss_context = new_rx_rss_context;
2043 nic_data->rx_rss_context_exclusive = false;
2044 efx_set_default_rx_indir_table(efx);
2045 return 0;
2046}
8127d661 2047
267c0157
JC
2048static int efx_ef10_rx_push_exclusive_rss_config(struct efx_nic *efx,
2049 const u32 *rx_indir_table)
2050{
2051 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2052 int rc;
2053 u32 new_rx_rss_context;
2054
2055 if (nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID ||
2056 !nic_data->rx_rss_context_exclusive) {
2057 rc = efx_ef10_alloc_rss_context(efx, &new_rx_rss_context,
2058 true, NULL);
2059 if (rc == -EOPNOTSUPP)
2060 return rc;
2061 else if (rc != 0)
2062 goto fail1;
2063 } else {
2064 new_rx_rss_context = nic_data->rx_rss_context;
8127d661
BH
2065 }
2066
267c0157
JC
2067 rc = efx_ef10_populate_rss_table(efx, new_rx_rss_context,
2068 rx_indir_table);
8127d661 2069 if (rc != 0)
267c0157 2070 goto fail2;
8127d661 2071
267c0157
JC
2072 if (nic_data->rx_rss_context != new_rx_rss_context)
2073 efx_ef10_rx_free_indir_table(efx);
2074 nic_data->rx_rss_context = new_rx_rss_context;
2075 nic_data->rx_rss_context_exclusive = true;
2076 if (rx_indir_table != efx->rx_indir_table)
2077 memcpy(efx->rx_indir_table, rx_indir_table,
2078 sizeof(efx->rx_indir_table));
2079 return 0;
8127d661 2080
267c0157
JC
2081fail2:
2082 if (new_rx_rss_context != nic_data->rx_rss_context)
2083 efx_ef10_free_rss_context(efx, new_rx_rss_context);
2084fail1:
8127d661 2085 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
267c0157
JC
2086 return rc;
2087}
2088
2089static int efx_ef10_pf_rx_push_rss_config(struct efx_nic *efx, bool user,
2090 const u32 *rx_indir_table)
2091{
2092 int rc;
2093
2094 if (efx->rss_spread == 1)
2095 return 0;
2096
2097 rc = efx_ef10_rx_push_exclusive_rss_config(efx, rx_indir_table);
2098
2099 if (rc == -ENOBUFS && !user) {
2100 unsigned context_size;
2101 bool mismatch = false;
2102 size_t i;
2103
2104 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table) && !mismatch;
2105 i++)
2106 mismatch = rx_indir_table[i] !=
2107 ethtool_rxfh_indir_default(i, efx->rss_spread);
2108
2109 rc = efx_ef10_rx_push_shared_rss_config(efx, &context_size);
2110 if (rc == 0) {
2111 if (context_size != efx->rss_spread)
2112 netif_warn(efx, probe, efx->net_dev,
2113 "Could not allocate an exclusive RSS"
2114 " context; allocated a shared one of"
2115 " different size."
2116 " Wanted %u, got %u.\n",
2117 efx->rss_spread, context_size);
2118 else if (mismatch)
2119 netif_warn(efx, probe, efx->net_dev,
2120 "Could not allocate an exclusive RSS"
2121 " context; allocated a shared one but"
2122 " could not apply custom"
2123 " indirection.\n");
2124 else
2125 netif_info(efx, probe, efx->net_dev,
2126 "Could not allocate an exclusive RSS"
2127 " context; allocated a shared one.\n");
2128 }
2129 }
2130 return rc;
2131}
2132
2133static int efx_ef10_vf_rx_push_rss_config(struct efx_nic *efx, bool user,
2134 const u32 *rx_indir_table
2135 __attribute__ ((unused)))
2136{
2137 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2138
2139 if (user)
2140 return -EOPNOTSUPP;
2141 if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
2142 return 0;
2143 return efx_ef10_rx_push_shared_rss_config(efx, NULL);
8127d661
BH
2144}
2145
2146static int efx_ef10_rx_probe(struct efx_rx_queue *rx_queue)
2147{
2148 return efx_nic_alloc_buffer(rx_queue->efx, &rx_queue->rxd.buf,
2149 (rx_queue->ptr_mask + 1) *
2150 sizeof(efx_qword_t),
2151 GFP_KERNEL);
2152}
2153
2154static void efx_ef10_rx_init(struct efx_rx_queue *rx_queue)
2155{
2156 MCDI_DECLARE_BUF(inbuf,
2157 MC_CMD_INIT_RXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
2158 EFX_BUF_SIZE));
8127d661
BH
2159 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
2160 size_t entries = rx_queue->rxd.buf.len / EFX_BUF_SIZE;
2161 struct efx_nic *efx = rx_queue->efx;
45b2449e 2162 struct efx_ef10_nic_data *nic_data = efx->nic_data;
aa09a3da 2163 size_t inlen;
8127d661
BH
2164 dma_addr_t dma_addr;
2165 int rc;
2166 int i;
aa09a3da 2167 BUILD_BUG_ON(MC_CMD_INIT_RXQ_OUT_LEN != 0);
8127d661
BH
2168
2169 rx_queue->scatter_n = 0;
2170 rx_queue->scatter_len = 0;
2171
2172 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_SIZE, rx_queue->ptr_mask + 1);
2173 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_TARGET_EVQ, channel->channel);
2174 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_LABEL, efx_rx_queue_index(rx_queue));
2175 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_INSTANCE,
2176 efx_rx_queue_index(rx_queue));
bd9a265d
JC
2177 MCDI_POPULATE_DWORD_2(inbuf, INIT_RXQ_IN_FLAGS,
2178 INIT_RXQ_IN_FLAG_PREFIX, 1,
2179 INIT_RXQ_IN_FLAG_TIMESTAMP, 1);
8127d661 2180 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_OWNER_ID, 0);
45b2449e 2181 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_PORT_ID, nic_data->vport_id);
8127d661
BH
2182
2183 dma_addr = rx_queue->rxd.buf.dma_addr;
2184
2185 netif_dbg(efx, hw, efx->net_dev, "pushing RXQ %d. %zu entries (%llx)\n",
2186 efx_rx_queue_index(rx_queue), entries, (u64)dma_addr);
2187
2188 for (i = 0; i < entries; ++i) {
2189 MCDI_SET_ARRAY_QWORD(inbuf, INIT_RXQ_IN_DMA_ADDR, i, dma_addr);
2190 dma_addr += EFX_BUF_SIZE;
2191 }
2192
2193 inlen = MC_CMD_INIT_RXQ_IN_LEN(entries);
2194
2195 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_RXQ, inbuf, inlen,
aa09a3da 2196 NULL, 0, NULL);
48ce5634
BH
2197 if (rc)
2198 netdev_WARN(efx->net_dev, "failed to initialise RXQ %d\n",
2199 efx_rx_queue_index(rx_queue));
8127d661
BH
2200}
2201
2202static void efx_ef10_rx_fini(struct efx_rx_queue *rx_queue)
2203{
2204 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_RXQ_IN_LEN);
aa09a3da 2205 MCDI_DECLARE_BUF_ERR(outbuf);
8127d661
BH
2206 struct efx_nic *efx = rx_queue->efx;
2207 size_t outlen;
2208 int rc;
2209
2210 MCDI_SET_DWORD(inbuf, FINI_RXQ_IN_INSTANCE,
2211 efx_rx_queue_index(rx_queue));
2212
1e0b8120 2213 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_RXQ, inbuf, sizeof(inbuf),
8127d661
BH
2214 outbuf, sizeof(outbuf), &outlen);
2215
2216 if (rc && rc != -EALREADY)
2217 goto fail;
2218
2219 return;
2220
2221fail:
1e0b8120
EC
2222 efx_mcdi_display_error(efx, MC_CMD_FINI_RXQ, MC_CMD_FINI_RXQ_IN_LEN,
2223 outbuf, outlen, rc);
8127d661
BH
2224}
2225
2226static void efx_ef10_rx_remove(struct efx_rx_queue *rx_queue)
2227{
2228 efx_nic_free_buffer(rx_queue->efx, &rx_queue->rxd.buf);
2229}
2230
2231/* This creates an entry in the RX descriptor queue */
2232static inline void
2233efx_ef10_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
2234{
2235 struct efx_rx_buffer *rx_buf;
2236 efx_qword_t *rxd;
2237
2238 rxd = efx_rx_desc(rx_queue, index);
2239 rx_buf = efx_rx_buffer(rx_queue, index);
2240 EFX_POPULATE_QWORD_2(*rxd,
2241 ESF_DZ_RX_KER_BYTE_CNT, rx_buf->len,
2242 ESF_DZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
2243}
2244
2245static void efx_ef10_rx_write(struct efx_rx_queue *rx_queue)
2246{
2247 struct efx_nic *efx = rx_queue->efx;
2248 unsigned int write_count;
2249 efx_dword_t reg;
2250
2251 /* Firmware requires that RX_DESC_WPTR be a multiple of 8 */
2252 write_count = rx_queue->added_count & ~7;
2253 if (rx_queue->notified_count == write_count)
2254 return;
2255
2256 do
2257 efx_ef10_build_rx_desc(
2258 rx_queue,
2259 rx_queue->notified_count & rx_queue->ptr_mask);
2260 while (++rx_queue->notified_count != write_count);
2261
2262 wmb();
2263 EFX_POPULATE_DWORD_1(reg, ERF_DZ_RX_DESC_WPTR,
2264 write_count & rx_queue->ptr_mask);
2265 efx_writed_page(efx, &reg, ER_DZ_RX_DESC_UPD,
2266 efx_rx_queue_index(rx_queue));
2267}
2268
2269static efx_mcdi_async_completer efx_ef10_rx_defer_refill_complete;
2270
2271static void efx_ef10_rx_defer_refill(struct efx_rx_queue *rx_queue)
2272{
2273 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
2274 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
2275 efx_qword_t event;
2276
2277 EFX_POPULATE_QWORD_2(event,
2278 ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
2279 ESF_DZ_EV_DATA, EFX_EF10_REFILL);
2280
2281 MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
2282
2283 /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
2284 * already swapped the data to little-endian order.
2285 */
2286 memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
2287 sizeof(efx_qword_t));
2288
2289 efx_mcdi_rpc_async(channel->efx, MC_CMD_DRIVER_EVENT,
2290 inbuf, sizeof(inbuf), 0,
2291 efx_ef10_rx_defer_refill_complete, 0);
2292}
2293
2294static void
2295efx_ef10_rx_defer_refill_complete(struct efx_nic *efx, unsigned long cookie,
2296 int rc, efx_dword_t *outbuf,
2297 size_t outlen_actual)
2298{
2299 /* nothing to do */
2300}
2301
2302static int efx_ef10_ev_probe(struct efx_channel *channel)
2303{
2304 return efx_nic_alloc_buffer(channel->efx, &channel->eventq.buf,
2305 (channel->eventq_mask + 1) *
2306 sizeof(efx_qword_t),
2307 GFP_KERNEL);
2308}
2309
46e612b0
DP
2310static void efx_ef10_ev_fini(struct efx_channel *channel)
2311{
2312 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_EVQ_IN_LEN);
2313 MCDI_DECLARE_BUF_ERR(outbuf);
2314 struct efx_nic *efx = channel->efx;
2315 size_t outlen;
2316 int rc;
2317
2318 MCDI_SET_DWORD(inbuf, FINI_EVQ_IN_INSTANCE, channel->channel);
2319
2320 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_EVQ, inbuf, sizeof(inbuf),
2321 outbuf, sizeof(outbuf), &outlen);
2322
2323 if (rc && rc != -EALREADY)
2324 goto fail;
2325
2326 return;
2327
2328fail:
2329 efx_mcdi_display_error(efx, MC_CMD_FINI_EVQ, MC_CMD_FINI_EVQ_IN_LEN,
2330 outbuf, outlen, rc);
2331}
2332
8127d661
BH
2333static int efx_ef10_ev_init(struct efx_channel *channel)
2334{
2335 MCDI_DECLARE_BUF(inbuf,
2336 MC_CMD_INIT_EVQ_IN_LEN(EFX_MAX_EVQ_SIZE * 8 /
2337 EFX_BUF_SIZE));
2338 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_EVQ_OUT_LEN);
2339 size_t entries = channel->eventq.buf.len / EFX_BUF_SIZE;
2340 struct efx_nic *efx = channel->efx;
2341 struct efx_ef10_nic_data *nic_data;
2342 bool supports_rx_merge;
2343 size_t inlen, outlen;
46e612b0 2344 unsigned int enabled, implemented;
8127d661
BH
2345 dma_addr_t dma_addr;
2346 int rc;
2347 int i;
2348
2349 nic_data = efx->nic_data;
2350 supports_rx_merge =
2351 !!(nic_data->datapath_caps &
2352 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN);
2353
2354 /* Fill event queue with all ones (i.e. empty events) */
2355 memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
2356
2357 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_SIZE, channel->eventq_mask + 1);
2358 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_INSTANCE, channel->channel);
2359 /* INIT_EVQ expects index in vector table, not absolute */
2360 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_IRQ_NUM, channel->channel);
2361 MCDI_POPULATE_DWORD_4(inbuf, INIT_EVQ_IN_FLAGS,
2362 INIT_EVQ_IN_FLAG_INTERRUPTING, 1,
2363 INIT_EVQ_IN_FLAG_RX_MERGE, 1,
2364 INIT_EVQ_IN_FLAG_TX_MERGE, 1,
2365 INIT_EVQ_IN_FLAG_CUT_THRU, !supports_rx_merge);
2366 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_MODE,
2367 MC_CMD_INIT_EVQ_IN_TMR_MODE_DIS);
2368 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_LOAD, 0);
2369 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_RELOAD, 0);
2370 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_MODE,
2371 MC_CMD_INIT_EVQ_IN_COUNT_MODE_DIS);
2372 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_THRSHLD, 0);
2373
2374 dma_addr = channel->eventq.buf.dma_addr;
2375 for (i = 0; i < entries; ++i) {
2376 MCDI_SET_ARRAY_QWORD(inbuf, INIT_EVQ_IN_DMA_ADDR, i, dma_addr);
2377 dma_addr += EFX_BUF_SIZE;
2378 }
2379
2380 inlen = MC_CMD_INIT_EVQ_IN_LEN(entries);
2381
2382 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_EVQ, inbuf, inlen,
2383 outbuf, sizeof(outbuf), &outlen);
8127d661 2384 /* IRQ return is ignored */
46e612b0
DP
2385 if (channel->channel || rc)
2386 return rc;
8127d661 2387
46e612b0
DP
2388 /* Successfully created event queue on channel 0 */
2389 rc = efx_mcdi_get_workarounds(efx, &implemented, &enabled);
832dc9ed
EC
2390 if (rc == -ENOSYS) {
2391 /* GET_WORKAROUNDS was implemented before the bug26807
2392 * workaround, thus the latter must be unavailable in this fw
2393 */
2394 nic_data->workaround_26807 = false;
2395 rc = 0;
2396 } else if (rc) {
8127d661 2397 goto fail;
832dc9ed
EC
2398 } else {
2399 nic_data->workaround_26807 =
2400 !!(enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807);
2401
2402 if (implemented & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807 &&
2403 !nic_data->workaround_26807) {
5a55a72a
DP
2404 unsigned int flags;
2405
34ccfe6f
DP
2406 rc = efx_mcdi_set_workaround(efx,
2407 MC_CMD_WORKAROUND_BUG26807,
5a55a72a
DP
2408 true, &flags);
2409
2410 if (!rc) {
2411 if (flags &
2412 1 << MC_CMD_WORKAROUND_EXT_OUT_FLR_DONE_LBN) {
2413 netif_info(efx, drv, efx->net_dev,
2414 "other functions on NIC have been reset\n");
abd86a55
DP
2415
2416 /* With MCFW v4.6.x and earlier, the
2417 * boot count will have incremented,
2418 * so re-read the warm_boot_count
2419 * value now to ensure this function
2420 * doesn't think it has changed next
2421 * time it checks.
2422 */
2423 rc = efx_ef10_get_warm_boot_count(efx);
2424 if (rc >= 0) {
2425 nic_data->warm_boot_count = rc;
2426 rc = 0;
2427 }
5a55a72a 2428 }
832dc9ed 2429 nic_data->workaround_26807 = true;
5a55a72a 2430 } else if (rc == -EPERM) {
832dc9ed 2431 rc = 0;
5a55a72a 2432 }
832dc9ed 2433 }
46e612b0
DP
2434 }
2435
2436 if (!rc)
2437 return 0;
8127d661
BH
2438
2439fail:
46e612b0
DP
2440 efx_ef10_ev_fini(channel);
2441 return rc;
8127d661
BH
2442}
2443
2444static void efx_ef10_ev_remove(struct efx_channel *channel)
2445{
2446 efx_nic_free_buffer(channel->efx, &channel->eventq.buf);
2447}
2448
2449static void efx_ef10_handle_rx_wrong_queue(struct efx_rx_queue *rx_queue,
2450 unsigned int rx_queue_label)
2451{
2452 struct efx_nic *efx = rx_queue->efx;
2453
2454 netif_info(efx, hw, efx->net_dev,
2455 "rx event arrived on queue %d labeled as queue %u\n",
2456 efx_rx_queue_index(rx_queue), rx_queue_label);
2457
2458 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
2459}
2460
2461static void
2462efx_ef10_handle_rx_bad_lbits(struct efx_rx_queue *rx_queue,
2463 unsigned int actual, unsigned int expected)
2464{
2465 unsigned int dropped = (actual - expected) & rx_queue->ptr_mask;
2466 struct efx_nic *efx = rx_queue->efx;
2467
2468 netif_info(efx, hw, efx->net_dev,
2469 "dropped %d events (index=%d expected=%d)\n",
2470 dropped, actual, expected);
2471
2472 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
2473}
2474
2475/* partially received RX was aborted. clean up. */
2476static void efx_ef10_handle_rx_abort(struct efx_rx_queue *rx_queue)
2477{
2478 unsigned int rx_desc_ptr;
2479
8127d661
BH
2480 netif_dbg(rx_queue->efx, hw, rx_queue->efx->net_dev,
2481 "scattered RX aborted (dropping %u buffers)\n",
2482 rx_queue->scatter_n);
2483
2484 rx_desc_ptr = rx_queue->removed_count & rx_queue->ptr_mask;
2485
2486 efx_rx_packet(rx_queue, rx_desc_ptr, rx_queue->scatter_n,
2487 0, EFX_RX_PKT_DISCARD);
2488
2489 rx_queue->removed_count += rx_queue->scatter_n;
2490 rx_queue->scatter_n = 0;
2491 rx_queue->scatter_len = 0;
2492 ++efx_rx_queue_channel(rx_queue)->n_rx_nodesc_trunc;
2493}
2494
2495static int efx_ef10_handle_rx_event(struct efx_channel *channel,
2496 const efx_qword_t *event)
2497{
2498 unsigned int rx_bytes, next_ptr_lbits, rx_queue_label, rx_l4_class;
2499 unsigned int n_descs, n_packets, i;
2500 struct efx_nic *efx = channel->efx;
2501 struct efx_rx_queue *rx_queue;
2502 bool rx_cont;
2503 u16 flags = 0;
2504
2505 if (unlikely(ACCESS_ONCE(efx->reset_pending)))
2506 return 0;
2507
2508 /* Basic packet information */
2509 rx_bytes = EFX_QWORD_FIELD(*event, ESF_DZ_RX_BYTES);
2510 next_ptr_lbits = EFX_QWORD_FIELD(*event, ESF_DZ_RX_DSC_PTR_LBITS);
2511 rx_queue_label = EFX_QWORD_FIELD(*event, ESF_DZ_RX_QLABEL);
2512 rx_l4_class = EFX_QWORD_FIELD(*event, ESF_DZ_RX_L4_CLASS);
2513 rx_cont = EFX_QWORD_FIELD(*event, ESF_DZ_RX_CONT);
2514
48ce5634
BH
2515 if (EFX_QWORD_FIELD(*event, ESF_DZ_RX_DROP_EVENT))
2516 netdev_WARN(efx->net_dev, "saw RX_DROP_EVENT: event="
2517 EFX_QWORD_FMT "\n",
2518 EFX_QWORD_VAL(*event));
8127d661
BH
2519
2520 rx_queue = efx_channel_get_rx_queue(channel);
2521
2522 if (unlikely(rx_queue_label != efx_rx_queue_index(rx_queue)))
2523 efx_ef10_handle_rx_wrong_queue(rx_queue, rx_queue_label);
2524
2525 n_descs = ((next_ptr_lbits - rx_queue->removed_count) &
2526 ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
2527
2528 if (n_descs != rx_queue->scatter_n + 1) {
92a04168
BH
2529 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2530
8127d661
BH
2531 /* detect rx abort */
2532 if (unlikely(n_descs == rx_queue->scatter_n)) {
48ce5634
BH
2533 if (rx_queue->scatter_n == 0 || rx_bytes != 0)
2534 netdev_WARN(efx->net_dev,
2535 "invalid RX abort: scatter_n=%u event="
2536 EFX_QWORD_FMT "\n",
2537 rx_queue->scatter_n,
2538 EFX_QWORD_VAL(*event));
8127d661
BH
2539 efx_ef10_handle_rx_abort(rx_queue);
2540 return 0;
2541 }
2542
92a04168
BH
2543 /* Check that RX completion merging is valid, i.e.
2544 * the current firmware supports it and this is a
2545 * non-scattered packet.
2546 */
2547 if (!(nic_data->datapath_caps &
2548 (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN)) ||
2549 rx_queue->scatter_n != 0 || rx_cont) {
8127d661
BH
2550 efx_ef10_handle_rx_bad_lbits(
2551 rx_queue, next_ptr_lbits,
2552 (rx_queue->removed_count +
2553 rx_queue->scatter_n + 1) &
2554 ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
2555 return 0;
2556 }
2557
2558 /* Merged completion for multiple non-scattered packets */
2559 rx_queue->scatter_n = 1;
2560 rx_queue->scatter_len = 0;
2561 n_packets = n_descs;
2562 ++channel->n_rx_merge_events;
2563 channel->n_rx_merge_packets += n_packets;
2564 flags |= EFX_RX_PKT_PREFIX_LEN;
2565 } else {
2566 ++rx_queue->scatter_n;
2567 rx_queue->scatter_len += rx_bytes;
2568 if (rx_cont)
2569 return 0;
2570 n_packets = 1;
2571 }
2572
2573 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_ECRC_ERR)))
2574 flags |= EFX_RX_PKT_DISCARD;
2575
2576 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_IPCKSUM_ERR))) {
2577 channel->n_rx_ip_hdr_chksum_err += n_packets;
2578 } else if (unlikely(EFX_QWORD_FIELD(*event,
2579 ESF_DZ_RX_TCPUDP_CKSUM_ERR))) {
2580 channel->n_rx_tcp_udp_chksum_err += n_packets;
2581 } else if (rx_l4_class == ESE_DZ_L4_CLASS_TCP ||
2582 rx_l4_class == ESE_DZ_L4_CLASS_UDP) {
2583 flags |= EFX_RX_PKT_CSUMMED;
2584 }
2585
2586 if (rx_l4_class == ESE_DZ_L4_CLASS_TCP)
2587 flags |= EFX_RX_PKT_TCP;
2588
2589 channel->irq_mod_score += 2 * n_packets;
2590
2591 /* Handle received packet(s) */
2592 for (i = 0; i < n_packets; i++) {
2593 efx_rx_packet(rx_queue,
2594 rx_queue->removed_count & rx_queue->ptr_mask,
2595 rx_queue->scatter_n, rx_queue->scatter_len,
2596 flags);
2597 rx_queue->removed_count += rx_queue->scatter_n;
2598 }
2599
2600 rx_queue->scatter_n = 0;
2601 rx_queue->scatter_len = 0;
2602
2603 return n_packets;
2604}
2605
2606static int
2607efx_ef10_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
2608{
2609 struct efx_nic *efx = channel->efx;
2610 struct efx_tx_queue *tx_queue;
2611 unsigned int tx_ev_desc_ptr;
2612 unsigned int tx_ev_q_label;
2613 int tx_descs = 0;
2614
2615 if (unlikely(ACCESS_ONCE(efx->reset_pending)))
2616 return 0;
2617
2618 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_TX_DROP_EVENT)))
2619 return 0;
2620
2621 /* Transmit completion */
2622 tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, ESF_DZ_TX_DESCR_INDX);
2623 tx_ev_q_label = EFX_QWORD_FIELD(*event, ESF_DZ_TX_QLABEL);
2624 tx_queue = efx_channel_get_tx_queue(channel,
2625 tx_ev_q_label % EFX_TXQ_TYPES);
2626 tx_descs = ((tx_ev_desc_ptr + 1 - tx_queue->read_count) &
2627 tx_queue->ptr_mask);
2628 efx_xmit_done(tx_queue, tx_ev_desc_ptr & tx_queue->ptr_mask);
2629
2630 return tx_descs;
2631}
2632
2633static void
2634efx_ef10_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
2635{
2636 struct efx_nic *efx = channel->efx;
2637 int subcode;
2638
2639 subcode = EFX_QWORD_FIELD(*event, ESF_DZ_DRV_SUB_CODE);
2640
2641 switch (subcode) {
2642 case ESE_DZ_DRV_TIMER_EV:
2643 case ESE_DZ_DRV_WAKE_UP_EV:
2644 break;
2645 case ESE_DZ_DRV_START_UP_EV:
2646 /* event queue init complete. ok. */
2647 break;
2648 default:
2649 netif_err(efx, hw, efx->net_dev,
2650 "channel %d unknown driver event type %d"
2651 " (data " EFX_QWORD_FMT ")\n",
2652 channel->channel, subcode,
2653 EFX_QWORD_VAL(*event));
2654
2655 }
2656}
2657
2658static void efx_ef10_handle_driver_generated_event(struct efx_channel *channel,
2659 efx_qword_t *event)
2660{
2661 struct efx_nic *efx = channel->efx;
2662 u32 subcode;
2663
2664 subcode = EFX_QWORD_FIELD(*event, EFX_DWORD_0);
2665
2666 switch (subcode) {
2667 case EFX_EF10_TEST:
2668 channel->event_test_cpu = raw_smp_processor_id();
2669 break;
2670 case EFX_EF10_REFILL:
2671 /* The queue must be empty, so we won't receive any rx
2672 * events, so efx_process_channel() won't refill the
2673 * queue. Refill it here
2674 */
cce28794 2675 efx_fast_push_rx_descriptors(&channel->rx_queue, true);
8127d661
BH
2676 break;
2677 default:
2678 netif_err(efx, hw, efx->net_dev,
2679 "channel %d unknown driver event type %u"
2680 " (data " EFX_QWORD_FMT ")\n",
2681 channel->channel, (unsigned) subcode,
2682 EFX_QWORD_VAL(*event));
2683 }
2684}
2685
2686static int efx_ef10_ev_process(struct efx_channel *channel, int quota)
2687{
2688 struct efx_nic *efx = channel->efx;
2689 efx_qword_t event, *p_event;
2690 unsigned int read_ptr;
2691 int ev_code;
2692 int tx_descs = 0;
2693 int spent = 0;
2694
75363a46
EB
2695 if (quota <= 0)
2696 return spent;
2697
8127d661
BH
2698 read_ptr = channel->eventq_read_ptr;
2699
2700 for (;;) {
2701 p_event = efx_event(channel, read_ptr);
2702 event = *p_event;
2703
2704 if (!efx_event_present(&event))
2705 break;
2706
2707 EFX_SET_QWORD(*p_event);
2708
2709 ++read_ptr;
2710
2711 ev_code = EFX_QWORD_FIELD(event, ESF_DZ_EV_CODE);
2712
2713 netif_vdbg(efx, drv, efx->net_dev,
2714 "processing event on %d " EFX_QWORD_FMT "\n",
2715 channel->channel, EFX_QWORD_VAL(event));
2716
2717 switch (ev_code) {
2718 case ESE_DZ_EV_CODE_MCDI_EV:
2719 efx_mcdi_process_event(channel, &event);
2720 break;
2721 case ESE_DZ_EV_CODE_RX_EV:
2722 spent += efx_ef10_handle_rx_event(channel, &event);
2723 if (spent >= quota) {
2724 /* XXX can we split a merged event to
2725 * avoid going over-quota?
2726 */
2727 spent = quota;
2728 goto out;
2729 }
2730 break;
2731 case ESE_DZ_EV_CODE_TX_EV:
2732 tx_descs += efx_ef10_handle_tx_event(channel, &event);
2733 if (tx_descs > efx->txq_entries) {
2734 spent = quota;
2735 goto out;
2736 } else if (++spent == quota) {
2737 goto out;
2738 }
2739 break;
2740 case ESE_DZ_EV_CODE_DRIVER_EV:
2741 efx_ef10_handle_driver_event(channel, &event);
2742 if (++spent == quota)
2743 goto out;
2744 break;
2745 case EFX_EF10_DRVGEN_EV:
2746 efx_ef10_handle_driver_generated_event(channel, &event);
2747 break;
2748 default:
2749 netif_err(efx, hw, efx->net_dev,
2750 "channel %d unknown event type %d"
2751 " (data " EFX_QWORD_FMT ")\n",
2752 channel->channel, ev_code,
2753 EFX_QWORD_VAL(event));
2754 }
2755 }
2756
2757out:
2758 channel->eventq_read_ptr = read_ptr;
2759 return spent;
2760}
2761
2762static void efx_ef10_ev_read_ack(struct efx_channel *channel)
2763{
2764 struct efx_nic *efx = channel->efx;
2765 efx_dword_t rptr;
2766
2767 if (EFX_EF10_WORKAROUND_35388(efx)) {
2768 BUILD_BUG_ON(EFX_MIN_EVQ_SIZE <
2769 (1 << ERF_DD_EVQ_IND_RPTR_WIDTH));
2770 BUILD_BUG_ON(EFX_MAX_EVQ_SIZE >
2771 (1 << 2 * ERF_DD_EVQ_IND_RPTR_WIDTH));
2772
2773 EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
2774 EFE_DD_EVQ_IND_RPTR_FLAGS_HIGH,
2775 ERF_DD_EVQ_IND_RPTR,
2776 (channel->eventq_read_ptr &
2777 channel->eventq_mask) >>
2778 ERF_DD_EVQ_IND_RPTR_WIDTH);
2779 efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
2780 channel->channel);
2781 EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
2782 EFE_DD_EVQ_IND_RPTR_FLAGS_LOW,
2783 ERF_DD_EVQ_IND_RPTR,
2784 channel->eventq_read_ptr &
2785 ((1 << ERF_DD_EVQ_IND_RPTR_WIDTH) - 1));
2786 efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
2787 channel->channel);
2788 } else {
2789 EFX_POPULATE_DWORD_1(rptr, ERF_DZ_EVQ_RPTR,
2790 channel->eventq_read_ptr &
2791 channel->eventq_mask);
2792 efx_writed_page(efx, &rptr, ER_DZ_EVQ_RPTR, channel->channel);
2793 }
2794}
2795
2796static void efx_ef10_ev_test_generate(struct efx_channel *channel)
2797{
2798 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
2799 struct efx_nic *efx = channel->efx;
2800 efx_qword_t event;
2801 int rc;
2802
2803 EFX_POPULATE_QWORD_2(event,
2804 ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
2805 ESF_DZ_EV_DATA, EFX_EF10_TEST);
2806
2807 MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
2808
2809 /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
2810 * already swapped the data to little-endian order.
2811 */
2812 memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
2813 sizeof(efx_qword_t));
2814
2815 rc = efx_mcdi_rpc(efx, MC_CMD_DRIVER_EVENT, inbuf, sizeof(inbuf),
2816 NULL, 0, NULL);
2817 if (rc != 0)
2818 goto fail;
2819
2820 return;
2821
2822fail:
2823 WARN_ON(true);
2824 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
2825}
2826
2827void efx_ef10_handle_drain_event(struct efx_nic *efx)
2828{
2829 if (atomic_dec_and_test(&efx->active_queues))
2830 wake_up(&efx->flush_wq);
2831
2832 WARN_ON(atomic_read(&efx->active_queues) < 0);
2833}
2834
2835static int efx_ef10_fini_dmaq(struct efx_nic *efx)
2836{
2837 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2838 struct efx_channel *channel;
2839 struct efx_tx_queue *tx_queue;
2840 struct efx_rx_queue *rx_queue;
2841 int pending;
2842
2843 /* If the MC has just rebooted, the TX/RX queues will have already been
2844 * torn down, but efx->active_queues needs to be set to zero.
2845 */
2846 if (nic_data->must_realloc_vis) {
2847 atomic_set(&efx->active_queues, 0);
2848 return 0;
2849 }
2850
2851 /* Do not attempt to write to the NIC during EEH recovery */
2852 if (efx->state != STATE_RECOVERY) {
2853 efx_for_each_channel(channel, efx) {
2854 efx_for_each_channel_rx_queue(rx_queue, channel)
2855 efx_ef10_rx_fini(rx_queue);
2856 efx_for_each_channel_tx_queue(tx_queue, channel)
2857 efx_ef10_tx_fini(tx_queue);
2858 }
2859
2860 wait_event_timeout(efx->flush_wq,
2861 atomic_read(&efx->active_queues) == 0,
2862 msecs_to_jiffies(EFX_MAX_FLUSH_TIME));
2863 pending = atomic_read(&efx->active_queues);
2864 if (pending) {
2865 netif_err(efx, hw, efx->net_dev, "failed to flush %d queues\n",
2866 pending);
2867 return -ETIMEDOUT;
2868 }
2869 }
2870
2871 return 0;
2872}
2873
e283546c
EC
2874static void efx_ef10_prepare_flr(struct efx_nic *efx)
2875{
2876 atomic_set(&efx->active_queues, 0);
2877}
2878
8127d661
BH
2879static bool efx_ef10_filter_equal(const struct efx_filter_spec *left,
2880 const struct efx_filter_spec *right)
2881{
2882 if ((left->match_flags ^ right->match_flags) |
2883 ((left->flags ^ right->flags) &
2884 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
2885 return false;
2886
2887 return memcmp(&left->outer_vid, &right->outer_vid,
2888 sizeof(struct efx_filter_spec) -
2889 offsetof(struct efx_filter_spec, outer_vid)) == 0;
2890}
2891
2892static unsigned int efx_ef10_filter_hash(const struct efx_filter_spec *spec)
2893{
2894 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
2895 return jhash2((const u32 *)&spec->outer_vid,
2896 (sizeof(struct efx_filter_spec) -
2897 offsetof(struct efx_filter_spec, outer_vid)) / 4,
2898 0);
2899 /* XXX should we randomise the initval? */
2900}
2901
2902/* Decide whether a filter should be exclusive or else should allow
2903 * delivery to additional recipients. Currently we decide that
2904 * filters for specific local unicast MAC and IP addresses are
2905 * exclusive.
2906 */
2907static bool efx_ef10_filter_is_exclusive(const struct efx_filter_spec *spec)
2908{
2909 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
2910 !is_multicast_ether_addr(spec->loc_mac))
2911 return true;
2912
2913 if ((spec->match_flags &
2914 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
2915 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
2916 if (spec->ether_type == htons(ETH_P_IP) &&
2917 !ipv4_is_multicast(spec->loc_host[0]))
2918 return true;
2919 if (spec->ether_type == htons(ETH_P_IPV6) &&
2920 ((const u8 *)spec->loc_host)[0] != 0xff)
2921 return true;
2922 }
2923
2924 return false;
2925}
2926
2927static struct efx_filter_spec *
2928efx_ef10_filter_entry_spec(const struct efx_ef10_filter_table *table,
2929 unsigned int filter_idx)
2930{
2931 return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
2932 ~EFX_EF10_FILTER_FLAGS);
2933}
2934
2935static unsigned int
2936efx_ef10_filter_entry_flags(const struct efx_ef10_filter_table *table,
2937 unsigned int filter_idx)
2938{
2939 return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
2940}
2941
2942static void
2943efx_ef10_filter_set_entry(struct efx_ef10_filter_table *table,
2944 unsigned int filter_idx,
2945 const struct efx_filter_spec *spec,
2946 unsigned int flags)
2947{
2948 table->entry[filter_idx].spec = (unsigned long)spec | flags;
2949}
2950
2951static void efx_ef10_filter_push_prep(struct efx_nic *efx,
2952 const struct efx_filter_spec *spec,
2953 efx_dword_t *inbuf, u64 handle,
2954 bool replacing)
2955{
2956 struct efx_ef10_nic_data *nic_data = efx->nic_data;
dcb4123c 2957 u32 flags = spec->flags;
8127d661
BH
2958
2959 memset(inbuf, 0, MC_CMD_FILTER_OP_IN_LEN);
2960
dcb4123c
JC
2961 /* Remove RSS flag if we don't have an RSS context. */
2962 if (flags & EFX_FILTER_FLAG_RX_RSS &&
2963 spec->rss_context == EFX_FILTER_RSS_CONTEXT_DEFAULT &&
2964 nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID)
2965 flags &= ~EFX_FILTER_FLAG_RX_RSS;
2966
8127d661
BH
2967 if (replacing) {
2968 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2969 MC_CMD_FILTER_OP_IN_OP_REPLACE);
2970 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
2971 } else {
2972 u32 match_fields = 0;
2973
2974 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2975 efx_ef10_filter_is_exclusive(spec) ?
2976 MC_CMD_FILTER_OP_IN_OP_INSERT :
2977 MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
2978
2979 /* Convert match flags and values. Unlike almost
2980 * everything else in MCDI, these fields are in
2981 * network byte order.
2982 */
2983 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
2984 match_fields |=
2985 is_multicast_ether_addr(spec->loc_mac) ?
2986 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST_LBN :
2987 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
2988#define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
2989 if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
2990 match_fields |= \
2991 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
2992 mcdi_field ## _LBN; \
2993 BUILD_BUG_ON( \
2994 MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
2995 sizeof(spec->gen_field)); \
2996 memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
2997 &spec->gen_field, sizeof(spec->gen_field)); \
2998 }
2999 COPY_FIELD(REM_HOST, rem_host, SRC_IP);
3000 COPY_FIELD(LOC_HOST, loc_host, DST_IP);
3001 COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
3002 COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
3003 COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
3004 COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
3005 COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
3006 COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
3007 COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
3008 COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
3009#undef COPY_FIELD
3010 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
3011 match_fields);
3012 }
3013
45b2449e 3014 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, nic_data->vport_id);
8127d661
BH
3015 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
3016 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
3017 MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
3018 MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
e3d36293 3019 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DOMAIN, 0);
8127d661
BH
3020 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
3021 MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
a0bc3487
BH
3022 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE,
3023 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
3024 0 : spec->dmaq_id);
8127d661 3025 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
dcb4123c 3026 (flags & EFX_FILTER_FLAG_RX_RSS) ?
8127d661
BH
3027 MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
3028 MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
dcb4123c 3029 if (flags & EFX_FILTER_FLAG_RX_RSS)
8127d661
BH
3030 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT,
3031 spec->rss_context !=
3032 EFX_FILTER_RSS_CONTEXT_DEFAULT ?
3033 spec->rss_context : nic_data->rx_rss_context);
3034}
3035
3036static int efx_ef10_filter_push(struct efx_nic *efx,
3037 const struct efx_filter_spec *spec,
3038 u64 *handle, bool replacing)
3039{
3040 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
3041 MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_OUT_LEN);
3042 int rc;
3043
3044 efx_ef10_filter_push_prep(efx, spec, inbuf, *handle, replacing);
3045 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
3046 outbuf, sizeof(outbuf), NULL);
3047 if (rc == 0)
3048 *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
065e64c4
BH
3049 if (rc == -ENOSPC)
3050 rc = -EBUSY; /* to match efx_farch_filter_insert() */
8127d661
BH
3051 return rc;
3052}
3053
3054static int efx_ef10_filter_rx_match_pri(struct efx_ef10_filter_table *table,
3055 enum efx_filter_match_flags match_flags)
3056{
3057 unsigned int match_pri;
3058
3059 for (match_pri = 0;
3060 match_pri < table->rx_match_count;
3061 match_pri++)
3062 if (table->rx_match_flags[match_pri] == match_flags)
3063 return match_pri;
3064
3065 return -EPROTONOSUPPORT;
3066}
3067
3068static s32 efx_ef10_filter_insert(struct efx_nic *efx,
3069 struct efx_filter_spec *spec,
3070 bool replace_equal)
3071{
3072 struct efx_ef10_filter_table *table = efx->filter_state;
3073 DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
3074 struct efx_filter_spec *saved_spec;
3075 unsigned int match_pri, hash;
3076 unsigned int priv_flags;
3077 bool replacing = false;
3078 int ins_index = -1;
3079 DEFINE_WAIT(wait);
3080 bool is_mc_recip;
3081 s32 rc;
3082
3083 /* For now, only support RX filters */
3084 if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
3085 EFX_FILTER_FLAG_RX)
3086 return -EINVAL;
3087
3088 rc = efx_ef10_filter_rx_match_pri(table, spec->match_flags);
3089 if (rc < 0)
3090 return rc;
3091 match_pri = rc;
3092
3093 hash = efx_ef10_filter_hash(spec);
3094 is_mc_recip = efx_filter_is_mc_recipient(spec);
3095 if (is_mc_recip)
3096 bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
3097
3098 /* Find any existing filters with the same match tuple or
3099 * else a free slot to insert at. If any of them are busy,
3100 * we have to wait and retry.
3101 */
3102 for (;;) {
3103 unsigned int depth = 1;
3104 unsigned int i;
3105
3106 spin_lock_bh(&efx->filter_lock);
3107
3108 for (;;) {
3109 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
3110 saved_spec = efx_ef10_filter_entry_spec(table, i);
3111
3112 if (!saved_spec) {
3113 if (ins_index < 0)
3114 ins_index = i;
3115 } else if (efx_ef10_filter_equal(spec, saved_spec)) {
3116 if (table->entry[i].spec &
3117 EFX_EF10_FILTER_FLAG_BUSY)
3118 break;
3119 if (spec->priority < saved_spec->priority &&
7665d1ab 3120 spec->priority != EFX_FILTER_PRI_AUTO) {
8127d661
BH
3121 rc = -EPERM;
3122 goto out_unlock;
3123 }
3124 if (!is_mc_recip) {
3125 /* This is the only one */
3126 if (spec->priority ==
3127 saved_spec->priority &&
3128 !replace_equal) {
3129 rc = -EEXIST;
3130 goto out_unlock;
3131 }
3132 ins_index = i;
3133 goto found;
3134 } else if (spec->priority >
3135 saved_spec->priority ||
3136 (spec->priority ==
3137 saved_spec->priority &&
3138 replace_equal)) {
3139 if (ins_index < 0)
3140 ins_index = i;
3141 else
3142 __set_bit(depth, mc_rem_map);
3143 }
3144 }
3145
3146 /* Once we reach the maximum search depth, use
3147 * the first suitable slot or return -EBUSY if
3148 * there was none
3149 */
3150 if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
3151 if (ins_index < 0) {
3152 rc = -EBUSY;
3153 goto out_unlock;
3154 }
3155 goto found;
3156 }
3157
3158 ++depth;
3159 }
3160
3161 prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
3162 spin_unlock_bh(&efx->filter_lock);
3163 schedule();
3164 }
3165
3166found:
3167 /* Create a software table entry if necessary, and mark it
3168 * busy. We might yet fail to insert, but any attempt to
3169 * insert a conflicting filter while we're waiting for the
3170 * firmware must find the busy entry.
3171 */
3172 saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
3173 if (saved_spec) {
7665d1ab
BH
3174 if (spec->priority == EFX_FILTER_PRI_AUTO &&
3175 saved_spec->priority >= EFX_FILTER_PRI_AUTO) {
8127d661 3176 /* Just make sure it won't be removed */
7665d1ab
BH
3177 if (saved_spec->priority > EFX_FILTER_PRI_AUTO)
3178 saved_spec->flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
8127d661 3179 table->entry[ins_index].spec &=
b59e6ef8 3180 ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
8127d661
BH
3181 rc = ins_index;
3182 goto out_unlock;
3183 }
3184 replacing = true;
3185 priv_flags = efx_ef10_filter_entry_flags(table, ins_index);
3186 } else {
3187 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
3188 if (!saved_spec) {
3189 rc = -ENOMEM;
3190 goto out_unlock;
3191 }
3192 *saved_spec = *spec;
3193 priv_flags = 0;
3194 }
3195 efx_ef10_filter_set_entry(table, ins_index, saved_spec,
3196 priv_flags | EFX_EF10_FILTER_FLAG_BUSY);
3197
3198 /* Mark lower-priority multicast recipients busy prior to removal */
3199 if (is_mc_recip) {
3200 unsigned int depth, i;
3201
3202 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
3203 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
3204 if (test_bit(depth, mc_rem_map))
3205 table->entry[i].spec |=
3206 EFX_EF10_FILTER_FLAG_BUSY;
3207 }
3208 }
3209
3210 spin_unlock_bh(&efx->filter_lock);
3211
3212 rc = efx_ef10_filter_push(efx, spec, &table->entry[ins_index].handle,
3213 replacing);
3214
3215 /* Finalise the software table entry */
3216 spin_lock_bh(&efx->filter_lock);
3217 if (rc == 0) {
3218 if (replacing) {
3219 /* Update the fields that may differ */
7665d1ab
BH
3220 if (saved_spec->priority == EFX_FILTER_PRI_AUTO)
3221 saved_spec->flags |=
3222 EFX_FILTER_FLAG_RX_OVER_AUTO;
8127d661 3223 saved_spec->priority = spec->priority;
7665d1ab 3224 saved_spec->flags &= EFX_FILTER_FLAG_RX_OVER_AUTO;
8127d661
BH
3225 saved_spec->flags |= spec->flags;
3226 saved_spec->rss_context = spec->rss_context;
3227 saved_spec->dmaq_id = spec->dmaq_id;
3228 }
3229 } else if (!replacing) {
3230 kfree(saved_spec);
3231 saved_spec = NULL;
3232 }
3233 efx_ef10_filter_set_entry(table, ins_index, saved_spec, priv_flags);
3234
3235 /* Remove and finalise entries for lower-priority multicast
3236 * recipients
3237 */
3238 if (is_mc_recip) {
3239 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
3240 unsigned int depth, i;
3241
3242 memset(inbuf, 0, sizeof(inbuf));
3243
3244 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
3245 if (!test_bit(depth, mc_rem_map))
3246 continue;
3247
3248 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
3249 saved_spec = efx_ef10_filter_entry_spec(table, i);
3250 priv_flags = efx_ef10_filter_entry_flags(table, i);
3251
3252 if (rc == 0) {
3253 spin_unlock_bh(&efx->filter_lock);
3254 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
3255 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
3256 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
3257 table->entry[i].handle);
3258 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
3259 inbuf, sizeof(inbuf),
3260 NULL, 0, NULL);
3261 spin_lock_bh(&efx->filter_lock);
3262 }
3263
3264 if (rc == 0) {
3265 kfree(saved_spec);
3266 saved_spec = NULL;
3267 priv_flags = 0;
3268 } else {
3269 priv_flags &= ~EFX_EF10_FILTER_FLAG_BUSY;
3270 }
3271 efx_ef10_filter_set_entry(table, i, saved_spec,
3272 priv_flags);
3273 }
3274 }
3275
3276 /* If successful, return the inserted filter ID */
3277 if (rc == 0)
3278 rc = match_pri * HUNT_FILTER_TBL_ROWS + ins_index;
3279
3280 wake_up_all(&table->waitq);
3281out_unlock:
3282 spin_unlock_bh(&efx->filter_lock);
3283 finish_wait(&table->waitq, &wait);
3284 return rc;
3285}
3286
9fd8095d 3287static void efx_ef10_filter_update_rx_scatter(struct efx_nic *efx)
8127d661
BH
3288{
3289 /* no need to do anything here on EF10 */
3290}
3291
3292/* Remove a filter.
b59e6ef8
BH
3293 * If !by_index, remove by ID
3294 * If by_index, remove by index
8127d661
BH
3295 * Filter ID may come from userland and must be range-checked.
3296 */
3297static int efx_ef10_filter_remove_internal(struct efx_nic *efx,
fbd79120 3298 unsigned int priority_mask,
b59e6ef8 3299 u32 filter_id, bool by_index)
8127d661
BH
3300{
3301 unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
3302 struct efx_ef10_filter_table *table = efx->filter_state;
3303 MCDI_DECLARE_BUF(inbuf,
3304 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
3305 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
3306 struct efx_filter_spec *spec;
3307 DEFINE_WAIT(wait);
3308 int rc;
3309
3310 /* Find the software table entry and mark it busy. Don't
3311 * remove it yet; any attempt to update while we're waiting
3312 * for the firmware must find the busy entry.
3313 */
3314 for (;;) {
3315 spin_lock_bh(&efx->filter_lock);
3316 if (!(table->entry[filter_idx].spec &
3317 EFX_EF10_FILTER_FLAG_BUSY))
3318 break;
3319 prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
3320 spin_unlock_bh(&efx->filter_lock);
3321 schedule();
3322 }
7665d1ab 3323
8127d661 3324 spec = efx_ef10_filter_entry_spec(table, filter_idx);
7665d1ab 3325 if (!spec ||
b59e6ef8 3326 (!by_index &&
8127d661
BH
3327 efx_ef10_filter_rx_match_pri(table, spec->match_flags) !=
3328 filter_id / HUNT_FILTER_TBL_ROWS)) {
3329 rc = -ENOENT;
3330 goto out_unlock;
3331 }
7665d1ab
BH
3332
3333 if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO &&
fbd79120 3334 priority_mask == (1U << EFX_FILTER_PRI_AUTO)) {
7665d1ab
BH
3335 /* Just remove flags */
3336 spec->flags &= ~EFX_FILTER_FLAG_RX_OVER_AUTO;
b59e6ef8 3337 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
7665d1ab
BH
3338 rc = 0;
3339 goto out_unlock;
3340 }
3341
fbd79120 3342 if (!(priority_mask & (1U << spec->priority))) {
7665d1ab
BH
3343 rc = -ENOENT;
3344 goto out_unlock;
3345 }
3346
8127d661
BH
3347 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
3348 spin_unlock_bh(&efx->filter_lock);
3349
7665d1ab 3350 if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
b59e6ef8 3351 /* Reset to an automatic filter */
8127d661
BH
3352
3353 struct efx_filter_spec new_spec = *spec;
3354
7665d1ab 3355 new_spec.priority = EFX_FILTER_PRI_AUTO;
8127d661 3356 new_spec.flags = (EFX_FILTER_FLAG_RX |
f1c2ef40
BK
3357 (efx_rss_enabled(efx) ?
3358 EFX_FILTER_FLAG_RX_RSS : 0));
8127d661
BH
3359 new_spec.dmaq_id = 0;
3360 new_spec.rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
3361 rc = efx_ef10_filter_push(efx, &new_spec,
3362 &table->entry[filter_idx].handle,
3363 true);
3364
3365 spin_lock_bh(&efx->filter_lock);
3366 if (rc == 0)
3367 *spec = new_spec;
3368 } else {
3369 /* Really remove the filter */
3370
3371 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
3372 efx_ef10_filter_is_exclusive(spec) ?
3373 MC_CMD_FILTER_OP_IN_OP_REMOVE :
3374 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
3375 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
3376 table->entry[filter_idx].handle);
3377 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
3378 inbuf, sizeof(inbuf), NULL, 0, NULL);
3379
3380 spin_lock_bh(&efx->filter_lock);
3381 if (rc == 0) {
3382 kfree(spec);
3383 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
3384 }
3385 }
7665d1ab 3386
8127d661
BH
3387 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
3388 wake_up_all(&table->waitq);
3389out_unlock:
3390 spin_unlock_bh(&efx->filter_lock);
3391 finish_wait(&table->waitq, &wait);
3392 return rc;
3393}
3394
3395static int efx_ef10_filter_remove_safe(struct efx_nic *efx,
3396 enum efx_filter_priority priority,
3397 u32 filter_id)
3398{
fbd79120
BH
3399 return efx_ef10_filter_remove_internal(efx, 1U << priority,
3400 filter_id, false);
8127d661
BH
3401}
3402
12fb0da4
EC
3403static u32 efx_ef10_filter_get_unsafe_id(struct efx_nic *efx, u32 filter_id)
3404{
3405 return filter_id % HUNT_FILTER_TBL_ROWS;
3406}
3407
3408static int efx_ef10_filter_remove_unsafe(struct efx_nic *efx,
3409 enum efx_filter_priority priority,
3410 u32 filter_id)
3411{
3412 return efx_ef10_filter_remove_internal(efx, 1U << priority,
3413 filter_id, true);
3414}
3415
8127d661
BH
3416static int efx_ef10_filter_get_safe(struct efx_nic *efx,
3417 enum efx_filter_priority priority,
3418 u32 filter_id, struct efx_filter_spec *spec)
3419{
3420 unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
3421 struct efx_ef10_filter_table *table = efx->filter_state;
3422 const struct efx_filter_spec *saved_spec;
3423 int rc;
3424
3425 spin_lock_bh(&efx->filter_lock);
3426 saved_spec = efx_ef10_filter_entry_spec(table, filter_idx);
3427 if (saved_spec && saved_spec->priority == priority &&
3428 efx_ef10_filter_rx_match_pri(table, saved_spec->match_flags) ==
3429 filter_id / HUNT_FILTER_TBL_ROWS) {
3430 *spec = *saved_spec;
3431 rc = 0;
3432 } else {
3433 rc = -ENOENT;
3434 }
3435 spin_unlock_bh(&efx->filter_lock);
3436 return rc;
3437}
3438
fbd79120 3439static int efx_ef10_filter_clear_rx(struct efx_nic *efx,
8127d661
BH
3440 enum efx_filter_priority priority)
3441{
fbd79120
BH
3442 unsigned int priority_mask;
3443 unsigned int i;
3444 int rc;
3445
3446 priority_mask = (((1U << (priority + 1)) - 1) &
3447 ~(1U << EFX_FILTER_PRI_AUTO));
3448
3449 for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
3450 rc = efx_ef10_filter_remove_internal(efx, priority_mask,
3451 i, true);
3452 if (rc && rc != -ENOENT)
3453 return rc;
3454 }
3455
3456 return 0;
8127d661
BH
3457}
3458
3459static u32 efx_ef10_filter_count_rx_used(struct efx_nic *efx,
3460 enum efx_filter_priority priority)
3461{
3462 struct efx_ef10_filter_table *table = efx->filter_state;
3463 unsigned int filter_idx;
3464 s32 count = 0;
3465
3466 spin_lock_bh(&efx->filter_lock);
3467 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
3468 if (table->entry[filter_idx].spec &&
3469 efx_ef10_filter_entry_spec(table, filter_idx)->priority ==
3470 priority)
3471 ++count;
3472 }
3473 spin_unlock_bh(&efx->filter_lock);
3474 return count;
3475}
3476
3477static u32 efx_ef10_filter_get_rx_id_limit(struct efx_nic *efx)
3478{
3479 struct efx_ef10_filter_table *table = efx->filter_state;
3480
3481 return table->rx_match_count * HUNT_FILTER_TBL_ROWS;
3482}
3483
3484static s32 efx_ef10_filter_get_rx_ids(struct efx_nic *efx,
3485 enum efx_filter_priority priority,
3486 u32 *buf, u32 size)
3487{
3488 struct efx_ef10_filter_table *table = efx->filter_state;
3489 struct efx_filter_spec *spec;
3490 unsigned int filter_idx;
3491 s32 count = 0;
3492
3493 spin_lock_bh(&efx->filter_lock);
3494 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
3495 spec = efx_ef10_filter_entry_spec(table, filter_idx);
3496 if (spec && spec->priority == priority) {
3497 if (count == size) {
3498 count = -EMSGSIZE;
3499 break;
3500 }
3501 buf[count++] = (efx_ef10_filter_rx_match_pri(
3502 table, spec->match_flags) *
3503 HUNT_FILTER_TBL_ROWS +
3504 filter_idx);
3505 }
3506 }
3507 spin_unlock_bh(&efx->filter_lock);
3508 return count;
3509}
3510
3511#ifdef CONFIG_RFS_ACCEL
3512
3513static efx_mcdi_async_completer efx_ef10_filter_rfs_insert_complete;
3514
3515static s32 efx_ef10_filter_rfs_insert(struct efx_nic *efx,
3516 struct efx_filter_spec *spec)
3517{
3518 struct efx_ef10_filter_table *table = efx->filter_state;
3519 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
3520 struct efx_filter_spec *saved_spec;
3521 unsigned int hash, i, depth = 1;
3522 bool replacing = false;
3523 int ins_index = -1;
3524 u64 cookie;
3525 s32 rc;
3526
3527 /* Must be an RX filter without RSS and not for a multicast
3528 * destination address (RFS only works for connected sockets).
3529 * These restrictions allow us to pass only a tiny amount of
3530 * data through to the completion function.
3531 */
3532 EFX_WARN_ON_PARANOID(spec->flags !=
3533 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_RX_SCATTER));
3534 EFX_WARN_ON_PARANOID(spec->priority != EFX_FILTER_PRI_HINT);
3535 EFX_WARN_ON_PARANOID(efx_filter_is_mc_recipient(spec));
3536
3537 hash = efx_ef10_filter_hash(spec);
3538
3539 spin_lock_bh(&efx->filter_lock);
3540
3541 /* Find any existing filter with the same match tuple or else
3542 * a free slot to insert at. If an existing filter is busy,
3543 * we have to give up.
3544 */
3545 for (;;) {
3546 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
3547 saved_spec = efx_ef10_filter_entry_spec(table, i);
3548
3549 if (!saved_spec) {
3550 if (ins_index < 0)
3551 ins_index = i;
3552 } else if (efx_ef10_filter_equal(spec, saved_spec)) {
3553 if (table->entry[i].spec & EFX_EF10_FILTER_FLAG_BUSY) {
3554 rc = -EBUSY;
3555 goto fail_unlock;
3556 }
8127d661
BH
3557 if (spec->priority < saved_spec->priority) {
3558 rc = -EPERM;
3559 goto fail_unlock;
3560 }
3561 ins_index = i;
3562 break;
3563 }
3564
3565 /* Once we reach the maximum search depth, use the
3566 * first suitable slot or return -EBUSY if there was
3567 * none
3568 */
3569 if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
3570 if (ins_index < 0) {
3571 rc = -EBUSY;
3572 goto fail_unlock;
3573 }
3574 break;
3575 }
3576
3577 ++depth;
3578 }
3579
3580 /* Create a software table entry if necessary, and mark it
3581 * busy. We might yet fail to insert, but any attempt to
3582 * insert a conflicting filter while we're waiting for the
3583 * firmware must find the busy entry.
3584 */
3585 saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
3586 if (saved_spec) {
3587 replacing = true;
3588 } else {
3589 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
3590 if (!saved_spec) {
3591 rc = -ENOMEM;
3592 goto fail_unlock;
3593 }
3594 *saved_spec = *spec;
3595 }
3596 efx_ef10_filter_set_entry(table, ins_index, saved_spec,
3597 EFX_EF10_FILTER_FLAG_BUSY);
3598
3599 spin_unlock_bh(&efx->filter_lock);
3600
3601 /* Pack up the variables needed on completion */
3602 cookie = replacing << 31 | ins_index << 16 | spec->dmaq_id;
3603
3604 efx_ef10_filter_push_prep(efx, spec, inbuf,
3605 table->entry[ins_index].handle, replacing);
3606 efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
3607 MC_CMD_FILTER_OP_OUT_LEN,
3608 efx_ef10_filter_rfs_insert_complete, cookie);
3609
3610 return ins_index;
3611
3612fail_unlock:
3613 spin_unlock_bh(&efx->filter_lock);
3614 return rc;
3615}
3616
3617static void
3618efx_ef10_filter_rfs_insert_complete(struct efx_nic *efx, unsigned long cookie,
3619 int rc, efx_dword_t *outbuf,
3620 size_t outlen_actual)
3621{
3622 struct efx_ef10_filter_table *table = efx->filter_state;
3623 unsigned int ins_index, dmaq_id;
3624 struct efx_filter_spec *spec;
3625 bool replacing;
3626
3627 /* Unpack the cookie */
3628 replacing = cookie >> 31;
3629 ins_index = (cookie >> 16) & (HUNT_FILTER_TBL_ROWS - 1);
3630 dmaq_id = cookie & 0xffff;
3631
3632 spin_lock_bh(&efx->filter_lock);
3633 spec = efx_ef10_filter_entry_spec(table, ins_index);
3634 if (rc == 0) {
3635 table->entry[ins_index].handle =
3636 MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
3637 if (replacing)
3638 spec->dmaq_id = dmaq_id;
3639 } else if (!replacing) {
3640 kfree(spec);
3641 spec = NULL;
3642 }
3643 efx_ef10_filter_set_entry(table, ins_index, spec, 0);
3644 spin_unlock_bh(&efx->filter_lock);
3645
3646 wake_up_all(&table->waitq);
3647}
3648
3649static void
3650efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
3651 unsigned long filter_idx,
3652 int rc, efx_dword_t *outbuf,
3653 size_t outlen_actual);
3654
3655static bool efx_ef10_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
3656 unsigned int filter_idx)
3657{
3658 struct efx_ef10_filter_table *table = efx->filter_state;
3659 struct efx_filter_spec *spec =
3660 efx_ef10_filter_entry_spec(table, filter_idx);
3661 MCDI_DECLARE_BUF(inbuf,
3662 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
3663 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
3664
3665 if (!spec ||
3666 (table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAG_BUSY) ||
3667 spec->priority != EFX_FILTER_PRI_HINT ||
3668 !rps_may_expire_flow(efx->net_dev, spec->dmaq_id,
3669 flow_id, filter_idx))
3670 return false;
3671
3672 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
3673 MC_CMD_FILTER_OP_IN_OP_REMOVE);
3674 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
3675 table->entry[filter_idx].handle);
3676 if (efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf), 0,
3677 efx_ef10_filter_rfs_expire_complete, filter_idx))
3678 return false;
3679
3680 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
3681 return true;
3682}
3683
3684static void
3685efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
3686 unsigned long filter_idx,
3687 int rc, efx_dword_t *outbuf,
3688 size_t outlen_actual)
3689{
3690 struct efx_ef10_filter_table *table = efx->filter_state;
3691 struct efx_filter_spec *spec =
3692 efx_ef10_filter_entry_spec(table, filter_idx);
3693
3694 spin_lock_bh(&efx->filter_lock);
3695 if (rc == 0) {
3696 kfree(spec);
3697 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
3698 }
3699 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
3700 wake_up_all(&table->waitq);
3701 spin_unlock_bh(&efx->filter_lock);
3702}
3703
3704#endif /* CONFIG_RFS_ACCEL */
3705
3706static int efx_ef10_filter_match_flags_from_mcdi(u32 mcdi_flags)
3707{
3708 int match_flags = 0;
3709
3710#define MAP_FLAG(gen_flag, mcdi_field) { \
3711 u32 old_mcdi_flags = mcdi_flags; \
3712 mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
3713 mcdi_field ## _LBN); \
3714 if (mcdi_flags != old_mcdi_flags) \
3715 match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
3716 }
3717 MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
3718 MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
3719 MAP_FLAG(REM_HOST, SRC_IP);
3720 MAP_FLAG(LOC_HOST, DST_IP);
3721 MAP_FLAG(REM_MAC, SRC_MAC);
3722 MAP_FLAG(REM_PORT, SRC_PORT);
3723 MAP_FLAG(LOC_MAC, DST_MAC);
3724 MAP_FLAG(LOC_PORT, DST_PORT);
3725 MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
3726 MAP_FLAG(INNER_VID, INNER_VLAN);
3727 MAP_FLAG(OUTER_VID, OUTER_VLAN);
3728 MAP_FLAG(IP_PROTO, IP_PROTO);
3729#undef MAP_FLAG
3730
3731 /* Did we map them all? */
3732 if (mcdi_flags)
3733 return -EINVAL;
3734
3735 return match_flags;
3736}
3737
3738static int efx_ef10_filter_table_probe(struct efx_nic *efx)
3739{
3740 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
3741 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
3742 unsigned int pd_match_pri, pd_match_count;
3743 struct efx_ef10_filter_table *table;
dc3273e0 3744 struct efx_ef10_filter_vlan *vlan;
6a37958b 3745 unsigned int i;
8127d661
BH
3746 size_t outlen;
3747 int rc;
3748
dd98708c
EC
3749 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
3750 return -EINVAL;
3751
3752 if (efx->filter_state) /* already probed */
3753 return 0;
3754
8127d661
BH
3755 table = kzalloc(sizeof(*table), GFP_KERNEL);
3756 if (!table)
3757 return -ENOMEM;
3758
3759 /* Find out which RX filter types are supported, and their priorities */
3760 MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
3761 MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
3762 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
3763 inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
3764 &outlen);
3765 if (rc)
3766 goto fail;
3767 pd_match_count = MCDI_VAR_ARRAY_LEN(
3768 outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
3769 table->rx_match_count = 0;
3770
3771 for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
3772 u32 mcdi_flags =
3773 MCDI_ARRAY_DWORD(
3774 outbuf,
3775 GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
3776 pd_match_pri);
3777 rc = efx_ef10_filter_match_flags_from_mcdi(mcdi_flags);
3778 if (rc < 0) {
3779 netif_dbg(efx, probe, efx->net_dev,
3780 "%s: fw flags %#x pri %u not supported in driver\n",
3781 __func__, mcdi_flags, pd_match_pri);
3782 } else {
3783 netif_dbg(efx, probe, efx->net_dev,
3784 "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
3785 __func__, mcdi_flags, pd_match_pri,
3786 rc, table->rx_match_count);
3787 table->rx_match_flags[table->rx_match_count++] = rc;
3788 }
3789 }
3790
3791 table->entry = vzalloc(HUNT_FILTER_TBL_ROWS * sizeof(*table->entry));
3792 if (!table->entry) {
3793 rc = -ENOMEM;
3794 goto fail;
3795 }
3796
dc3273e0
AR
3797 vlan = &table->vlan;
3798 for (i = 0; i < ARRAY_SIZE(vlan->uc); i++)
3799 vlan->uc[i] = EFX_EF10_FILTER_ID_INVALID;
3800 for (i = 0; i < ARRAY_SIZE(vlan->mc); i++)
3801 vlan->mc[i] = EFX_EF10_FILTER_ID_INVALID;
3802 vlan->ucdef = EFX_EF10_FILTER_ID_INVALID;
3803 vlan->bcast = EFX_EF10_FILTER_ID_INVALID;
3804 vlan->mcdef = EFX_EF10_FILTER_ID_INVALID;
b071c3a2 3805 table->mc_promisc_last = false;
12fb0da4 3806
8127d661
BH
3807 efx->filter_state = table;
3808 init_waitqueue_head(&table->waitq);
3809 return 0;
3810
3811fail:
3812 kfree(table);
3813 return rc;
3814}
3815
0d322413
EC
3816/* Caller must hold efx->filter_sem for read if race against
3817 * efx_ef10_filter_table_remove() is possible
3818 */
8127d661
BH
3819static void efx_ef10_filter_table_restore(struct efx_nic *efx)
3820{
3821 struct efx_ef10_filter_table *table = efx->filter_state;
3822 struct efx_ef10_nic_data *nic_data = efx->nic_data;
3823 struct efx_filter_spec *spec;
3824 unsigned int filter_idx;
3825 bool failed = false;
3826 int rc;
3827
0d322413
EC
3828 WARN_ON(!rwsem_is_locked(&efx->filter_sem));
3829
8127d661
BH
3830 if (!nic_data->must_restore_filters)
3831 return;
3832
0d322413
EC
3833 if (!table)
3834 return;
3835
8127d661
BH
3836 spin_lock_bh(&efx->filter_lock);
3837
3838 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
3839 spec = efx_ef10_filter_entry_spec(table, filter_idx);
3840 if (!spec)
3841 continue;
3842
3843 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
3844 spin_unlock_bh(&efx->filter_lock);
3845
3846 rc = efx_ef10_filter_push(efx, spec,
3847 &table->entry[filter_idx].handle,
3848 false);
3849 if (rc)
3850 failed = true;
3851
3852 spin_lock_bh(&efx->filter_lock);
3853 if (rc) {
3854 kfree(spec);
3855 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
3856 } else {
3857 table->entry[filter_idx].spec &=
3858 ~EFX_EF10_FILTER_FLAG_BUSY;
3859 }
3860 }
3861
3862 spin_unlock_bh(&efx->filter_lock);
3863
3864 if (failed)
3865 netif_err(efx, hw, efx->net_dev,
3866 "unable to restore all filters\n");
3867 else
3868 nic_data->must_restore_filters = false;
3869}
3870
3871static void efx_ef10_filter_table_remove(struct efx_nic *efx)
3872{
3873 struct efx_ef10_filter_table *table = efx->filter_state;
3874 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
3875 struct efx_filter_spec *spec;
3876 unsigned int filter_idx;
3877 int rc;
3878
0d322413 3879 efx->filter_state = NULL;
dd98708c
EC
3880 /* If we were called without locking, then it's not safe to free
3881 * the table as others might be using it. So we just WARN, leak
3882 * the memory, and potentially get an inconsistent filter table
3883 * state.
3884 * This should never actually happen.
3885 */
3886 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
3887 return;
3888
0d322413
EC
3889 if (!table)
3890 return;
3891
8127d661
BH
3892 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
3893 spec = efx_ef10_filter_entry_spec(table, filter_idx);
3894 if (!spec)
3895 continue;
3896
3897 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
3898 efx_ef10_filter_is_exclusive(spec) ?
3899 MC_CMD_FILTER_OP_IN_OP_REMOVE :
3900 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
3901 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
3902 table->entry[filter_idx].handle);
e65a5109
BK
3903 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FILTER_OP, inbuf,
3904 sizeof(inbuf), NULL, 0, NULL);
48ce5634 3905 if (rc)
e65a5109
BK
3906 netif_info(efx, drv, efx->net_dev,
3907 "%s: filter %04x remove failed\n",
3908 __func__, filter_idx);
8127d661
BH
3909 kfree(spec);
3910 }
3911
3912 vfree(table->entry);
3913 kfree(table);
3914}
3915
6a37958b
AR
3916static void efx_ef10_filter_mark_one_old(struct efx_nic *efx, uint16_t *id)
3917{
3918 struct efx_ef10_filter_table *table = efx->filter_state;
3919 unsigned int filter_idx;
3920
3921 if (*id != EFX_EF10_FILTER_ID_INVALID) {
3922 filter_idx = efx_ef10_filter_get_unsafe_id(efx, *id);
3923 if (!table->entry[filter_idx].spec)
3924 netif_dbg(efx, drv, efx->net_dev,
3925 "marked null spec old %04x:%04x\n", *id,
3926 filter_idx);
3927 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_AUTO_OLD;
3928 *id = EFX_EF10_FILTER_ID_INVALID;
e65a5109 3929 }
6a37958b
AR
3930}
3931
822b96f8 3932static void efx_ef10_filter_mark_old(struct efx_nic *efx)
8127d661
BH
3933{
3934 struct efx_ef10_filter_table *table = efx->filter_state;
dc3273e0 3935 struct efx_ef10_filter_vlan *vlan = &table->vlan;
6a37958b 3936 unsigned int i;
8127d661 3937
0d322413
EC
3938 if (!table)
3939 return;
3940
8127d661
BH
3941 /* Mark old filters that may need to be removed */
3942 spin_lock_bh(&efx->filter_lock);
12fb0da4 3943 for (i = 0; i < table->dev_uc_count; i++)
dc3273e0 3944 efx_ef10_filter_mark_one_old(efx, &vlan->uc[i]);
12fb0da4 3945 for (i = 0; i < table->dev_mc_count; i++)
dc3273e0
AR
3946 efx_ef10_filter_mark_one_old(efx, &vlan->mc[i]);
3947 efx_ef10_filter_mark_one_old(efx, &vlan->ucdef);
3948 efx_ef10_filter_mark_one_old(efx, &vlan->bcast);
3949 efx_ef10_filter_mark_one_old(efx, &vlan->mcdef);
8127d661 3950 spin_unlock_bh(&efx->filter_lock);
822b96f8
DP
3951}
3952
afa4ce12 3953static void efx_ef10_filter_uc_addr_list(struct efx_nic *efx)
822b96f8
DP
3954{
3955 struct efx_ef10_filter_table *table = efx->filter_state;
3956 struct net_device *net_dev = efx->net_dev;
3957 struct netdev_hw_addr *uc;
12fb0da4 3958 int addr_count;
822b96f8 3959 unsigned int i;
8127d661 3960
12fb0da4 3961 addr_count = netdev_uc_count(net_dev);
afa4ce12 3962 table->uc_promisc = !!(net_dev->flags & IFF_PROMISC);
12fb0da4 3963 table->dev_uc_count = 1 + addr_count;
822b96f8
DP
3964 ether_addr_copy(table->dev_uc_list[0].addr, net_dev->dev_addr);
3965 i = 1;
3966 netdev_for_each_uc_addr(uc, net_dev) {
12fb0da4 3967 if (i >= EFX_EF10_FILTER_DEV_UC_MAX) {
afa4ce12 3968 table->uc_promisc = true;
12fb0da4
EC
3969 break;
3970 }
822b96f8
DP
3971 ether_addr_copy(table->dev_uc_list[i].addr, uc->addr);
3972 i++;
3973 }
3974}
3975
afa4ce12 3976static void efx_ef10_filter_mc_addr_list(struct efx_nic *efx)
822b96f8
DP
3977{
3978 struct efx_ef10_filter_table *table = efx->filter_state;
3979 struct net_device *net_dev = efx->net_dev;
3980 struct netdev_hw_addr *mc;
ab8b1f7c 3981 unsigned int i, addr_count;
822b96f8 3982
afa4ce12 3983 table->mc_promisc = !!(net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI));
ab8b1f7c 3984
12fb0da4
EC
3985 addr_count = netdev_mc_count(net_dev);
3986 i = 0;
ab8b1f7c 3987 netdev_for_each_mc_addr(mc, net_dev) {
12fb0da4 3988 if (i >= EFX_EF10_FILTER_DEV_MC_MAX) {
afa4ce12 3989 table->mc_promisc = true;
12fb0da4
EC
3990 break;
3991 }
ab8b1f7c
DP
3992 ether_addr_copy(table->dev_mc_list[i].addr, mc->addr);
3993 i++;
8127d661 3994 }
12fb0da4
EC
3995
3996 table->dev_mc_count = i;
822b96f8 3997}
8127d661 3998
12fb0da4
EC
3999static int efx_ef10_filter_insert_addr_list(struct efx_nic *efx,
4000 bool multicast, bool rollback)
822b96f8
DP
4001{
4002 struct efx_ef10_filter_table *table = efx->filter_state;
dc3273e0 4003 struct efx_ef10_filter_vlan *vlan = &table->vlan;
822b96f8 4004 struct efx_ef10_dev_addr *addr_list;
f1c2ef40 4005 enum efx_filter_flags filter_flags;
822b96f8 4006 struct efx_filter_spec spec;
12fb0da4
EC
4007 u8 baddr[ETH_ALEN];
4008 unsigned int i, j;
4009 int addr_count;
dc3273e0 4010 u16 *ids;
822b96f8
DP
4011 int rc;
4012
4013 if (multicast) {
4014 addr_list = table->dev_mc_list;
12fb0da4 4015 addr_count = table->dev_mc_count;
dc3273e0 4016 ids = vlan->mc;
822b96f8
DP
4017 } else {
4018 addr_list = table->dev_uc_list;
12fb0da4 4019 addr_count = table->dev_uc_count;
dc3273e0 4020 ids = vlan->uc;
8127d661
BH
4021 }
4022
f1c2ef40
BK
4023 filter_flags = efx_rss_enabled(efx) ? EFX_FILTER_FLAG_RX_RSS : 0;
4024
822b96f8 4025 /* Insert/renew filters */
12fb0da4 4026 for (i = 0; i < addr_count; i++) {
f1c2ef40 4027 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO, filter_flags, 0);
b6f568e2 4028 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
822b96f8 4029 addr_list[i].addr);
b6f568e2
JC
4030 rc = efx_ef10_filter_insert(efx, &spec, true);
4031 if (rc < 0) {
12fb0da4
EC
4032 if (rollback) {
4033 netif_info(efx, drv, efx->net_dev,
4034 "efx_ef10_filter_insert failed rc=%d\n",
4035 rc);
4036 /* Fall back to promiscuous */
4037 for (j = 0; j < i; j++) {
dc3273e0 4038 if (ids[j] == EFX_EF10_FILTER_ID_INVALID)
12fb0da4
EC
4039 continue;
4040 efx_ef10_filter_remove_unsafe(
4041 efx, EFX_FILTER_PRI_AUTO,
dc3273e0
AR
4042 ids[j]);
4043 ids[j] = EFX_EF10_FILTER_ID_INVALID;
12fb0da4
EC
4044 }
4045 return rc;
4046 } else {
4047 /* mark as not inserted, and carry on */
4048 rc = EFX_EF10_FILTER_ID_INVALID;
822b96f8 4049 }
8127d661 4050 }
dc3273e0 4051 ids[i] = efx_ef10_filter_get_unsafe_id(efx, rc);
8127d661 4052 }
822b96f8 4053
12fb0da4
EC
4054 if (multicast && rollback) {
4055 /* Also need an Ethernet broadcast filter */
f1c2ef40 4056 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO, filter_flags, 0);
12fb0da4
EC
4057 eth_broadcast_addr(baddr);
4058 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC, baddr);
8127d661 4059 rc = efx_ef10_filter_insert(efx, &spec, true);
12fb0da4 4060 if (rc < 0) {
822b96f8 4061 netif_warn(efx, drv, efx->net_dev,
12fb0da4
EC
4062 "Broadcast filter insert failed rc=%d\n", rc);
4063 /* Fall back to promiscuous */
4064 for (j = 0; j < i; j++) {
dc3273e0 4065 if (ids[j] == EFX_EF10_FILTER_ID_INVALID)
12fb0da4
EC
4066 continue;
4067 efx_ef10_filter_remove_unsafe(
4068 efx, EFX_FILTER_PRI_AUTO,
dc3273e0
AR
4069 ids[j]);
4070 ids[j] = EFX_EF10_FILTER_ID_INVALID;
12fb0da4
EC
4071 }
4072 return rc;
4073 } else {
dc3273e0 4074 EFX_WARN_ON_PARANOID(vlan->bcast !=
6a37958b 4075 EFX_EF10_FILTER_ID_INVALID);
dc3273e0 4076 vlan->bcast = efx_ef10_filter_get_unsafe_id(efx, rc);
12fb0da4 4077 }
8127d661 4078 }
12fb0da4
EC
4079
4080 return 0;
4081}
4082
4083static int efx_ef10_filter_insert_def(struct efx_nic *efx, bool multicast,
4084 bool rollback)
4085{
4086 struct efx_ef10_filter_table *table = efx->filter_state;
4087 struct efx_ef10_nic_data *nic_data = efx->nic_data;
dc3273e0 4088 struct efx_ef10_filter_vlan *vlan = &table->vlan;
f1c2ef40 4089 enum efx_filter_flags filter_flags;
12fb0da4
EC
4090 struct efx_filter_spec spec;
4091 u8 baddr[ETH_ALEN];
4092 int rc;
4093
f1c2ef40
BK
4094 filter_flags = efx_rss_enabled(efx) ? EFX_FILTER_FLAG_RX_RSS : 0;
4095
4096 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO, filter_flags, 0);
12fb0da4
EC
4097
4098 if (multicast)
4099 efx_filter_set_mc_def(&spec);
4100 else
4101 efx_filter_set_uc_def(&spec);
4102
4103 rc = efx_ef10_filter_insert(efx, &spec, true);
4104 if (rc < 0) {
09a04204
BK
4105 netif_printk(efx, drv, rc == -EPERM ? KERN_DEBUG : KERN_WARNING,
4106 efx->net_dev,
4107 "%scast mismatch filter insert failed rc=%d\n",
4108 multicast ? "Multi" : "Uni", rc);
12fb0da4 4109 } else if (multicast) {
dc3273e0
AR
4110 EFX_WARN_ON_PARANOID(vlan->mcdef != EFX_EF10_FILTER_ID_INVALID);
4111 vlan->mcdef = efx_ef10_filter_get_unsafe_id(efx, rc);
12fb0da4
EC
4112 if (!nic_data->workaround_26807) {
4113 /* Also need an Ethernet broadcast filter */
4114 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
f1c2ef40 4115 filter_flags, 0);
12fb0da4
EC
4116 eth_broadcast_addr(baddr);
4117 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
4118 baddr);
4119 rc = efx_ef10_filter_insert(efx, &spec, true);
4120 if (rc < 0) {
4121 netif_warn(efx, drv, efx->net_dev,
4122 "Broadcast filter insert failed rc=%d\n",
4123 rc);
4124 if (rollback) {
4125 /* Roll back the mc_def filter */
4126 efx_ef10_filter_remove_unsafe(
4127 efx, EFX_FILTER_PRI_AUTO,
dc3273e0
AR
4128 vlan->mcdef);
4129 vlan->mcdef = EFX_EF10_FILTER_ID_INVALID;
12fb0da4
EC
4130 return rc;
4131 }
4132 } else {
dc3273e0 4133 EFX_WARN_ON_PARANOID(vlan->bcast !=
6a37958b 4134 EFX_EF10_FILTER_ID_INVALID);
dc3273e0 4135 vlan->bcast = efx_ef10_filter_get_unsafe_id(efx, rc);
12fb0da4
EC
4136 }
4137 }
4138 rc = 0;
4139 } else {
dc3273e0
AR
4140 EFX_WARN_ON_PARANOID(vlan->ucdef != EFX_EF10_FILTER_ID_INVALID);
4141 vlan->ucdef = rc;
12fb0da4
EC
4142 rc = 0;
4143 }
4144 return rc;
822b96f8
DP
4145}
4146
4147/* Remove filters that weren't renewed. Since nothing else changes the AUTO_OLD
4148 * flag or removes these filters, we don't need to hold the filter_lock while
4149 * scanning for these filters.
4150 */
4151static void efx_ef10_filter_remove_old(struct efx_nic *efx)
4152{
4153 struct efx_ef10_filter_table *table = efx->filter_state;
e65a5109
BK
4154 int remove_failed = 0;
4155 int remove_noent = 0;
4156 int rc;
822b96f8 4157 int i;
8127d661 4158
8127d661
BH
4159 for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
4160 if (ACCESS_ONCE(table->entry[i].spec) &
b59e6ef8 4161 EFX_EF10_FILTER_FLAG_AUTO_OLD) {
e65a5109
BK
4162 rc = efx_ef10_filter_remove_internal(efx,
4163 1U << EFX_FILTER_PRI_AUTO, i, true);
4164 if (rc == -ENOENT)
4165 remove_noent++;
4166 else if (rc)
4167 remove_failed++;
8127d661
BH
4168 }
4169 }
e65a5109
BK
4170
4171 if (remove_failed)
4172 netif_info(efx, drv, efx->net_dev,
4173 "%s: failed to remove %d filters\n",
4174 __func__, remove_failed);
4175 if (remove_noent)
4176 netif_info(efx, drv, efx->net_dev,
4177 "%s: failed to remove %d non-existent filters\n",
4178 __func__, remove_noent);
8127d661
BH
4179}
4180
7a186f47
DP
4181static int efx_ef10_vport_set_mac_address(struct efx_nic *efx)
4182{
4183 struct efx_ef10_nic_data *nic_data = efx->nic_data;
4184 u8 mac_old[ETH_ALEN];
4185 int rc, rc2;
4186
4187 /* Only reconfigure a PF-created vport */
4188 if (is_zero_ether_addr(nic_data->vport_mac))
4189 return 0;
4190
4191 efx_device_detach_sync(efx);
4192 efx_net_stop(efx->net_dev);
4193 down_write(&efx->filter_sem);
4194 efx_ef10_filter_table_remove(efx);
4195 up_write(&efx->filter_sem);
4196
4197 rc = efx_ef10_vadaptor_free(efx, nic_data->vport_id);
4198 if (rc)
4199 goto restore_filters;
4200
4201 ether_addr_copy(mac_old, nic_data->vport_mac);
4202 rc = efx_ef10_vport_del_mac(efx, nic_data->vport_id,
4203 nic_data->vport_mac);
4204 if (rc)
4205 goto restore_vadaptor;
4206
4207 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id,
4208 efx->net_dev->dev_addr);
4209 if (!rc) {
4210 ether_addr_copy(nic_data->vport_mac, efx->net_dev->dev_addr);
4211 } else {
4212 rc2 = efx_ef10_vport_add_mac(efx, nic_data->vport_id, mac_old);
4213 if (rc2) {
4214 /* Failed to add original MAC, so clear vport_mac */
4215 eth_zero_addr(nic_data->vport_mac);
4216 goto reset_nic;
4217 }
4218 }
4219
4220restore_vadaptor:
4221 rc2 = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
4222 if (rc2)
4223 goto reset_nic;
4224restore_filters:
4225 down_write(&efx->filter_sem);
4226 rc2 = efx_ef10_filter_table_probe(efx);
4227 up_write(&efx->filter_sem);
4228 if (rc2)
4229 goto reset_nic;
4230
4231 rc2 = efx_net_open(efx->net_dev);
4232 if (rc2)
4233 goto reset_nic;
4234
4235 netif_device_attach(efx->net_dev);
4236
4237 return rc;
4238
4239reset_nic:
4240 netif_err(efx, drv, efx->net_dev,
4241 "Failed to restore when changing MAC address - scheduling reset\n");
4242 efx_schedule_reset(efx, RESET_TYPE_DATAPATH);
4243
4244 return rc ? rc : rc2;
4245}
4246
822b96f8
DP
4247/* Caller must hold efx->filter_sem for read if race against
4248 * efx_ef10_filter_table_remove() is possible
4249 */
4250static void efx_ef10_filter_sync_rx_mode(struct efx_nic *efx)
4251{
4252 struct efx_ef10_filter_table *table = efx->filter_state;
ab8b1f7c 4253 struct efx_ef10_nic_data *nic_data = efx->nic_data;
822b96f8 4254 struct net_device *net_dev = efx->net_dev;
822b96f8
DP
4255
4256 if (!efx_dev_registered(efx))
4257 return;
4258
4259 if (!table)
4260 return;
4261
4262 efx_ef10_filter_mark_old(efx);
4263
4264 /* Copy/convert the address lists; add the primary station
4265 * address and broadcast address
4266 */
4267 netif_addr_lock_bh(net_dev);
afa4ce12
AR
4268 efx_ef10_filter_uc_addr_list(efx);
4269 efx_ef10_filter_mc_addr_list(efx);
822b96f8
DP
4270 netif_addr_unlock_bh(net_dev);
4271
12fb0da4 4272 /* Insert/renew unicast filters */
afa4ce12 4273 if (table->uc_promisc) {
12fb0da4
EC
4274 efx_ef10_filter_insert_def(efx, false, false);
4275 efx_ef10_filter_insert_addr_list(efx, false, false);
4276 } else {
4277 /* If any of the filters failed to insert, fall back to
4278 * promiscuous mode - add in the uc_def filter. But keep
4279 * our individual unicast filters.
4280 */
4281 if (efx_ef10_filter_insert_addr_list(efx, false, false))
4282 efx_ef10_filter_insert_def(efx, false, false);
4283 }
ab8b1f7c 4284
12fb0da4 4285 /* Insert/renew multicast filters */
ab8b1f7c
DP
4286 /* If changing promiscuous state with cascaded multicast filters, remove
4287 * old filters first, so that packets are dropped rather than duplicated
4288 */
afa4ce12
AR
4289 if (nic_data->workaround_26807 &&
4290 table->mc_promisc_last != table->mc_promisc)
ab8b1f7c 4291 efx_ef10_filter_remove_old(efx);
afa4ce12 4292 if (table->mc_promisc) {
12fb0da4
EC
4293 if (nic_data->workaround_26807) {
4294 /* If we failed to insert promiscuous filters, rollback
4295 * and fall back to individual multicast filters
4296 */
4297 if (efx_ef10_filter_insert_def(efx, true, true)) {
4298 /* Changing promisc state, so remove old filters */
4299 efx_ef10_filter_remove_old(efx);
4300 efx_ef10_filter_insert_addr_list(efx, true, false);
4301 }
4302 } else {
4303 /* If we failed to insert promiscuous filters, don't
4304 * rollback. Regardless, also insert the mc_list
4305 */
4306 efx_ef10_filter_insert_def(efx, true, false);
4307 efx_ef10_filter_insert_addr_list(efx, true, false);
4308 }
4309 } else {
4310 /* If any filters failed to insert, rollback and fall back to
4311 * promiscuous mode - mc_def filter and maybe broadcast. If
4312 * that fails, roll back again and insert as many of our
4313 * individual multicast filters as we can.
4314 */
4315 if (efx_ef10_filter_insert_addr_list(efx, true, true)) {
4316 /* Changing promisc state, so remove old filters */
4317 if (nic_data->workaround_26807)
4318 efx_ef10_filter_remove_old(efx);
4319 if (efx_ef10_filter_insert_def(efx, true, true))
4320 efx_ef10_filter_insert_addr_list(efx, true, false);
4321 }
4322 }
822b96f8
DP
4323
4324 efx_ef10_filter_remove_old(efx);
afa4ce12 4325 table->mc_promisc_last = table->mc_promisc;
822b96f8
DP
4326}
4327
910c8789
SS
4328static int efx_ef10_set_mac_address(struct efx_nic *efx)
4329{
4330 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_SET_MAC_IN_LEN);
4331 struct efx_ef10_nic_data *nic_data = efx->nic_data;
4332 bool was_enabled = efx->port_enabled;
4333 int rc;
4334
4335 efx_device_detach_sync(efx);
4336 efx_net_stop(efx->net_dev);
4337 down_write(&efx->filter_sem);
4338 efx_ef10_filter_table_remove(efx);
4339
4340 ether_addr_copy(MCDI_PTR(inbuf, VADAPTOR_SET_MAC_IN_MACADDR),
4341 efx->net_dev->dev_addr);
4342 MCDI_SET_DWORD(inbuf, VADAPTOR_SET_MAC_IN_UPSTREAM_PORT_ID,
4343 nic_data->vport_id);
535a6177
DP
4344 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VADAPTOR_SET_MAC, inbuf,
4345 sizeof(inbuf), NULL, 0, NULL);
910c8789
SS
4346
4347 efx_ef10_filter_table_probe(efx);
4348 up_write(&efx->filter_sem);
4349 if (was_enabled)
4350 efx_net_open(efx->net_dev);
4351 netif_device_attach(efx->net_dev);
4352
9e9f665a
DP
4353#ifdef CONFIG_SFC_SRIOV
4354 if (efx->pci_dev->is_virtfn && efx->pci_dev->physfn) {
910c8789
SS
4355 struct pci_dev *pci_dev_pf = efx->pci_dev->physfn;
4356
9e9f665a
DP
4357 if (rc == -EPERM) {
4358 struct efx_nic *efx_pf;
910c8789 4359
9e9f665a
DP
4360 /* Switch to PF and change MAC address on vport */
4361 efx_pf = pci_get_drvdata(pci_dev_pf);
910c8789 4362
9e9f665a
DP
4363 rc = efx_ef10_sriov_set_vf_mac(efx_pf,
4364 nic_data->vf_index,
4365 efx->net_dev->dev_addr);
4366 } else if (!rc) {
910c8789
SS
4367 struct efx_nic *efx_pf = pci_get_drvdata(pci_dev_pf);
4368 struct efx_ef10_nic_data *nic_data = efx_pf->nic_data;
4369 unsigned int i;
4370
9e9f665a
DP
4371 /* MAC address successfully changed by VF (with MAC
4372 * spoofing) so update the parent PF if possible.
4373 */
910c8789
SS
4374 for (i = 0; i < efx_pf->vf_count; ++i) {
4375 struct ef10_vf *vf = nic_data->vf + i;
4376
4377 if (vf->efx == efx) {
4378 ether_addr_copy(vf->mac,
4379 efx->net_dev->dev_addr);
4380 return 0;
4381 }
4382 }
4383 }
9e9f665a 4384 } else
910c8789 4385#endif
9e9f665a
DP
4386 if (rc == -EPERM) {
4387 netif_err(efx, drv, efx->net_dev,
4388 "Cannot change MAC address; use sfboot to enable"
4389 " mac-spoofing on this interface\n");
7a186f47
DP
4390 } else if (rc == -ENOSYS && !efx_ef10_is_vf(efx)) {
4391 /* If the active MCFW does not support MC_CMD_VADAPTOR_SET_MAC
4392 * fall-back to the method of changing the MAC address on the
4393 * vport. This only applies to PFs because such versions of
4394 * MCFW do not support VFs.
4395 */
4396 rc = efx_ef10_vport_set_mac_address(efx);
535a6177
DP
4397 } else {
4398 efx_mcdi_display_error(efx, MC_CMD_VADAPTOR_SET_MAC,
4399 sizeof(inbuf), NULL, 0, rc);
9e9f665a
DP
4400 }
4401
910c8789
SS
4402 return rc;
4403}
4404
8127d661
BH
4405static int efx_ef10_mac_reconfigure(struct efx_nic *efx)
4406{
4407 efx_ef10_filter_sync_rx_mode(efx);
4408
4409 return efx_mcdi_set_mac(efx);
4410}
4411
862f894c
SS
4412static int efx_ef10_mac_reconfigure_vf(struct efx_nic *efx)
4413{
4414 efx_ef10_filter_sync_rx_mode(efx);
4415
4416 return 0;
4417}
4418
74cd60a4
JC
4419static int efx_ef10_start_bist(struct efx_nic *efx, u32 bist_type)
4420{
4421 MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
4422
4423 MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_type);
4424 return efx_mcdi_rpc(efx, MC_CMD_START_BIST, inbuf, sizeof(inbuf),
4425 NULL, 0, NULL);
4426}
4427
4428/* MC BISTs follow a different poll mechanism to phy BISTs.
4429 * The BIST is done in the poll handler on the MC, and the MCDI command
4430 * will block until the BIST is done.
4431 */
4432static int efx_ef10_poll_bist(struct efx_nic *efx)
4433{
4434 int rc;
4435 MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_LEN);
4436 size_t outlen;
4437 u32 result;
4438
4439 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
4440 outbuf, sizeof(outbuf), &outlen);
4441 if (rc != 0)
4442 return rc;
4443
4444 if (outlen < MC_CMD_POLL_BIST_OUT_LEN)
4445 return -EIO;
4446
4447 result = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
4448 switch (result) {
4449 case MC_CMD_POLL_BIST_PASSED:
4450 netif_dbg(efx, hw, efx->net_dev, "BIST passed.\n");
4451 return 0;
4452 case MC_CMD_POLL_BIST_TIMEOUT:
4453 netif_err(efx, hw, efx->net_dev, "BIST timed out\n");
4454 return -EIO;
4455 case MC_CMD_POLL_BIST_FAILED:
4456 netif_err(efx, hw, efx->net_dev, "BIST failed.\n");
4457 return -EIO;
4458 default:
4459 netif_err(efx, hw, efx->net_dev,
4460 "BIST returned unknown result %u", result);
4461 return -EIO;
4462 }
4463}
4464
4465static int efx_ef10_run_bist(struct efx_nic *efx, u32 bist_type)
4466{
4467 int rc;
4468
4469 netif_dbg(efx, drv, efx->net_dev, "starting BIST type %u\n", bist_type);
4470
4471 rc = efx_ef10_start_bist(efx, bist_type);
4472 if (rc != 0)
4473 return rc;
4474
4475 return efx_ef10_poll_bist(efx);
4476}
4477
4478static int
4479efx_ef10_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
4480{
4481 int rc, rc2;
4482
4483 efx_reset_down(efx, RESET_TYPE_WORLD);
4484
4485 rc = efx_mcdi_rpc(efx, MC_CMD_ENABLE_OFFLINE_BIST,
4486 NULL, 0, NULL, 0, NULL);
4487 if (rc != 0)
4488 goto out;
4489
4490 tests->memory = efx_ef10_run_bist(efx, MC_CMD_MC_MEM_BIST) ? -1 : 1;
4491 tests->registers = efx_ef10_run_bist(efx, MC_CMD_REG_BIST) ? -1 : 1;
4492
4493 rc = efx_mcdi_reset(efx, RESET_TYPE_WORLD);
4494
4495out:
27324820
DP
4496 if (rc == -EPERM)
4497 rc = 0;
74cd60a4
JC
4498 rc2 = efx_reset_up(efx, RESET_TYPE_WORLD, rc == 0);
4499 return rc ? rc : rc2;
4500}
4501
8127d661
BH
4502#ifdef CONFIG_SFC_MTD
4503
4504struct efx_ef10_nvram_type_info {
4505 u16 type, type_mask;
4506 u8 port;
4507 const char *name;
4508};
4509
4510static const struct efx_ef10_nvram_type_info efx_ef10_nvram_types[] = {
4511 { NVRAM_PARTITION_TYPE_MC_FIRMWARE, 0, 0, "sfc_mcfw" },
4512 { NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 0, 0, "sfc_mcfw_backup" },
4513 { NVRAM_PARTITION_TYPE_EXPANSION_ROM, 0, 0, "sfc_exp_rom" },
4514 { NVRAM_PARTITION_TYPE_STATIC_CONFIG, 0, 0, "sfc_static_cfg" },
4515 { NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 0, 0, "sfc_dynamic_cfg" },
4516 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 0, 0, "sfc_exp_rom_cfg" },
4517 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 0, 1, "sfc_exp_rom_cfg" },
4518 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 0, 2, "sfc_exp_rom_cfg" },
4519 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 0, 3, "sfc_exp_rom_cfg" },
a84f3bf9 4520 { NVRAM_PARTITION_TYPE_LICENSE, 0, 0, "sfc_license" },
8127d661
BH
4521 { NVRAM_PARTITION_TYPE_PHY_MIN, 0xff, 0, "sfc_phy_fw" },
4522};
4523
4524static int efx_ef10_mtd_probe_partition(struct efx_nic *efx,
4525 struct efx_mcdi_mtd_partition *part,
4526 unsigned int type)
4527{
4528 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_METADATA_IN_LEN);
4529 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_METADATA_OUT_LENMAX);
4530 const struct efx_ef10_nvram_type_info *info;
4531 size_t size, erase_size, outlen;
4532 bool protected;
4533 int rc;
4534
4535 for (info = efx_ef10_nvram_types; ; info++) {
4536 if (info ==
4537 efx_ef10_nvram_types + ARRAY_SIZE(efx_ef10_nvram_types))
4538 return -ENODEV;
4539 if ((type & ~info->type_mask) == info->type)
4540 break;
4541 }
4542 if (info->port != efx_port_num(efx))
4543 return -ENODEV;
4544
4545 rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
4546 if (rc)
4547 return rc;
4548 if (protected)
4549 return -ENODEV; /* hide it */
4550
4551 part->nvram_type = type;
4552
4553 MCDI_SET_DWORD(inbuf, NVRAM_METADATA_IN_TYPE, type);
4554 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_METADATA, inbuf, sizeof(inbuf),
4555 outbuf, sizeof(outbuf), &outlen);
4556 if (rc)
4557 return rc;
4558 if (outlen < MC_CMD_NVRAM_METADATA_OUT_LENMIN)
4559 return -EIO;
4560 if (MCDI_DWORD(outbuf, NVRAM_METADATA_OUT_FLAGS) &
4561 (1 << MC_CMD_NVRAM_METADATA_OUT_SUBTYPE_VALID_LBN))
4562 part->fw_subtype = MCDI_DWORD(outbuf,
4563 NVRAM_METADATA_OUT_SUBTYPE);
4564
4565 part->common.dev_type_name = "EF10 NVRAM manager";
4566 part->common.type_name = info->name;
4567
4568 part->common.mtd.type = MTD_NORFLASH;
4569 part->common.mtd.flags = MTD_CAP_NORFLASH;
4570 part->common.mtd.size = size;
4571 part->common.mtd.erasesize = erase_size;
4572
4573 return 0;
4574}
4575
4576static int efx_ef10_mtd_probe(struct efx_nic *efx)
4577{
4578 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_PARTITIONS_OUT_LENMAX);
4579 struct efx_mcdi_mtd_partition *parts;
4580 size_t outlen, n_parts_total, i, n_parts;
4581 unsigned int type;
4582 int rc;
4583
4584 ASSERT_RTNL();
4585
4586 BUILD_BUG_ON(MC_CMD_NVRAM_PARTITIONS_IN_LEN != 0);
4587 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_PARTITIONS, NULL, 0,
4588 outbuf, sizeof(outbuf), &outlen);
4589 if (rc)
4590 return rc;
4591 if (outlen < MC_CMD_NVRAM_PARTITIONS_OUT_LENMIN)
4592 return -EIO;
4593
4594 n_parts_total = MCDI_DWORD(outbuf, NVRAM_PARTITIONS_OUT_NUM_PARTITIONS);
4595 if (n_parts_total >
4596 MCDI_VAR_ARRAY_LEN(outlen, NVRAM_PARTITIONS_OUT_TYPE_ID))
4597 return -EIO;
4598
4599 parts = kcalloc(n_parts_total, sizeof(*parts), GFP_KERNEL);
4600 if (!parts)
4601 return -ENOMEM;
4602
4603 n_parts = 0;
4604 for (i = 0; i < n_parts_total; i++) {
4605 type = MCDI_ARRAY_DWORD(outbuf, NVRAM_PARTITIONS_OUT_TYPE_ID,
4606 i);
4607 rc = efx_ef10_mtd_probe_partition(efx, &parts[n_parts], type);
4608 if (rc == 0)
4609 n_parts++;
4610 else if (rc != -ENODEV)
4611 goto fail;
4612 }
4613
4614 rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
4615fail:
4616 if (rc)
4617 kfree(parts);
4618 return rc;
4619}
4620
4621#endif /* CONFIG_SFC_MTD */
4622
4623static void efx_ef10_ptp_write_host_time(struct efx_nic *efx, u32 host_time)
4624{
4625 _efx_writed(efx, cpu_to_le32(host_time), ER_DZ_MC_DB_LWRD);
4626}
4627
02246a7f
SS
4628static void efx_ef10_ptp_write_host_time_vf(struct efx_nic *efx,
4629 u32 host_time) {}
4630
bd9a265d
JC
4631static int efx_ef10_rx_enable_timestamping(struct efx_channel *channel,
4632 bool temp)
4633{
4634 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_TIME_EVENT_SUBSCRIBE_LEN);
4635 int rc;
4636
4637 if (channel->sync_events_state == SYNC_EVENTS_REQUESTED ||
4638 channel->sync_events_state == SYNC_EVENTS_VALID ||
4639 (temp && channel->sync_events_state == SYNC_EVENTS_DISABLED))
4640 return 0;
4641 channel->sync_events_state = SYNC_EVENTS_REQUESTED;
4642
4643 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_TIME_EVENT_SUBSCRIBE);
4644 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
4645 MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_SUBSCRIBE_QUEUE,
4646 channel->channel);
4647
4648 rc = efx_mcdi_rpc(channel->efx, MC_CMD_PTP,
4649 inbuf, sizeof(inbuf), NULL, 0, NULL);
4650
4651 if (rc != 0)
4652 channel->sync_events_state = temp ? SYNC_EVENTS_QUIESCENT :
4653 SYNC_EVENTS_DISABLED;
4654
4655 return rc;
4656}
4657
4658static int efx_ef10_rx_disable_timestamping(struct efx_channel *channel,
4659 bool temp)
4660{
4661 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_TIME_EVENT_UNSUBSCRIBE_LEN);
4662 int rc;
4663
4664 if (channel->sync_events_state == SYNC_EVENTS_DISABLED ||
4665 (temp && channel->sync_events_state == SYNC_EVENTS_QUIESCENT))
4666 return 0;
4667 if (channel->sync_events_state == SYNC_EVENTS_QUIESCENT) {
4668 channel->sync_events_state = SYNC_EVENTS_DISABLED;
4669 return 0;
4670 }
4671 channel->sync_events_state = temp ? SYNC_EVENTS_QUIESCENT :
4672 SYNC_EVENTS_DISABLED;
4673
4674 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_TIME_EVENT_UNSUBSCRIBE);
4675 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
4676 MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_UNSUBSCRIBE_CONTROL,
4677 MC_CMD_PTP_IN_TIME_EVENT_UNSUBSCRIBE_SINGLE);
4678 MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_UNSUBSCRIBE_QUEUE,
4679 channel->channel);
4680
4681 rc = efx_mcdi_rpc(channel->efx, MC_CMD_PTP,
4682 inbuf, sizeof(inbuf), NULL, 0, NULL);
4683
4684 return rc;
4685}
4686
4687static int efx_ef10_ptp_set_ts_sync_events(struct efx_nic *efx, bool en,
4688 bool temp)
4689{
4690 int (*set)(struct efx_channel *channel, bool temp);
4691 struct efx_channel *channel;
4692
4693 set = en ?
4694 efx_ef10_rx_enable_timestamping :
4695 efx_ef10_rx_disable_timestamping;
4696
4697 efx_for_each_channel(channel, efx) {
4698 int rc = set(channel, temp);
4699 if (en && rc != 0) {
4700 efx_ef10_ptp_set_ts_sync_events(efx, false, temp);
4701 return rc;
4702 }
4703 }
4704
4705 return 0;
4706}
4707
02246a7f
SS
4708static int efx_ef10_ptp_set_ts_config_vf(struct efx_nic *efx,
4709 struct hwtstamp_config *init)
4710{
4711 return -EOPNOTSUPP;
4712}
4713
bd9a265d
JC
4714static int efx_ef10_ptp_set_ts_config(struct efx_nic *efx,
4715 struct hwtstamp_config *init)
4716{
4717 int rc;
4718
4719 switch (init->rx_filter) {
4720 case HWTSTAMP_FILTER_NONE:
4721 efx_ef10_ptp_set_ts_sync_events(efx, false, false);
4722 /* if TX timestamping is still requested then leave PTP on */
4723 return efx_ptp_change_mode(efx,
4724 init->tx_type != HWTSTAMP_TX_OFF, 0);
4725 case HWTSTAMP_FILTER_ALL:
4726 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
4727 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
4728 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
4729 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
4730 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
4731 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
4732 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
4733 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
4734 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
4735 case HWTSTAMP_FILTER_PTP_V2_EVENT:
4736 case HWTSTAMP_FILTER_PTP_V2_SYNC:
4737 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
4738 init->rx_filter = HWTSTAMP_FILTER_ALL;
4739 rc = efx_ptp_change_mode(efx, true, 0);
4740 if (!rc)
4741 rc = efx_ef10_ptp_set_ts_sync_events(efx, true, false);
4742 if (rc)
4743 efx_ptp_change_mode(efx, false, 0);
4744 return rc;
4745 default:
4746 return -ERANGE;
4747 }
4748}
4749
100a9db5
AR
4750#define EF10_OFFLOAD_FEATURES \
4751 (NETIF_F_IP_CSUM | \
4752 NETIF_F_IPV6_CSUM | \
4753 NETIF_F_RXHASH | \
4754 NETIF_F_NTUPLE)
4755
02246a7f 4756const struct efx_nic_type efx_hunt_a0_vf_nic_type = {
6f7f8aa6 4757 .is_vf = true,
02246a7f
SS
4758 .mem_bar = EFX_MEM_VF_BAR,
4759 .mem_map_size = efx_ef10_mem_map_size,
4760 .probe = efx_ef10_probe_vf,
4761 .remove = efx_ef10_remove,
4762 .dimension_resources = efx_ef10_dimension_resources,
4763 .init = efx_ef10_init_nic,
4764 .fini = efx_port_dummy_op_void,
087e9025 4765 .map_reset_reason = efx_ef10_map_reset_reason,
02246a7f
SS
4766 .map_reset_flags = efx_ef10_map_reset_flags,
4767 .reset = efx_ef10_reset,
4768 .probe_port = efx_mcdi_port_probe,
4769 .remove_port = efx_mcdi_port_remove,
4770 .fini_dmaq = efx_ef10_fini_dmaq,
4771 .prepare_flr = efx_ef10_prepare_flr,
4772 .finish_flr = efx_port_dummy_op_void,
4773 .describe_stats = efx_ef10_describe_stats,
d7788196 4774 .update_stats = efx_ef10_update_stats_vf,
02246a7f
SS
4775 .start_stats = efx_port_dummy_op_void,
4776 .pull_stats = efx_port_dummy_op_void,
4777 .stop_stats = efx_port_dummy_op_void,
4778 .set_id_led = efx_mcdi_set_id_led,
4779 .push_irq_moderation = efx_ef10_push_irq_moderation,
862f894c 4780 .reconfigure_mac = efx_ef10_mac_reconfigure_vf,
02246a7f
SS
4781 .check_mac_fault = efx_mcdi_mac_check_fault,
4782 .reconfigure_port = efx_mcdi_port_reconfigure,
4783 .get_wol = efx_ef10_get_wol_vf,
4784 .set_wol = efx_ef10_set_wol_vf,
4785 .resume_wol = efx_port_dummy_op_void,
4786 .mcdi_request = efx_ef10_mcdi_request,
4787 .mcdi_poll_response = efx_ef10_mcdi_poll_response,
4788 .mcdi_read_response = efx_ef10_mcdi_read_response,
4789 .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
c577e59e 4790 .mcdi_reboot_detected = efx_ef10_mcdi_reboot_detected,
02246a7f
SS
4791 .irq_enable_master = efx_port_dummy_op_void,
4792 .irq_test_generate = efx_ef10_irq_test_generate,
4793 .irq_disable_non_ev = efx_port_dummy_op_void,
4794 .irq_handle_msi = efx_ef10_msi_interrupt,
4795 .irq_handle_legacy = efx_ef10_legacy_interrupt,
4796 .tx_probe = efx_ef10_tx_probe,
4797 .tx_init = efx_ef10_tx_init,
4798 .tx_remove = efx_ef10_tx_remove,
4799 .tx_write = efx_ef10_tx_write,
267c0157 4800 .rx_push_rss_config = efx_ef10_vf_rx_push_rss_config,
02246a7f
SS
4801 .rx_probe = efx_ef10_rx_probe,
4802 .rx_init = efx_ef10_rx_init,
4803 .rx_remove = efx_ef10_rx_remove,
4804 .rx_write = efx_ef10_rx_write,
4805 .rx_defer_refill = efx_ef10_rx_defer_refill,
4806 .ev_probe = efx_ef10_ev_probe,
4807 .ev_init = efx_ef10_ev_init,
4808 .ev_fini = efx_ef10_ev_fini,
4809 .ev_remove = efx_ef10_ev_remove,
4810 .ev_process = efx_ef10_ev_process,
4811 .ev_read_ack = efx_ef10_ev_read_ack,
4812 .ev_test_generate = efx_ef10_ev_test_generate,
4813 .filter_table_probe = efx_ef10_filter_table_probe,
4814 .filter_table_restore = efx_ef10_filter_table_restore,
4815 .filter_table_remove = efx_ef10_filter_table_remove,
4816 .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
4817 .filter_insert = efx_ef10_filter_insert,
4818 .filter_remove_safe = efx_ef10_filter_remove_safe,
4819 .filter_get_safe = efx_ef10_filter_get_safe,
4820 .filter_clear_rx = efx_ef10_filter_clear_rx,
4821 .filter_count_rx_used = efx_ef10_filter_count_rx_used,
4822 .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
4823 .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
4824#ifdef CONFIG_RFS_ACCEL
4825 .filter_rfs_insert = efx_ef10_filter_rfs_insert,
4826 .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
4827#endif
4828#ifdef CONFIG_SFC_MTD
4829 .mtd_probe = efx_port_dummy_op_int,
4830#endif
4831 .ptp_write_host_time = efx_ef10_ptp_write_host_time_vf,
4832 .ptp_set_ts_config = efx_ef10_ptp_set_ts_config_vf,
4833#ifdef CONFIG_SFC_SRIOV
7b8c7b54
SS
4834 .vswitching_probe = efx_ef10_vswitching_probe_vf,
4835 .vswitching_restore = efx_ef10_vswitching_restore_vf,
4836 .vswitching_remove = efx_ef10_vswitching_remove_vf,
1d051e00 4837 .sriov_get_phys_port_id = efx_ef10_sriov_get_phys_port_id,
02246a7f 4838#endif
0d5e0fbb 4839 .get_mac_address = efx_ef10_get_mac_address_vf,
910c8789 4840 .set_mac_address = efx_ef10_set_mac_address,
0d5e0fbb 4841
02246a7f
SS
4842 .revision = EFX_REV_HUNT_A0,
4843 .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
4844 .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
4845 .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
4846 .rx_ts_offset = ES_DZ_RX_PREFIX_TSTAMP_OFST,
4847 .can_rx_scatter = true,
4848 .always_rx_scatter = true,
4849 .max_interrupt_mode = EFX_INT_MODE_MSIX,
4850 .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
100a9db5 4851 .offload_features = EF10_OFFLOAD_FEATURES,
02246a7f
SS
4852 .mcdi_max_ver = 2,
4853 .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
4854 .hwtstamp_filters = 1 << HWTSTAMP_FILTER_NONE |
4855 1 << HWTSTAMP_FILTER_ALL,
4856};
4857
8127d661 4858const struct efx_nic_type efx_hunt_a0_nic_type = {
6f7f8aa6 4859 .is_vf = false,
02246a7f 4860 .mem_bar = EFX_MEM_BAR,
8127d661 4861 .mem_map_size = efx_ef10_mem_map_size,
02246a7f 4862 .probe = efx_ef10_probe_pf,
8127d661
BH
4863 .remove = efx_ef10_remove,
4864 .dimension_resources = efx_ef10_dimension_resources,
4865 .init = efx_ef10_init_nic,
4866 .fini = efx_port_dummy_op_void,
087e9025 4867 .map_reset_reason = efx_ef10_map_reset_reason,
8127d661 4868 .map_reset_flags = efx_ef10_map_reset_flags,
3e336261 4869 .reset = efx_ef10_reset,
8127d661
BH
4870 .probe_port = efx_mcdi_port_probe,
4871 .remove_port = efx_mcdi_port_remove,
4872 .fini_dmaq = efx_ef10_fini_dmaq,
e283546c
EC
4873 .prepare_flr = efx_ef10_prepare_flr,
4874 .finish_flr = efx_port_dummy_op_void,
8127d661 4875 .describe_stats = efx_ef10_describe_stats,
d7788196 4876 .update_stats = efx_ef10_update_stats_pf,
8127d661 4877 .start_stats = efx_mcdi_mac_start_stats,
f8f3b5ae 4878 .pull_stats = efx_mcdi_mac_pull_stats,
8127d661
BH
4879 .stop_stats = efx_mcdi_mac_stop_stats,
4880 .set_id_led = efx_mcdi_set_id_led,
4881 .push_irq_moderation = efx_ef10_push_irq_moderation,
4882 .reconfigure_mac = efx_ef10_mac_reconfigure,
4883 .check_mac_fault = efx_mcdi_mac_check_fault,
4884 .reconfigure_port = efx_mcdi_port_reconfigure,
4885 .get_wol = efx_ef10_get_wol,
4886 .set_wol = efx_ef10_set_wol,
4887 .resume_wol = efx_port_dummy_op_void,
74cd60a4 4888 .test_chip = efx_ef10_test_chip,
8127d661
BH
4889 .test_nvram = efx_mcdi_nvram_test_all,
4890 .mcdi_request = efx_ef10_mcdi_request,
4891 .mcdi_poll_response = efx_ef10_mcdi_poll_response,
4892 .mcdi_read_response = efx_ef10_mcdi_read_response,
4893 .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
c577e59e 4894 .mcdi_reboot_detected = efx_ef10_mcdi_reboot_detected,
8127d661
BH
4895 .irq_enable_master = efx_port_dummy_op_void,
4896 .irq_test_generate = efx_ef10_irq_test_generate,
4897 .irq_disable_non_ev = efx_port_dummy_op_void,
4898 .irq_handle_msi = efx_ef10_msi_interrupt,
4899 .irq_handle_legacy = efx_ef10_legacy_interrupt,
4900 .tx_probe = efx_ef10_tx_probe,
4901 .tx_init = efx_ef10_tx_init,
4902 .tx_remove = efx_ef10_tx_remove,
4903 .tx_write = efx_ef10_tx_write,
267c0157 4904 .rx_push_rss_config = efx_ef10_pf_rx_push_rss_config,
8127d661
BH
4905 .rx_probe = efx_ef10_rx_probe,
4906 .rx_init = efx_ef10_rx_init,
4907 .rx_remove = efx_ef10_rx_remove,
4908 .rx_write = efx_ef10_rx_write,
4909 .rx_defer_refill = efx_ef10_rx_defer_refill,
4910 .ev_probe = efx_ef10_ev_probe,
4911 .ev_init = efx_ef10_ev_init,
4912 .ev_fini = efx_ef10_ev_fini,
4913 .ev_remove = efx_ef10_ev_remove,
4914 .ev_process = efx_ef10_ev_process,
4915 .ev_read_ack = efx_ef10_ev_read_ack,
4916 .ev_test_generate = efx_ef10_ev_test_generate,
4917 .filter_table_probe = efx_ef10_filter_table_probe,
4918 .filter_table_restore = efx_ef10_filter_table_restore,
4919 .filter_table_remove = efx_ef10_filter_table_remove,
4920 .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
4921 .filter_insert = efx_ef10_filter_insert,
4922 .filter_remove_safe = efx_ef10_filter_remove_safe,
4923 .filter_get_safe = efx_ef10_filter_get_safe,
4924 .filter_clear_rx = efx_ef10_filter_clear_rx,
4925 .filter_count_rx_used = efx_ef10_filter_count_rx_used,
4926 .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
4927 .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
4928#ifdef CONFIG_RFS_ACCEL
4929 .filter_rfs_insert = efx_ef10_filter_rfs_insert,
4930 .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
4931#endif
4932#ifdef CONFIG_SFC_MTD
4933 .mtd_probe = efx_ef10_mtd_probe,
4934 .mtd_rename = efx_mcdi_mtd_rename,
4935 .mtd_read = efx_mcdi_mtd_read,
4936 .mtd_erase = efx_mcdi_mtd_erase,
4937 .mtd_write = efx_mcdi_mtd_write,
4938 .mtd_sync = efx_mcdi_mtd_sync,
4939#endif
4940 .ptp_write_host_time = efx_ef10_ptp_write_host_time,
bd9a265d
JC
4941 .ptp_set_ts_sync_events = efx_ef10_ptp_set_ts_sync_events,
4942 .ptp_set_ts_config = efx_ef10_ptp_set_ts_config,
7fa8d547 4943#ifdef CONFIG_SFC_SRIOV
834e23dd 4944 .sriov_configure = efx_ef10_sriov_configure,
d98a4ffe
SS
4945 .sriov_init = efx_ef10_sriov_init,
4946 .sriov_fini = efx_ef10_sriov_fini,
d98a4ffe
SS
4947 .sriov_wanted = efx_ef10_sriov_wanted,
4948 .sriov_reset = efx_ef10_sriov_reset,
7fa8d547
SS
4949 .sriov_flr = efx_ef10_sriov_flr,
4950 .sriov_set_vf_mac = efx_ef10_sriov_set_vf_mac,
4951 .sriov_set_vf_vlan = efx_ef10_sriov_set_vf_vlan,
4952 .sriov_set_vf_spoofchk = efx_ef10_sriov_set_vf_spoofchk,
4953 .sriov_get_vf_config = efx_ef10_sriov_get_vf_config,
4392dc69 4954 .sriov_set_vf_link_state = efx_ef10_sriov_set_vf_link_state,
7b8c7b54
SS
4955 .vswitching_probe = efx_ef10_vswitching_probe_pf,
4956 .vswitching_restore = efx_ef10_vswitching_restore_pf,
4957 .vswitching_remove = efx_ef10_vswitching_remove_pf,
7fa8d547 4958#endif
0d5e0fbb 4959 .get_mac_address = efx_ef10_get_mac_address_pf,
910c8789 4960 .set_mac_address = efx_ef10_set_mac_address,
8127d661
BH
4961
4962 .revision = EFX_REV_HUNT_A0,
4963 .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
4964 .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
4965 .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
bd9a265d 4966 .rx_ts_offset = ES_DZ_RX_PREFIX_TSTAMP_OFST,
8127d661
BH
4967 .can_rx_scatter = true,
4968 .always_rx_scatter = true,
4969 .max_interrupt_mode = EFX_INT_MODE_MSIX,
4970 .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
100a9db5 4971 .offload_features = EF10_OFFLOAD_FEATURES,
8127d661
BH
4972 .mcdi_max_ver = 2,
4973 .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
bd9a265d
JC
4974 .hwtstamp_filters = 1 << HWTSTAMP_FILTER_NONE |
4975 1 << HWTSTAMP_FILTER_ALL,
8127d661 4976};