blob: a4956b86d1451032366bda790d2256f200360f6b [file] [log] [blame]
Ben Hutchings8127d662013-08-29 19:19:29 +01001/****************************************************************************
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"
17#include <linux/in.h>
18#include <linux/jhash.h>
19#include <linux/wait.h>
20#include <linux/workqueue.h>
21
22/* Hardware control for EF10 architecture including 'Huntington'. */
23
24#define EFX_EF10_DRVGEN_EV 7
25enum {
26 EFX_EF10_TEST = 1,
27 EFX_EF10_REFILL,
28};
29
30/* The reserved RSS context value */
31#define EFX_EF10_RSS_CONTEXT_INVALID 0xffffffff
32
33/* The filter table(s) are managed by firmware and we have write-only
34 * access. When removing filters we must identify them to the
35 * firmware by a 64-bit handle, but this is too wide for Linux kernel
36 * interfaces (32-bit for RX NFC, 16-bit for RFS). Also, we need to
37 * be able to tell in advance whether a requested insertion will
38 * replace an existing filter. Therefore we maintain a software hash
39 * table, which should be at least as large as the hardware hash
40 * table.
41 *
42 * Huntington has a single 8K filter table shared between all filter
43 * types and both ports.
44 */
45#define HUNT_FILTER_TBL_ROWS 8192
46
47struct efx_ef10_filter_table {
48/* The RX match field masks supported by this fw & hw, in order of priority */
49 enum efx_filter_match_flags rx_match_flags[
50 MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_MAXNUM];
51 unsigned int rx_match_count;
52
53 struct {
54 unsigned long spec; /* pointer to spec plus flag bits */
55/* BUSY flag indicates that an update is in progress. STACK_OLD is
56 * used to mark and sweep stack-owned MAC filters.
57 */
58#define EFX_EF10_FILTER_FLAG_BUSY 1UL
59#define EFX_EF10_FILTER_FLAG_STACK_OLD 2UL
60#define EFX_EF10_FILTER_FLAGS 3UL
61 u64 handle; /* firmware handle */
62 } *entry;
63 wait_queue_head_t waitq;
64/* Shadow of net_device address lists, guarded by mac_lock */
65#define EFX_EF10_FILTER_STACK_UC_MAX 32
66#define EFX_EF10_FILTER_STACK_MC_MAX 256
67 struct {
68 u8 addr[ETH_ALEN];
69 u16 id;
70 } stack_uc_list[EFX_EF10_FILTER_STACK_UC_MAX],
71 stack_mc_list[EFX_EF10_FILTER_STACK_MC_MAX];
72 int stack_uc_count; /* negative for PROMISC */
73 int stack_mc_count; /* negative for PROMISC/ALLMULTI */
74};
75
76/* An arbitrary search limit for the software hash table */
77#define EFX_EF10_FILTER_SEARCH_LIMIT 200
78
79static void efx_ef10_rx_push_indir_table(struct efx_nic *efx);
80static void efx_ef10_rx_free_indir_table(struct efx_nic *efx);
81static void efx_ef10_filter_table_remove(struct efx_nic *efx);
82
83static int efx_ef10_get_warm_boot_count(struct efx_nic *efx)
84{
85 efx_dword_t reg;
86
87 efx_readd(efx, &reg, ER_DZ_BIU_MC_SFT_STATUS);
88 return EFX_DWORD_FIELD(reg, EFX_WORD_1) == 0xb007 ?
89 EFX_DWORD_FIELD(reg, EFX_WORD_0) : -EIO;
90}
91
92static unsigned int efx_ef10_mem_map_size(struct efx_nic *efx)
93{
94 return resource_size(&efx->pci_dev->resource[EFX_MEM_BAR]);
95}
96
Ben Hutchingse5a25382013-09-05 22:50:59 +010097static int efx_ef10_init_datapath_caps(struct efx_nic *efx)
Ben Hutchings8127d662013-08-29 19:19:29 +010098{
99 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CAPABILITIES_OUT_LEN);
100 struct efx_ef10_nic_data *nic_data = efx->nic_data;
101 size_t outlen;
102 int rc;
103
104 BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
105
106 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
107 outbuf, sizeof(outbuf), &outlen);
108 if (rc)
109 return rc;
Ben Hutchingse5a25382013-09-05 22:50:59 +0100110 if (outlen < sizeof(outbuf)) {
111 netif_err(efx, drv, efx->net_dev,
112 "unable to read datapath firmware capabilities\n");
113 return -EIO;
114 }
Ben Hutchings8127d662013-08-29 19:19:29 +0100115
Ben Hutchingse5a25382013-09-05 22:50:59 +0100116 nic_data->datapath_caps =
117 MCDI_DWORD(outbuf, GET_CAPABILITIES_OUT_FLAGS1);
118
119 if (!(nic_data->datapath_caps &
120 (1 << MC_CMD_GET_CAPABILITIES_OUT_TX_TSO_LBN))) {
121 netif_err(efx, drv, efx->net_dev,
122 "current firmware does not support TSO\n");
123 return -ENODEV;
124 }
125
126 if (!(nic_data->datapath_caps &
127 (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_PREFIX_LEN_14_LBN))) {
128 netif_err(efx, probe, efx->net_dev,
129 "current firmware does not support an RX prefix\n");
130 return -ENODEV;
Ben Hutchings8127d662013-08-29 19:19:29 +0100131 }
132
133 return 0;
134}
135
136static int efx_ef10_get_sysclk_freq(struct efx_nic *efx)
137{
138 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CLOCK_OUT_LEN);
139 int rc;
140
141 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CLOCK, NULL, 0,
142 outbuf, sizeof(outbuf), NULL);
143 if (rc)
144 return rc;
145 rc = MCDI_DWORD(outbuf, GET_CLOCK_OUT_SYS_FREQ);
146 return rc > 0 ? rc : -ERANGE;
147}
148
149static int efx_ef10_get_mac_address(struct efx_nic *efx, u8 *mac_address)
150{
151 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_MAC_ADDRESSES_OUT_LEN);
152 size_t outlen;
153 int rc;
154
155 BUILD_BUG_ON(MC_CMD_GET_MAC_ADDRESSES_IN_LEN != 0);
156
157 rc = efx_mcdi_rpc(efx, MC_CMD_GET_MAC_ADDRESSES, NULL, 0,
158 outbuf, sizeof(outbuf), &outlen);
159 if (rc)
160 return rc;
161 if (outlen < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)
162 return -EIO;
163
164 memcpy(mac_address,
165 MCDI_PTR(outbuf, GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE), ETH_ALEN);
166 return 0;
167}
168
169static int efx_ef10_probe(struct efx_nic *efx)
170{
171 struct efx_ef10_nic_data *nic_data;
172 int i, rc;
173
174 /* We can have one VI for each 8K region. However we need
175 * multiple TX queues per channel.
176 */
177 efx->max_channels =
178 min_t(unsigned int,
179 EFX_MAX_CHANNELS,
180 resource_size(&efx->pci_dev->resource[EFX_MEM_BAR]) /
181 (EFX_VI_PAGE_SIZE * EFX_TXQ_TYPES));
182 BUG_ON(efx->max_channels == 0);
183
184 nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
185 if (!nic_data)
186 return -ENOMEM;
187 efx->nic_data = nic_data;
188
189 rc = efx_nic_alloc_buffer(efx, &nic_data->mcdi_buf,
190 8 + MCDI_CTL_SDU_LEN_MAX_V2, GFP_KERNEL);
191 if (rc)
192 goto fail1;
193
194 /* Get the MC's warm boot count. In case it's rebooting right
195 * now, be prepared to retry.
196 */
197 i = 0;
198 for (;;) {
199 rc = efx_ef10_get_warm_boot_count(efx);
200 if (rc >= 0)
201 break;
202 if (++i == 5)
203 goto fail2;
204 ssleep(1);
205 }
206 nic_data->warm_boot_count = rc;
207
208 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
209
210 /* In case we're recovering from a crash (kexec), we want to
211 * cancel any outstanding request by the previous user of this
212 * function. We send a special message using the least
213 * significant bits of the 'high' (doorbell) register.
214 */
215 _efx_writed(efx, cpu_to_le32(1), ER_DZ_MC_DB_HWRD);
216
217 rc = efx_mcdi_init(efx);
218 if (rc)
219 goto fail2;
220
221 /* Reset (most) configuration for this function */
222 rc = efx_mcdi_reset(efx, RESET_TYPE_ALL);
223 if (rc)
224 goto fail3;
225
226 /* Enable event logging */
227 rc = efx_mcdi_log_ctrl(efx, true, false, 0);
228 if (rc)
229 goto fail3;
230
Ben Hutchingse5a25382013-09-05 22:50:59 +0100231 rc = efx_ef10_init_datapath_caps(efx);
Ben Hutchings8127d662013-08-29 19:19:29 +0100232 if (rc < 0)
233 goto fail3;
234
235 efx->rx_packet_len_offset =
236 ES_DZ_RX_PREFIX_PKTLEN_OFST - ES_DZ_RX_PREFIX_SIZE;
237
Ben Hutchings8127d662013-08-29 19:19:29 +0100238 rc = efx_mcdi_port_get_number(efx);
239 if (rc < 0)
240 goto fail3;
241 efx->port_num = rc;
242
243 rc = efx_ef10_get_mac_address(efx, efx->net_dev->perm_addr);
244 if (rc)
245 goto fail3;
246
247 rc = efx_ef10_get_sysclk_freq(efx);
248 if (rc < 0)
249 goto fail3;
250 efx->timer_quantum_ns = 1536000 / rc; /* 1536 cycles */
251
252 /* Check whether firmware supports bug 35388 workaround */
253 rc = efx_mcdi_set_workaround(efx, MC_CMD_WORKAROUND_BUG35388, true);
254 if (rc == 0)
255 nic_data->workaround_35388 = true;
256 else if (rc != -ENOSYS && rc != -ENOENT)
257 goto fail3;
258 netif_dbg(efx, probe, efx->net_dev,
259 "workaround for bug 35388 is %sabled\n",
260 nic_data->workaround_35388 ? "en" : "dis");
261
262 rc = efx_mcdi_mon_probe(efx);
263 if (rc)
264 goto fail3;
265
Ben Hutchings8127d662013-08-29 19:19:29 +0100266 return 0;
267
268fail3:
269 efx_mcdi_fini(efx);
270fail2:
271 efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
272fail1:
273 kfree(nic_data);
274 efx->nic_data = NULL;
275 return rc;
276}
277
278static int efx_ef10_free_vis(struct efx_nic *efx)
279{
280 int rc = efx_mcdi_rpc(efx, MC_CMD_FREE_VIS, NULL, 0, NULL, 0, NULL);
281
282 /* -EALREADY means nothing to free, so ignore */
283 if (rc == -EALREADY)
284 rc = 0;
285 return rc;
286}
287
288static void efx_ef10_remove(struct efx_nic *efx)
289{
290 struct efx_ef10_nic_data *nic_data = efx->nic_data;
291 int rc;
292
293 efx_mcdi_mon_remove(efx);
294
295 /* This needs to be after efx_ptp_remove_channel() with no filters */
296 efx_ef10_rx_free_indir_table(efx);
297
298 rc = efx_ef10_free_vis(efx);
299 WARN_ON(rc != 0);
300
301 efx_mcdi_fini(efx);
302 efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
303 kfree(nic_data);
304}
305
306static int efx_ef10_alloc_vis(struct efx_nic *efx,
307 unsigned int min_vis, unsigned int max_vis)
308{
309 MCDI_DECLARE_BUF(inbuf, MC_CMD_ALLOC_VIS_IN_LEN);
310 MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_VIS_OUT_LEN);
311 struct efx_ef10_nic_data *nic_data = efx->nic_data;
312 size_t outlen;
313 int rc;
314
315 MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MIN_VI_COUNT, min_vis);
316 MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MAX_VI_COUNT, max_vis);
317 rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_VIS, inbuf, sizeof(inbuf),
318 outbuf, sizeof(outbuf), &outlen);
319 if (rc != 0)
320 return rc;
321
322 if (outlen < MC_CMD_ALLOC_VIS_OUT_LEN)
323 return -EIO;
324
325 netif_dbg(efx, drv, efx->net_dev, "base VI is A0x%03x\n",
326 MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE));
327
328 nic_data->vi_base = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE);
329 nic_data->n_allocated_vis = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_COUNT);
330 return 0;
331}
332
333static int efx_ef10_dimension_resources(struct efx_nic *efx)
334{
335 unsigned int n_vis =
336 max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
337
338 return efx_ef10_alloc_vis(efx, n_vis, n_vis);
339}
340
341static int efx_ef10_init_nic(struct efx_nic *efx)
342{
343 struct efx_ef10_nic_data *nic_data = efx->nic_data;
344 int rc;
345
346 if (nic_data->must_realloc_vis) {
347 /* We cannot let the number of VIs change now */
348 rc = efx_ef10_alloc_vis(efx, nic_data->n_allocated_vis,
349 nic_data->n_allocated_vis);
350 if (rc)
351 return rc;
352 nic_data->must_realloc_vis = false;
353 }
354
355 efx_ef10_rx_push_indir_table(efx);
356 return 0;
357}
358
359static int efx_ef10_map_reset_flags(u32 *flags)
360{
361 enum {
362 EF10_RESET_PORT = ((ETH_RESET_MAC | ETH_RESET_PHY) <<
363 ETH_RESET_SHARED_SHIFT),
364 EF10_RESET_MC = ((ETH_RESET_DMA | ETH_RESET_FILTER |
365 ETH_RESET_OFFLOAD | ETH_RESET_MAC |
366 ETH_RESET_PHY | ETH_RESET_MGMT) <<
367 ETH_RESET_SHARED_SHIFT)
368 };
369
370 /* We assume for now that our PCI function is permitted to
371 * reset everything.
372 */
373
374 if ((*flags & EF10_RESET_MC) == EF10_RESET_MC) {
375 *flags &= ~EF10_RESET_MC;
376 return RESET_TYPE_WORLD;
377 }
378
379 if ((*flags & EF10_RESET_PORT) == EF10_RESET_PORT) {
380 *flags &= ~EF10_RESET_PORT;
381 return RESET_TYPE_ALL;
382 }
383
384 /* no invisible reset implemented */
385
386 return -EINVAL;
387}
388
389#define EF10_DMA_STAT(ext_name, mcdi_name) \
390 [EF10_STAT_ ## ext_name] = \
391 { #ext_name, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
392#define EF10_DMA_INVIS_STAT(int_name, mcdi_name) \
393 [EF10_STAT_ ## int_name] = \
394 { NULL, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
395#define EF10_OTHER_STAT(ext_name) \
396 [EF10_STAT_ ## ext_name] = { #ext_name, 0, 0 }
397
398static const struct efx_hw_stat_desc efx_ef10_stat_desc[EF10_STAT_COUNT] = {
399 EF10_DMA_STAT(tx_bytes, TX_BYTES),
400 EF10_DMA_STAT(tx_packets, TX_PKTS),
401 EF10_DMA_STAT(tx_pause, TX_PAUSE_PKTS),
402 EF10_DMA_STAT(tx_control, TX_CONTROL_PKTS),
403 EF10_DMA_STAT(tx_unicast, TX_UNICAST_PKTS),
404 EF10_DMA_STAT(tx_multicast, TX_MULTICAST_PKTS),
405 EF10_DMA_STAT(tx_broadcast, TX_BROADCAST_PKTS),
406 EF10_DMA_STAT(tx_lt64, TX_LT64_PKTS),
407 EF10_DMA_STAT(tx_64, TX_64_PKTS),
408 EF10_DMA_STAT(tx_65_to_127, TX_65_TO_127_PKTS),
409 EF10_DMA_STAT(tx_128_to_255, TX_128_TO_255_PKTS),
410 EF10_DMA_STAT(tx_256_to_511, TX_256_TO_511_PKTS),
411 EF10_DMA_STAT(tx_512_to_1023, TX_512_TO_1023_PKTS),
412 EF10_DMA_STAT(tx_1024_to_15xx, TX_1024_TO_15XX_PKTS),
413 EF10_DMA_STAT(tx_15xx_to_jumbo, TX_15XX_TO_JUMBO_PKTS),
414 EF10_DMA_STAT(rx_bytes, RX_BYTES),
415 EF10_DMA_INVIS_STAT(rx_bytes_minus_good_bytes, RX_BAD_BYTES),
416 EF10_OTHER_STAT(rx_good_bytes),
417 EF10_OTHER_STAT(rx_bad_bytes),
418 EF10_DMA_STAT(rx_packets, RX_PKTS),
419 EF10_DMA_STAT(rx_good, RX_GOOD_PKTS),
420 EF10_DMA_STAT(rx_bad, RX_BAD_FCS_PKTS),
421 EF10_DMA_STAT(rx_pause, RX_PAUSE_PKTS),
422 EF10_DMA_STAT(rx_control, RX_CONTROL_PKTS),
423 EF10_DMA_STAT(rx_unicast, RX_UNICAST_PKTS),
424 EF10_DMA_STAT(rx_multicast, RX_MULTICAST_PKTS),
425 EF10_DMA_STAT(rx_broadcast, RX_BROADCAST_PKTS),
426 EF10_DMA_STAT(rx_lt64, RX_UNDERSIZE_PKTS),
427 EF10_DMA_STAT(rx_64, RX_64_PKTS),
428 EF10_DMA_STAT(rx_65_to_127, RX_65_TO_127_PKTS),
429 EF10_DMA_STAT(rx_128_to_255, RX_128_TO_255_PKTS),
430 EF10_DMA_STAT(rx_256_to_511, RX_256_TO_511_PKTS),
431 EF10_DMA_STAT(rx_512_to_1023, RX_512_TO_1023_PKTS),
432 EF10_DMA_STAT(rx_1024_to_15xx, RX_1024_TO_15XX_PKTS),
433 EF10_DMA_STAT(rx_15xx_to_jumbo, RX_15XX_TO_JUMBO_PKTS),
434 EF10_DMA_STAT(rx_gtjumbo, RX_GTJUMBO_PKTS),
435 EF10_DMA_STAT(rx_bad_gtjumbo, RX_JABBER_PKTS),
436 EF10_DMA_STAT(rx_overflow, RX_OVERFLOW_PKTS),
437 EF10_DMA_STAT(rx_align_error, RX_ALIGN_ERROR_PKTS),
438 EF10_DMA_STAT(rx_length_error, RX_LENGTH_ERROR_PKTS),
439 EF10_DMA_STAT(rx_nodesc_drops, RX_NODESC_DROPS),
440};
441
442#define HUNT_COMMON_STAT_MASK ((1ULL << EF10_STAT_tx_bytes) | \
443 (1ULL << EF10_STAT_tx_packets) | \
444 (1ULL << EF10_STAT_tx_pause) | \
445 (1ULL << EF10_STAT_tx_unicast) | \
446 (1ULL << EF10_STAT_tx_multicast) | \
447 (1ULL << EF10_STAT_tx_broadcast) | \
448 (1ULL << EF10_STAT_rx_bytes) | \
449 (1ULL << EF10_STAT_rx_bytes_minus_good_bytes) | \
450 (1ULL << EF10_STAT_rx_good_bytes) | \
451 (1ULL << EF10_STAT_rx_bad_bytes) | \
452 (1ULL << EF10_STAT_rx_packets) | \
453 (1ULL << EF10_STAT_rx_good) | \
454 (1ULL << EF10_STAT_rx_bad) | \
455 (1ULL << EF10_STAT_rx_pause) | \
456 (1ULL << EF10_STAT_rx_control) | \
457 (1ULL << EF10_STAT_rx_unicast) | \
458 (1ULL << EF10_STAT_rx_multicast) | \
459 (1ULL << EF10_STAT_rx_broadcast) | \
460 (1ULL << EF10_STAT_rx_lt64) | \
461 (1ULL << EF10_STAT_rx_64) | \
462 (1ULL << EF10_STAT_rx_65_to_127) | \
463 (1ULL << EF10_STAT_rx_128_to_255) | \
464 (1ULL << EF10_STAT_rx_256_to_511) | \
465 (1ULL << EF10_STAT_rx_512_to_1023) | \
466 (1ULL << EF10_STAT_rx_1024_to_15xx) | \
467 (1ULL << EF10_STAT_rx_15xx_to_jumbo) | \
468 (1ULL << EF10_STAT_rx_gtjumbo) | \
469 (1ULL << EF10_STAT_rx_bad_gtjumbo) | \
470 (1ULL << EF10_STAT_rx_overflow) | \
471 (1ULL << EF10_STAT_rx_nodesc_drops))
472
473/* These statistics are only provided by the 10G MAC. For a 10G/40G
474 * switchable port we do not expose these because they might not
475 * include all the packets they should.
476 */
477#define HUNT_10G_ONLY_STAT_MASK ((1ULL << EF10_STAT_tx_control) | \
478 (1ULL << EF10_STAT_tx_lt64) | \
479 (1ULL << EF10_STAT_tx_64) | \
480 (1ULL << EF10_STAT_tx_65_to_127) | \
481 (1ULL << EF10_STAT_tx_128_to_255) | \
482 (1ULL << EF10_STAT_tx_256_to_511) | \
483 (1ULL << EF10_STAT_tx_512_to_1023) | \
484 (1ULL << EF10_STAT_tx_1024_to_15xx) | \
485 (1ULL << EF10_STAT_tx_15xx_to_jumbo))
486
487/* These statistics are only provided by the 40G MAC. For a 10G/40G
488 * switchable port we do expose these because the errors will otherwise
489 * be silent.
490 */
491#define HUNT_40G_EXTRA_STAT_MASK ((1ULL << EF10_STAT_rx_align_error) | \
492 (1ULL << EF10_STAT_rx_length_error))
493
494#if BITS_PER_LONG == 64
495#define STAT_MASK_BITMAP(bits) (bits)
496#else
497#define STAT_MASK_BITMAP(bits) (bits) & 0xffffffff, (bits) >> 32
498#endif
499
500static const unsigned long *efx_ef10_stat_mask(struct efx_nic *efx)
501{
502 static const unsigned long hunt_40g_stat_mask[] = {
503 STAT_MASK_BITMAP(HUNT_COMMON_STAT_MASK |
504 HUNT_40G_EXTRA_STAT_MASK)
505 };
506 static const unsigned long hunt_10g_only_stat_mask[] = {
507 STAT_MASK_BITMAP(HUNT_COMMON_STAT_MASK |
508 HUNT_10G_ONLY_STAT_MASK)
509 };
510 u32 port_caps = efx_mcdi_phy_get_caps(efx);
511
512 if (port_caps & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
513 return hunt_40g_stat_mask;
514 else
515 return hunt_10g_only_stat_mask;
516}
517
518static size_t efx_ef10_describe_stats(struct efx_nic *efx, u8 *names)
519{
520 return efx_nic_describe_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
521 efx_ef10_stat_mask(efx), names);
522}
523
524static int efx_ef10_try_update_nic_stats(struct efx_nic *efx)
525{
526 struct efx_ef10_nic_data *nic_data = efx->nic_data;
527 const unsigned long *stats_mask = efx_ef10_stat_mask(efx);
528 __le64 generation_start, generation_end;
529 u64 *stats = nic_data->stats;
530 __le64 *dma_stats;
531
532 dma_stats = efx->stats_buffer.addr;
533 nic_data = efx->nic_data;
534
535 generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
536 if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
537 return 0;
538 rmb();
539 efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, stats_mask,
540 stats, efx->stats_buffer.addr, false);
541 generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
542 if (generation_end != generation_start)
543 return -EAGAIN;
544
545 /* Update derived statistics */
546 stats[EF10_STAT_rx_good_bytes] =
547 stats[EF10_STAT_rx_bytes] -
548 stats[EF10_STAT_rx_bytes_minus_good_bytes];
549 efx_update_diff_stat(&stats[EF10_STAT_rx_bad_bytes],
550 stats[EF10_STAT_rx_bytes_minus_good_bytes]);
551
552 return 0;
553}
554
555
556static size_t efx_ef10_update_stats(struct efx_nic *efx, u64 *full_stats,
557 struct rtnl_link_stats64 *core_stats)
558{
559 const unsigned long *mask = efx_ef10_stat_mask(efx);
560 struct efx_ef10_nic_data *nic_data = efx->nic_data;
561 u64 *stats = nic_data->stats;
562 size_t stats_count = 0, index;
563 int retry;
564
565 /* If we're unlucky enough to read statistics during the DMA, wait
566 * up to 10ms for it to finish (typically takes <500us)
567 */
568 for (retry = 0; retry < 100; ++retry) {
569 if (efx_ef10_try_update_nic_stats(efx) == 0)
570 break;
571 udelay(100);
572 }
573
574 if (full_stats) {
575 for_each_set_bit(index, mask, EF10_STAT_COUNT) {
576 if (efx_ef10_stat_desc[index].name) {
577 *full_stats++ = stats[index];
578 ++stats_count;
579 }
580 }
581 }
582
583 if (core_stats) {
584 core_stats->rx_packets = stats[EF10_STAT_rx_packets];
585 core_stats->tx_packets = stats[EF10_STAT_tx_packets];
586 core_stats->rx_bytes = stats[EF10_STAT_rx_bytes];
587 core_stats->tx_bytes = stats[EF10_STAT_tx_bytes];
588 core_stats->rx_dropped = stats[EF10_STAT_rx_nodesc_drops];
589 core_stats->multicast = stats[EF10_STAT_rx_multicast];
590 core_stats->rx_length_errors =
591 stats[EF10_STAT_rx_gtjumbo] +
592 stats[EF10_STAT_rx_length_error];
593 core_stats->rx_crc_errors = stats[EF10_STAT_rx_bad];
594 core_stats->rx_frame_errors = stats[EF10_STAT_rx_align_error];
595 core_stats->rx_fifo_errors = stats[EF10_STAT_rx_overflow];
596 core_stats->rx_errors = (core_stats->rx_length_errors +
597 core_stats->rx_crc_errors +
598 core_stats->rx_frame_errors);
599 }
600
601 return stats_count;
602}
603
604static void efx_ef10_push_irq_moderation(struct efx_channel *channel)
605{
606 struct efx_nic *efx = channel->efx;
607 unsigned int mode, value;
608 efx_dword_t timer_cmd;
609
610 if (channel->irq_moderation) {
611 mode = 3;
612 value = channel->irq_moderation - 1;
613 } else {
614 mode = 0;
615 value = 0;
616 }
617
618 if (EFX_EF10_WORKAROUND_35388(efx)) {
619 EFX_POPULATE_DWORD_3(timer_cmd, ERF_DD_EVQ_IND_TIMER_FLAGS,
620 EFE_DD_EVQ_IND_TIMER_FLAGS,
621 ERF_DD_EVQ_IND_TIMER_MODE, mode,
622 ERF_DD_EVQ_IND_TIMER_VAL, value);
623 efx_writed_page(efx, &timer_cmd, ER_DD_EVQ_INDIRECT,
624 channel->channel);
625 } else {
626 EFX_POPULATE_DWORD_2(timer_cmd, ERF_DZ_TC_TIMER_MODE, mode,
627 ERF_DZ_TC_TIMER_VAL, value);
628 efx_writed_page(efx, &timer_cmd, ER_DZ_EVQ_TMR,
629 channel->channel);
630 }
631}
632
633static void efx_ef10_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
634{
635 wol->supported = 0;
636 wol->wolopts = 0;
637 memset(&wol->sopass, 0, sizeof(wol->sopass));
638}
639
640static int efx_ef10_set_wol(struct efx_nic *efx, u32 type)
641{
642 if (type != 0)
643 return -EINVAL;
644 return 0;
645}
646
647static void efx_ef10_mcdi_request(struct efx_nic *efx,
648 const efx_dword_t *hdr, size_t hdr_len,
649 const efx_dword_t *sdu, size_t sdu_len)
650{
651 struct efx_ef10_nic_data *nic_data = efx->nic_data;
652 u8 *pdu = nic_data->mcdi_buf.addr;
653
654 memcpy(pdu, hdr, hdr_len);
655 memcpy(pdu + hdr_len, sdu, sdu_len);
656 wmb();
657
658 /* The hardware provides 'low' and 'high' (doorbell) registers
659 * for passing the 64-bit address of an MCDI request to
660 * firmware. However the dwords are swapped by firmware. The
661 * least significant bits of the doorbell are then 0 for all
662 * MCDI requests due to alignment.
663 */
664 _efx_writed(efx, cpu_to_le32((u64)nic_data->mcdi_buf.dma_addr >> 32),
665 ER_DZ_MC_DB_LWRD);
666 _efx_writed(efx, cpu_to_le32((u32)nic_data->mcdi_buf.dma_addr),
667 ER_DZ_MC_DB_HWRD);
668}
669
670static bool efx_ef10_mcdi_poll_response(struct efx_nic *efx)
671{
672 struct efx_ef10_nic_data *nic_data = efx->nic_data;
673 const efx_dword_t hdr = *(const efx_dword_t *)nic_data->mcdi_buf.addr;
674
675 rmb();
676 return EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE);
677}
678
679static void
680efx_ef10_mcdi_read_response(struct efx_nic *efx, efx_dword_t *outbuf,
681 size_t offset, size_t outlen)
682{
683 struct efx_ef10_nic_data *nic_data = efx->nic_data;
684 const u8 *pdu = nic_data->mcdi_buf.addr;
685
686 memcpy(outbuf, pdu + offset, outlen);
687}
688
689static int efx_ef10_mcdi_poll_reboot(struct efx_nic *efx)
690{
691 struct efx_ef10_nic_data *nic_data = efx->nic_data;
692 int rc;
693
694 rc = efx_ef10_get_warm_boot_count(efx);
695 if (rc < 0) {
696 /* The firmware is presumably in the process of
697 * rebooting. However, we are supposed to report each
698 * reboot just once, so we must only do that once we
699 * can read and store the updated warm boot count.
700 */
701 return 0;
702 }
703
704 if (rc == nic_data->warm_boot_count)
705 return 0;
706
707 nic_data->warm_boot_count = rc;
708
709 /* All our allocations have been reset */
710 nic_data->must_realloc_vis = true;
711 nic_data->must_restore_filters = true;
712 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
713
Ben Hutchings869070c2013-09-05 22:46:10 +0100714 /* MAC statistics have been cleared on the NIC; clear the local
715 * statistic that we update with efx_update_diff_stat().
716 */
717 nic_data->stats[EF10_STAT_rx_bad_bytes] = 0;
718
Ben Hutchings8127d662013-08-29 19:19:29 +0100719 return -EIO;
720}
721
722/* Handle an MSI interrupt
723 *
724 * Handle an MSI hardware interrupt. This routine schedules event
725 * queue processing. No interrupt acknowledgement cycle is necessary.
726 * Also, we never need to check that the interrupt is for us, since
727 * MSI interrupts cannot be shared.
728 */
729static irqreturn_t efx_ef10_msi_interrupt(int irq, void *dev_id)
730{
731 struct efx_msi_context *context = dev_id;
732 struct efx_nic *efx = context->efx;
733
734 netif_vdbg(efx, intr, efx->net_dev,
735 "IRQ %d on CPU %d\n", irq, raw_smp_processor_id());
736
737 if (likely(ACCESS_ONCE(efx->irq_soft_enabled))) {
738 /* Note test interrupts */
739 if (context->index == efx->irq_level)
740 efx->last_irq_cpu = raw_smp_processor_id();
741
742 /* Schedule processing of the channel */
743 efx_schedule_channel_irq(efx->channel[context->index]);
744 }
745
746 return IRQ_HANDLED;
747}
748
749static irqreturn_t efx_ef10_legacy_interrupt(int irq, void *dev_id)
750{
751 struct efx_nic *efx = dev_id;
752 bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
753 struct efx_channel *channel;
754 efx_dword_t reg;
755 u32 queues;
756
757 /* Read the ISR which also ACKs the interrupts */
758 efx_readd(efx, &reg, ER_DZ_BIU_INT_ISR);
759 queues = EFX_DWORD_FIELD(reg, ERF_DZ_ISR_REG);
760
761 if (queues == 0)
762 return IRQ_NONE;
763
764 if (likely(soft_enabled)) {
765 /* Note test interrupts */
766 if (queues & (1U << efx->irq_level))
767 efx->last_irq_cpu = raw_smp_processor_id();
768
769 efx_for_each_channel(channel, efx) {
770 if (queues & 1)
771 efx_schedule_channel_irq(channel);
772 queues >>= 1;
773 }
774 }
775
776 netif_vdbg(efx, intr, efx->net_dev,
777 "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
778 irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
779
780 return IRQ_HANDLED;
781}
782
783static void efx_ef10_irq_test_generate(struct efx_nic *efx)
784{
785 MCDI_DECLARE_BUF(inbuf, MC_CMD_TRIGGER_INTERRUPT_IN_LEN);
786
787 BUILD_BUG_ON(MC_CMD_TRIGGER_INTERRUPT_OUT_LEN != 0);
788
789 MCDI_SET_DWORD(inbuf, TRIGGER_INTERRUPT_IN_INTR_LEVEL, efx->irq_level);
790 (void) efx_mcdi_rpc(efx, MC_CMD_TRIGGER_INTERRUPT,
791 inbuf, sizeof(inbuf), NULL, 0, NULL);
792}
793
794static int efx_ef10_tx_probe(struct efx_tx_queue *tx_queue)
795{
796 return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
797 (tx_queue->ptr_mask + 1) *
798 sizeof(efx_qword_t),
799 GFP_KERNEL);
800}
801
802/* This writes to the TX_DESC_WPTR and also pushes data */
803static inline void efx_ef10_push_tx_desc(struct efx_tx_queue *tx_queue,
804 const efx_qword_t *txd)
805{
806 unsigned int write_ptr;
807 efx_oword_t reg;
808
809 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
810 EFX_POPULATE_OWORD_1(reg, ERF_DZ_TX_DESC_WPTR, write_ptr);
811 reg.qword[0] = *txd;
812 efx_writeo_page(tx_queue->efx, &reg,
813 ER_DZ_TX_DESC_UPD, tx_queue->queue);
814}
815
816static void efx_ef10_tx_init(struct efx_tx_queue *tx_queue)
817{
818 MCDI_DECLARE_BUF(inbuf, MC_CMD_INIT_TXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
819 EFX_BUF_SIZE));
820 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_TXQ_OUT_LEN);
821 bool csum_offload = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
822 size_t entries = tx_queue->txd.buf.len / EFX_BUF_SIZE;
823 struct efx_channel *channel = tx_queue->channel;
824 struct efx_nic *efx = tx_queue->efx;
825 size_t inlen, outlen;
826 dma_addr_t dma_addr;
827 efx_qword_t *txd;
828 int rc;
829 int i;
830
831 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_SIZE, tx_queue->ptr_mask + 1);
832 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_TARGET_EVQ, channel->channel);
833 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_LABEL, tx_queue->queue);
834 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_INSTANCE, tx_queue->queue);
835 MCDI_POPULATE_DWORD_2(inbuf, INIT_TXQ_IN_FLAGS,
836 INIT_TXQ_IN_FLAG_IP_CSUM_DIS, !csum_offload,
837 INIT_TXQ_IN_FLAG_TCP_CSUM_DIS, !csum_offload);
838 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_OWNER_ID, 0);
839 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
840
841 dma_addr = tx_queue->txd.buf.dma_addr;
842
843 netif_dbg(efx, hw, efx->net_dev, "pushing TXQ %d. %zu entries (%llx)\n",
844 tx_queue->queue, entries, (u64)dma_addr);
845
846 for (i = 0; i < entries; ++i) {
847 MCDI_SET_ARRAY_QWORD(inbuf, INIT_TXQ_IN_DMA_ADDR, i, dma_addr);
848 dma_addr += EFX_BUF_SIZE;
849 }
850
851 inlen = MC_CMD_INIT_TXQ_IN_LEN(entries);
852
853 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_TXQ, inbuf, inlen,
854 outbuf, sizeof(outbuf), &outlen);
855 if (rc)
856 goto fail;
857
858 /* A previous user of this TX queue might have set us up the
859 * bomb by writing a descriptor to the TX push collector but
860 * not the doorbell. (Each collector belongs to a port, not a
861 * queue or function, so cannot easily be reset.) We must
862 * attempt to push a no-op descriptor in its place.
863 */
864 tx_queue->buffer[0].flags = EFX_TX_BUF_OPTION;
865 tx_queue->insert_count = 1;
866 txd = efx_tx_desc(tx_queue, 0);
867 EFX_POPULATE_QWORD_4(*txd,
868 ESF_DZ_TX_DESC_IS_OPT, true,
869 ESF_DZ_TX_OPTION_TYPE,
870 ESE_DZ_TX_OPTION_DESC_CRC_CSUM,
871 ESF_DZ_TX_OPTION_UDP_TCP_CSUM, csum_offload,
872 ESF_DZ_TX_OPTION_IP_CSUM, csum_offload);
873 tx_queue->write_count = 1;
874 wmb();
875 efx_ef10_push_tx_desc(tx_queue, txd);
876
877 return;
878
879fail:
880 WARN_ON(true);
881 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
882}
883
884static void efx_ef10_tx_fini(struct efx_tx_queue *tx_queue)
885{
886 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_TXQ_IN_LEN);
887 MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_TXQ_OUT_LEN);
888 struct efx_nic *efx = tx_queue->efx;
889 size_t outlen;
890 int rc;
891
892 MCDI_SET_DWORD(inbuf, FINI_TXQ_IN_INSTANCE,
893 tx_queue->queue);
894
895 rc = efx_mcdi_rpc(efx, MC_CMD_FINI_TXQ, inbuf, sizeof(inbuf),
896 outbuf, sizeof(outbuf), &outlen);
897
898 if (rc && rc != -EALREADY)
899 goto fail;
900
901 return;
902
903fail:
904 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
905}
906
907static void efx_ef10_tx_remove(struct efx_tx_queue *tx_queue)
908{
909 efx_nic_free_buffer(tx_queue->efx, &tx_queue->txd.buf);
910}
911
912/* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
913static inline void efx_ef10_notify_tx_desc(struct efx_tx_queue *tx_queue)
914{
915 unsigned int write_ptr;
916 efx_dword_t reg;
917
918 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
919 EFX_POPULATE_DWORD_1(reg, ERF_DZ_TX_DESC_WPTR_DWORD, write_ptr);
920 efx_writed_page(tx_queue->efx, &reg,
921 ER_DZ_TX_DESC_UPD_DWORD, tx_queue->queue);
922}
923
924static void efx_ef10_tx_write(struct efx_tx_queue *tx_queue)
925{
926 unsigned int old_write_count = tx_queue->write_count;
927 struct efx_tx_buffer *buffer;
928 unsigned int write_ptr;
929 efx_qword_t *txd;
930
931 BUG_ON(tx_queue->write_count == tx_queue->insert_count);
932
933 do {
934 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
935 buffer = &tx_queue->buffer[write_ptr];
936 txd = efx_tx_desc(tx_queue, write_ptr);
937 ++tx_queue->write_count;
938
939 /* Create TX descriptor ring entry */
940 if (buffer->flags & EFX_TX_BUF_OPTION) {
941 *txd = buffer->option;
942 } else {
943 BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
944 EFX_POPULATE_QWORD_3(
945 *txd,
946 ESF_DZ_TX_KER_CONT,
947 buffer->flags & EFX_TX_BUF_CONT,
948 ESF_DZ_TX_KER_BYTE_CNT, buffer->len,
949 ESF_DZ_TX_KER_BUF_ADDR, buffer->dma_addr);
950 }
951 } while (tx_queue->write_count != tx_queue->insert_count);
952
953 wmb(); /* Ensure descriptors are written before they are fetched */
954
955 if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
956 txd = efx_tx_desc(tx_queue,
957 old_write_count & tx_queue->ptr_mask);
958 efx_ef10_push_tx_desc(tx_queue, txd);
959 ++tx_queue->pushes;
960 } else {
961 efx_ef10_notify_tx_desc(tx_queue);
962 }
963}
964
965static int efx_ef10_alloc_rss_context(struct efx_nic *efx, u32 *context)
966{
967 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
968 MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
969 size_t outlen;
970 int rc;
971
972 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
973 EVB_PORT_ID_ASSIGNED);
974 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE,
975 MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE);
976 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES,
977 EFX_MAX_CHANNELS);
978
979 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
980 outbuf, sizeof(outbuf), &outlen);
981 if (rc != 0)
982 return rc;
983
984 if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
985 return -EIO;
986
987 *context = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
988
989 return 0;
990}
991
992static void efx_ef10_free_rss_context(struct efx_nic *efx, u32 context)
993{
994 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
995 int rc;
996
997 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
998 context);
999
1000 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
1001 NULL, 0, NULL);
1002 WARN_ON(rc != 0);
1003}
1004
1005static int efx_ef10_populate_rss_table(struct efx_nic *efx, u32 context)
1006{
1007 MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
1008 MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
1009 int i, rc;
1010
1011 MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
1012 context);
1013 BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
1014 MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
1015
1016 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); ++i)
1017 MCDI_PTR(tablebuf,
1018 RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
1019 (u8) efx->rx_indir_table[i];
1020
1021 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
1022 sizeof(tablebuf), NULL, 0, NULL);
1023 if (rc != 0)
1024 return rc;
1025
1026 MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
1027 context);
1028 BUILD_BUG_ON(ARRAY_SIZE(efx->rx_hash_key) !=
1029 MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
1030 for (i = 0; i < ARRAY_SIZE(efx->rx_hash_key); ++i)
1031 MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] =
1032 efx->rx_hash_key[i];
1033
1034 return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
1035 sizeof(keybuf), NULL, 0, NULL);
1036}
1037
1038static void efx_ef10_rx_free_indir_table(struct efx_nic *efx)
1039{
1040 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1041
1042 if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
1043 efx_ef10_free_rss_context(efx, nic_data->rx_rss_context);
1044 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
1045}
1046
1047static void efx_ef10_rx_push_indir_table(struct efx_nic *efx)
1048{
1049 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1050 int rc;
1051
1052 netif_dbg(efx, drv, efx->net_dev, "pushing RX indirection table\n");
1053
1054 if (nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID) {
1055 rc = efx_ef10_alloc_rss_context(efx, &nic_data->rx_rss_context);
1056 if (rc != 0)
1057 goto fail;
1058 }
1059
1060 rc = efx_ef10_populate_rss_table(efx, nic_data->rx_rss_context);
1061 if (rc != 0)
1062 goto fail;
1063
1064 return;
1065
1066fail:
1067 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1068}
1069
1070static int efx_ef10_rx_probe(struct efx_rx_queue *rx_queue)
1071{
1072 return efx_nic_alloc_buffer(rx_queue->efx, &rx_queue->rxd.buf,
1073 (rx_queue->ptr_mask + 1) *
1074 sizeof(efx_qword_t),
1075 GFP_KERNEL);
1076}
1077
1078static void efx_ef10_rx_init(struct efx_rx_queue *rx_queue)
1079{
1080 MCDI_DECLARE_BUF(inbuf,
1081 MC_CMD_INIT_RXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
1082 EFX_BUF_SIZE));
1083 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_RXQ_OUT_LEN);
1084 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
1085 size_t entries = rx_queue->rxd.buf.len / EFX_BUF_SIZE;
1086 struct efx_nic *efx = rx_queue->efx;
1087 size_t inlen, outlen;
1088 dma_addr_t dma_addr;
1089 int rc;
1090 int i;
1091
1092 rx_queue->scatter_n = 0;
1093 rx_queue->scatter_len = 0;
1094
1095 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_SIZE, rx_queue->ptr_mask + 1);
1096 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_TARGET_EVQ, channel->channel);
1097 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_LABEL, efx_rx_queue_index(rx_queue));
1098 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_INSTANCE,
1099 efx_rx_queue_index(rx_queue));
1100 MCDI_POPULATE_DWORD_1(inbuf, INIT_RXQ_IN_FLAGS,
1101 INIT_RXQ_IN_FLAG_PREFIX, 1);
1102 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_OWNER_ID, 0);
1103 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
1104
1105 dma_addr = rx_queue->rxd.buf.dma_addr;
1106
1107 netif_dbg(efx, hw, efx->net_dev, "pushing RXQ %d. %zu entries (%llx)\n",
1108 efx_rx_queue_index(rx_queue), entries, (u64)dma_addr);
1109
1110 for (i = 0; i < entries; ++i) {
1111 MCDI_SET_ARRAY_QWORD(inbuf, INIT_RXQ_IN_DMA_ADDR, i, dma_addr);
1112 dma_addr += EFX_BUF_SIZE;
1113 }
1114
1115 inlen = MC_CMD_INIT_RXQ_IN_LEN(entries);
1116
1117 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_RXQ, inbuf, inlen,
1118 outbuf, sizeof(outbuf), &outlen);
1119 if (rc)
1120 goto fail;
1121
1122 return;
1123
1124fail:
1125 WARN_ON(true);
1126 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1127}
1128
1129static void efx_ef10_rx_fini(struct efx_rx_queue *rx_queue)
1130{
1131 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_RXQ_IN_LEN);
1132 MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_RXQ_OUT_LEN);
1133 struct efx_nic *efx = rx_queue->efx;
1134 size_t outlen;
1135 int rc;
1136
1137 MCDI_SET_DWORD(inbuf, FINI_RXQ_IN_INSTANCE,
1138 efx_rx_queue_index(rx_queue));
1139
1140 rc = efx_mcdi_rpc(efx, MC_CMD_FINI_RXQ, inbuf, sizeof(inbuf),
1141 outbuf, sizeof(outbuf), &outlen);
1142
1143 if (rc && rc != -EALREADY)
1144 goto fail;
1145
1146 return;
1147
1148fail:
1149 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1150}
1151
1152static void efx_ef10_rx_remove(struct efx_rx_queue *rx_queue)
1153{
1154 efx_nic_free_buffer(rx_queue->efx, &rx_queue->rxd.buf);
1155}
1156
1157/* This creates an entry in the RX descriptor queue */
1158static inline void
1159efx_ef10_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
1160{
1161 struct efx_rx_buffer *rx_buf;
1162 efx_qword_t *rxd;
1163
1164 rxd = efx_rx_desc(rx_queue, index);
1165 rx_buf = efx_rx_buffer(rx_queue, index);
1166 EFX_POPULATE_QWORD_2(*rxd,
1167 ESF_DZ_RX_KER_BYTE_CNT, rx_buf->len,
1168 ESF_DZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
1169}
1170
1171static void efx_ef10_rx_write(struct efx_rx_queue *rx_queue)
1172{
1173 struct efx_nic *efx = rx_queue->efx;
1174 unsigned int write_count;
1175 efx_dword_t reg;
1176
1177 /* Firmware requires that RX_DESC_WPTR be a multiple of 8 */
1178 write_count = rx_queue->added_count & ~7;
1179 if (rx_queue->notified_count == write_count)
1180 return;
1181
1182 do
1183 efx_ef10_build_rx_desc(
1184 rx_queue,
1185 rx_queue->notified_count & rx_queue->ptr_mask);
1186 while (++rx_queue->notified_count != write_count);
1187
1188 wmb();
1189 EFX_POPULATE_DWORD_1(reg, ERF_DZ_RX_DESC_WPTR,
1190 write_count & rx_queue->ptr_mask);
1191 efx_writed_page(efx, &reg, ER_DZ_RX_DESC_UPD,
1192 efx_rx_queue_index(rx_queue));
1193}
1194
1195static efx_mcdi_async_completer efx_ef10_rx_defer_refill_complete;
1196
1197static void efx_ef10_rx_defer_refill(struct efx_rx_queue *rx_queue)
1198{
1199 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
1200 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
1201 efx_qword_t event;
1202
1203 EFX_POPULATE_QWORD_2(event,
1204 ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
1205 ESF_DZ_EV_DATA, EFX_EF10_REFILL);
1206
1207 MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
1208
1209 /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
1210 * already swapped the data to little-endian order.
1211 */
1212 memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
1213 sizeof(efx_qword_t));
1214
1215 efx_mcdi_rpc_async(channel->efx, MC_CMD_DRIVER_EVENT,
1216 inbuf, sizeof(inbuf), 0,
1217 efx_ef10_rx_defer_refill_complete, 0);
1218}
1219
1220static void
1221efx_ef10_rx_defer_refill_complete(struct efx_nic *efx, unsigned long cookie,
1222 int rc, efx_dword_t *outbuf,
1223 size_t outlen_actual)
1224{
1225 /* nothing to do */
1226}
1227
1228static int efx_ef10_ev_probe(struct efx_channel *channel)
1229{
1230 return efx_nic_alloc_buffer(channel->efx, &channel->eventq.buf,
1231 (channel->eventq_mask + 1) *
1232 sizeof(efx_qword_t),
1233 GFP_KERNEL);
1234}
1235
1236static int efx_ef10_ev_init(struct efx_channel *channel)
1237{
1238 MCDI_DECLARE_BUF(inbuf,
1239 MC_CMD_INIT_EVQ_IN_LEN(EFX_MAX_EVQ_SIZE * 8 /
1240 EFX_BUF_SIZE));
1241 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_EVQ_OUT_LEN);
1242 size_t entries = channel->eventq.buf.len / EFX_BUF_SIZE;
1243 struct efx_nic *efx = channel->efx;
1244 struct efx_ef10_nic_data *nic_data;
1245 bool supports_rx_merge;
1246 size_t inlen, outlen;
1247 dma_addr_t dma_addr;
1248 int rc;
1249 int i;
1250
1251 nic_data = efx->nic_data;
1252 supports_rx_merge =
1253 !!(nic_data->datapath_caps &
1254 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN);
1255
1256 /* Fill event queue with all ones (i.e. empty events) */
1257 memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
1258
1259 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_SIZE, channel->eventq_mask + 1);
1260 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_INSTANCE, channel->channel);
1261 /* INIT_EVQ expects index in vector table, not absolute */
1262 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_IRQ_NUM, channel->channel);
1263 MCDI_POPULATE_DWORD_4(inbuf, INIT_EVQ_IN_FLAGS,
1264 INIT_EVQ_IN_FLAG_INTERRUPTING, 1,
1265 INIT_EVQ_IN_FLAG_RX_MERGE, 1,
1266 INIT_EVQ_IN_FLAG_TX_MERGE, 1,
1267 INIT_EVQ_IN_FLAG_CUT_THRU, !supports_rx_merge);
1268 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_MODE,
1269 MC_CMD_INIT_EVQ_IN_TMR_MODE_DIS);
1270 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_LOAD, 0);
1271 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_RELOAD, 0);
1272 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_MODE,
1273 MC_CMD_INIT_EVQ_IN_COUNT_MODE_DIS);
1274 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_THRSHLD, 0);
1275
1276 dma_addr = channel->eventq.buf.dma_addr;
1277 for (i = 0; i < entries; ++i) {
1278 MCDI_SET_ARRAY_QWORD(inbuf, INIT_EVQ_IN_DMA_ADDR, i, dma_addr);
1279 dma_addr += EFX_BUF_SIZE;
1280 }
1281
1282 inlen = MC_CMD_INIT_EVQ_IN_LEN(entries);
1283
1284 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_EVQ, inbuf, inlen,
1285 outbuf, sizeof(outbuf), &outlen);
1286 if (rc)
1287 goto fail;
1288
1289 /* IRQ return is ignored */
1290
1291 return 0;
1292
1293fail:
1294 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1295 return rc;
1296}
1297
1298static void efx_ef10_ev_fini(struct efx_channel *channel)
1299{
1300 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_EVQ_IN_LEN);
1301 MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_EVQ_OUT_LEN);
1302 struct efx_nic *efx = channel->efx;
1303 size_t outlen;
1304 int rc;
1305
1306 MCDI_SET_DWORD(inbuf, FINI_EVQ_IN_INSTANCE, channel->channel);
1307
1308 rc = efx_mcdi_rpc(efx, MC_CMD_FINI_EVQ, inbuf, sizeof(inbuf),
1309 outbuf, sizeof(outbuf), &outlen);
1310
1311 if (rc && rc != -EALREADY)
1312 goto fail;
1313
1314 return;
1315
1316fail:
1317 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1318}
1319
1320static void efx_ef10_ev_remove(struct efx_channel *channel)
1321{
1322 efx_nic_free_buffer(channel->efx, &channel->eventq.buf);
1323}
1324
1325static void efx_ef10_handle_rx_wrong_queue(struct efx_rx_queue *rx_queue,
1326 unsigned int rx_queue_label)
1327{
1328 struct efx_nic *efx = rx_queue->efx;
1329
1330 netif_info(efx, hw, efx->net_dev,
1331 "rx event arrived on queue %d labeled as queue %u\n",
1332 efx_rx_queue_index(rx_queue), rx_queue_label);
1333
1334 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
1335}
1336
1337static void
1338efx_ef10_handle_rx_bad_lbits(struct efx_rx_queue *rx_queue,
1339 unsigned int actual, unsigned int expected)
1340{
1341 unsigned int dropped = (actual - expected) & rx_queue->ptr_mask;
1342 struct efx_nic *efx = rx_queue->efx;
1343
1344 netif_info(efx, hw, efx->net_dev,
1345 "dropped %d events (index=%d expected=%d)\n",
1346 dropped, actual, expected);
1347
1348 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
1349}
1350
1351/* partially received RX was aborted. clean up. */
1352static void efx_ef10_handle_rx_abort(struct efx_rx_queue *rx_queue)
1353{
1354 unsigned int rx_desc_ptr;
1355
1356 WARN_ON(rx_queue->scatter_n == 0);
1357
1358 netif_dbg(rx_queue->efx, hw, rx_queue->efx->net_dev,
1359 "scattered RX aborted (dropping %u buffers)\n",
1360 rx_queue->scatter_n);
1361
1362 rx_desc_ptr = rx_queue->removed_count & rx_queue->ptr_mask;
1363
1364 efx_rx_packet(rx_queue, rx_desc_ptr, rx_queue->scatter_n,
1365 0, EFX_RX_PKT_DISCARD);
1366
1367 rx_queue->removed_count += rx_queue->scatter_n;
1368 rx_queue->scatter_n = 0;
1369 rx_queue->scatter_len = 0;
1370 ++efx_rx_queue_channel(rx_queue)->n_rx_nodesc_trunc;
1371}
1372
1373static int efx_ef10_handle_rx_event(struct efx_channel *channel,
1374 const efx_qword_t *event)
1375{
1376 unsigned int rx_bytes, next_ptr_lbits, rx_queue_label, rx_l4_class;
1377 unsigned int n_descs, n_packets, i;
1378 struct efx_nic *efx = channel->efx;
1379 struct efx_rx_queue *rx_queue;
1380 bool rx_cont;
1381 u16 flags = 0;
1382
1383 if (unlikely(ACCESS_ONCE(efx->reset_pending)))
1384 return 0;
1385
1386 /* Basic packet information */
1387 rx_bytes = EFX_QWORD_FIELD(*event, ESF_DZ_RX_BYTES);
1388 next_ptr_lbits = EFX_QWORD_FIELD(*event, ESF_DZ_RX_DSC_PTR_LBITS);
1389 rx_queue_label = EFX_QWORD_FIELD(*event, ESF_DZ_RX_QLABEL);
1390 rx_l4_class = EFX_QWORD_FIELD(*event, ESF_DZ_RX_L4_CLASS);
1391 rx_cont = EFX_QWORD_FIELD(*event, ESF_DZ_RX_CONT);
1392
1393 WARN_ON(EFX_QWORD_FIELD(*event, ESF_DZ_RX_DROP_EVENT));
1394
1395 rx_queue = efx_channel_get_rx_queue(channel);
1396
1397 if (unlikely(rx_queue_label != efx_rx_queue_index(rx_queue)))
1398 efx_ef10_handle_rx_wrong_queue(rx_queue, rx_queue_label);
1399
1400 n_descs = ((next_ptr_lbits - rx_queue->removed_count) &
1401 ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
1402
1403 if (n_descs != rx_queue->scatter_n + 1) {
1404 /* detect rx abort */
1405 if (unlikely(n_descs == rx_queue->scatter_n)) {
1406 WARN_ON(rx_bytes != 0);
1407 efx_ef10_handle_rx_abort(rx_queue);
1408 return 0;
1409 }
1410
1411 if (unlikely(rx_queue->scatter_n != 0)) {
1412 /* Scattered packet completions cannot be
1413 * merged, so something has gone wrong.
1414 */
1415 efx_ef10_handle_rx_bad_lbits(
1416 rx_queue, next_ptr_lbits,
1417 (rx_queue->removed_count +
1418 rx_queue->scatter_n + 1) &
1419 ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
1420 return 0;
1421 }
1422
1423 /* Merged completion for multiple non-scattered packets */
1424 rx_queue->scatter_n = 1;
1425 rx_queue->scatter_len = 0;
1426 n_packets = n_descs;
1427 ++channel->n_rx_merge_events;
1428 channel->n_rx_merge_packets += n_packets;
1429 flags |= EFX_RX_PKT_PREFIX_LEN;
1430 } else {
1431 ++rx_queue->scatter_n;
1432 rx_queue->scatter_len += rx_bytes;
1433 if (rx_cont)
1434 return 0;
1435 n_packets = 1;
1436 }
1437
1438 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_ECRC_ERR)))
1439 flags |= EFX_RX_PKT_DISCARD;
1440
1441 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_IPCKSUM_ERR))) {
1442 channel->n_rx_ip_hdr_chksum_err += n_packets;
1443 } else if (unlikely(EFX_QWORD_FIELD(*event,
1444 ESF_DZ_RX_TCPUDP_CKSUM_ERR))) {
1445 channel->n_rx_tcp_udp_chksum_err += n_packets;
1446 } else if (rx_l4_class == ESE_DZ_L4_CLASS_TCP ||
1447 rx_l4_class == ESE_DZ_L4_CLASS_UDP) {
1448 flags |= EFX_RX_PKT_CSUMMED;
1449 }
1450
1451 if (rx_l4_class == ESE_DZ_L4_CLASS_TCP)
1452 flags |= EFX_RX_PKT_TCP;
1453
1454 channel->irq_mod_score += 2 * n_packets;
1455
1456 /* Handle received packet(s) */
1457 for (i = 0; i < n_packets; i++) {
1458 efx_rx_packet(rx_queue,
1459 rx_queue->removed_count & rx_queue->ptr_mask,
1460 rx_queue->scatter_n, rx_queue->scatter_len,
1461 flags);
1462 rx_queue->removed_count += rx_queue->scatter_n;
1463 }
1464
1465 rx_queue->scatter_n = 0;
1466 rx_queue->scatter_len = 0;
1467
1468 return n_packets;
1469}
1470
1471static int
1472efx_ef10_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
1473{
1474 struct efx_nic *efx = channel->efx;
1475 struct efx_tx_queue *tx_queue;
1476 unsigned int tx_ev_desc_ptr;
1477 unsigned int tx_ev_q_label;
1478 int tx_descs = 0;
1479
1480 if (unlikely(ACCESS_ONCE(efx->reset_pending)))
1481 return 0;
1482
1483 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_TX_DROP_EVENT)))
1484 return 0;
1485
1486 /* Transmit completion */
1487 tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, ESF_DZ_TX_DESCR_INDX);
1488 tx_ev_q_label = EFX_QWORD_FIELD(*event, ESF_DZ_TX_QLABEL);
1489 tx_queue = efx_channel_get_tx_queue(channel,
1490 tx_ev_q_label % EFX_TXQ_TYPES);
1491 tx_descs = ((tx_ev_desc_ptr + 1 - tx_queue->read_count) &
1492 tx_queue->ptr_mask);
1493 efx_xmit_done(tx_queue, tx_ev_desc_ptr & tx_queue->ptr_mask);
1494
1495 return tx_descs;
1496}
1497
1498static void
1499efx_ef10_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
1500{
1501 struct efx_nic *efx = channel->efx;
1502 int subcode;
1503
1504 subcode = EFX_QWORD_FIELD(*event, ESF_DZ_DRV_SUB_CODE);
1505
1506 switch (subcode) {
1507 case ESE_DZ_DRV_TIMER_EV:
1508 case ESE_DZ_DRV_WAKE_UP_EV:
1509 break;
1510 case ESE_DZ_DRV_START_UP_EV:
1511 /* event queue init complete. ok. */
1512 break;
1513 default:
1514 netif_err(efx, hw, efx->net_dev,
1515 "channel %d unknown driver event type %d"
1516 " (data " EFX_QWORD_FMT ")\n",
1517 channel->channel, subcode,
1518 EFX_QWORD_VAL(*event));
1519
1520 }
1521}
1522
1523static void efx_ef10_handle_driver_generated_event(struct efx_channel *channel,
1524 efx_qword_t *event)
1525{
1526 struct efx_nic *efx = channel->efx;
1527 u32 subcode;
1528
1529 subcode = EFX_QWORD_FIELD(*event, EFX_DWORD_0);
1530
1531 switch (subcode) {
1532 case EFX_EF10_TEST:
1533 channel->event_test_cpu = raw_smp_processor_id();
1534 break;
1535 case EFX_EF10_REFILL:
1536 /* The queue must be empty, so we won't receive any rx
1537 * events, so efx_process_channel() won't refill the
1538 * queue. Refill it here
1539 */
1540 efx_fast_push_rx_descriptors(&channel->rx_queue);
1541 break;
1542 default:
1543 netif_err(efx, hw, efx->net_dev,
1544 "channel %d unknown driver event type %u"
1545 " (data " EFX_QWORD_FMT ")\n",
1546 channel->channel, (unsigned) subcode,
1547 EFX_QWORD_VAL(*event));
1548 }
1549}
1550
1551static int efx_ef10_ev_process(struct efx_channel *channel, int quota)
1552{
1553 struct efx_nic *efx = channel->efx;
1554 efx_qword_t event, *p_event;
1555 unsigned int read_ptr;
1556 int ev_code;
1557 int tx_descs = 0;
1558 int spent = 0;
1559
1560 read_ptr = channel->eventq_read_ptr;
1561
1562 for (;;) {
1563 p_event = efx_event(channel, read_ptr);
1564 event = *p_event;
1565
1566 if (!efx_event_present(&event))
1567 break;
1568
1569 EFX_SET_QWORD(*p_event);
1570
1571 ++read_ptr;
1572
1573 ev_code = EFX_QWORD_FIELD(event, ESF_DZ_EV_CODE);
1574
1575 netif_vdbg(efx, drv, efx->net_dev,
1576 "processing event on %d " EFX_QWORD_FMT "\n",
1577 channel->channel, EFX_QWORD_VAL(event));
1578
1579 switch (ev_code) {
1580 case ESE_DZ_EV_CODE_MCDI_EV:
1581 efx_mcdi_process_event(channel, &event);
1582 break;
1583 case ESE_DZ_EV_CODE_RX_EV:
1584 spent += efx_ef10_handle_rx_event(channel, &event);
1585 if (spent >= quota) {
1586 /* XXX can we split a merged event to
1587 * avoid going over-quota?
1588 */
1589 spent = quota;
1590 goto out;
1591 }
1592 break;
1593 case ESE_DZ_EV_CODE_TX_EV:
1594 tx_descs += efx_ef10_handle_tx_event(channel, &event);
1595 if (tx_descs > efx->txq_entries) {
1596 spent = quota;
1597 goto out;
1598 } else if (++spent == quota) {
1599 goto out;
1600 }
1601 break;
1602 case ESE_DZ_EV_CODE_DRIVER_EV:
1603 efx_ef10_handle_driver_event(channel, &event);
1604 if (++spent == quota)
1605 goto out;
1606 break;
1607 case EFX_EF10_DRVGEN_EV:
1608 efx_ef10_handle_driver_generated_event(channel, &event);
1609 break;
1610 default:
1611 netif_err(efx, hw, efx->net_dev,
1612 "channel %d unknown event type %d"
1613 " (data " EFX_QWORD_FMT ")\n",
1614 channel->channel, ev_code,
1615 EFX_QWORD_VAL(event));
1616 }
1617 }
1618
1619out:
1620 channel->eventq_read_ptr = read_ptr;
1621 return spent;
1622}
1623
1624static void efx_ef10_ev_read_ack(struct efx_channel *channel)
1625{
1626 struct efx_nic *efx = channel->efx;
1627 efx_dword_t rptr;
1628
1629 if (EFX_EF10_WORKAROUND_35388(efx)) {
1630 BUILD_BUG_ON(EFX_MIN_EVQ_SIZE <
1631 (1 << ERF_DD_EVQ_IND_RPTR_WIDTH));
1632 BUILD_BUG_ON(EFX_MAX_EVQ_SIZE >
1633 (1 << 2 * ERF_DD_EVQ_IND_RPTR_WIDTH));
1634
1635 EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
1636 EFE_DD_EVQ_IND_RPTR_FLAGS_HIGH,
1637 ERF_DD_EVQ_IND_RPTR,
1638 (channel->eventq_read_ptr &
1639 channel->eventq_mask) >>
1640 ERF_DD_EVQ_IND_RPTR_WIDTH);
1641 efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
1642 channel->channel);
1643 EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
1644 EFE_DD_EVQ_IND_RPTR_FLAGS_LOW,
1645 ERF_DD_EVQ_IND_RPTR,
1646 channel->eventq_read_ptr &
1647 ((1 << ERF_DD_EVQ_IND_RPTR_WIDTH) - 1));
1648 efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
1649 channel->channel);
1650 } else {
1651 EFX_POPULATE_DWORD_1(rptr, ERF_DZ_EVQ_RPTR,
1652 channel->eventq_read_ptr &
1653 channel->eventq_mask);
1654 efx_writed_page(efx, &rptr, ER_DZ_EVQ_RPTR, channel->channel);
1655 }
1656}
1657
1658static void efx_ef10_ev_test_generate(struct efx_channel *channel)
1659{
1660 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
1661 struct efx_nic *efx = channel->efx;
1662 efx_qword_t event;
1663 int rc;
1664
1665 EFX_POPULATE_QWORD_2(event,
1666 ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
1667 ESF_DZ_EV_DATA, EFX_EF10_TEST);
1668
1669 MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
1670
1671 /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
1672 * already swapped the data to little-endian order.
1673 */
1674 memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
1675 sizeof(efx_qword_t));
1676
1677 rc = efx_mcdi_rpc(efx, MC_CMD_DRIVER_EVENT, inbuf, sizeof(inbuf),
1678 NULL, 0, NULL);
1679 if (rc != 0)
1680 goto fail;
1681
1682 return;
1683
1684fail:
1685 WARN_ON(true);
1686 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1687}
1688
1689void efx_ef10_handle_drain_event(struct efx_nic *efx)
1690{
1691 if (atomic_dec_and_test(&efx->active_queues))
1692 wake_up(&efx->flush_wq);
1693
1694 WARN_ON(atomic_read(&efx->active_queues) < 0);
1695}
1696
1697static int efx_ef10_fini_dmaq(struct efx_nic *efx)
1698{
1699 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1700 struct efx_channel *channel;
1701 struct efx_tx_queue *tx_queue;
1702 struct efx_rx_queue *rx_queue;
1703 int pending;
1704
1705 /* If the MC has just rebooted, the TX/RX queues will have already been
1706 * torn down, but efx->active_queues needs to be set to zero.
1707 */
1708 if (nic_data->must_realloc_vis) {
1709 atomic_set(&efx->active_queues, 0);
1710 return 0;
1711 }
1712
1713 /* Do not attempt to write to the NIC during EEH recovery */
1714 if (efx->state != STATE_RECOVERY) {
1715 efx_for_each_channel(channel, efx) {
1716 efx_for_each_channel_rx_queue(rx_queue, channel)
1717 efx_ef10_rx_fini(rx_queue);
1718 efx_for_each_channel_tx_queue(tx_queue, channel)
1719 efx_ef10_tx_fini(tx_queue);
1720 }
1721
1722 wait_event_timeout(efx->flush_wq,
1723 atomic_read(&efx->active_queues) == 0,
1724 msecs_to_jiffies(EFX_MAX_FLUSH_TIME));
1725 pending = atomic_read(&efx->active_queues);
1726 if (pending) {
1727 netif_err(efx, hw, efx->net_dev, "failed to flush %d queues\n",
1728 pending);
1729 return -ETIMEDOUT;
1730 }
1731 }
1732
1733 return 0;
1734}
1735
1736static bool efx_ef10_filter_equal(const struct efx_filter_spec *left,
1737 const struct efx_filter_spec *right)
1738{
1739 if ((left->match_flags ^ right->match_flags) |
1740 ((left->flags ^ right->flags) &
1741 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
1742 return false;
1743
1744 return memcmp(&left->outer_vid, &right->outer_vid,
1745 sizeof(struct efx_filter_spec) -
1746 offsetof(struct efx_filter_spec, outer_vid)) == 0;
1747}
1748
1749static unsigned int efx_ef10_filter_hash(const struct efx_filter_spec *spec)
1750{
1751 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
1752 return jhash2((const u32 *)&spec->outer_vid,
1753 (sizeof(struct efx_filter_spec) -
1754 offsetof(struct efx_filter_spec, outer_vid)) / 4,
1755 0);
1756 /* XXX should we randomise the initval? */
1757}
1758
1759/* Decide whether a filter should be exclusive or else should allow
1760 * delivery to additional recipients. Currently we decide that
1761 * filters for specific local unicast MAC and IP addresses are
1762 * exclusive.
1763 */
1764static bool efx_ef10_filter_is_exclusive(const struct efx_filter_spec *spec)
1765{
1766 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
1767 !is_multicast_ether_addr(spec->loc_mac))
1768 return true;
1769
1770 if ((spec->match_flags &
1771 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
1772 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
1773 if (spec->ether_type == htons(ETH_P_IP) &&
1774 !ipv4_is_multicast(spec->loc_host[0]))
1775 return true;
1776 if (spec->ether_type == htons(ETH_P_IPV6) &&
1777 ((const u8 *)spec->loc_host)[0] != 0xff)
1778 return true;
1779 }
1780
1781 return false;
1782}
1783
1784static struct efx_filter_spec *
1785efx_ef10_filter_entry_spec(const struct efx_ef10_filter_table *table,
1786 unsigned int filter_idx)
1787{
1788 return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
1789 ~EFX_EF10_FILTER_FLAGS);
1790}
1791
1792static unsigned int
1793efx_ef10_filter_entry_flags(const struct efx_ef10_filter_table *table,
1794 unsigned int filter_idx)
1795{
1796 return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
1797}
1798
1799static void
1800efx_ef10_filter_set_entry(struct efx_ef10_filter_table *table,
1801 unsigned int filter_idx,
1802 const struct efx_filter_spec *spec,
1803 unsigned int flags)
1804{
1805 table->entry[filter_idx].spec = (unsigned long)spec | flags;
1806}
1807
1808static void efx_ef10_filter_push_prep(struct efx_nic *efx,
1809 const struct efx_filter_spec *spec,
1810 efx_dword_t *inbuf, u64 handle,
1811 bool replacing)
1812{
1813 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1814
1815 memset(inbuf, 0, MC_CMD_FILTER_OP_IN_LEN);
1816
1817 if (replacing) {
1818 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
1819 MC_CMD_FILTER_OP_IN_OP_REPLACE);
1820 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
1821 } else {
1822 u32 match_fields = 0;
1823
1824 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
1825 efx_ef10_filter_is_exclusive(spec) ?
1826 MC_CMD_FILTER_OP_IN_OP_INSERT :
1827 MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
1828
1829 /* Convert match flags and values. Unlike almost
1830 * everything else in MCDI, these fields are in
1831 * network byte order.
1832 */
1833 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
1834 match_fields |=
1835 is_multicast_ether_addr(spec->loc_mac) ?
1836 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST_LBN :
1837 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
1838#define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
1839 if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
1840 match_fields |= \
1841 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
1842 mcdi_field ## _LBN; \
1843 BUILD_BUG_ON( \
1844 MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
1845 sizeof(spec->gen_field)); \
1846 memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
1847 &spec->gen_field, sizeof(spec->gen_field)); \
1848 }
1849 COPY_FIELD(REM_HOST, rem_host, SRC_IP);
1850 COPY_FIELD(LOC_HOST, loc_host, DST_IP);
1851 COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
1852 COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
1853 COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
1854 COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
1855 COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
1856 COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
1857 COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
1858 COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
1859#undef COPY_FIELD
1860 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
1861 match_fields);
1862 }
1863
1864 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
1865 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
1866 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
1867 MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
1868 MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
1869 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
1870 MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
1871 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE, spec->dmaq_id);
1872 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
1873 (spec->flags & EFX_FILTER_FLAG_RX_RSS) ?
1874 MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
1875 MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
1876 if (spec->flags & EFX_FILTER_FLAG_RX_RSS)
1877 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT,
1878 spec->rss_context !=
1879 EFX_FILTER_RSS_CONTEXT_DEFAULT ?
1880 spec->rss_context : nic_data->rx_rss_context);
1881}
1882
1883static int efx_ef10_filter_push(struct efx_nic *efx,
1884 const struct efx_filter_spec *spec,
1885 u64 *handle, bool replacing)
1886{
1887 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
1888 MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_OUT_LEN);
1889 int rc;
1890
1891 efx_ef10_filter_push_prep(efx, spec, inbuf, *handle, replacing);
1892 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
1893 outbuf, sizeof(outbuf), NULL);
1894 if (rc == 0)
1895 *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
1896 return rc;
1897}
1898
1899static int efx_ef10_filter_rx_match_pri(struct efx_ef10_filter_table *table,
1900 enum efx_filter_match_flags match_flags)
1901{
1902 unsigned int match_pri;
1903
1904 for (match_pri = 0;
1905 match_pri < table->rx_match_count;
1906 match_pri++)
1907 if (table->rx_match_flags[match_pri] == match_flags)
1908 return match_pri;
1909
1910 return -EPROTONOSUPPORT;
1911}
1912
1913static s32 efx_ef10_filter_insert(struct efx_nic *efx,
1914 struct efx_filter_spec *spec,
1915 bool replace_equal)
1916{
1917 struct efx_ef10_filter_table *table = efx->filter_state;
1918 DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
1919 struct efx_filter_spec *saved_spec;
1920 unsigned int match_pri, hash;
1921 unsigned int priv_flags;
1922 bool replacing = false;
1923 int ins_index = -1;
1924 DEFINE_WAIT(wait);
1925 bool is_mc_recip;
1926 s32 rc;
1927
1928 /* For now, only support RX filters */
1929 if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
1930 EFX_FILTER_FLAG_RX)
1931 return -EINVAL;
1932
1933 rc = efx_ef10_filter_rx_match_pri(table, spec->match_flags);
1934 if (rc < 0)
1935 return rc;
1936 match_pri = rc;
1937
1938 hash = efx_ef10_filter_hash(spec);
1939 is_mc_recip = efx_filter_is_mc_recipient(spec);
1940 if (is_mc_recip)
1941 bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
1942
1943 /* Find any existing filters with the same match tuple or
1944 * else a free slot to insert at. If any of them are busy,
1945 * we have to wait and retry.
1946 */
1947 for (;;) {
1948 unsigned int depth = 1;
1949 unsigned int i;
1950
1951 spin_lock_bh(&efx->filter_lock);
1952
1953 for (;;) {
1954 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
1955 saved_spec = efx_ef10_filter_entry_spec(table, i);
1956
1957 if (!saved_spec) {
1958 if (ins_index < 0)
1959 ins_index = i;
1960 } else if (efx_ef10_filter_equal(spec, saved_spec)) {
1961 if (table->entry[i].spec &
1962 EFX_EF10_FILTER_FLAG_BUSY)
1963 break;
1964 if (spec->priority < saved_spec->priority &&
1965 !(saved_spec->priority ==
1966 EFX_FILTER_PRI_REQUIRED &&
1967 saved_spec->flags &
1968 EFX_FILTER_FLAG_RX_STACK)) {
1969 rc = -EPERM;
1970 goto out_unlock;
1971 }
1972 if (!is_mc_recip) {
1973 /* This is the only one */
1974 if (spec->priority ==
1975 saved_spec->priority &&
1976 !replace_equal) {
1977 rc = -EEXIST;
1978 goto out_unlock;
1979 }
1980 ins_index = i;
1981 goto found;
1982 } else if (spec->priority >
1983 saved_spec->priority ||
1984 (spec->priority ==
1985 saved_spec->priority &&
1986 replace_equal)) {
1987 if (ins_index < 0)
1988 ins_index = i;
1989 else
1990 __set_bit(depth, mc_rem_map);
1991 }
1992 }
1993
1994 /* Once we reach the maximum search depth, use
1995 * the first suitable slot or return -EBUSY if
1996 * there was none
1997 */
1998 if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
1999 if (ins_index < 0) {
2000 rc = -EBUSY;
2001 goto out_unlock;
2002 }
2003 goto found;
2004 }
2005
2006 ++depth;
2007 }
2008
2009 prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
2010 spin_unlock_bh(&efx->filter_lock);
2011 schedule();
2012 }
2013
2014found:
2015 /* Create a software table entry if necessary, and mark it
2016 * busy. We might yet fail to insert, but any attempt to
2017 * insert a conflicting filter while we're waiting for the
2018 * firmware must find the busy entry.
2019 */
2020 saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
2021 if (saved_spec) {
2022 if (spec->flags & EFX_FILTER_FLAG_RX_STACK) {
2023 /* Just make sure it won't be removed */
2024 saved_spec->flags |= EFX_FILTER_FLAG_RX_STACK;
2025 table->entry[ins_index].spec &=
2026 ~EFX_EF10_FILTER_FLAG_STACK_OLD;
2027 rc = ins_index;
2028 goto out_unlock;
2029 }
2030 replacing = true;
2031 priv_flags = efx_ef10_filter_entry_flags(table, ins_index);
2032 } else {
2033 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
2034 if (!saved_spec) {
2035 rc = -ENOMEM;
2036 goto out_unlock;
2037 }
2038 *saved_spec = *spec;
2039 priv_flags = 0;
2040 }
2041 efx_ef10_filter_set_entry(table, ins_index, saved_spec,
2042 priv_flags | EFX_EF10_FILTER_FLAG_BUSY);
2043
2044 /* Mark lower-priority multicast recipients busy prior to removal */
2045 if (is_mc_recip) {
2046 unsigned int depth, i;
2047
2048 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
2049 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2050 if (test_bit(depth, mc_rem_map))
2051 table->entry[i].spec |=
2052 EFX_EF10_FILTER_FLAG_BUSY;
2053 }
2054 }
2055
2056 spin_unlock_bh(&efx->filter_lock);
2057
2058 rc = efx_ef10_filter_push(efx, spec, &table->entry[ins_index].handle,
2059 replacing);
2060
2061 /* Finalise the software table entry */
2062 spin_lock_bh(&efx->filter_lock);
2063 if (rc == 0) {
2064 if (replacing) {
2065 /* Update the fields that may differ */
2066 saved_spec->priority = spec->priority;
2067 saved_spec->flags &= EFX_FILTER_FLAG_RX_STACK;
2068 saved_spec->flags |= spec->flags;
2069 saved_spec->rss_context = spec->rss_context;
2070 saved_spec->dmaq_id = spec->dmaq_id;
2071 }
2072 } else if (!replacing) {
2073 kfree(saved_spec);
2074 saved_spec = NULL;
2075 }
2076 efx_ef10_filter_set_entry(table, ins_index, saved_spec, priv_flags);
2077
2078 /* Remove and finalise entries for lower-priority multicast
2079 * recipients
2080 */
2081 if (is_mc_recip) {
2082 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
2083 unsigned int depth, i;
2084
2085 memset(inbuf, 0, sizeof(inbuf));
2086
2087 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
2088 if (!test_bit(depth, mc_rem_map))
2089 continue;
2090
2091 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2092 saved_spec = efx_ef10_filter_entry_spec(table, i);
2093 priv_flags = efx_ef10_filter_entry_flags(table, i);
2094
2095 if (rc == 0) {
2096 spin_unlock_bh(&efx->filter_lock);
2097 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2098 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
2099 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2100 table->entry[i].handle);
2101 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
2102 inbuf, sizeof(inbuf),
2103 NULL, 0, NULL);
2104 spin_lock_bh(&efx->filter_lock);
2105 }
2106
2107 if (rc == 0) {
2108 kfree(saved_spec);
2109 saved_spec = NULL;
2110 priv_flags = 0;
2111 } else {
2112 priv_flags &= ~EFX_EF10_FILTER_FLAG_BUSY;
2113 }
2114 efx_ef10_filter_set_entry(table, i, saved_spec,
2115 priv_flags);
2116 }
2117 }
2118
2119 /* If successful, return the inserted filter ID */
2120 if (rc == 0)
2121 rc = match_pri * HUNT_FILTER_TBL_ROWS + ins_index;
2122
2123 wake_up_all(&table->waitq);
2124out_unlock:
2125 spin_unlock_bh(&efx->filter_lock);
2126 finish_wait(&table->waitq, &wait);
2127 return rc;
2128}
2129
2130void efx_ef10_filter_update_rx_scatter(struct efx_nic *efx)
2131{
2132 /* no need to do anything here on EF10 */
2133}
2134
2135/* Remove a filter.
2136 * If !stack_requested, remove by ID
2137 * If stack_requested, remove by index
2138 * Filter ID may come from userland and must be range-checked.
2139 */
2140static int efx_ef10_filter_remove_internal(struct efx_nic *efx,
2141 enum efx_filter_priority priority,
2142 u32 filter_id, bool stack_requested)
2143{
2144 unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
2145 struct efx_ef10_filter_table *table = efx->filter_state;
2146 MCDI_DECLARE_BUF(inbuf,
2147 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
2148 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
2149 struct efx_filter_spec *spec;
2150 DEFINE_WAIT(wait);
2151 int rc;
2152
2153 /* Find the software table entry and mark it busy. Don't
2154 * remove it yet; any attempt to update while we're waiting
2155 * for the firmware must find the busy entry.
2156 */
2157 for (;;) {
2158 spin_lock_bh(&efx->filter_lock);
2159 if (!(table->entry[filter_idx].spec &
2160 EFX_EF10_FILTER_FLAG_BUSY))
2161 break;
2162 prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
2163 spin_unlock_bh(&efx->filter_lock);
2164 schedule();
2165 }
2166 spec = efx_ef10_filter_entry_spec(table, filter_idx);
2167 if (!spec || spec->priority > priority ||
2168 (!stack_requested &&
2169 efx_ef10_filter_rx_match_pri(table, spec->match_flags) !=
2170 filter_id / HUNT_FILTER_TBL_ROWS)) {
2171 rc = -ENOENT;
2172 goto out_unlock;
2173 }
2174 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
2175 spin_unlock_bh(&efx->filter_lock);
2176
2177 if (spec->flags & EFX_FILTER_FLAG_RX_STACK && !stack_requested) {
2178 /* Reset steering of a stack-owned filter */
2179
2180 struct efx_filter_spec new_spec = *spec;
2181
2182 new_spec.priority = EFX_FILTER_PRI_REQUIRED;
2183 new_spec.flags = (EFX_FILTER_FLAG_RX |
2184 EFX_FILTER_FLAG_RX_RSS |
2185 EFX_FILTER_FLAG_RX_STACK);
2186 new_spec.dmaq_id = 0;
2187 new_spec.rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
2188 rc = efx_ef10_filter_push(efx, &new_spec,
2189 &table->entry[filter_idx].handle,
2190 true);
2191
2192 spin_lock_bh(&efx->filter_lock);
2193 if (rc == 0)
2194 *spec = new_spec;
2195 } else {
2196 /* Really remove the filter */
2197
2198 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2199 efx_ef10_filter_is_exclusive(spec) ?
2200 MC_CMD_FILTER_OP_IN_OP_REMOVE :
2201 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
2202 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2203 table->entry[filter_idx].handle);
2204 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
2205 inbuf, sizeof(inbuf), NULL, 0, NULL);
2206
2207 spin_lock_bh(&efx->filter_lock);
2208 if (rc == 0) {
2209 kfree(spec);
2210 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
2211 }
2212 }
2213 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
2214 wake_up_all(&table->waitq);
2215out_unlock:
2216 spin_unlock_bh(&efx->filter_lock);
2217 finish_wait(&table->waitq, &wait);
2218 return rc;
2219}
2220
2221static int efx_ef10_filter_remove_safe(struct efx_nic *efx,
2222 enum efx_filter_priority priority,
2223 u32 filter_id)
2224{
2225 return efx_ef10_filter_remove_internal(efx, priority, filter_id, false);
2226}
2227
2228static int efx_ef10_filter_get_safe(struct efx_nic *efx,
2229 enum efx_filter_priority priority,
2230 u32 filter_id, struct efx_filter_spec *spec)
2231{
2232 unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
2233 struct efx_ef10_filter_table *table = efx->filter_state;
2234 const struct efx_filter_spec *saved_spec;
2235 int rc;
2236
2237 spin_lock_bh(&efx->filter_lock);
2238 saved_spec = efx_ef10_filter_entry_spec(table, filter_idx);
2239 if (saved_spec && saved_spec->priority == priority &&
2240 efx_ef10_filter_rx_match_pri(table, saved_spec->match_flags) ==
2241 filter_id / HUNT_FILTER_TBL_ROWS) {
2242 *spec = *saved_spec;
2243 rc = 0;
2244 } else {
2245 rc = -ENOENT;
2246 }
2247 spin_unlock_bh(&efx->filter_lock);
2248 return rc;
2249}
2250
2251static void efx_ef10_filter_clear_rx(struct efx_nic *efx,
2252 enum efx_filter_priority priority)
2253{
2254 /* TODO */
2255}
2256
2257static u32 efx_ef10_filter_count_rx_used(struct efx_nic *efx,
2258 enum efx_filter_priority priority)
2259{
2260 struct efx_ef10_filter_table *table = efx->filter_state;
2261 unsigned int filter_idx;
2262 s32 count = 0;
2263
2264 spin_lock_bh(&efx->filter_lock);
2265 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
2266 if (table->entry[filter_idx].spec &&
2267 efx_ef10_filter_entry_spec(table, filter_idx)->priority ==
2268 priority)
2269 ++count;
2270 }
2271 spin_unlock_bh(&efx->filter_lock);
2272 return count;
2273}
2274
2275static u32 efx_ef10_filter_get_rx_id_limit(struct efx_nic *efx)
2276{
2277 struct efx_ef10_filter_table *table = efx->filter_state;
2278
2279 return table->rx_match_count * HUNT_FILTER_TBL_ROWS;
2280}
2281
2282static s32 efx_ef10_filter_get_rx_ids(struct efx_nic *efx,
2283 enum efx_filter_priority priority,
2284 u32 *buf, u32 size)
2285{
2286 struct efx_ef10_filter_table *table = efx->filter_state;
2287 struct efx_filter_spec *spec;
2288 unsigned int filter_idx;
2289 s32 count = 0;
2290
2291 spin_lock_bh(&efx->filter_lock);
2292 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
2293 spec = efx_ef10_filter_entry_spec(table, filter_idx);
2294 if (spec && spec->priority == priority) {
2295 if (count == size) {
2296 count = -EMSGSIZE;
2297 break;
2298 }
2299 buf[count++] = (efx_ef10_filter_rx_match_pri(
2300 table, spec->match_flags) *
2301 HUNT_FILTER_TBL_ROWS +
2302 filter_idx);
2303 }
2304 }
2305 spin_unlock_bh(&efx->filter_lock);
2306 return count;
2307}
2308
2309#ifdef CONFIG_RFS_ACCEL
2310
2311static efx_mcdi_async_completer efx_ef10_filter_rfs_insert_complete;
2312
2313static s32 efx_ef10_filter_rfs_insert(struct efx_nic *efx,
2314 struct efx_filter_spec *spec)
2315{
2316 struct efx_ef10_filter_table *table = efx->filter_state;
2317 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
2318 struct efx_filter_spec *saved_spec;
2319 unsigned int hash, i, depth = 1;
2320 bool replacing = false;
2321 int ins_index = -1;
2322 u64 cookie;
2323 s32 rc;
2324
2325 /* Must be an RX filter without RSS and not for a multicast
2326 * destination address (RFS only works for connected sockets).
2327 * These restrictions allow us to pass only a tiny amount of
2328 * data through to the completion function.
2329 */
2330 EFX_WARN_ON_PARANOID(spec->flags !=
2331 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_RX_SCATTER));
2332 EFX_WARN_ON_PARANOID(spec->priority != EFX_FILTER_PRI_HINT);
2333 EFX_WARN_ON_PARANOID(efx_filter_is_mc_recipient(spec));
2334
2335 hash = efx_ef10_filter_hash(spec);
2336
2337 spin_lock_bh(&efx->filter_lock);
2338
2339 /* Find any existing filter with the same match tuple or else
2340 * a free slot to insert at. If an existing filter is busy,
2341 * we have to give up.
2342 */
2343 for (;;) {
2344 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2345 saved_spec = efx_ef10_filter_entry_spec(table, i);
2346
2347 if (!saved_spec) {
2348 if (ins_index < 0)
2349 ins_index = i;
2350 } else if (efx_ef10_filter_equal(spec, saved_spec)) {
2351 if (table->entry[i].spec & EFX_EF10_FILTER_FLAG_BUSY) {
2352 rc = -EBUSY;
2353 goto fail_unlock;
2354 }
2355 EFX_WARN_ON_PARANOID(saved_spec->flags &
2356 EFX_FILTER_FLAG_RX_STACK);
2357 if (spec->priority < saved_spec->priority) {
2358 rc = -EPERM;
2359 goto fail_unlock;
2360 }
2361 ins_index = i;
2362 break;
2363 }
2364
2365 /* Once we reach the maximum search depth, use the
2366 * first suitable slot or return -EBUSY if there was
2367 * none
2368 */
2369 if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
2370 if (ins_index < 0) {
2371 rc = -EBUSY;
2372 goto fail_unlock;
2373 }
2374 break;
2375 }
2376
2377 ++depth;
2378 }
2379
2380 /* Create a software table entry if necessary, and mark it
2381 * busy. We might yet fail to insert, but any attempt to
2382 * insert a conflicting filter while we're waiting for the
2383 * firmware must find the busy entry.
2384 */
2385 saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
2386 if (saved_spec) {
2387 replacing = true;
2388 } else {
2389 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
2390 if (!saved_spec) {
2391 rc = -ENOMEM;
2392 goto fail_unlock;
2393 }
2394 *saved_spec = *spec;
2395 }
2396 efx_ef10_filter_set_entry(table, ins_index, saved_spec,
2397 EFX_EF10_FILTER_FLAG_BUSY);
2398
2399 spin_unlock_bh(&efx->filter_lock);
2400
2401 /* Pack up the variables needed on completion */
2402 cookie = replacing << 31 | ins_index << 16 | spec->dmaq_id;
2403
2404 efx_ef10_filter_push_prep(efx, spec, inbuf,
2405 table->entry[ins_index].handle, replacing);
2406 efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
2407 MC_CMD_FILTER_OP_OUT_LEN,
2408 efx_ef10_filter_rfs_insert_complete, cookie);
2409
2410 return ins_index;
2411
2412fail_unlock:
2413 spin_unlock_bh(&efx->filter_lock);
2414 return rc;
2415}
2416
2417static void
2418efx_ef10_filter_rfs_insert_complete(struct efx_nic *efx, unsigned long cookie,
2419 int rc, efx_dword_t *outbuf,
2420 size_t outlen_actual)
2421{
2422 struct efx_ef10_filter_table *table = efx->filter_state;
2423 unsigned int ins_index, dmaq_id;
2424 struct efx_filter_spec *spec;
2425 bool replacing;
2426
2427 /* Unpack the cookie */
2428 replacing = cookie >> 31;
2429 ins_index = (cookie >> 16) & (HUNT_FILTER_TBL_ROWS - 1);
2430 dmaq_id = cookie & 0xffff;
2431
2432 spin_lock_bh(&efx->filter_lock);
2433 spec = efx_ef10_filter_entry_spec(table, ins_index);
2434 if (rc == 0) {
2435 table->entry[ins_index].handle =
2436 MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
2437 if (replacing)
2438 spec->dmaq_id = dmaq_id;
2439 } else if (!replacing) {
2440 kfree(spec);
2441 spec = NULL;
2442 }
2443 efx_ef10_filter_set_entry(table, ins_index, spec, 0);
2444 spin_unlock_bh(&efx->filter_lock);
2445
2446 wake_up_all(&table->waitq);
2447}
2448
2449static void
2450efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
2451 unsigned long filter_idx,
2452 int rc, efx_dword_t *outbuf,
2453 size_t outlen_actual);
2454
2455static bool efx_ef10_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
2456 unsigned int filter_idx)
2457{
2458 struct efx_ef10_filter_table *table = efx->filter_state;
2459 struct efx_filter_spec *spec =
2460 efx_ef10_filter_entry_spec(table, filter_idx);
2461 MCDI_DECLARE_BUF(inbuf,
2462 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
2463 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
2464
2465 if (!spec ||
2466 (table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAG_BUSY) ||
2467 spec->priority != EFX_FILTER_PRI_HINT ||
2468 !rps_may_expire_flow(efx->net_dev, spec->dmaq_id,
2469 flow_id, filter_idx))
2470 return false;
2471
2472 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2473 MC_CMD_FILTER_OP_IN_OP_REMOVE);
2474 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2475 table->entry[filter_idx].handle);
2476 if (efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf), 0,
2477 efx_ef10_filter_rfs_expire_complete, filter_idx))
2478 return false;
2479
2480 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
2481 return true;
2482}
2483
2484static void
2485efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
2486 unsigned long filter_idx,
2487 int rc, efx_dword_t *outbuf,
2488 size_t outlen_actual)
2489{
2490 struct efx_ef10_filter_table *table = efx->filter_state;
2491 struct efx_filter_spec *spec =
2492 efx_ef10_filter_entry_spec(table, filter_idx);
2493
2494 spin_lock_bh(&efx->filter_lock);
2495 if (rc == 0) {
2496 kfree(spec);
2497 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
2498 }
2499 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
2500 wake_up_all(&table->waitq);
2501 spin_unlock_bh(&efx->filter_lock);
2502}
2503
2504#endif /* CONFIG_RFS_ACCEL */
2505
2506static int efx_ef10_filter_match_flags_from_mcdi(u32 mcdi_flags)
2507{
2508 int match_flags = 0;
2509
2510#define MAP_FLAG(gen_flag, mcdi_field) { \
2511 u32 old_mcdi_flags = mcdi_flags; \
2512 mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
2513 mcdi_field ## _LBN); \
2514 if (mcdi_flags != old_mcdi_flags) \
2515 match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
2516 }
2517 MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
2518 MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
2519 MAP_FLAG(REM_HOST, SRC_IP);
2520 MAP_FLAG(LOC_HOST, DST_IP);
2521 MAP_FLAG(REM_MAC, SRC_MAC);
2522 MAP_FLAG(REM_PORT, SRC_PORT);
2523 MAP_FLAG(LOC_MAC, DST_MAC);
2524 MAP_FLAG(LOC_PORT, DST_PORT);
2525 MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
2526 MAP_FLAG(INNER_VID, INNER_VLAN);
2527 MAP_FLAG(OUTER_VID, OUTER_VLAN);
2528 MAP_FLAG(IP_PROTO, IP_PROTO);
2529#undef MAP_FLAG
2530
2531 /* Did we map them all? */
2532 if (mcdi_flags)
2533 return -EINVAL;
2534
2535 return match_flags;
2536}
2537
2538static int efx_ef10_filter_table_probe(struct efx_nic *efx)
2539{
2540 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
2541 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
2542 unsigned int pd_match_pri, pd_match_count;
2543 struct efx_ef10_filter_table *table;
2544 size_t outlen;
2545 int rc;
2546
2547 table = kzalloc(sizeof(*table), GFP_KERNEL);
2548 if (!table)
2549 return -ENOMEM;
2550
2551 /* Find out which RX filter types are supported, and their priorities */
2552 MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
2553 MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
2554 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
2555 inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
2556 &outlen);
2557 if (rc)
2558 goto fail;
2559 pd_match_count = MCDI_VAR_ARRAY_LEN(
2560 outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
2561 table->rx_match_count = 0;
2562
2563 for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
2564 u32 mcdi_flags =
2565 MCDI_ARRAY_DWORD(
2566 outbuf,
2567 GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
2568 pd_match_pri);
2569 rc = efx_ef10_filter_match_flags_from_mcdi(mcdi_flags);
2570 if (rc < 0) {
2571 netif_dbg(efx, probe, efx->net_dev,
2572 "%s: fw flags %#x pri %u not supported in driver\n",
2573 __func__, mcdi_flags, pd_match_pri);
2574 } else {
2575 netif_dbg(efx, probe, efx->net_dev,
2576 "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
2577 __func__, mcdi_flags, pd_match_pri,
2578 rc, table->rx_match_count);
2579 table->rx_match_flags[table->rx_match_count++] = rc;
2580 }
2581 }
2582
2583 table->entry = vzalloc(HUNT_FILTER_TBL_ROWS * sizeof(*table->entry));
2584 if (!table->entry) {
2585 rc = -ENOMEM;
2586 goto fail;
2587 }
2588
2589 efx->filter_state = table;
2590 init_waitqueue_head(&table->waitq);
2591 return 0;
2592
2593fail:
2594 kfree(table);
2595 return rc;
2596}
2597
2598static void efx_ef10_filter_table_restore(struct efx_nic *efx)
2599{
2600 struct efx_ef10_filter_table *table = efx->filter_state;
2601 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2602 struct efx_filter_spec *spec;
2603 unsigned int filter_idx;
2604 bool failed = false;
2605 int rc;
2606
2607 if (!nic_data->must_restore_filters)
2608 return;
2609
2610 spin_lock_bh(&efx->filter_lock);
2611
2612 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
2613 spec = efx_ef10_filter_entry_spec(table, filter_idx);
2614 if (!spec)
2615 continue;
2616
2617 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
2618 spin_unlock_bh(&efx->filter_lock);
2619
2620 rc = efx_ef10_filter_push(efx, spec,
2621 &table->entry[filter_idx].handle,
2622 false);
2623 if (rc)
2624 failed = true;
2625
2626 spin_lock_bh(&efx->filter_lock);
2627 if (rc) {
2628 kfree(spec);
2629 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
2630 } else {
2631 table->entry[filter_idx].spec &=
2632 ~EFX_EF10_FILTER_FLAG_BUSY;
2633 }
2634 }
2635
2636 spin_unlock_bh(&efx->filter_lock);
2637
2638 if (failed)
2639 netif_err(efx, hw, efx->net_dev,
2640 "unable to restore all filters\n");
2641 else
2642 nic_data->must_restore_filters = false;
2643}
2644
2645static void efx_ef10_filter_table_remove(struct efx_nic *efx)
2646{
2647 struct efx_ef10_filter_table *table = efx->filter_state;
2648 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
2649 struct efx_filter_spec *spec;
2650 unsigned int filter_idx;
2651 int rc;
2652
2653 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
2654 spec = efx_ef10_filter_entry_spec(table, filter_idx);
2655 if (!spec)
2656 continue;
2657
2658 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2659 efx_ef10_filter_is_exclusive(spec) ?
2660 MC_CMD_FILTER_OP_IN_OP_REMOVE :
2661 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
2662 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2663 table->entry[filter_idx].handle);
2664 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
2665 NULL, 0, NULL);
2666
2667 WARN_ON(rc != 0);
2668 kfree(spec);
2669 }
2670
2671 vfree(table->entry);
2672 kfree(table);
2673}
2674
2675static void efx_ef10_filter_sync_rx_mode(struct efx_nic *efx)
2676{
2677 struct efx_ef10_filter_table *table = efx->filter_state;
2678 struct net_device *net_dev = efx->net_dev;
2679 struct efx_filter_spec spec;
2680 bool remove_failed = false;
2681 struct netdev_hw_addr *uc;
2682 struct netdev_hw_addr *mc;
2683 unsigned int filter_idx;
2684 int i, n, rc;
2685
2686 if (!efx_dev_registered(efx))
2687 return;
2688
2689 /* Mark old filters that may need to be removed */
2690 spin_lock_bh(&efx->filter_lock);
2691 n = table->stack_uc_count < 0 ? 1 : table->stack_uc_count;
2692 for (i = 0; i < n; i++) {
2693 filter_idx = table->stack_uc_list[i].id % HUNT_FILTER_TBL_ROWS;
2694 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_STACK_OLD;
2695 }
2696 n = table->stack_mc_count < 0 ? 1 : table->stack_mc_count;
2697 for (i = 0; i < n; i++) {
2698 filter_idx = table->stack_mc_list[i].id % HUNT_FILTER_TBL_ROWS;
2699 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_STACK_OLD;
2700 }
2701 spin_unlock_bh(&efx->filter_lock);
2702
2703 /* Copy/convert the address lists; add the primary station
2704 * address and broadcast address
2705 */
2706 netif_addr_lock_bh(net_dev);
2707 if (net_dev->flags & IFF_PROMISC ||
2708 netdev_uc_count(net_dev) >= EFX_EF10_FILTER_STACK_UC_MAX) {
2709 table->stack_uc_count = -1;
2710 } else {
2711 table->stack_uc_count = 1 + netdev_uc_count(net_dev);
2712 memcpy(table->stack_uc_list[0].addr, net_dev->dev_addr,
2713 ETH_ALEN);
2714 i = 1;
2715 netdev_for_each_uc_addr(uc, net_dev) {
2716 memcpy(table->stack_uc_list[i].addr,
2717 uc->addr, ETH_ALEN);
2718 i++;
2719 }
2720 }
2721 if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
2722 netdev_mc_count(net_dev) >= EFX_EF10_FILTER_STACK_MC_MAX) {
2723 table->stack_mc_count = -1;
2724 } else {
2725 table->stack_mc_count = 1 + netdev_mc_count(net_dev);
2726 eth_broadcast_addr(table->stack_mc_list[0].addr);
2727 i = 1;
2728 netdev_for_each_mc_addr(mc, net_dev) {
2729 memcpy(table->stack_mc_list[i].addr,
2730 mc->addr, ETH_ALEN);
2731 i++;
2732 }
2733 }
2734 netif_addr_unlock_bh(net_dev);
2735
2736 /* Insert/renew unicast filters */
2737 if (table->stack_uc_count >= 0) {
2738 for (i = 0; i < table->stack_uc_count; i++) {
2739 efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
2740 EFX_FILTER_FLAG_RX_RSS |
2741 EFX_FILTER_FLAG_RX_STACK,
2742 0);
2743 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
2744 table->stack_uc_list[i].addr);
2745 rc = efx_ef10_filter_insert(efx, &spec, true);
2746 if (rc < 0) {
2747 /* Fall back to unicast-promisc */
2748 while (i--)
2749 efx_ef10_filter_remove_safe(
2750 efx, EFX_FILTER_PRI_REQUIRED,
2751 table->stack_uc_list[i].id);
2752 table->stack_uc_count = -1;
2753 break;
2754 }
2755 table->stack_uc_list[i].id = rc;
2756 }
2757 }
2758 if (table->stack_uc_count < 0) {
2759 efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
2760 EFX_FILTER_FLAG_RX_RSS |
2761 EFX_FILTER_FLAG_RX_STACK,
2762 0);
2763 efx_filter_set_uc_def(&spec);
2764 rc = efx_ef10_filter_insert(efx, &spec, true);
2765 if (rc < 0) {
2766 WARN_ON(1);
2767 table->stack_uc_count = 0;
2768 } else {
2769 table->stack_uc_list[0].id = rc;
2770 }
2771 }
2772
2773 /* Insert/renew multicast filters */
2774 if (table->stack_mc_count >= 0) {
2775 for (i = 0; i < table->stack_mc_count; i++) {
2776 efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
2777 EFX_FILTER_FLAG_RX_RSS |
2778 EFX_FILTER_FLAG_RX_STACK,
2779 0);
2780 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
2781 table->stack_mc_list[i].addr);
2782 rc = efx_ef10_filter_insert(efx, &spec, true);
2783 if (rc < 0) {
2784 /* Fall back to multicast-promisc */
2785 while (i--)
2786 efx_ef10_filter_remove_safe(
2787 efx, EFX_FILTER_PRI_REQUIRED,
2788 table->stack_mc_list[i].id);
2789 table->stack_mc_count = -1;
2790 break;
2791 }
2792 table->stack_mc_list[i].id = rc;
2793 }
2794 }
2795 if (table->stack_mc_count < 0) {
2796 efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
2797 EFX_FILTER_FLAG_RX_RSS |
2798 EFX_FILTER_FLAG_RX_STACK,
2799 0);
2800 efx_filter_set_mc_def(&spec);
2801 rc = efx_ef10_filter_insert(efx, &spec, true);
2802 if (rc < 0) {
2803 WARN_ON(1);
2804 table->stack_mc_count = 0;
2805 } else {
2806 table->stack_mc_list[0].id = rc;
2807 }
2808 }
2809
2810 /* Remove filters that weren't renewed. Since nothing else
2811 * changes the STACK_OLD flag or removes these filters, we
2812 * don't need to hold the filter_lock while scanning for
2813 * these filters.
2814 */
2815 for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
2816 if (ACCESS_ONCE(table->entry[i].spec) &
2817 EFX_EF10_FILTER_FLAG_STACK_OLD) {
2818 if (efx_ef10_filter_remove_internal(efx,
2819 EFX_FILTER_PRI_REQUIRED,
2820 i, true) < 0)
2821 remove_failed = true;
2822 }
2823 }
2824 WARN_ON(remove_failed);
2825}
2826
2827static int efx_ef10_mac_reconfigure(struct efx_nic *efx)
2828{
2829 efx_ef10_filter_sync_rx_mode(efx);
2830
2831 return efx_mcdi_set_mac(efx);
2832}
2833
2834#ifdef CONFIG_SFC_MTD
2835
2836struct efx_ef10_nvram_type_info {
2837 u16 type, type_mask;
2838 u8 port;
2839 const char *name;
2840};
2841
2842static const struct efx_ef10_nvram_type_info efx_ef10_nvram_types[] = {
2843 { NVRAM_PARTITION_TYPE_MC_FIRMWARE, 0, 0, "sfc_mcfw" },
2844 { NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 0, 0, "sfc_mcfw_backup" },
2845 { NVRAM_PARTITION_TYPE_EXPANSION_ROM, 0, 0, "sfc_exp_rom" },
2846 { NVRAM_PARTITION_TYPE_STATIC_CONFIG, 0, 0, "sfc_static_cfg" },
2847 { NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 0, 0, "sfc_dynamic_cfg" },
2848 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 0, 0, "sfc_exp_rom_cfg" },
2849 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 0, 1, "sfc_exp_rom_cfg" },
2850 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 0, 2, "sfc_exp_rom_cfg" },
2851 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 0, 3, "sfc_exp_rom_cfg" },
2852 { NVRAM_PARTITION_TYPE_PHY_MIN, 0xff, 0, "sfc_phy_fw" },
2853};
2854
2855static int efx_ef10_mtd_probe_partition(struct efx_nic *efx,
2856 struct efx_mcdi_mtd_partition *part,
2857 unsigned int type)
2858{
2859 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_METADATA_IN_LEN);
2860 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_METADATA_OUT_LENMAX);
2861 const struct efx_ef10_nvram_type_info *info;
2862 size_t size, erase_size, outlen;
2863 bool protected;
2864 int rc;
2865
2866 for (info = efx_ef10_nvram_types; ; info++) {
2867 if (info ==
2868 efx_ef10_nvram_types + ARRAY_SIZE(efx_ef10_nvram_types))
2869 return -ENODEV;
2870 if ((type & ~info->type_mask) == info->type)
2871 break;
2872 }
2873 if (info->port != efx_port_num(efx))
2874 return -ENODEV;
2875
2876 rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
2877 if (rc)
2878 return rc;
2879 if (protected)
2880 return -ENODEV; /* hide it */
2881
2882 part->nvram_type = type;
2883
2884 MCDI_SET_DWORD(inbuf, NVRAM_METADATA_IN_TYPE, type);
2885 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_METADATA, inbuf, sizeof(inbuf),
2886 outbuf, sizeof(outbuf), &outlen);
2887 if (rc)
2888 return rc;
2889 if (outlen < MC_CMD_NVRAM_METADATA_OUT_LENMIN)
2890 return -EIO;
2891 if (MCDI_DWORD(outbuf, NVRAM_METADATA_OUT_FLAGS) &
2892 (1 << MC_CMD_NVRAM_METADATA_OUT_SUBTYPE_VALID_LBN))
2893 part->fw_subtype = MCDI_DWORD(outbuf,
2894 NVRAM_METADATA_OUT_SUBTYPE);
2895
2896 part->common.dev_type_name = "EF10 NVRAM manager";
2897 part->common.type_name = info->name;
2898
2899 part->common.mtd.type = MTD_NORFLASH;
2900 part->common.mtd.flags = MTD_CAP_NORFLASH;
2901 part->common.mtd.size = size;
2902 part->common.mtd.erasesize = erase_size;
2903
2904 return 0;
2905}
2906
2907static int efx_ef10_mtd_probe(struct efx_nic *efx)
2908{
2909 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_PARTITIONS_OUT_LENMAX);
2910 struct efx_mcdi_mtd_partition *parts;
2911 size_t outlen, n_parts_total, i, n_parts;
2912 unsigned int type;
2913 int rc;
2914
2915 ASSERT_RTNL();
2916
2917 BUILD_BUG_ON(MC_CMD_NVRAM_PARTITIONS_IN_LEN != 0);
2918 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_PARTITIONS, NULL, 0,
2919 outbuf, sizeof(outbuf), &outlen);
2920 if (rc)
2921 return rc;
2922 if (outlen < MC_CMD_NVRAM_PARTITIONS_OUT_LENMIN)
2923 return -EIO;
2924
2925 n_parts_total = MCDI_DWORD(outbuf, NVRAM_PARTITIONS_OUT_NUM_PARTITIONS);
2926 if (n_parts_total >
2927 MCDI_VAR_ARRAY_LEN(outlen, NVRAM_PARTITIONS_OUT_TYPE_ID))
2928 return -EIO;
2929
2930 parts = kcalloc(n_parts_total, sizeof(*parts), GFP_KERNEL);
2931 if (!parts)
2932 return -ENOMEM;
2933
2934 n_parts = 0;
2935 for (i = 0; i < n_parts_total; i++) {
2936 type = MCDI_ARRAY_DWORD(outbuf, NVRAM_PARTITIONS_OUT_TYPE_ID,
2937 i);
2938 rc = efx_ef10_mtd_probe_partition(efx, &parts[n_parts], type);
2939 if (rc == 0)
2940 n_parts++;
2941 else if (rc != -ENODEV)
2942 goto fail;
2943 }
2944
2945 rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
2946fail:
2947 if (rc)
2948 kfree(parts);
2949 return rc;
2950}
2951
2952#endif /* CONFIG_SFC_MTD */
2953
2954static void efx_ef10_ptp_write_host_time(struct efx_nic *efx, u32 host_time)
2955{
2956 _efx_writed(efx, cpu_to_le32(host_time), ER_DZ_MC_DB_LWRD);
2957}
2958
2959const struct efx_nic_type efx_hunt_a0_nic_type = {
2960 .mem_map_size = efx_ef10_mem_map_size,
2961 .probe = efx_ef10_probe,
2962 .remove = efx_ef10_remove,
2963 .dimension_resources = efx_ef10_dimension_resources,
2964 .init = efx_ef10_init_nic,
2965 .fini = efx_port_dummy_op_void,
2966 .map_reset_reason = efx_mcdi_map_reset_reason,
2967 .map_reset_flags = efx_ef10_map_reset_flags,
2968 .reset = efx_mcdi_reset,
2969 .probe_port = efx_mcdi_port_probe,
2970 .remove_port = efx_mcdi_port_remove,
2971 .fini_dmaq = efx_ef10_fini_dmaq,
2972 .describe_stats = efx_ef10_describe_stats,
2973 .update_stats = efx_ef10_update_stats,
2974 .start_stats = efx_mcdi_mac_start_stats,
2975 .stop_stats = efx_mcdi_mac_stop_stats,
2976 .set_id_led = efx_mcdi_set_id_led,
2977 .push_irq_moderation = efx_ef10_push_irq_moderation,
2978 .reconfigure_mac = efx_ef10_mac_reconfigure,
2979 .check_mac_fault = efx_mcdi_mac_check_fault,
2980 .reconfigure_port = efx_mcdi_port_reconfigure,
2981 .get_wol = efx_ef10_get_wol,
2982 .set_wol = efx_ef10_set_wol,
2983 .resume_wol = efx_port_dummy_op_void,
2984 /* TODO: test_chip */
2985 .test_nvram = efx_mcdi_nvram_test_all,
2986 .mcdi_request = efx_ef10_mcdi_request,
2987 .mcdi_poll_response = efx_ef10_mcdi_poll_response,
2988 .mcdi_read_response = efx_ef10_mcdi_read_response,
2989 .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
2990 .irq_enable_master = efx_port_dummy_op_void,
2991 .irq_test_generate = efx_ef10_irq_test_generate,
2992 .irq_disable_non_ev = efx_port_dummy_op_void,
2993 .irq_handle_msi = efx_ef10_msi_interrupt,
2994 .irq_handle_legacy = efx_ef10_legacy_interrupt,
2995 .tx_probe = efx_ef10_tx_probe,
2996 .tx_init = efx_ef10_tx_init,
2997 .tx_remove = efx_ef10_tx_remove,
2998 .tx_write = efx_ef10_tx_write,
2999 .rx_push_indir_table = efx_ef10_rx_push_indir_table,
3000 .rx_probe = efx_ef10_rx_probe,
3001 .rx_init = efx_ef10_rx_init,
3002 .rx_remove = efx_ef10_rx_remove,
3003 .rx_write = efx_ef10_rx_write,
3004 .rx_defer_refill = efx_ef10_rx_defer_refill,
3005 .ev_probe = efx_ef10_ev_probe,
3006 .ev_init = efx_ef10_ev_init,
3007 .ev_fini = efx_ef10_ev_fini,
3008 .ev_remove = efx_ef10_ev_remove,
3009 .ev_process = efx_ef10_ev_process,
3010 .ev_read_ack = efx_ef10_ev_read_ack,
3011 .ev_test_generate = efx_ef10_ev_test_generate,
3012 .filter_table_probe = efx_ef10_filter_table_probe,
3013 .filter_table_restore = efx_ef10_filter_table_restore,
3014 .filter_table_remove = efx_ef10_filter_table_remove,
3015 .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
3016 .filter_insert = efx_ef10_filter_insert,
3017 .filter_remove_safe = efx_ef10_filter_remove_safe,
3018 .filter_get_safe = efx_ef10_filter_get_safe,
3019 .filter_clear_rx = efx_ef10_filter_clear_rx,
3020 .filter_count_rx_used = efx_ef10_filter_count_rx_used,
3021 .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
3022 .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
3023#ifdef CONFIG_RFS_ACCEL
3024 .filter_rfs_insert = efx_ef10_filter_rfs_insert,
3025 .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
3026#endif
3027#ifdef CONFIG_SFC_MTD
3028 .mtd_probe = efx_ef10_mtd_probe,
3029 .mtd_rename = efx_mcdi_mtd_rename,
3030 .mtd_read = efx_mcdi_mtd_read,
3031 .mtd_erase = efx_mcdi_mtd_erase,
3032 .mtd_write = efx_mcdi_mtd_write,
3033 .mtd_sync = efx_mcdi_mtd_sync,
3034#endif
3035 .ptp_write_host_time = efx_ef10_ptp_write_host_time,
3036
3037 .revision = EFX_REV_HUNT_A0,
3038 .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
3039 .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
3040 .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
3041 .can_rx_scatter = true,
3042 .always_rx_scatter = true,
3043 .max_interrupt_mode = EFX_INT_MODE_MSIX,
3044 .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
3045 .offload_features = (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3046 NETIF_F_RXHASH | NETIF_F_NTUPLE),
3047 .mcdi_max_ver = 2,
3048 .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
3049};