blob: 4d6d194dbc4c6acf733d7971a9e5dfa5c6b746d0 [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"
Jon Cooper74cd60a2013-09-16 14:18:51 +010017#include "selftest.h"
Shradha Shah7fa8d542015-05-06 00:55:13 +010018#include "ef10_sriov.h"
Ben Hutchings8127d662013-08-29 19:19:29 +010019#include <linux/in.h>
20#include <linux/jhash.h>
21#include <linux/wait.h>
22#include <linux/workqueue.h>
23
24/* Hardware control for EF10 architecture including 'Huntington'. */
25
26#define EFX_EF10_DRVGEN_EV 7
27enum {
28 EFX_EF10_TEST = 1,
29 EFX_EF10_REFILL,
30};
31
32/* The reserved RSS context value */
33#define EFX_EF10_RSS_CONTEXT_INVALID 0xffffffff
34
35/* The filter table(s) are managed by firmware and we have write-only
36 * access. When removing filters we must identify them to the
37 * firmware by a 64-bit handle, but this is too wide for Linux kernel
38 * interfaces (32-bit for RX NFC, 16-bit for RFS). Also, we need to
39 * be able to tell in advance whether a requested insertion will
40 * replace an existing filter. Therefore we maintain a software hash
41 * table, which should be at least as large as the hardware hash
42 * table.
43 *
44 * Huntington has a single 8K filter table shared between all filter
45 * types and both ports.
46 */
47#define HUNT_FILTER_TBL_ROWS 8192
48
49struct efx_ef10_filter_table {
50/* The RX match field masks supported by this fw & hw, in order of priority */
51 enum efx_filter_match_flags rx_match_flags[
52 MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_MAXNUM];
53 unsigned int rx_match_count;
54
55 struct {
56 unsigned long spec; /* pointer to spec plus flag bits */
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +000057/* BUSY flag indicates that an update is in progress. AUTO_OLD is
58 * used to mark and sweep MAC filters for the device address lists.
Ben Hutchings8127d662013-08-29 19:19:29 +010059 */
60#define EFX_EF10_FILTER_FLAG_BUSY 1UL
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +000061#define EFX_EF10_FILTER_FLAG_AUTO_OLD 2UL
Ben Hutchings8127d662013-08-29 19:19:29 +010062#define EFX_EF10_FILTER_FLAGS 3UL
63 u64 handle; /* firmware handle */
64 } *entry;
65 wait_queue_head_t waitq;
66/* Shadow of net_device address lists, guarded by mac_lock */
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +000067#define EFX_EF10_FILTER_DEV_UC_MAX 32
68#define EFX_EF10_FILTER_DEV_MC_MAX 256
Ben Hutchings8127d662013-08-29 19:19:29 +010069 struct {
70 u8 addr[ETH_ALEN];
71 u16 id;
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +000072 } dev_uc_list[EFX_EF10_FILTER_DEV_UC_MAX],
73 dev_mc_list[EFX_EF10_FILTER_DEV_MC_MAX];
74 int dev_uc_count; /* negative for PROMISC */
75 int dev_mc_count; /* negative for PROMISC/ALLMULTI */
Ben Hutchings8127d662013-08-29 19:19:29 +010076};
77
78/* An arbitrary search limit for the software hash table */
79#define EFX_EF10_FILTER_SEARCH_LIMIT 200
80
Andrew Rybchenkod43050c2013-11-14 09:00:27 +040081static void efx_ef10_rx_push_rss_config(struct efx_nic *efx);
Ben Hutchings8127d662013-08-29 19:19:29 +010082static void efx_ef10_rx_free_indir_table(struct efx_nic *efx);
83static void efx_ef10_filter_table_remove(struct efx_nic *efx);
84
85static int efx_ef10_get_warm_boot_count(struct efx_nic *efx)
86{
87 efx_dword_t reg;
88
89 efx_readd(efx, &reg, ER_DZ_BIU_MC_SFT_STATUS);
90 return EFX_DWORD_FIELD(reg, EFX_WORD_1) == 0xb007 ?
91 EFX_DWORD_FIELD(reg, EFX_WORD_0) : -EIO;
92}
93
94static unsigned int efx_ef10_mem_map_size(struct efx_nic *efx)
95{
96 return resource_size(&efx->pci_dev->resource[EFX_MEM_BAR]);
97}
98
Daniel Pieczko1cd9ecb2015-05-06 00:57:53 +010099static int efx_ef10_get_pf_index(struct efx_nic *efx)
100{
101 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_FUNCTION_INFO_OUT_LEN);
102 struct efx_ef10_nic_data *nic_data = efx->nic_data;
103 size_t outlen;
104 int rc;
105
106 rc = efx_mcdi_rpc(efx, MC_CMD_GET_FUNCTION_INFO, NULL, 0, outbuf,
107 sizeof(outbuf), &outlen);
108 if (rc)
109 return rc;
110 if (outlen < sizeof(outbuf))
111 return -EIO;
112
113 nic_data->pf_index = MCDI_DWORD(outbuf, GET_FUNCTION_INFO_OUT_PF);
114 return 0;
115}
116
Ben Hutchingse5a25382013-09-05 22:50:59 +0100117static int efx_ef10_init_datapath_caps(struct efx_nic *efx)
Ben Hutchings8127d662013-08-29 19:19:29 +0100118{
119 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CAPABILITIES_OUT_LEN);
120 struct efx_ef10_nic_data *nic_data = efx->nic_data;
121 size_t outlen;
122 int rc;
123
124 BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
125
126 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
127 outbuf, sizeof(outbuf), &outlen);
128 if (rc)
129 return rc;
Ben Hutchingse5a25382013-09-05 22:50:59 +0100130 if (outlen < sizeof(outbuf)) {
131 netif_err(efx, drv, efx->net_dev,
132 "unable to read datapath firmware capabilities\n");
133 return -EIO;
134 }
Ben Hutchings8127d662013-08-29 19:19:29 +0100135
Ben Hutchingse5a25382013-09-05 22:50:59 +0100136 nic_data->datapath_caps =
137 MCDI_DWORD(outbuf, GET_CAPABILITIES_OUT_FLAGS1);
138
Daniel Pieczko8d9f9dd2015-05-06 00:56:55 +0100139 /* record the DPCPU firmware IDs to determine VEB vswitching support.
140 */
141 nic_data->rx_dpcpu_fw_id =
142 MCDI_WORD(outbuf, GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID);
143 nic_data->tx_dpcpu_fw_id =
144 MCDI_WORD(outbuf, GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID);
145
Ben Hutchingse5a25382013-09-05 22:50:59 +0100146 if (!(nic_data->datapath_caps &
147 (1 << MC_CMD_GET_CAPABILITIES_OUT_TX_TSO_LBN))) {
148 netif_err(efx, drv, efx->net_dev,
149 "current firmware does not support TSO\n");
150 return -ENODEV;
151 }
152
153 if (!(nic_data->datapath_caps &
154 (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_PREFIX_LEN_14_LBN))) {
155 netif_err(efx, probe, efx->net_dev,
156 "current firmware does not support an RX prefix\n");
157 return -ENODEV;
Ben Hutchings8127d662013-08-29 19:19:29 +0100158 }
159
160 return 0;
161}
162
163static int efx_ef10_get_sysclk_freq(struct efx_nic *efx)
164{
165 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CLOCK_OUT_LEN);
166 int rc;
167
168 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CLOCK, NULL, 0,
169 outbuf, sizeof(outbuf), NULL);
170 if (rc)
171 return rc;
172 rc = MCDI_DWORD(outbuf, GET_CLOCK_OUT_SYS_FREQ);
173 return rc > 0 ? rc : -ERANGE;
174}
175
176static int efx_ef10_get_mac_address(struct efx_nic *efx, u8 *mac_address)
177{
178 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_MAC_ADDRESSES_OUT_LEN);
179 size_t outlen;
180 int rc;
181
182 BUILD_BUG_ON(MC_CMD_GET_MAC_ADDRESSES_IN_LEN != 0);
183
184 rc = efx_mcdi_rpc(efx, MC_CMD_GET_MAC_ADDRESSES, NULL, 0,
185 outbuf, sizeof(outbuf), &outlen);
186 if (rc)
187 return rc;
188 if (outlen < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)
189 return -EIO;
190
Edward Creecd84ff42014-03-07 18:27:41 +0000191 ether_addr_copy(mac_address,
192 MCDI_PTR(outbuf, GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE));
Ben Hutchings8127d662013-08-29 19:19:29 +0100193 return 0;
194}
195
196static int efx_ef10_probe(struct efx_nic *efx)
197{
198 struct efx_ef10_nic_data *nic_data;
199 int i, rc;
200
Ben Hutchingsaa3930e2014-02-12 18:59:19 +0000201 /* We can have one VI for each 8K region. However, until we
202 * use TX option descriptors we need two TX queues per channel.
Ben Hutchings8127d662013-08-29 19:19:29 +0100203 */
204 efx->max_channels =
205 min_t(unsigned int,
206 EFX_MAX_CHANNELS,
207 resource_size(&efx->pci_dev->resource[EFX_MEM_BAR]) /
208 (EFX_VI_PAGE_SIZE * EFX_TXQ_TYPES));
Edward Cree9fd3d3a2014-11-03 14:14:35 +0000209 if (WARN_ON(efx->max_channels == 0))
210 return -EIO;
Ben Hutchings8127d662013-08-29 19:19:29 +0100211
212 nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
213 if (!nic_data)
214 return -ENOMEM;
215 efx->nic_data = nic_data;
216
217 rc = efx_nic_alloc_buffer(efx, &nic_data->mcdi_buf,
218 8 + MCDI_CTL_SDU_LEN_MAX_V2, GFP_KERNEL);
219 if (rc)
220 goto fail1;
221
222 /* Get the MC's warm boot count. In case it's rebooting right
223 * now, be prepared to retry.
224 */
225 i = 0;
226 for (;;) {
227 rc = efx_ef10_get_warm_boot_count(efx);
228 if (rc >= 0)
229 break;
230 if (++i == 5)
231 goto fail2;
232 ssleep(1);
233 }
234 nic_data->warm_boot_count = rc;
235
236 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
237
Daniel Pieczko45b24492015-05-06 00:57:14 +0100238 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
239
Ben Hutchings8127d662013-08-29 19:19:29 +0100240 /* In case we're recovering from a crash (kexec), we want to
241 * cancel any outstanding request by the previous user of this
242 * function. We send a special message using the least
243 * significant bits of the 'high' (doorbell) register.
244 */
245 _efx_writed(efx, cpu_to_le32(1), ER_DZ_MC_DB_HWRD);
246
247 rc = efx_mcdi_init(efx);
248 if (rc)
249 goto fail2;
250
251 /* Reset (most) configuration for this function */
252 rc = efx_mcdi_reset(efx, RESET_TYPE_ALL);
253 if (rc)
254 goto fail3;
255
256 /* Enable event logging */
257 rc = efx_mcdi_log_ctrl(efx, true, false, 0);
258 if (rc)
259 goto fail3;
260
Daniel Pieczko1cd9ecb2015-05-06 00:57:53 +0100261 rc = efx_ef10_get_pf_index(efx);
262 if (rc)
263 goto fail3;
264
Ben Hutchingse5a25382013-09-05 22:50:59 +0100265 rc = efx_ef10_init_datapath_caps(efx);
Ben Hutchings8127d662013-08-29 19:19:29 +0100266 if (rc < 0)
267 goto fail3;
268
269 efx->rx_packet_len_offset =
270 ES_DZ_RX_PREFIX_PKTLEN_OFST - ES_DZ_RX_PREFIX_SIZE;
271
Ben Hutchings8127d662013-08-29 19:19:29 +0100272 rc = efx_mcdi_port_get_number(efx);
273 if (rc < 0)
274 goto fail3;
275 efx->port_num = rc;
276
277 rc = efx_ef10_get_mac_address(efx, efx->net_dev->perm_addr);
278 if (rc)
279 goto fail3;
280
281 rc = efx_ef10_get_sysclk_freq(efx);
282 if (rc < 0)
283 goto fail3;
284 efx->timer_quantum_ns = 1536000 / rc; /* 1536 cycles */
285
286 /* Check whether firmware supports bug 35388 workaround */
287 rc = efx_mcdi_set_workaround(efx, MC_CMD_WORKAROUND_BUG35388, true);
288 if (rc == 0)
289 nic_data->workaround_35388 = true;
290 else if (rc != -ENOSYS && rc != -ENOENT)
291 goto fail3;
292 netif_dbg(efx, probe, efx->net_dev,
293 "workaround for bug 35388 is %sabled\n",
294 nic_data->workaround_35388 ? "en" : "dis");
295
296 rc = efx_mcdi_mon_probe(efx);
297 if (rc)
298 goto fail3;
299
Ben Hutchings9aecda92013-12-05 21:28:42 +0000300 efx_ptp_probe(efx, NULL);
301
Ben Hutchings8127d662013-08-29 19:19:29 +0100302 return 0;
303
304fail3:
305 efx_mcdi_fini(efx);
306fail2:
307 efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
308fail1:
309 kfree(nic_data);
310 efx->nic_data = NULL;
311 return rc;
312}
313
314static int efx_ef10_free_vis(struct efx_nic *efx)
315{
Edward Cree1e0b8122013-05-31 18:36:12 +0100316 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
317 size_t outlen;
318 int rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FREE_VIS, NULL, 0,
319 outbuf, sizeof(outbuf), &outlen);
Ben Hutchings8127d662013-08-29 19:19:29 +0100320
321 /* -EALREADY means nothing to free, so ignore */
322 if (rc == -EALREADY)
323 rc = 0;
Edward Cree1e0b8122013-05-31 18:36:12 +0100324 if (rc)
325 efx_mcdi_display_error(efx, MC_CMD_FREE_VIS, 0, outbuf, outlen,
326 rc);
Ben Hutchings8127d662013-08-29 19:19:29 +0100327 return rc;
328}
329
Ben Hutchings183233b2013-06-28 21:47:12 +0100330#ifdef EFX_USE_PIO
331
332static void efx_ef10_free_piobufs(struct efx_nic *efx)
333{
334 struct efx_ef10_nic_data *nic_data = efx->nic_data;
335 MCDI_DECLARE_BUF(inbuf, MC_CMD_FREE_PIOBUF_IN_LEN);
336 unsigned int i;
337 int rc;
338
339 BUILD_BUG_ON(MC_CMD_FREE_PIOBUF_OUT_LEN != 0);
340
341 for (i = 0; i < nic_data->n_piobufs; i++) {
342 MCDI_SET_DWORD(inbuf, FREE_PIOBUF_IN_PIOBUF_HANDLE,
343 nic_data->piobuf_handle[i]);
344 rc = efx_mcdi_rpc(efx, MC_CMD_FREE_PIOBUF, inbuf, sizeof(inbuf),
345 NULL, 0, NULL);
346 WARN_ON(rc);
347 }
348
349 nic_data->n_piobufs = 0;
350}
351
352static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
353{
354 struct efx_ef10_nic_data *nic_data = efx->nic_data;
355 MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_PIOBUF_OUT_LEN);
356 unsigned int i;
357 size_t outlen;
358 int rc = 0;
359
360 BUILD_BUG_ON(MC_CMD_ALLOC_PIOBUF_IN_LEN != 0);
361
362 for (i = 0; i < n; i++) {
363 rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_PIOBUF, NULL, 0,
364 outbuf, sizeof(outbuf), &outlen);
365 if (rc)
366 break;
367 if (outlen < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
368 rc = -EIO;
369 break;
370 }
371 nic_data->piobuf_handle[i] =
372 MCDI_DWORD(outbuf, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
373 netif_dbg(efx, probe, efx->net_dev,
374 "allocated PIO buffer %u handle %x\n", i,
375 nic_data->piobuf_handle[i]);
376 }
377
378 nic_data->n_piobufs = i;
379 if (rc)
380 efx_ef10_free_piobufs(efx);
381 return rc;
382}
383
384static int efx_ef10_link_piobufs(struct efx_nic *efx)
385{
386 struct efx_ef10_nic_data *nic_data = efx->nic_data;
387 MCDI_DECLARE_BUF(inbuf,
388 max(MC_CMD_LINK_PIOBUF_IN_LEN,
389 MC_CMD_UNLINK_PIOBUF_IN_LEN));
390 struct efx_channel *channel;
391 struct efx_tx_queue *tx_queue;
392 unsigned int offset, index;
393 int rc;
394
395 BUILD_BUG_ON(MC_CMD_LINK_PIOBUF_OUT_LEN != 0);
396 BUILD_BUG_ON(MC_CMD_UNLINK_PIOBUF_OUT_LEN != 0);
397
398 /* Link a buffer to each VI in the write-combining mapping */
399 for (index = 0; index < nic_data->n_piobufs; ++index) {
400 MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_PIOBUF_HANDLE,
401 nic_data->piobuf_handle[index]);
402 MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_TXQ_INSTANCE,
403 nic_data->pio_write_vi_base + index);
404 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
405 inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
406 NULL, 0, NULL);
407 if (rc) {
408 netif_err(efx, drv, efx->net_dev,
409 "failed to link VI %u to PIO buffer %u (%d)\n",
410 nic_data->pio_write_vi_base + index, index,
411 rc);
412 goto fail;
413 }
414 netif_dbg(efx, probe, efx->net_dev,
415 "linked VI %u to PIO buffer %u\n",
416 nic_data->pio_write_vi_base + index, index);
417 }
418
419 /* Link a buffer to each TX queue */
420 efx_for_each_channel(channel, efx) {
421 efx_for_each_channel_tx_queue(tx_queue, channel) {
422 /* We assign the PIO buffers to queues in
423 * reverse order to allow for the following
424 * special case.
425 */
426 offset = ((efx->tx_channel_offset + efx->n_tx_channels -
427 tx_queue->channel->channel - 1) *
428 efx_piobuf_size);
429 index = offset / ER_DZ_TX_PIOBUF_SIZE;
430 offset = offset % ER_DZ_TX_PIOBUF_SIZE;
431
432 /* When the host page size is 4K, the first
433 * host page in the WC mapping may be within
434 * the same VI page as the last TX queue. We
435 * can only link one buffer to each VI.
436 */
437 if (tx_queue->queue == nic_data->pio_write_vi_base) {
438 BUG_ON(index != 0);
439 rc = 0;
440 } else {
441 MCDI_SET_DWORD(inbuf,
442 LINK_PIOBUF_IN_PIOBUF_HANDLE,
443 nic_data->piobuf_handle[index]);
444 MCDI_SET_DWORD(inbuf,
445 LINK_PIOBUF_IN_TXQ_INSTANCE,
446 tx_queue->queue);
447 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
448 inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
449 NULL, 0, NULL);
450 }
451
452 if (rc) {
453 /* This is non-fatal; the TX path just
454 * won't use PIO for this queue
455 */
456 netif_err(efx, drv, efx->net_dev,
457 "failed to link VI %u to PIO buffer %u (%d)\n",
458 tx_queue->queue, index, rc);
459 tx_queue->piobuf = NULL;
460 } else {
461 tx_queue->piobuf =
462 nic_data->pio_write_base +
463 index * EFX_VI_PAGE_SIZE + offset;
464 tx_queue->piobuf_offset = offset;
465 netif_dbg(efx, probe, efx->net_dev,
466 "linked VI %u to PIO buffer %u offset %x addr %p\n",
467 tx_queue->queue, index,
468 tx_queue->piobuf_offset,
469 tx_queue->piobuf);
470 }
471 }
472 }
473
474 return 0;
475
476fail:
477 while (index--) {
478 MCDI_SET_DWORD(inbuf, UNLINK_PIOBUF_IN_TXQ_INSTANCE,
479 nic_data->pio_write_vi_base + index);
480 efx_mcdi_rpc(efx, MC_CMD_UNLINK_PIOBUF,
481 inbuf, MC_CMD_UNLINK_PIOBUF_IN_LEN,
482 NULL, 0, NULL);
483 }
484 return rc;
485}
486
487#else /* !EFX_USE_PIO */
488
489static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
490{
491 return n == 0 ? 0 : -ENOBUFS;
492}
493
494static int efx_ef10_link_piobufs(struct efx_nic *efx)
495{
496 return 0;
497}
498
499static void efx_ef10_free_piobufs(struct efx_nic *efx)
500{
501}
502
503#endif /* EFX_USE_PIO */
504
Ben Hutchings8127d662013-08-29 19:19:29 +0100505static void efx_ef10_remove(struct efx_nic *efx)
506{
507 struct efx_ef10_nic_data *nic_data = efx->nic_data;
508 int rc;
509
Ben Hutchings9aecda92013-12-05 21:28:42 +0000510 efx_ptp_remove(efx);
511
Ben Hutchings8127d662013-08-29 19:19:29 +0100512 efx_mcdi_mon_remove(efx);
513
Ben Hutchings8127d662013-08-29 19:19:29 +0100514 efx_ef10_rx_free_indir_table(efx);
515
Ben Hutchings183233b2013-06-28 21:47:12 +0100516 if (nic_data->wc_membase)
517 iounmap(nic_data->wc_membase);
518
Ben Hutchings8127d662013-08-29 19:19:29 +0100519 rc = efx_ef10_free_vis(efx);
520 WARN_ON(rc != 0);
521
Ben Hutchings183233b2013-06-28 21:47:12 +0100522 if (!nic_data->must_restore_piobufs)
523 efx_ef10_free_piobufs(efx);
524
Ben Hutchings8127d662013-08-29 19:19:29 +0100525 efx_mcdi_fini(efx);
526 efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
527 kfree(nic_data);
528}
529
530static int efx_ef10_alloc_vis(struct efx_nic *efx,
531 unsigned int min_vis, unsigned int max_vis)
532{
533 MCDI_DECLARE_BUF(inbuf, MC_CMD_ALLOC_VIS_IN_LEN);
534 MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_VIS_OUT_LEN);
535 struct efx_ef10_nic_data *nic_data = efx->nic_data;
536 size_t outlen;
537 int rc;
538
539 MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MIN_VI_COUNT, min_vis);
540 MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MAX_VI_COUNT, max_vis);
541 rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_VIS, inbuf, sizeof(inbuf),
542 outbuf, sizeof(outbuf), &outlen);
543 if (rc != 0)
544 return rc;
545
546 if (outlen < MC_CMD_ALLOC_VIS_OUT_LEN)
547 return -EIO;
548
549 netif_dbg(efx, drv, efx->net_dev, "base VI is A0x%03x\n",
550 MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE));
551
552 nic_data->vi_base = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE);
553 nic_data->n_allocated_vis = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_COUNT);
554 return 0;
555}
556
Ben Hutchings183233b2013-06-28 21:47:12 +0100557/* Note that the failure path of this function does not free
558 * resources, as this will be done by efx_ef10_remove().
559 */
Ben Hutchings8127d662013-08-29 19:19:29 +0100560static int efx_ef10_dimension_resources(struct efx_nic *efx)
561{
Ben Hutchings183233b2013-06-28 21:47:12 +0100562 struct efx_ef10_nic_data *nic_data = efx->nic_data;
563 unsigned int uc_mem_map_size, wc_mem_map_size;
564 unsigned int min_vis, pio_write_vi_base, max_vis;
565 void __iomem *membase;
566 int rc;
Ben Hutchings8127d662013-08-29 19:19:29 +0100567
Ben Hutchings183233b2013-06-28 21:47:12 +0100568 min_vis = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
569
570#ifdef EFX_USE_PIO
571 /* Try to allocate PIO buffers if wanted and if the full
572 * number of PIO buffers would be sufficient to allocate one
573 * copy-buffer per TX channel. Failure is non-fatal, as there
574 * are only a small number of PIO buffers shared between all
575 * functions of the controller.
576 */
577 if (efx_piobuf_size != 0 &&
578 ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size * EF10_TX_PIOBUF_COUNT >=
579 efx->n_tx_channels) {
580 unsigned int n_piobufs =
581 DIV_ROUND_UP(efx->n_tx_channels,
582 ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size);
583
584 rc = efx_ef10_alloc_piobufs(efx, n_piobufs);
585 if (rc)
586 netif_err(efx, probe, efx->net_dev,
587 "failed to allocate PIO buffers (%d)\n", rc);
588 else
589 netif_dbg(efx, probe, efx->net_dev,
590 "allocated %u PIO buffers\n", n_piobufs);
591 }
592#else
593 nic_data->n_piobufs = 0;
594#endif
595
596 /* PIO buffers should be mapped with write-combining enabled,
597 * and we want to make single UC and WC mappings rather than
598 * several of each (in fact that's the only option if host
599 * page size is >4K). So we may allocate some extra VIs just
600 * for writing PIO buffers through.
Daniel Pieczko52ad7622014-04-01 13:10:34 +0100601 *
602 * The UC mapping contains (min_vis - 1) complete VIs and the
603 * first half of the next VI. Then the WC mapping begins with
604 * the second half of this last VI.
Ben Hutchings183233b2013-06-28 21:47:12 +0100605 */
606 uc_mem_map_size = PAGE_ALIGN((min_vis - 1) * EFX_VI_PAGE_SIZE +
607 ER_DZ_TX_PIOBUF);
608 if (nic_data->n_piobufs) {
Daniel Pieczko52ad7622014-04-01 13:10:34 +0100609 /* pio_write_vi_base rounds down to give the number of complete
610 * VIs inside the UC mapping.
611 */
Ben Hutchings183233b2013-06-28 21:47:12 +0100612 pio_write_vi_base = uc_mem_map_size / EFX_VI_PAGE_SIZE;
613 wc_mem_map_size = (PAGE_ALIGN((pio_write_vi_base +
614 nic_data->n_piobufs) *
615 EFX_VI_PAGE_SIZE) -
616 uc_mem_map_size);
617 max_vis = pio_write_vi_base + nic_data->n_piobufs;
618 } else {
619 pio_write_vi_base = 0;
620 wc_mem_map_size = 0;
621 max_vis = min_vis;
622 }
623
624 /* In case the last attached driver failed to free VIs, do it now */
625 rc = efx_ef10_free_vis(efx);
626 if (rc != 0)
627 return rc;
628
629 rc = efx_ef10_alloc_vis(efx, min_vis, max_vis);
630 if (rc != 0)
631 return rc;
632
633 /* If we didn't get enough VIs to map all the PIO buffers, free the
634 * PIO buffers
635 */
636 if (nic_data->n_piobufs &&
637 nic_data->n_allocated_vis <
638 pio_write_vi_base + nic_data->n_piobufs) {
639 netif_dbg(efx, probe, efx->net_dev,
640 "%u VIs are not sufficient to map %u PIO buffers\n",
641 nic_data->n_allocated_vis, nic_data->n_piobufs);
642 efx_ef10_free_piobufs(efx);
643 }
644
645 /* Shrink the original UC mapping of the memory BAR */
646 membase = ioremap_nocache(efx->membase_phys, uc_mem_map_size);
647 if (!membase) {
648 netif_err(efx, probe, efx->net_dev,
649 "could not shrink memory BAR to %x\n",
650 uc_mem_map_size);
651 return -ENOMEM;
652 }
653 iounmap(efx->membase);
654 efx->membase = membase;
655
656 /* Set up the WC mapping if needed */
657 if (wc_mem_map_size) {
658 nic_data->wc_membase = ioremap_wc(efx->membase_phys +
659 uc_mem_map_size,
660 wc_mem_map_size);
661 if (!nic_data->wc_membase) {
662 netif_err(efx, probe, efx->net_dev,
663 "could not allocate WC mapping of size %x\n",
664 wc_mem_map_size);
665 return -ENOMEM;
666 }
667 nic_data->pio_write_vi_base = pio_write_vi_base;
668 nic_data->pio_write_base =
669 nic_data->wc_membase +
670 (pio_write_vi_base * EFX_VI_PAGE_SIZE + ER_DZ_TX_PIOBUF -
671 uc_mem_map_size);
672
673 rc = efx_ef10_link_piobufs(efx);
674 if (rc)
675 efx_ef10_free_piobufs(efx);
676 }
677
678 netif_dbg(efx, probe, efx->net_dev,
679 "memory BAR at %pa (virtual %p+%x UC, %p+%x WC)\n",
680 &efx->membase_phys, efx->membase, uc_mem_map_size,
681 nic_data->wc_membase, wc_mem_map_size);
682
683 return 0;
Ben Hutchings8127d662013-08-29 19:19:29 +0100684}
685
686static int efx_ef10_init_nic(struct efx_nic *efx)
687{
688 struct efx_ef10_nic_data *nic_data = efx->nic_data;
689 int rc;
690
Ben Hutchingsa915ccc2013-09-05 22:51:55 +0100691 if (nic_data->must_check_datapath_caps) {
692 rc = efx_ef10_init_datapath_caps(efx);
693 if (rc)
694 return rc;
695 nic_data->must_check_datapath_caps = false;
696 }
697
Ben Hutchings8127d662013-08-29 19:19:29 +0100698 if (nic_data->must_realloc_vis) {
699 /* We cannot let the number of VIs change now */
700 rc = efx_ef10_alloc_vis(efx, nic_data->n_allocated_vis,
701 nic_data->n_allocated_vis);
702 if (rc)
703 return rc;
704 nic_data->must_realloc_vis = false;
705 }
706
Ben Hutchings183233b2013-06-28 21:47:12 +0100707 if (nic_data->must_restore_piobufs && nic_data->n_piobufs) {
708 rc = efx_ef10_alloc_piobufs(efx, nic_data->n_piobufs);
709 if (rc == 0) {
710 rc = efx_ef10_link_piobufs(efx);
711 if (rc)
712 efx_ef10_free_piobufs(efx);
713 }
714
715 /* Log an error on failure, but this is non-fatal */
716 if (rc)
717 netif_err(efx, drv, efx->net_dev,
718 "failed to restore PIO buffers (%d)\n", rc);
719 nic_data->must_restore_piobufs = false;
720 }
721
Andrew Rybchenkod43050c2013-11-14 09:00:27 +0400722 efx_ef10_rx_push_rss_config(efx);
Ben Hutchings8127d662013-08-29 19:19:29 +0100723 return 0;
724}
725
Jon Cooper3e336262014-01-17 19:48:06 +0000726static void efx_ef10_reset_mc_allocations(struct efx_nic *efx)
727{
728 struct efx_ef10_nic_data *nic_data = efx->nic_data;
729
730 /* All our allocations have been reset */
731 nic_data->must_realloc_vis = true;
732 nic_data->must_restore_filters = true;
733 nic_data->must_restore_piobufs = true;
734 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
735}
736
Ben Hutchings8127d662013-08-29 19:19:29 +0100737static int efx_ef10_map_reset_flags(u32 *flags)
738{
739 enum {
740 EF10_RESET_PORT = ((ETH_RESET_MAC | ETH_RESET_PHY) <<
741 ETH_RESET_SHARED_SHIFT),
742 EF10_RESET_MC = ((ETH_RESET_DMA | ETH_RESET_FILTER |
743 ETH_RESET_OFFLOAD | ETH_RESET_MAC |
744 ETH_RESET_PHY | ETH_RESET_MGMT) <<
745 ETH_RESET_SHARED_SHIFT)
746 };
747
748 /* We assume for now that our PCI function is permitted to
749 * reset everything.
750 */
751
752 if ((*flags & EF10_RESET_MC) == EF10_RESET_MC) {
753 *flags &= ~EF10_RESET_MC;
754 return RESET_TYPE_WORLD;
755 }
756
757 if ((*flags & EF10_RESET_PORT) == EF10_RESET_PORT) {
758 *flags &= ~EF10_RESET_PORT;
759 return RESET_TYPE_ALL;
760 }
761
762 /* no invisible reset implemented */
763
764 return -EINVAL;
765}
766
Jon Cooper3e336262014-01-17 19:48:06 +0000767static int efx_ef10_reset(struct efx_nic *efx, enum reset_type reset_type)
768{
769 int rc = efx_mcdi_reset(efx, reset_type);
770
771 /* If it was a port reset, trigger reallocation of MC resources.
772 * Note that on an MC reset nothing needs to be done now because we'll
773 * detect the MC reset later and handle it then.
Edward Creee2835462014-04-16 19:27:48 +0100774 * For an FLR, we never get an MC reset event, but the MC has reset all
775 * resources assigned to us, so we have to trigger reallocation now.
Jon Cooper3e336262014-01-17 19:48:06 +0000776 */
Edward Creee2835462014-04-16 19:27:48 +0100777 if ((reset_type == RESET_TYPE_ALL ||
778 reset_type == RESET_TYPE_MCDI_TIMEOUT) && !rc)
Jon Cooper3e336262014-01-17 19:48:06 +0000779 efx_ef10_reset_mc_allocations(efx);
780 return rc;
781}
782
Ben Hutchings8127d662013-08-29 19:19:29 +0100783#define EF10_DMA_STAT(ext_name, mcdi_name) \
784 [EF10_STAT_ ## ext_name] = \
785 { #ext_name, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
786#define EF10_DMA_INVIS_STAT(int_name, mcdi_name) \
787 [EF10_STAT_ ## int_name] = \
788 { NULL, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
789#define EF10_OTHER_STAT(ext_name) \
790 [EF10_STAT_ ## ext_name] = { #ext_name, 0, 0 }
Edward Creee4d112e2014-07-15 11:58:12 +0100791#define GENERIC_SW_STAT(ext_name) \
792 [GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
Ben Hutchings8127d662013-08-29 19:19:29 +0100793
794static const struct efx_hw_stat_desc efx_ef10_stat_desc[EF10_STAT_COUNT] = {
795 EF10_DMA_STAT(tx_bytes, TX_BYTES),
796 EF10_DMA_STAT(tx_packets, TX_PKTS),
797 EF10_DMA_STAT(tx_pause, TX_PAUSE_PKTS),
798 EF10_DMA_STAT(tx_control, TX_CONTROL_PKTS),
799 EF10_DMA_STAT(tx_unicast, TX_UNICAST_PKTS),
800 EF10_DMA_STAT(tx_multicast, TX_MULTICAST_PKTS),
801 EF10_DMA_STAT(tx_broadcast, TX_BROADCAST_PKTS),
802 EF10_DMA_STAT(tx_lt64, TX_LT64_PKTS),
803 EF10_DMA_STAT(tx_64, TX_64_PKTS),
804 EF10_DMA_STAT(tx_65_to_127, TX_65_TO_127_PKTS),
805 EF10_DMA_STAT(tx_128_to_255, TX_128_TO_255_PKTS),
806 EF10_DMA_STAT(tx_256_to_511, TX_256_TO_511_PKTS),
807 EF10_DMA_STAT(tx_512_to_1023, TX_512_TO_1023_PKTS),
808 EF10_DMA_STAT(tx_1024_to_15xx, TX_1024_TO_15XX_PKTS),
809 EF10_DMA_STAT(tx_15xx_to_jumbo, TX_15XX_TO_JUMBO_PKTS),
810 EF10_DMA_STAT(rx_bytes, RX_BYTES),
811 EF10_DMA_INVIS_STAT(rx_bytes_minus_good_bytes, RX_BAD_BYTES),
812 EF10_OTHER_STAT(rx_good_bytes),
813 EF10_OTHER_STAT(rx_bad_bytes),
814 EF10_DMA_STAT(rx_packets, RX_PKTS),
815 EF10_DMA_STAT(rx_good, RX_GOOD_PKTS),
816 EF10_DMA_STAT(rx_bad, RX_BAD_FCS_PKTS),
817 EF10_DMA_STAT(rx_pause, RX_PAUSE_PKTS),
818 EF10_DMA_STAT(rx_control, RX_CONTROL_PKTS),
819 EF10_DMA_STAT(rx_unicast, RX_UNICAST_PKTS),
820 EF10_DMA_STAT(rx_multicast, RX_MULTICAST_PKTS),
821 EF10_DMA_STAT(rx_broadcast, RX_BROADCAST_PKTS),
822 EF10_DMA_STAT(rx_lt64, RX_UNDERSIZE_PKTS),
823 EF10_DMA_STAT(rx_64, RX_64_PKTS),
824 EF10_DMA_STAT(rx_65_to_127, RX_65_TO_127_PKTS),
825 EF10_DMA_STAT(rx_128_to_255, RX_128_TO_255_PKTS),
826 EF10_DMA_STAT(rx_256_to_511, RX_256_TO_511_PKTS),
827 EF10_DMA_STAT(rx_512_to_1023, RX_512_TO_1023_PKTS),
828 EF10_DMA_STAT(rx_1024_to_15xx, RX_1024_TO_15XX_PKTS),
829 EF10_DMA_STAT(rx_15xx_to_jumbo, RX_15XX_TO_JUMBO_PKTS),
830 EF10_DMA_STAT(rx_gtjumbo, RX_GTJUMBO_PKTS),
831 EF10_DMA_STAT(rx_bad_gtjumbo, RX_JABBER_PKTS),
832 EF10_DMA_STAT(rx_overflow, RX_OVERFLOW_PKTS),
833 EF10_DMA_STAT(rx_align_error, RX_ALIGN_ERROR_PKTS),
834 EF10_DMA_STAT(rx_length_error, RX_LENGTH_ERROR_PKTS),
835 EF10_DMA_STAT(rx_nodesc_drops, RX_NODESC_DROPS),
Edward Creee4d112e2014-07-15 11:58:12 +0100836 GENERIC_SW_STAT(rx_nodesc_trunc),
837 GENERIC_SW_STAT(rx_noskb_drops),
Edward Cree568d7a02013-09-25 17:32:09 +0100838 EF10_DMA_STAT(rx_pm_trunc_bb_overflow, PM_TRUNC_BB_OVERFLOW),
839 EF10_DMA_STAT(rx_pm_discard_bb_overflow, PM_DISCARD_BB_OVERFLOW),
840 EF10_DMA_STAT(rx_pm_trunc_vfifo_full, PM_TRUNC_VFIFO_FULL),
841 EF10_DMA_STAT(rx_pm_discard_vfifo_full, PM_DISCARD_VFIFO_FULL),
842 EF10_DMA_STAT(rx_pm_trunc_qbb, PM_TRUNC_QBB),
843 EF10_DMA_STAT(rx_pm_discard_qbb, PM_DISCARD_QBB),
844 EF10_DMA_STAT(rx_pm_discard_mapping, PM_DISCARD_MAPPING),
845 EF10_DMA_STAT(rx_dp_q_disabled_packets, RXDP_Q_DISABLED_PKTS),
846 EF10_DMA_STAT(rx_dp_di_dropped_packets, RXDP_DI_DROPPED_PKTS),
847 EF10_DMA_STAT(rx_dp_streaming_packets, RXDP_STREAMING_PKTS),
Shradha Shah79ac47a2013-11-28 18:48:49 +0000848 EF10_DMA_STAT(rx_dp_hlb_fetch, RXDP_EMERGENCY_FETCH_CONDITIONS),
849 EF10_DMA_STAT(rx_dp_hlb_wait, RXDP_EMERGENCY_WAIT_CONDITIONS),
Ben Hutchings8127d662013-08-29 19:19:29 +0100850};
851
852#define HUNT_COMMON_STAT_MASK ((1ULL << EF10_STAT_tx_bytes) | \
853 (1ULL << EF10_STAT_tx_packets) | \
854 (1ULL << EF10_STAT_tx_pause) | \
855 (1ULL << EF10_STAT_tx_unicast) | \
856 (1ULL << EF10_STAT_tx_multicast) | \
857 (1ULL << EF10_STAT_tx_broadcast) | \
858 (1ULL << EF10_STAT_rx_bytes) | \
859 (1ULL << EF10_STAT_rx_bytes_minus_good_bytes) | \
860 (1ULL << EF10_STAT_rx_good_bytes) | \
861 (1ULL << EF10_STAT_rx_bad_bytes) | \
862 (1ULL << EF10_STAT_rx_packets) | \
863 (1ULL << EF10_STAT_rx_good) | \
864 (1ULL << EF10_STAT_rx_bad) | \
865 (1ULL << EF10_STAT_rx_pause) | \
866 (1ULL << EF10_STAT_rx_control) | \
867 (1ULL << EF10_STAT_rx_unicast) | \
868 (1ULL << EF10_STAT_rx_multicast) | \
869 (1ULL << EF10_STAT_rx_broadcast) | \
870 (1ULL << EF10_STAT_rx_lt64) | \
871 (1ULL << EF10_STAT_rx_64) | \
872 (1ULL << EF10_STAT_rx_65_to_127) | \
873 (1ULL << EF10_STAT_rx_128_to_255) | \
874 (1ULL << EF10_STAT_rx_256_to_511) | \
875 (1ULL << EF10_STAT_rx_512_to_1023) | \
876 (1ULL << EF10_STAT_rx_1024_to_15xx) | \
877 (1ULL << EF10_STAT_rx_15xx_to_jumbo) | \
878 (1ULL << EF10_STAT_rx_gtjumbo) | \
879 (1ULL << EF10_STAT_rx_bad_gtjumbo) | \
880 (1ULL << EF10_STAT_rx_overflow) | \
Edward Creee4d112e2014-07-15 11:58:12 +0100881 (1ULL << EF10_STAT_rx_nodesc_drops) | \
882 (1ULL << GENERIC_STAT_rx_nodesc_trunc) | \
883 (1ULL << GENERIC_STAT_rx_noskb_drops))
Ben Hutchings8127d662013-08-29 19:19:29 +0100884
885/* These statistics are only provided by the 10G MAC. For a 10G/40G
886 * switchable port we do not expose these because they might not
887 * include all the packets they should.
888 */
889#define HUNT_10G_ONLY_STAT_MASK ((1ULL << EF10_STAT_tx_control) | \
890 (1ULL << EF10_STAT_tx_lt64) | \
891 (1ULL << EF10_STAT_tx_64) | \
892 (1ULL << EF10_STAT_tx_65_to_127) | \
893 (1ULL << EF10_STAT_tx_128_to_255) | \
894 (1ULL << EF10_STAT_tx_256_to_511) | \
895 (1ULL << EF10_STAT_tx_512_to_1023) | \
896 (1ULL << EF10_STAT_tx_1024_to_15xx) | \
897 (1ULL << EF10_STAT_tx_15xx_to_jumbo))
898
899/* These statistics are only provided by the 40G MAC. For a 10G/40G
900 * switchable port we do expose these because the errors will otherwise
901 * be silent.
902 */
903#define HUNT_40G_EXTRA_STAT_MASK ((1ULL << EF10_STAT_rx_align_error) | \
904 (1ULL << EF10_STAT_rx_length_error))
905
Edward Cree568d7a02013-09-25 17:32:09 +0100906/* These statistics are only provided if the firmware supports the
907 * capability PM_AND_RXDP_COUNTERS.
908 */
909#define HUNT_PM_AND_RXDP_STAT_MASK ( \
910 (1ULL << EF10_STAT_rx_pm_trunc_bb_overflow) | \
911 (1ULL << EF10_STAT_rx_pm_discard_bb_overflow) | \
912 (1ULL << EF10_STAT_rx_pm_trunc_vfifo_full) | \
913 (1ULL << EF10_STAT_rx_pm_discard_vfifo_full) | \
914 (1ULL << EF10_STAT_rx_pm_trunc_qbb) | \
915 (1ULL << EF10_STAT_rx_pm_discard_qbb) | \
916 (1ULL << EF10_STAT_rx_pm_discard_mapping) | \
917 (1ULL << EF10_STAT_rx_dp_q_disabled_packets) | \
918 (1ULL << EF10_STAT_rx_dp_di_dropped_packets) | \
919 (1ULL << EF10_STAT_rx_dp_streaming_packets) | \
Shradha Shah79ac47a2013-11-28 18:48:49 +0000920 (1ULL << EF10_STAT_rx_dp_hlb_fetch) | \
921 (1ULL << EF10_STAT_rx_dp_hlb_wait))
Ben Hutchings8127d662013-08-29 19:19:29 +0100922
Edward Cree4bae9132013-09-27 18:52:49 +0100923static u64 efx_ef10_raw_stat_mask(struct efx_nic *efx)
Ben Hutchings8127d662013-08-29 19:19:29 +0100924{
Edward Cree4bae9132013-09-27 18:52:49 +0100925 u64 raw_mask = HUNT_COMMON_STAT_MASK;
Ben Hutchings8127d662013-08-29 19:19:29 +0100926 u32 port_caps = efx_mcdi_phy_get_caps(efx);
Edward Cree568d7a02013-09-25 17:32:09 +0100927 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Ben Hutchings8127d662013-08-29 19:19:29 +0100928
929 if (port_caps & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
Edward Cree4bae9132013-09-27 18:52:49 +0100930 raw_mask |= HUNT_40G_EXTRA_STAT_MASK;
Ben Hutchings8127d662013-08-29 19:19:29 +0100931 else
Edward Cree4bae9132013-09-27 18:52:49 +0100932 raw_mask |= HUNT_10G_ONLY_STAT_MASK;
Edward Cree568d7a02013-09-25 17:32:09 +0100933
934 if (nic_data->datapath_caps &
935 (1 << MC_CMD_GET_CAPABILITIES_OUT_PM_AND_RXDP_COUNTERS_LBN))
936 raw_mask |= HUNT_PM_AND_RXDP_STAT_MASK;
937
Edward Cree4bae9132013-09-27 18:52:49 +0100938 return raw_mask;
939}
940
941static void efx_ef10_get_stat_mask(struct efx_nic *efx, unsigned long *mask)
942{
943 u64 raw_mask = efx_ef10_raw_stat_mask(efx);
944
945#if BITS_PER_LONG == 64
946 mask[0] = raw_mask;
947#else
948 mask[0] = raw_mask & 0xffffffff;
949 mask[1] = raw_mask >> 32;
950#endif
Ben Hutchings8127d662013-08-29 19:19:29 +0100951}
952
953static size_t efx_ef10_describe_stats(struct efx_nic *efx, u8 *names)
954{
Edward Cree4bae9132013-09-27 18:52:49 +0100955 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
956
957 efx_ef10_get_stat_mask(efx, mask);
Ben Hutchings8127d662013-08-29 19:19:29 +0100958 return efx_nic_describe_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
Edward Cree4bae9132013-09-27 18:52:49 +0100959 mask, names);
Ben Hutchings8127d662013-08-29 19:19:29 +0100960}
961
962static int efx_ef10_try_update_nic_stats(struct efx_nic *efx)
963{
964 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Edward Cree4bae9132013-09-27 18:52:49 +0100965 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
Ben Hutchings8127d662013-08-29 19:19:29 +0100966 __le64 generation_start, generation_end;
967 u64 *stats = nic_data->stats;
968 __le64 *dma_stats;
969
Edward Cree4bae9132013-09-27 18:52:49 +0100970 efx_ef10_get_stat_mask(efx, mask);
971
Ben Hutchings8127d662013-08-29 19:19:29 +0100972 dma_stats = efx->stats_buffer.addr;
973 nic_data = efx->nic_data;
974
975 generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
976 if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
977 return 0;
978 rmb();
Edward Cree4bae9132013-09-27 18:52:49 +0100979 efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, mask,
Ben Hutchings8127d662013-08-29 19:19:29 +0100980 stats, efx->stats_buffer.addr, false);
Jon Cooperd546a892013-09-27 18:26:30 +0100981 rmb();
Ben Hutchings8127d662013-08-29 19:19:29 +0100982 generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
983 if (generation_end != generation_start)
984 return -EAGAIN;
985
986 /* Update derived statistics */
Jon Cooperf8f3b5a2013-09-30 17:36:50 +0100987 efx_nic_fix_nodesc_drop_stat(efx, &stats[EF10_STAT_rx_nodesc_drops]);
Ben Hutchings8127d662013-08-29 19:19:29 +0100988 stats[EF10_STAT_rx_good_bytes] =
989 stats[EF10_STAT_rx_bytes] -
990 stats[EF10_STAT_rx_bytes_minus_good_bytes];
991 efx_update_diff_stat(&stats[EF10_STAT_rx_bad_bytes],
992 stats[EF10_STAT_rx_bytes_minus_good_bytes]);
Edward Creee4d112e2014-07-15 11:58:12 +0100993 efx_update_sw_stats(efx, stats);
Ben Hutchings8127d662013-08-29 19:19:29 +0100994 return 0;
995}
996
997
998static size_t efx_ef10_update_stats(struct efx_nic *efx, u64 *full_stats,
999 struct rtnl_link_stats64 *core_stats)
1000{
Edward Cree4bae9132013-09-27 18:52:49 +01001001 DECLARE_BITMAP(mask, EF10_STAT_COUNT);
Ben Hutchings8127d662013-08-29 19:19:29 +01001002 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1003 u64 *stats = nic_data->stats;
1004 size_t stats_count = 0, index;
1005 int retry;
1006
Edward Cree4bae9132013-09-27 18:52:49 +01001007 efx_ef10_get_stat_mask(efx, mask);
1008
Ben Hutchings8127d662013-08-29 19:19:29 +01001009 /* If we're unlucky enough to read statistics during the DMA, wait
1010 * up to 10ms for it to finish (typically takes <500us)
1011 */
1012 for (retry = 0; retry < 100; ++retry) {
1013 if (efx_ef10_try_update_nic_stats(efx) == 0)
1014 break;
1015 udelay(100);
1016 }
1017
1018 if (full_stats) {
1019 for_each_set_bit(index, mask, EF10_STAT_COUNT) {
1020 if (efx_ef10_stat_desc[index].name) {
1021 *full_stats++ = stats[index];
1022 ++stats_count;
1023 }
1024 }
1025 }
1026
1027 if (core_stats) {
1028 core_stats->rx_packets = stats[EF10_STAT_rx_packets];
1029 core_stats->tx_packets = stats[EF10_STAT_tx_packets];
1030 core_stats->rx_bytes = stats[EF10_STAT_rx_bytes];
1031 core_stats->tx_bytes = stats[EF10_STAT_tx_bytes];
Edward Creee4d112e2014-07-15 11:58:12 +01001032 core_stats->rx_dropped = stats[EF10_STAT_rx_nodesc_drops] +
1033 stats[GENERIC_STAT_rx_nodesc_trunc] +
1034 stats[GENERIC_STAT_rx_noskb_drops];
Ben Hutchings8127d662013-08-29 19:19:29 +01001035 core_stats->multicast = stats[EF10_STAT_rx_multicast];
1036 core_stats->rx_length_errors =
1037 stats[EF10_STAT_rx_gtjumbo] +
1038 stats[EF10_STAT_rx_length_error];
1039 core_stats->rx_crc_errors = stats[EF10_STAT_rx_bad];
1040 core_stats->rx_frame_errors = stats[EF10_STAT_rx_align_error];
1041 core_stats->rx_fifo_errors = stats[EF10_STAT_rx_overflow];
1042 core_stats->rx_errors = (core_stats->rx_length_errors +
1043 core_stats->rx_crc_errors +
1044 core_stats->rx_frame_errors);
1045 }
1046
1047 return stats_count;
1048}
1049
1050static void efx_ef10_push_irq_moderation(struct efx_channel *channel)
1051{
1052 struct efx_nic *efx = channel->efx;
1053 unsigned int mode, value;
1054 efx_dword_t timer_cmd;
1055
1056 if (channel->irq_moderation) {
1057 mode = 3;
1058 value = channel->irq_moderation - 1;
1059 } else {
1060 mode = 0;
1061 value = 0;
1062 }
1063
1064 if (EFX_EF10_WORKAROUND_35388(efx)) {
1065 EFX_POPULATE_DWORD_3(timer_cmd, ERF_DD_EVQ_IND_TIMER_FLAGS,
1066 EFE_DD_EVQ_IND_TIMER_FLAGS,
1067 ERF_DD_EVQ_IND_TIMER_MODE, mode,
1068 ERF_DD_EVQ_IND_TIMER_VAL, value);
1069 efx_writed_page(efx, &timer_cmd, ER_DD_EVQ_INDIRECT,
1070 channel->channel);
1071 } else {
1072 EFX_POPULATE_DWORD_2(timer_cmd, ERF_DZ_TC_TIMER_MODE, mode,
1073 ERF_DZ_TC_TIMER_VAL, value);
1074 efx_writed_page(efx, &timer_cmd, ER_DZ_EVQ_TMR,
1075 channel->channel);
1076 }
1077}
1078
1079static void efx_ef10_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
1080{
1081 wol->supported = 0;
1082 wol->wolopts = 0;
1083 memset(&wol->sopass, 0, sizeof(wol->sopass));
1084}
1085
1086static int efx_ef10_set_wol(struct efx_nic *efx, u32 type)
1087{
1088 if (type != 0)
1089 return -EINVAL;
1090 return 0;
1091}
1092
1093static void efx_ef10_mcdi_request(struct efx_nic *efx,
1094 const efx_dword_t *hdr, size_t hdr_len,
1095 const efx_dword_t *sdu, size_t sdu_len)
1096{
1097 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1098 u8 *pdu = nic_data->mcdi_buf.addr;
1099
1100 memcpy(pdu, hdr, hdr_len);
1101 memcpy(pdu + hdr_len, sdu, sdu_len);
1102 wmb();
1103
1104 /* The hardware provides 'low' and 'high' (doorbell) registers
1105 * for passing the 64-bit address of an MCDI request to
1106 * firmware. However the dwords are swapped by firmware. The
1107 * least significant bits of the doorbell are then 0 for all
1108 * MCDI requests due to alignment.
1109 */
1110 _efx_writed(efx, cpu_to_le32((u64)nic_data->mcdi_buf.dma_addr >> 32),
1111 ER_DZ_MC_DB_LWRD);
1112 _efx_writed(efx, cpu_to_le32((u32)nic_data->mcdi_buf.dma_addr),
1113 ER_DZ_MC_DB_HWRD);
1114}
1115
1116static bool efx_ef10_mcdi_poll_response(struct efx_nic *efx)
1117{
1118 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1119 const efx_dword_t hdr = *(const efx_dword_t *)nic_data->mcdi_buf.addr;
1120
1121 rmb();
1122 return EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE);
1123}
1124
1125static void
1126efx_ef10_mcdi_read_response(struct efx_nic *efx, efx_dword_t *outbuf,
1127 size_t offset, size_t outlen)
1128{
1129 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1130 const u8 *pdu = nic_data->mcdi_buf.addr;
1131
1132 memcpy(outbuf, pdu + offset, outlen);
1133}
1134
1135static int efx_ef10_mcdi_poll_reboot(struct efx_nic *efx)
1136{
1137 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1138 int rc;
1139
1140 rc = efx_ef10_get_warm_boot_count(efx);
1141 if (rc < 0) {
1142 /* The firmware is presumably in the process of
1143 * rebooting. However, we are supposed to report each
1144 * reboot just once, so we must only do that once we
1145 * can read and store the updated warm boot count.
1146 */
1147 return 0;
1148 }
1149
1150 if (rc == nic_data->warm_boot_count)
1151 return 0;
1152
1153 nic_data->warm_boot_count = rc;
1154
1155 /* All our allocations have been reset */
Jon Cooper3e336262014-01-17 19:48:06 +00001156 efx_ef10_reset_mc_allocations(efx);
Ben Hutchings8127d662013-08-29 19:19:29 +01001157
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001158 /* Driver-created vswitches and vports must be re-created */
1159 nic_data->must_probe_vswitching = true;
1160 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
1161
Ben Hutchingsa915ccc2013-09-05 22:51:55 +01001162 /* The datapath firmware might have been changed */
1163 nic_data->must_check_datapath_caps = true;
1164
Ben Hutchings869070c2013-09-05 22:46:10 +01001165 /* MAC statistics have been cleared on the NIC; clear the local
1166 * statistic that we update with efx_update_diff_stat().
1167 */
1168 nic_data->stats[EF10_STAT_rx_bad_bytes] = 0;
1169
Ben Hutchings8127d662013-08-29 19:19:29 +01001170 return -EIO;
1171}
1172
1173/* Handle an MSI interrupt
1174 *
1175 * Handle an MSI hardware interrupt. This routine schedules event
1176 * queue processing. No interrupt acknowledgement cycle is necessary.
1177 * Also, we never need to check that the interrupt is for us, since
1178 * MSI interrupts cannot be shared.
1179 */
1180static irqreturn_t efx_ef10_msi_interrupt(int irq, void *dev_id)
1181{
1182 struct efx_msi_context *context = dev_id;
1183 struct efx_nic *efx = context->efx;
1184
1185 netif_vdbg(efx, intr, efx->net_dev,
1186 "IRQ %d on CPU %d\n", irq, raw_smp_processor_id());
1187
1188 if (likely(ACCESS_ONCE(efx->irq_soft_enabled))) {
1189 /* Note test interrupts */
1190 if (context->index == efx->irq_level)
1191 efx->last_irq_cpu = raw_smp_processor_id();
1192
1193 /* Schedule processing of the channel */
1194 efx_schedule_channel_irq(efx->channel[context->index]);
1195 }
1196
1197 return IRQ_HANDLED;
1198}
1199
1200static irqreturn_t efx_ef10_legacy_interrupt(int irq, void *dev_id)
1201{
1202 struct efx_nic *efx = dev_id;
1203 bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
1204 struct efx_channel *channel;
1205 efx_dword_t reg;
1206 u32 queues;
1207
1208 /* Read the ISR which also ACKs the interrupts */
1209 efx_readd(efx, &reg, ER_DZ_BIU_INT_ISR);
1210 queues = EFX_DWORD_FIELD(reg, ERF_DZ_ISR_REG);
1211
1212 if (queues == 0)
1213 return IRQ_NONE;
1214
1215 if (likely(soft_enabled)) {
1216 /* Note test interrupts */
1217 if (queues & (1U << efx->irq_level))
1218 efx->last_irq_cpu = raw_smp_processor_id();
1219
1220 efx_for_each_channel(channel, efx) {
1221 if (queues & 1)
1222 efx_schedule_channel_irq(channel);
1223 queues >>= 1;
1224 }
1225 }
1226
1227 netif_vdbg(efx, intr, efx->net_dev,
1228 "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
1229 irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
1230
1231 return IRQ_HANDLED;
1232}
1233
1234static void efx_ef10_irq_test_generate(struct efx_nic *efx)
1235{
1236 MCDI_DECLARE_BUF(inbuf, MC_CMD_TRIGGER_INTERRUPT_IN_LEN);
1237
1238 BUILD_BUG_ON(MC_CMD_TRIGGER_INTERRUPT_OUT_LEN != 0);
1239
1240 MCDI_SET_DWORD(inbuf, TRIGGER_INTERRUPT_IN_INTR_LEVEL, efx->irq_level);
1241 (void) efx_mcdi_rpc(efx, MC_CMD_TRIGGER_INTERRUPT,
1242 inbuf, sizeof(inbuf), NULL, 0, NULL);
1243}
1244
1245static int efx_ef10_tx_probe(struct efx_tx_queue *tx_queue)
1246{
1247 return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
1248 (tx_queue->ptr_mask + 1) *
1249 sizeof(efx_qword_t),
1250 GFP_KERNEL);
1251}
1252
1253/* This writes to the TX_DESC_WPTR and also pushes data */
1254static inline void efx_ef10_push_tx_desc(struct efx_tx_queue *tx_queue,
1255 const efx_qword_t *txd)
1256{
1257 unsigned int write_ptr;
1258 efx_oword_t reg;
1259
1260 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
1261 EFX_POPULATE_OWORD_1(reg, ERF_DZ_TX_DESC_WPTR, write_ptr);
1262 reg.qword[0] = *txd;
1263 efx_writeo_page(tx_queue->efx, &reg,
1264 ER_DZ_TX_DESC_UPD, tx_queue->queue);
1265}
1266
1267static void efx_ef10_tx_init(struct efx_tx_queue *tx_queue)
1268{
1269 MCDI_DECLARE_BUF(inbuf, MC_CMD_INIT_TXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
1270 EFX_BUF_SIZE));
1271 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_TXQ_OUT_LEN);
1272 bool csum_offload = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
1273 size_t entries = tx_queue->txd.buf.len / EFX_BUF_SIZE;
1274 struct efx_channel *channel = tx_queue->channel;
1275 struct efx_nic *efx = tx_queue->efx;
Daniel Pieczko45b24492015-05-06 00:57:14 +01001276 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Ben Hutchings8127d662013-08-29 19:19:29 +01001277 size_t inlen, outlen;
1278 dma_addr_t dma_addr;
1279 efx_qword_t *txd;
1280 int rc;
1281 int i;
1282
1283 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_SIZE, tx_queue->ptr_mask + 1);
1284 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_TARGET_EVQ, channel->channel);
1285 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_LABEL, tx_queue->queue);
1286 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_INSTANCE, tx_queue->queue);
1287 MCDI_POPULATE_DWORD_2(inbuf, INIT_TXQ_IN_FLAGS,
1288 INIT_TXQ_IN_FLAG_IP_CSUM_DIS, !csum_offload,
1289 INIT_TXQ_IN_FLAG_TCP_CSUM_DIS, !csum_offload);
1290 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_OWNER_ID, 0);
Daniel Pieczko45b24492015-05-06 00:57:14 +01001291 MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_PORT_ID, nic_data->vport_id);
Ben Hutchings8127d662013-08-29 19:19:29 +01001292
1293 dma_addr = tx_queue->txd.buf.dma_addr;
1294
1295 netif_dbg(efx, hw, efx->net_dev, "pushing TXQ %d. %zu entries (%llx)\n",
1296 tx_queue->queue, entries, (u64)dma_addr);
1297
1298 for (i = 0; i < entries; ++i) {
1299 MCDI_SET_ARRAY_QWORD(inbuf, INIT_TXQ_IN_DMA_ADDR, i, dma_addr);
1300 dma_addr += EFX_BUF_SIZE;
1301 }
1302
1303 inlen = MC_CMD_INIT_TXQ_IN_LEN(entries);
1304
1305 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_TXQ, inbuf, inlen,
1306 outbuf, sizeof(outbuf), &outlen);
1307 if (rc)
1308 goto fail;
1309
1310 /* A previous user of this TX queue might have set us up the
1311 * bomb by writing a descriptor to the TX push collector but
1312 * not the doorbell. (Each collector belongs to a port, not a
1313 * queue or function, so cannot easily be reset.) We must
1314 * attempt to push a no-op descriptor in its place.
1315 */
1316 tx_queue->buffer[0].flags = EFX_TX_BUF_OPTION;
1317 tx_queue->insert_count = 1;
1318 txd = efx_tx_desc(tx_queue, 0);
1319 EFX_POPULATE_QWORD_4(*txd,
1320 ESF_DZ_TX_DESC_IS_OPT, true,
1321 ESF_DZ_TX_OPTION_TYPE,
1322 ESE_DZ_TX_OPTION_DESC_CRC_CSUM,
1323 ESF_DZ_TX_OPTION_UDP_TCP_CSUM, csum_offload,
1324 ESF_DZ_TX_OPTION_IP_CSUM, csum_offload);
1325 tx_queue->write_count = 1;
1326 wmb();
1327 efx_ef10_push_tx_desc(tx_queue, txd);
1328
1329 return;
1330
1331fail:
Ben Hutchings48ce5632013-11-01 16:42:44 +00001332 netdev_WARN(efx->net_dev, "failed to initialise TXQ %d\n",
1333 tx_queue->queue);
Ben Hutchings8127d662013-08-29 19:19:29 +01001334}
1335
1336static void efx_ef10_tx_fini(struct efx_tx_queue *tx_queue)
1337{
1338 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_TXQ_IN_LEN);
1339 MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_TXQ_OUT_LEN);
1340 struct efx_nic *efx = tx_queue->efx;
1341 size_t outlen;
1342 int rc;
1343
1344 MCDI_SET_DWORD(inbuf, FINI_TXQ_IN_INSTANCE,
1345 tx_queue->queue);
1346
Edward Cree1e0b8122013-05-31 18:36:12 +01001347 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_TXQ, inbuf, sizeof(inbuf),
Ben Hutchings8127d662013-08-29 19:19:29 +01001348 outbuf, sizeof(outbuf), &outlen);
1349
1350 if (rc && rc != -EALREADY)
1351 goto fail;
1352
1353 return;
1354
1355fail:
Edward Cree1e0b8122013-05-31 18:36:12 +01001356 efx_mcdi_display_error(efx, MC_CMD_FINI_TXQ, MC_CMD_FINI_TXQ_IN_LEN,
1357 outbuf, outlen, rc);
Ben Hutchings8127d662013-08-29 19:19:29 +01001358}
1359
1360static void efx_ef10_tx_remove(struct efx_tx_queue *tx_queue)
1361{
1362 efx_nic_free_buffer(tx_queue->efx, &tx_queue->txd.buf);
1363}
1364
1365/* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
1366static inline void efx_ef10_notify_tx_desc(struct efx_tx_queue *tx_queue)
1367{
1368 unsigned int write_ptr;
1369 efx_dword_t reg;
1370
1371 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
1372 EFX_POPULATE_DWORD_1(reg, ERF_DZ_TX_DESC_WPTR_DWORD, write_ptr);
1373 efx_writed_page(tx_queue->efx, &reg,
1374 ER_DZ_TX_DESC_UPD_DWORD, tx_queue->queue);
1375}
1376
1377static void efx_ef10_tx_write(struct efx_tx_queue *tx_queue)
1378{
1379 unsigned int old_write_count = tx_queue->write_count;
1380 struct efx_tx_buffer *buffer;
1381 unsigned int write_ptr;
1382 efx_qword_t *txd;
1383
1384 BUG_ON(tx_queue->write_count == tx_queue->insert_count);
1385
1386 do {
1387 write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
1388 buffer = &tx_queue->buffer[write_ptr];
1389 txd = efx_tx_desc(tx_queue, write_ptr);
1390 ++tx_queue->write_count;
1391
1392 /* Create TX descriptor ring entry */
1393 if (buffer->flags & EFX_TX_BUF_OPTION) {
1394 *txd = buffer->option;
1395 } else {
1396 BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
1397 EFX_POPULATE_QWORD_3(
1398 *txd,
1399 ESF_DZ_TX_KER_CONT,
1400 buffer->flags & EFX_TX_BUF_CONT,
1401 ESF_DZ_TX_KER_BYTE_CNT, buffer->len,
1402 ESF_DZ_TX_KER_BUF_ADDR, buffer->dma_addr);
1403 }
1404 } while (tx_queue->write_count != tx_queue->insert_count);
1405
1406 wmb(); /* Ensure descriptors are written before they are fetched */
1407
1408 if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
1409 txd = efx_tx_desc(tx_queue,
1410 old_write_count & tx_queue->ptr_mask);
1411 efx_ef10_push_tx_desc(tx_queue, txd);
1412 ++tx_queue->pushes;
1413 } else {
1414 efx_ef10_notify_tx_desc(tx_queue);
1415 }
1416}
1417
1418static int efx_ef10_alloc_rss_context(struct efx_nic *efx, u32 *context)
1419{
1420 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
1421 MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
Daniel Pieczko45b24492015-05-06 00:57:14 +01001422 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Ben Hutchings8127d662013-08-29 19:19:29 +01001423 size_t outlen;
1424 int rc;
1425
1426 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
Daniel Pieczko45b24492015-05-06 00:57:14 +01001427 nic_data->vport_id);
Ben Hutchings8127d662013-08-29 19:19:29 +01001428 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE,
1429 MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE);
1430 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES,
1431 EFX_MAX_CHANNELS);
1432
1433 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
1434 outbuf, sizeof(outbuf), &outlen);
1435 if (rc != 0)
1436 return rc;
1437
1438 if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
1439 return -EIO;
1440
1441 *context = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
1442
1443 return 0;
1444}
1445
1446static void efx_ef10_free_rss_context(struct efx_nic *efx, u32 context)
1447{
1448 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
1449 int rc;
1450
1451 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
1452 context);
1453
1454 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
1455 NULL, 0, NULL);
1456 WARN_ON(rc != 0);
1457}
1458
1459static int efx_ef10_populate_rss_table(struct efx_nic *efx, u32 context)
1460{
1461 MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
1462 MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
1463 int i, rc;
1464
1465 MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
1466 context);
1467 BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
1468 MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
1469
1470 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); ++i)
1471 MCDI_PTR(tablebuf,
1472 RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
1473 (u8) efx->rx_indir_table[i];
1474
1475 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
1476 sizeof(tablebuf), NULL, 0, NULL);
1477 if (rc != 0)
1478 return rc;
1479
1480 MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
1481 context);
1482 BUILD_BUG_ON(ARRAY_SIZE(efx->rx_hash_key) !=
1483 MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
1484 for (i = 0; i < ARRAY_SIZE(efx->rx_hash_key); ++i)
1485 MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] =
1486 efx->rx_hash_key[i];
1487
1488 return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
1489 sizeof(keybuf), NULL, 0, NULL);
1490}
1491
1492static void efx_ef10_rx_free_indir_table(struct efx_nic *efx)
1493{
1494 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1495
1496 if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
1497 efx_ef10_free_rss_context(efx, nic_data->rx_rss_context);
1498 nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
1499}
1500
Andrew Rybchenkod43050c2013-11-14 09:00:27 +04001501static void efx_ef10_rx_push_rss_config(struct efx_nic *efx)
Ben Hutchings8127d662013-08-29 19:19:29 +01001502{
1503 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1504 int rc;
1505
Andrew Rybchenkod43050c2013-11-14 09:00:27 +04001506 netif_dbg(efx, drv, efx->net_dev, "pushing RSS config\n");
Ben Hutchings8127d662013-08-29 19:19:29 +01001507
1508 if (nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID) {
1509 rc = efx_ef10_alloc_rss_context(efx, &nic_data->rx_rss_context);
1510 if (rc != 0)
1511 goto fail;
1512 }
1513
1514 rc = efx_ef10_populate_rss_table(efx, nic_data->rx_rss_context);
1515 if (rc != 0)
1516 goto fail;
1517
1518 return;
1519
1520fail:
1521 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1522}
1523
1524static int efx_ef10_rx_probe(struct efx_rx_queue *rx_queue)
1525{
1526 return efx_nic_alloc_buffer(rx_queue->efx, &rx_queue->rxd.buf,
1527 (rx_queue->ptr_mask + 1) *
1528 sizeof(efx_qword_t),
1529 GFP_KERNEL);
1530}
1531
1532static void efx_ef10_rx_init(struct efx_rx_queue *rx_queue)
1533{
1534 MCDI_DECLARE_BUF(inbuf,
1535 MC_CMD_INIT_RXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
1536 EFX_BUF_SIZE));
1537 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_RXQ_OUT_LEN);
1538 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
1539 size_t entries = rx_queue->rxd.buf.len / EFX_BUF_SIZE;
1540 struct efx_nic *efx = rx_queue->efx;
Daniel Pieczko45b24492015-05-06 00:57:14 +01001541 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Ben Hutchings8127d662013-08-29 19:19:29 +01001542 size_t inlen, outlen;
1543 dma_addr_t dma_addr;
1544 int rc;
1545 int i;
1546
1547 rx_queue->scatter_n = 0;
1548 rx_queue->scatter_len = 0;
1549
1550 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_SIZE, rx_queue->ptr_mask + 1);
1551 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_TARGET_EVQ, channel->channel);
1552 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_LABEL, efx_rx_queue_index(rx_queue));
1553 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_INSTANCE,
1554 efx_rx_queue_index(rx_queue));
Jon Cooperbd9a2652013-11-18 12:54:41 +00001555 MCDI_POPULATE_DWORD_2(inbuf, INIT_RXQ_IN_FLAGS,
1556 INIT_RXQ_IN_FLAG_PREFIX, 1,
1557 INIT_RXQ_IN_FLAG_TIMESTAMP, 1);
Ben Hutchings8127d662013-08-29 19:19:29 +01001558 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_OWNER_ID, 0);
Daniel Pieczko45b24492015-05-06 00:57:14 +01001559 MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_PORT_ID, nic_data->vport_id);
Ben Hutchings8127d662013-08-29 19:19:29 +01001560
1561 dma_addr = rx_queue->rxd.buf.dma_addr;
1562
1563 netif_dbg(efx, hw, efx->net_dev, "pushing RXQ %d. %zu entries (%llx)\n",
1564 efx_rx_queue_index(rx_queue), entries, (u64)dma_addr);
1565
1566 for (i = 0; i < entries; ++i) {
1567 MCDI_SET_ARRAY_QWORD(inbuf, INIT_RXQ_IN_DMA_ADDR, i, dma_addr);
1568 dma_addr += EFX_BUF_SIZE;
1569 }
1570
1571 inlen = MC_CMD_INIT_RXQ_IN_LEN(entries);
1572
1573 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_RXQ, inbuf, inlen,
1574 outbuf, sizeof(outbuf), &outlen);
Ben Hutchings48ce5632013-11-01 16:42:44 +00001575 if (rc)
1576 netdev_WARN(efx->net_dev, "failed to initialise RXQ %d\n",
1577 efx_rx_queue_index(rx_queue));
Ben Hutchings8127d662013-08-29 19:19:29 +01001578}
1579
1580static void efx_ef10_rx_fini(struct efx_rx_queue *rx_queue)
1581{
1582 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_RXQ_IN_LEN);
1583 MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_RXQ_OUT_LEN);
1584 struct efx_nic *efx = rx_queue->efx;
1585 size_t outlen;
1586 int rc;
1587
1588 MCDI_SET_DWORD(inbuf, FINI_RXQ_IN_INSTANCE,
1589 efx_rx_queue_index(rx_queue));
1590
Edward Cree1e0b8122013-05-31 18:36:12 +01001591 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_RXQ, inbuf, sizeof(inbuf),
Ben Hutchings8127d662013-08-29 19:19:29 +01001592 outbuf, sizeof(outbuf), &outlen);
1593
1594 if (rc && rc != -EALREADY)
1595 goto fail;
1596
1597 return;
1598
1599fail:
Edward Cree1e0b8122013-05-31 18:36:12 +01001600 efx_mcdi_display_error(efx, MC_CMD_FINI_RXQ, MC_CMD_FINI_RXQ_IN_LEN,
1601 outbuf, outlen, rc);
Ben Hutchings8127d662013-08-29 19:19:29 +01001602}
1603
1604static void efx_ef10_rx_remove(struct efx_rx_queue *rx_queue)
1605{
1606 efx_nic_free_buffer(rx_queue->efx, &rx_queue->rxd.buf);
1607}
1608
1609/* This creates an entry in the RX descriptor queue */
1610static inline void
1611efx_ef10_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
1612{
1613 struct efx_rx_buffer *rx_buf;
1614 efx_qword_t *rxd;
1615
1616 rxd = efx_rx_desc(rx_queue, index);
1617 rx_buf = efx_rx_buffer(rx_queue, index);
1618 EFX_POPULATE_QWORD_2(*rxd,
1619 ESF_DZ_RX_KER_BYTE_CNT, rx_buf->len,
1620 ESF_DZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
1621}
1622
1623static void efx_ef10_rx_write(struct efx_rx_queue *rx_queue)
1624{
1625 struct efx_nic *efx = rx_queue->efx;
1626 unsigned int write_count;
1627 efx_dword_t reg;
1628
1629 /* Firmware requires that RX_DESC_WPTR be a multiple of 8 */
1630 write_count = rx_queue->added_count & ~7;
1631 if (rx_queue->notified_count == write_count)
1632 return;
1633
1634 do
1635 efx_ef10_build_rx_desc(
1636 rx_queue,
1637 rx_queue->notified_count & rx_queue->ptr_mask);
1638 while (++rx_queue->notified_count != write_count);
1639
1640 wmb();
1641 EFX_POPULATE_DWORD_1(reg, ERF_DZ_RX_DESC_WPTR,
1642 write_count & rx_queue->ptr_mask);
1643 efx_writed_page(efx, &reg, ER_DZ_RX_DESC_UPD,
1644 efx_rx_queue_index(rx_queue));
1645}
1646
1647static efx_mcdi_async_completer efx_ef10_rx_defer_refill_complete;
1648
1649static void efx_ef10_rx_defer_refill(struct efx_rx_queue *rx_queue)
1650{
1651 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
1652 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
1653 efx_qword_t event;
1654
1655 EFX_POPULATE_QWORD_2(event,
1656 ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
1657 ESF_DZ_EV_DATA, EFX_EF10_REFILL);
1658
1659 MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
1660
1661 /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
1662 * already swapped the data to little-endian order.
1663 */
1664 memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
1665 sizeof(efx_qword_t));
1666
1667 efx_mcdi_rpc_async(channel->efx, MC_CMD_DRIVER_EVENT,
1668 inbuf, sizeof(inbuf), 0,
1669 efx_ef10_rx_defer_refill_complete, 0);
1670}
1671
1672static void
1673efx_ef10_rx_defer_refill_complete(struct efx_nic *efx, unsigned long cookie,
1674 int rc, efx_dword_t *outbuf,
1675 size_t outlen_actual)
1676{
1677 /* nothing to do */
1678}
1679
1680static int efx_ef10_ev_probe(struct efx_channel *channel)
1681{
1682 return efx_nic_alloc_buffer(channel->efx, &channel->eventq.buf,
1683 (channel->eventq_mask + 1) *
1684 sizeof(efx_qword_t),
1685 GFP_KERNEL);
1686}
1687
1688static int efx_ef10_ev_init(struct efx_channel *channel)
1689{
1690 MCDI_DECLARE_BUF(inbuf,
1691 MC_CMD_INIT_EVQ_IN_LEN(EFX_MAX_EVQ_SIZE * 8 /
1692 EFX_BUF_SIZE));
1693 MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_EVQ_OUT_LEN);
1694 size_t entries = channel->eventq.buf.len / EFX_BUF_SIZE;
1695 struct efx_nic *efx = channel->efx;
1696 struct efx_ef10_nic_data *nic_data;
1697 bool supports_rx_merge;
1698 size_t inlen, outlen;
1699 dma_addr_t dma_addr;
1700 int rc;
1701 int i;
1702
1703 nic_data = efx->nic_data;
1704 supports_rx_merge =
1705 !!(nic_data->datapath_caps &
1706 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN);
1707
1708 /* Fill event queue with all ones (i.e. empty events) */
1709 memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
1710
1711 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_SIZE, channel->eventq_mask + 1);
1712 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_INSTANCE, channel->channel);
1713 /* INIT_EVQ expects index in vector table, not absolute */
1714 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_IRQ_NUM, channel->channel);
1715 MCDI_POPULATE_DWORD_4(inbuf, INIT_EVQ_IN_FLAGS,
1716 INIT_EVQ_IN_FLAG_INTERRUPTING, 1,
1717 INIT_EVQ_IN_FLAG_RX_MERGE, 1,
1718 INIT_EVQ_IN_FLAG_TX_MERGE, 1,
1719 INIT_EVQ_IN_FLAG_CUT_THRU, !supports_rx_merge);
1720 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_MODE,
1721 MC_CMD_INIT_EVQ_IN_TMR_MODE_DIS);
1722 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_LOAD, 0);
1723 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_RELOAD, 0);
1724 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_MODE,
1725 MC_CMD_INIT_EVQ_IN_COUNT_MODE_DIS);
1726 MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_THRSHLD, 0);
1727
1728 dma_addr = channel->eventq.buf.dma_addr;
1729 for (i = 0; i < entries; ++i) {
1730 MCDI_SET_ARRAY_QWORD(inbuf, INIT_EVQ_IN_DMA_ADDR, i, dma_addr);
1731 dma_addr += EFX_BUF_SIZE;
1732 }
1733
1734 inlen = MC_CMD_INIT_EVQ_IN_LEN(entries);
1735
1736 rc = efx_mcdi_rpc(efx, MC_CMD_INIT_EVQ, inbuf, inlen,
1737 outbuf, sizeof(outbuf), &outlen);
Ben Hutchings8127d662013-08-29 19:19:29 +01001738 /* IRQ return is ignored */
Ben Hutchings8127d662013-08-29 19:19:29 +01001739 return rc;
1740}
1741
1742static void efx_ef10_ev_fini(struct efx_channel *channel)
1743{
1744 MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_EVQ_IN_LEN);
1745 MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_EVQ_OUT_LEN);
1746 struct efx_nic *efx = channel->efx;
1747 size_t outlen;
1748 int rc;
1749
1750 MCDI_SET_DWORD(inbuf, FINI_EVQ_IN_INSTANCE, channel->channel);
1751
Edward Cree1e0b8122013-05-31 18:36:12 +01001752 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_EVQ, inbuf, sizeof(inbuf),
Ben Hutchings8127d662013-08-29 19:19:29 +01001753 outbuf, sizeof(outbuf), &outlen);
1754
1755 if (rc && rc != -EALREADY)
1756 goto fail;
1757
1758 return;
1759
1760fail:
Edward Cree1e0b8122013-05-31 18:36:12 +01001761 efx_mcdi_display_error(efx, MC_CMD_FINI_EVQ, MC_CMD_FINI_EVQ_IN_LEN,
1762 outbuf, outlen, rc);
Ben Hutchings8127d662013-08-29 19:19:29 +01001763}
1764
1765static void efx_ef10_ev_remove(struct efx_channel *channel)
1766{
1767 efx_nic_free_buffer(channel->efx, &channel->eventq.buf);
1768}
1769
1770static void efx_ef10_handle_rx_wrong_queue(struct efx_rx_queue *rx_queue,
1771 unsigned int rx_queue_label)
1772{
1773 struct efx_nic *efx = rx_queue->efx;
1774
1775 netif_info(efx, hw, efx->net_dev,
1776 "rx event arrived on queue %d labeled as queue %u\n",
1777 efx_rx_queue_index(rx_queue), rx_queue_label);
1778
1779 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
1780}
1781
1782static void
1783efx_ef10_handle_rx_bad_lbits(struct efx_rx_queue *rx_queue,
1784 unsigned int actual, unsigned int expected)
1785{
1786 unsigned int dropped = (actual - expected) & rx_queue->ptr_mask;
1787 struct efx_nic *efx = rx_queue->efx;
1788
1789 netif_info(efx, hw, efx->net_dev,
1790 "dropped %d events (index=%d expected=%d)\n",
1791 dropped, actual, expected);
1792
1793 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
1794}
1795
1796/* partially received RX was aborted. clean up. */
1797static void efx_ef10_handle_rx_abort(struct efx_rx_queue *rx_queue)
1798{
1799 unsigned int rx_desc_ptr;
1800
Ben Hutchings8127d662013-08-29 19:19:29 +01001801 netif_dbg(rx_queue->efx, hw, rx_queue->efx->net_dev,
1802 "scattered RX aborted (dropping %u buffers)\n",
1803 rx_queue->scatter_n);
1804
1805 rx_desc_ptr = rx_queue->removed_count & rx_queue->ptr_mask;
1806
1807 efx_rx_packet(rx_queue, rx_desc_ptr, rx_queue->scatter_n,
1808 0, EFX_RX_PKT_DISCARD);
1809
1810 rx_queue->removed_count += rx_queue->scatter_n;
1811 rx_queue->scatter_n = 0;
1812 rx_queue->scatter_len = 0;
1813 ++efx_rx_queue_channel(rx_queue)->n_rx_nodesc_trunc;
1814}
1815
1816static int efx_ef10_handle_rx_event(struct efx_channel *channel,
1817 const efx_qword_t *event)
1818{
1819 unsigned int rx_bytes, next_ptr_lbits, rx_queue_label, rx_l4_class;
1820 unsigned int n_descs, n_packets, i;
1821 struct efx_nic *efx = channel->efx;
1822 struct efx_rx_queue *rx_queue;
1823 bool rx_cont;
1824 u16 flags = 0;
1825
1826 if (unlikely(ACCESS_ONCE(efx->reset_pending)))
1827 return 0;
1828
1829 /* Basic packet information */
1830 rx_bytes = EFX_QWORD_FIELD(*event, ESF_DZ_RX_BYTES);
1831 next_ptr_lbits = EFX_QWORD_FIELD(*event, ESF_DZ_RX_DSC_PTR_LBITS);
1832 rx_queue_label = EFX_QWORD_FIELD(*event, ESF_DZ_RX_QLABEL);
1833 rx_l4_class = EFX_QWORD_FIELD(*event, ESF_DZ_RX_L4_CLASS);
1834 rx_cont = EFX_QWORD_FIELD(*event, ESF_DZ_RX_CONT);
1835
Ben Hutchings48ce5632013-11-01 16:42:44 +00001836 if (EFX_QWORD_FIELD(*event, ESF_DZ_RX_DROP_EVENT))
1837 netdev_WARN(efx->net_dev, "saw RX_DROP_EVENT: event="
1838 EFX_QWORD_FMT "\n",
1839 EFX_QWORD_VAL(*event));
Ben Hutchings8127d662013-08-29 19:19:29 +01001840
1841 rx_queue = efx_channel_get_rx_queue(channel);
1842
1843 if (unlikely(rx_queue_label != efx_rx_queue_index(rx_queue)))
1844 efx_ef10_handle_rx_wrong_queue(rx_queue, rx_queue_label);
1845
1846 n_descs = ((next_ptr_lbits - rx_queue->removed_count) &
1847 ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
1848
1849 if (n_descs != rx_queue->scatter_n + 1) {
Ben Hutchings92a04162013-09-24 23:21:57 +01001850 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1851
Ben Hutchings8127d662013-08-29 19:19:29 +01001852 /* detect rx abort */
1853 if (unlikely(n_descs == rx_queue->scatter_n)) {
Ben Hutchings48ce5632013-11-01 16:42:44 +00001854 if (rx_queue->scatter_n == 0 || rx_bytes != 0)
1855 netdev_WARN(efx->net_dev,
1856 "invalid RX abort: scatter_n=%u event="
1857 EFX_QWORD_FMT "\n",
1858 rx_queue->scatter_n,
1859 EFX_QWORD_VAL(*event));
Ben Hutchings8127d662013-08-29 19:19:29 +01001860 efx_ef10_handle_rx_abort(rx_queue);
1861 return 0;
1862 }
1863
Ben Hutchings92a04162013-09-24 23:21:57 +01001864 /* Check that RX completion merging is valid, i.e.
1865 * the current firmware supports it and this is a
1866 * non-scattered packet.
1867 */
1868 if (!(nic_data->datapath_caps &
1869 (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN)) ||
1870 rx_queue->scatter_n != 0 || rx_cont) {
Ben Hutchings8127d662013-08-29 19:19:29 +01001871 efx_ef10_handle_rx_bad_lbits(
1872 rx_queue, next_ptr_lbits,
1873 (rx_queue->removed_count +
1874 rx_queue->scatter_n + 1) &
1875 ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
1876 return 0;
1877 }
1878
1879 /* Merged completion for multiple non-scattered packets */
1880 rx_queue->scatter_n = 1;
1881 rx_queue->scatter_len = 0;
1882 n_packets = n_descs;
1883 ++channel->n_rx_merge_events;
1884 channel->n_rx_merge_packets += n_packets;
1885 flags |= EFX_RX_PKT_PREFIX_LEN;
1886 } else {
1887 ++rx_queue->scatter_n;
1888 rx_queue->scatter_len += rx_bytes;
1889 if (rx_cont)
1890 return 0;
1891 n_packets = 1;
1892 }
1893
1894 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_ECRC_ERR)))
1895 flags |= EFX_RX_PKT_DISCARD;
1896
1897 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_IPCKSUM_ERR))) {
1898 channel->n_rx_ip_hdr_chksum_err += n_packets;
1899 } else if (unlikely(EFX_QWORD_FIELD(*event,
1900 ESF_DZ_RX_TCPUDP_CKSUM_ERR))) {
1901 channel->n_rx_tcp_udp_chksum_err += n_packets;
1902 } else if (rx_l4_class == ESE_DZ_L4_CLASS_TCP ||
1903 rx_l4_class == ESE_DZ_L4_CLASS_UDP) {
1904 flags |= EFX_RX_PKT_CSUMMED;
1905 }
1906
1907 if (rx_l4_class == ESE_DZ_L4_CLASS_TCP)
1908 flags |= EFX_RX_PKT_TCP;
1909
1910 channel->irq_mod_score += 2 * n_packets;
1911
1912 /* Handle received packet(s) */
1913 for (i = 0; i < n_packets; i++) {
1914 efx_rx_packet(rx_queue,
1915 rx_queue->removed_count & rx_queue->ptr_mask,
1916 rx_queue->scatter_n, rx_queue->scatter_len,
1917 flags);
1918 rx_queue->removed_count += rx_queue->scatter_n;
1919 }
1920
1921 rx_queue->scatter_n = 0;
1922 rx_queue->scatter_len = 0;
1923
1924 return n_packets;
1925}
1926
1927static int
1928efx_ef10_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
1929{
1930 struct efx_nic *efx = channel->efx;
1931 struct efx_tx_queue *tx_queue;
1932 unsigned int tx_ev_desc_ptr;
1933 unsigned int tx_ev_q_label;
1934 int tx_descs = 0;
1935
1936 if (unlikely(ACCESS_ONCE(efx->reset_pending)))
1937 return 0;
1938
1939 if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_TX_DROP_EVENT)))
1940 return 0;
1941
1942 /* Transmit completion */
1943 tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, ESF_DZ_TX_DESCR_INDX);
1944 tx_ev_q_label = EFX_QWORD_FIELD(*event, ESF_DZ_TX_QLABEL);
1945 tx_queue = efx_channel_get_tx_queue(channel,
1946 tx_ev_q_label % EFX_TXQ_TYPES);
1947 tx_descs = ((tx_ev_desc_ptr + 1 - tx_queue->read_count) &
1948 tx_queue->ptr_mask);
1949 efx_xmit_done(tx_queue, tx_ev_desc_ptr & tx_queue->ptr_mask);
1950
1951 return tx_descs;
1952}
1953
1954static void
1955efx_ef10_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
1956{
1957 struct efx_nic *efx = channel->efx;
1958 int subcode;
1959
1960 subcode = EFX_QWORD_FIELD(*event, ESF_DZ_DRV_SUB_CODE);
1961
1962 switch (subcode) {
1963 case ESE_DZ_DRV_TIMER_EV:
1964 case ESE_DZ_DRV_WAKE_UP_EV:
1965 break;
1966 case ESE_DZ_DRV_START_UP_EV:
1967 /* event queue init complete. ok. */
1968 break;
1969 default:
1970 netif_err(efx, hw, efx->net_dev,
1971 "channel %d unknown driver event type %d"
1972 " (data " EFX_QWORD_FMT ")\n",
1973 channel->channel, subcode,
1974 EFX_QWORD_VAL(*event));
1975
1976 }
1977}
1978
1979static void efx_ef10_handle_driver_generated_event(struct efx_channel *channel,
1980 efx_qword_t *event)
1981{
1982 struct efx_nic *efx = channel->efx;
1983 u32 subcode;
1984
1985 subcode = EFX_QWORD_FIELD(*event, EFX_DWORD_0);
1986
1987 switch (subcode) {
1988 case EFX_EF10_TEST:
1989 channel->event_test_cpu = raw_smp_processor_id();
1990 break;
1991 case EFX_EF10_REFILL:
1992 /* The queue must be empty, so we won't receive any rx
1993 * events, so efx_process_channel() won't refill the
1994 * queue. Refill it here
1995 */
Jon Coopercce28792013-10-02 11:04:14 +01001996 efx_fast_push_rx_descriptors(&channel->rx_queue, true);
Ben Hutchings8127d662013-08-29 19:19:29 +01001997 break;
1998 default:
1999 netif_err(efx, hw, efx->net_dev,
2000 "channel %d unknown driver event type %u"
2001 " (data " EFX_QWORD_FMT ")\n",
2002 channel->channel, (unsigned) subcode,
2003 EFX_QWORD_VAL(*event));
2004 }
2005}
2006
2007static int efx_ef10_ev_process(struct efx_channel *channel, int quota)
2008{
2009 struct efx_nic *efx = channel->efx;
2010 efx_qword_t event, *p_event;
2011 unsigned int read_ptr;
2012 int ev_code;
2013 int tx_descs = 0;
2014 int spent = 0;
2015
Eric W. Biederman75363a42014-03-14 18:11:22 -07002016 if (quota <= 0)
2017 return spent;
2018
Ben Hutchings8127d662013-08-29 19:19:29 +01002019 read_ptr = channel->eventq_read_ptr;
2020
2021 for (;;) {
2022 p_event = efx_event(channel, read_ptr);
2023 event = *p_event;
2024
2025 if (!efx_event_present(&event))
2026 break;
2027
2028 EFX_SET_QWORD(*p_event);
2029
2030 ++read_ptr;
2031
2032 ev_code = EFX_QWORD_FIELD(event, ESF_DZ_EV_CODE);
2033
2034 netif_vdbg(efx, drv, efx->net_dev,
2035 "processing event on %d " EFX_QWORD_FMT "\n",
2036 channel->channel, EFX_QWORD_VAL(event));
2037
2038 switch (ev_code) {
2039 case ESE_DZ_EV_CODE_MCDI_EV:
2040 efx_mcdi_process_event(channel, &event);
2041 break;
2042 case ESE_DZ_EV_CODE_RX_EV:
2043 spent += efx_ef10_handle_rx_event(channel, &event);
2044 if (spent >= quota) {
2045 /* XXX can we split a merged event to
2046 * avoid going over-quota?
2047 */
2048 spent = quota;
2049 goto out;
2050 }
2051 break;
2052 case ESE_DZ_EV_CODE_TX_EV:
2053 tx_descs += efx_ef10_handle_tx_event(channel, &event);
2054 if (tx_descs > efx->txq_entries) {
2055 spent = quota;
2056 goto out;
2057 } else if (++spent == quota) {
2058 goto out;
2059 }
2060 break;
2061 case ESE_DZ_EV_CODE_DRIVER_EV:
2062 efx_ef10_handle_driver_event(channel, &event);
2063 if (++spent == quota)
2064 goto out;
2065 break;
2066 case EFX_EF10_DRVGEN_EV:
2067 efx_ef10_handle_driver_generated_event(channel, &event);
2068 break;
2069 default:
2070 netif_err(efx, hw, efx->net_dev,
2071 "channel %d unknown event type %d"
2072 " (data " EFX_QWORD_FMT ")\n",
2073 channel->channel, ev_code,
2074 EFX_QWORD_VAL(event));
2075 }
2076 }
2077
2078out:
2079 channel->eventq_read_ptr = read_ptr;
2080 return spent;
2081}
2082
2083static void efx_ef10_ev_read_ack(struct efx_channel *channel)
2084{
2085 struct efx_nic *efx = channel->efx;
2086 efx_dword_t rptr;
2087
2088 if (EFX_EF10_WORKAROUND_35388(efx)) {
2089 BUILD_BUG_ON(EFX_MIN_EVQ_SIZE <
2090 (1 << ERF_DD_EVQ_IND_RPTR_WIDTH));
2091 BUILD_BUG_ON(EFX_MAX_EVQ_SIZE >
2092 (1 << 2 * ERF_DD_EVQ_IND_RPTR_WIDTH));
2093
2094 EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
2095 EFE_DD_EVQ_IND_RPTR_FLAGS_HIGH,
2096 ERF_DD_EVQ_IND_RPTR,
2097 (channel->eventq_read_ptr &
2098 channel->eventq_mask) >>
2099 ERF_DD_EVQ_IND_RPTR_WIDTH);
2100 efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
2101 channel->channel);
2102 EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
2103 EFE_DD_EVQ_IND_RPTR_FLAGS_LOW,
2104 ERF_DD_EVQ_IND_RPTR,
2105 channel->eventq_read_ptr &
2106 ((1 << ERF_DD_EVQ_IND_RPTR_WIDTH) - 1));
2107 efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
2108 channel->channel);
2109 } else {
2110 EFX_POPULATE_DWORD_1(rptr, ERF_DZ_EVQ_RPTR,
2111 channel->eventq_read_ptr &
2112 channel->eventq_mask);
2113 efx_writed_page(efx, &rptr, ER_DZ_EVQ_RPTR, channel->channel);
2114 }
2115}
2116
2117static void efx_ef10_ev_test_generate(struct efx_channel *channel)
2118{
2119 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
2120 struct efx_nic *efx = channel->efx;
2121 efx_qword_t event;
2122 int rc;
2123
2124 EFX_POPULATE_QWORD_2(event,
2125 ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
2126 ESF_DZ_EV_DATA, EFX_EF10_TEST);
2127
2128 MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
2129
2130 /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
2131 * already swapped the data to little-endian order.
2132 */
2133 memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
2134 sizeof(efx_qword_t));
2135
2136 rc = efx_mcdi_rpc(efx, MC_CMD_DRIVER_EVENT, inbuf, sizeof(inbuf),
2137 NULL, 0, NULL);
2138 if (rc != 0)
2139 goto fail;
2140
2141 return;
2142
2143fail:
2144 WARN_ON(true);
2145 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
2146}
2147
2148void efx_ef10_handle_drain_event(struct efx_nic *efx)
2149{
2150 if (atomic_dec_and_test(&efx->active_queues))
2151 wake_up(&efx->flush_wq);
2152
2153 WARN_ON(atomic_read(&efx->active_queues) < 0);
2154}
2155
2156static int efx_ef10_fini_dmaq(struct efx_nic *efx)
2157{
2158 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2159 struct efx_channel *channel;
2160 struct efx_tx_queue *tx_queue;
2161 struct efx_rx_queue *rx_queue;
2162 int pending;
2163
2164 /* If the MC has just rebooted, the TX/RX queues will have already been
2165 * torn down, but efx->active_queues needs to be set to zero.
2166 */
2167 if (nic_data->must_realloc_vis) {
2168 atomic_set(&efx->active_queues, 0);
2169 return 0;
2170 }
2171
2172 /* Do not attempt to write to the NIC during EEH recovery */
2173 if (efx->state != STATE_RECOVERY) {
2174 efx_for_each_channel(channel, efx) {
2175 efx_for_each_channel_rx_queue(rx_queue, channel)
2176 efx_ef10_rx_fini(rx_queue);
2177 efx_for_each_channel_tx_queue(tx_queue, channel)
2178 efx_ef10_tx_fini(tx_queue);
2179 }
2180
2181 wait_event_timeout(efx->flush_wq,
2182 atomic_read(&efx->active_queues) == 0,
2183 msecs_to_jiffies(EFX_MAX_FLUSH_TIME));
2184 pending = atomic_read(&efx->active_queues);
2185 if (pending) {
2186 netif_err(efx, hw, efx->net_dev, "failed to flush %d queues\n",
2187 pending);
2188 return -ETIMEDOUT;
2189 }
2190 }
2191
2192 return 0;
2193}
2194
Edward Creee2835462014-04-16 19:27:48 +01002195static void efx_ef10_prepare_flr(struct efx_nic *efx)
2196{
2197 atomic_set(&efx->active_queues, 0);
2198}
2199
Ben Hutchings8127d662013-08-29 19:19:29 +01002200static bool efx_ef10_filter_equal(const struct efx_filter_spec *left,
2201 const struct efx_filter_spec *right)
2202{
2203 if ((left->match_flags ^ right->match_flags) |
2204 ((left->flags ^ right->flags) &
2205 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
2206 return false;
2207
2208 return memcmp(&left->outer_vid, &right->outer_vid,
2209 sizeof(struct efx_filter_spec) -
2210 offsetof(struct efx_filter_spec, outer_vid)) == 0;
2211}
2212
2213static unsigned int efx_ef10_filter_hash(const struct efx_filter_spec *spec)
2214{
2215 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
2216 return jhash2((const u32 *)&spec->outer_vid,
2217 (sizeof(struct efx_filter_spec) -
2218 offsetof(struct efx_filter_spec, outer_vid)) / 4,
2219 0);
2220 /* XXX should we randomise the initval? */
2221}
2222
2223/* Decide whether a filter should be exclusive or else should allow
2224 * delivery to additional recipients. Currently we decide that
2225 * filters for specific local unicast MAC and IP addresses are
2226 * exclusive.
2227 */
2228static bool efx_ef10_filter_is_exclusive(const struct efx_filter_spec *spec)
2229{
2230 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
2231 !is_multicast_ether_addr(spec->loc_mac))
2232 return true;
2233
2234 if ((spec->match_flags &
2235 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
2236 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
2237 if (spec->ether_type == htons(ETH_P_IP) &&
2238 !ipv4_is_multicast(spec->loc_host[0]))
2239 return true;
2240 if (spec->ether_type == htons(ETH_P_IPV6) &&
2241 ((const u8 *)spec->loc_host)[0] != 0xff)
2242 return true;
2243 }
2244
2245 return false;
2246}
2247
2248static struct efx_filter_spec *
2249efx_ef10_filter_entry_spec(const struct efx_ef10_filter_table *table,
2250 unsigned int filter_idx)
2251{
2252 return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
2253 ~EFX_EF10_FILTER_FLAGS);
2254}
2255
2256static unsigned int
2257efx_ef10_filter_entry_flags(const struct efx_ef10_filter_table *table,
2258 unsigned int filter_idx)
2259{
2260 return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
2261}
2262
2263static void
2264efx_ef10_filter_set_entry(struct efx_ef10_filter_table *table,
2265 unsigned int filter_idx,
2266 const struct efx_filter_spec *spec,
2267 unsigned int flags)
2268{
2269 table->entry[filter_idx].spec = (unsigned long)spec | flags;
2270}
2271
2272static void efx_ef10_filter_push_prep(struct efx_nic *efx,
2273 const struct efx_filter_spec *spec,
2274 efx_dword_t *inbuf, u64 handle,
2275 bool replacing)
2276{
2277 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2278
2279 memset(inbuf, 0, MC_CMD_FILTER_OP_IN_LEN);
2280
2281 if (replacing) {
2282 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2283 MC_CMD_FILTER_OP_IN_OP_REPLACE);
2284 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
2285 } else {
2286 u32 match_fields = 0;
2287
2288 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2289 efx_ef10_filter_is_exclusive(spec) ?
2290 MC_CMD_FILTER_OP_IN_OP_INSERT :
2291 MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
2292
2293 /* Convert match flags and values. Unlike almost
2294 * everything else in MCDI, these fields are in
2295 * network byte order.
2296 */
2297 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
2298 match_fields |=
2299 is_multicast_ether_addr(spec->loc_mac) ?
2300 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST_LBN :
2301 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
2302#define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
2303 if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
2304 match_fields |= \
2305 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
2306 mcdi_field ## _LBN; \
2307 BUILD_BUG_ON( \
2308 MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
2309 sizeof(spec->gen_field)); \
2310 memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
2311 &spec->gen_field, sizeof(spec->gen_field)); \
2312 }
2313 COPY_FIELD(REM_HOST, rem_host, SRC_IP);
2314 COPY_FIELD(LOC_HOST, loc_host, DST_IP);
2315 COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
2316 COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
2317 COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
2318 COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
2319 COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
2320 COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
2321 COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
2322 COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
2323#undef COPY_FIELD
2324 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
2325 match_fields);
2326 }
2327
Daniel Pieczko45b24492015-05-06 00:57:14 +01002328 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, nic_data->vport_id);
Ben Hutchings8127d662013-08-29 19:19:29 +01002329 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
2330 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
2331 MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
2332 MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
Shradha Shahe3d36292015-05-06 00:56:24 +01002333 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DOMAIN, 0);
Ben Hutchings8127d662013-08-29 19:19:29 +01002334 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
2335 MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
Ben Hutchingsa0bc3482013-12-16 18:56:24 +00002336 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE,
2337 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
2338 0 : spec->dmaq_id);
Ben Hutchings8127d662013-08-29 19:19:29 +01002339 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
2340 (spec->flags & EFX_FILTER_FLAG_RX_RSS) ?
2341 MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
2342 MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
2343 if (spec->flags & EFX_FILTER_FLAG_RX_RSS)
2344 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT,
2345 spec->rss_context !=
2346 EFX_FILTER_RSS_CONTEXT_DEFAULT ?
2347 spec->rss_context : nic_data->rx_rss_context);
2348}
2349
2350static int efx_ef10_filter_push(struct efx_nic *efx,
2351 const struct efx_filter_spec *spec,
2352 u64 *handle, bool replacing)
2353{
2354 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
2355 MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_OUT_LEN);
2356 int rc;
2357
2358 efx_ef10_filter_push_prep(efx, spec, inbuf, *handle, replacing);
2359 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
2360 outbuf, sizeof(outbuf), NULL);
2361 if (rc == 0)
2362 *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
Ben Hutchings065e64c2013-10-09 14:17:27 +01002363 if (rc == -ENOSPC)
2364 rc = -EBUSY; /* to match efx_farch_filter_insert() */
Ben Hutchings8127d662013-08-29 19:19:29 +01002365 return rc;
2366}
2367
2368static int efx_ef10_filter_rx_match_pri(struct efx_ef10_filter_table *table,
2369 enum efx_filter_match_flags match_flags)
2370{
2371 unsigned int match_pri;
2372
2373 for (match_pri = 0;
2374 match_pri < table->rx_match_count;
2375 match_pri++)
2376 if (table->rx_match_flags[match_pri] == match_flags)
2377 return match_pri;
2378
2379 return -EPROTONOSUPPORT;
2380}
2381
2382static s32 efx_ef10_filter_insert(struct efx_nic *efx,
2383 struct efx_filter_spec *spec,
2384 bool replace_equal)
2385{
2386 struct efx_ef10_filter_table *table = efx->filter_state;
2387 DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
2388 struct efx_filter_spec *saved_spec;
2389 unsigned int match_pri, hash;
2390 unsigned int priv_flags;
2391 bool replacing = false;
2392 int ins_index = -1;
2393 DEFINE_WAIT(wait);
2394 bool is_mc_recip;
2395 s32 rc;
2396
2397 /* For now, only support RX filters */
2398 if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
2399 EFX_FILTER_FLAG_RX)
2400 return -EINVAL;
2401
2402 rc = efx_ef10_filter_rx_match_pri(table, spec->match_flags);
2403 if (rc < 0)
2404 return rc;
2405 match_pri = rc;
2406
2407 hash = efx_ef10_filter_hash(spec);
2408 is_mc_recip = efx_filter_is_mc_recipient(spec);
2409 if (is_mc_recip)
2410 bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
2411
2412 /* Find any existing filters with the same match tuple or
2413 * else a free slot to insert at. If any of them are busy,
2414 * we have to wait and retry.
2415 */
2416 for (;;) {
2417 unsigned int depth = 1;
2418 unsigned int i;
2419
2420 spin_lock_bh(&efx->filter_lock);
2421
2422 for (;;) {
2423 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2424 saved_spec = efx_ef10_filter_entry_spec(table, i);
2425
2426 if (!saved_spec) {
2427 if (ins_index < 0)
2428 ins_index = i;
2429 } else if (efx_ef10_filter_equal(spec, saved_spec)) {
2430 if (table->entry[i].spec &
2431 EFX_EF10_FILTER_FLAG_BUSY)
2432 break;
2433 if (spec->priority < saved_spec->priority &&
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002434 spec->priority != EFX_FILTER_PRI_AUTO) {
Ben Hutchings8127d662013-08-29 19:19:29 +01002435 rc = -EPERM;
2436 goto out_unlock;
2437 }
2438 if (!is_mc_recip) {
2439 /* This is the only one */
2440 if (spec->priority ==
2441 saved_spec->priority &&
2442 !replace_equal) {
2443 rc = -EEXIST;
2444 goto out_unlock;
2445 }
2446 ins_index = i;
2447 goto found;
2448 } else if (spec->priority >
2449 saved_spec->priority ||
2450 (spec->priority ==
2451 saved_spec->priority &&
2452 replace_equal)) {
2453 if (ins_index < 0)
2454 ins_index = i;
2455 else
2456 __set_bit(depth, mc_rem_map);
2457 }
2458 }
2459
2460 /* Once we reach the maximum search depth, use
2461 * the first suitable slot or return -EBUSY if
2462 * there was none
2463 */
2464 if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
2465 if (ins_index < 0) {
2466 rc = -EBUSY;
2467 goto out_unlock;
2468 }
2469 goto found;
2470 }
2471
2472 ++depth;
2473 }
2474
2475 prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
2476 spin_unlock_bh(&efx->filter_lock);
2477 schedule();
2478 }
2479
2480found:
2481 /* Create a software table entry if necessary, and mark it
2482 * busy. We might yet fail to insert, but any attempt to
2483 * insert a conflicting filter while we're waiting for the
2484 * firmware must find the busy entry.
2485 */
2486 saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
2487 if (saved_spec) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002488 if (spec->priority == EFX_FILTER_PRI_AUTO &&
2489 saved_spec->priority >= EFX_FILTER_PRI_AUTO) {
Ben Hutchings8127d662013-08-29 19:19:29 +01002490 /* Just make sure it won't be removed */
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002491 if (saved_spec->priority > EFX_FILTER_PRI_AUTO)
2492 saved_spec->flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
Ben Hutchings8127d662013-08-29 19:19:29 +01002493 table->entry[ins_index].spec &=
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00002494 ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
Ben Hutchings8127d662013-08-29 19:19:29 +01002495 rc = ins_index;
2496 goto out_unlock;
2497 }
2498 replacing = true;
2499 priv_flags = efx_ef10_filter_entry_flags(table, ins_index);
2500 } else {
2501 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
2502 if (!saved_spec) {
2503 rc = -ENOMEM;
2504 goto out_unlock;
2505 }
2506 *saved_spec = *spec;
2507 priv_flags = 0;
2508 }
2509 efx_ef10_filter_set_entry(table, ins_index, saved_spec,
2510 priv_flags | EFX_EF10_FILTER_FLAG_BUSY);
2511
2512 /* Mark lower-priority multicast recipients busy prior to removal */
2513 if (is_mc_recip) {
2514 unsigned int depth, i;
2515
2516 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
2517 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2518 if (test_bit(depth, mc_rem_map))
2519 table->entry[i].spec |=
2520 EFX_EF10_FILTER_FLAG_BUSY;
2521 }
2522 }
2523
2524 spin_unlock_bh(&efx->filter_lock);
2525
2526 rc = efx_ef10_filter_push(efx, spec, &table->entry[ins_index].handle,
2527 replacing);
2528
2529 /* Finalise the software table entry */
2530 spin_lock_bh(&efx->filter_lock);
2531 if (rc == 0) {
2532 if (replacing) {
2533 /* Update the fields that may differ */
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002534 if (saved_spec->priority == EFX_FILTER_PRI_AUTO)
2535 saved_spec->flags |=
2536 EFX_FILTER_FLAG_RX_OVER_AUTO;
Ben Hutchings8127d662013-08-29 19:19:29 +01002537 saved_spec->priority = spec->priority;
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002538 saved_spec->flags &= EFX_FILTER_FLAG_RX_OVER_AUTO;
Ben Hutchings8127d662013-08-29 19:19:29 +01002539 saved_spec->flags |= spec->flags;
2540 saved_spec->rss_context = spec->rss_context;
2541 saved_spec->dmaq_id = spec->dmaq_id;
2542 }
2543 } else if (!replacing) {
2544 kfree(saved_spec);
2545 saved_spec = NULL;
2546 }
2547 efx_ef10_filter_set_entry(table, ins_index, saved_spec, priv_flags);
2548
2549 /* Remove and finalise entries for lower-priority multicast
2550 * recipients
2551 */
2552 if (is_mc_recip) {
2553 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
2554 unsigned int depth, i;
2555
2556 memset(inbuf, 0, sizeof(inbuf));
2557
2558 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
2559 if (!test_bit(depth, mc_rem_map))
2560 continue;
2561
2562 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2563 saved_spec = efx_ef10_filter_entry_spec(table, i);
2564 priv_flags = efx_ef10_filter_entry_flags(table, i);
2565
2566 if (rc == 0) {
2567 spin_unlock_bh(&efx->filter_lock);
2568 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2569 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
2570 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2571 table->entry[i].handle);
2572 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
2573 inbuf, sizeof(inbuf),
2574 NULL, 0, NULL);
2575 spin_lock_bh(&efx->filter_lock);
2576 }
2577
2578 if (rc == 0) {
2579 kfree(saved_spec);
2580 saved_spec = NULL;
2581 priv_flags = 0;
2582 } else {
2583 priv_flags &= ~EFX_EF10_FILTER_FLAG_BUSY;
2584 }
2585 efx_ef10_filter_set_entry(table, i, saved_spec,
2586 priv_flags);
2587 }
2588 }
2589
2590 /* If successful, return the inserted filter ID */
2591 if (rc == 0)
2592 rc = match_pri * HUNT_FILTER_TBL_ROWS + ins_index;
2593
2594 wake_up_all(&table->waitq);
2595out_unlock:
2596 spin_unlock_bh(&efx->filter_lock);
2597 finish_wait(&table->waitq, &wait);
2598 return rc;
2599}
2600
Fengguang Wu9fd8095d2013-08-31 06:54:05 +08002601static void efx_ef10_filter_update_rx_scatter(struct efx_nic *efx)
Ben Hutchings8127d662013-08-29 19:19:29 +01002602{
2603 /* no need to do anything here on EF10 */
2604}
2605
2606/* Remove a filter.
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00002607 * If !by_index, remove by ID
2608 * If by_index, remove by index
Ben Hutchings8127d662013-08-29 19:19:29 +01002609 * Filter ID may come from userland and must be range-checked.
2610 */
2611static int efx_ef10_filter_remove_internal(struct efx_nic *efx,
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002612 unsigned int priority_mask,
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00002613 u32 filter_id, bool by_index)
Ben Hutchings8127d662013-08-29 19:19:29 +01002614{
2615 unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
2616 struct efx_ef10_filter_table *table = efx->filter_state;
2617 MCDI_DECLARE_BUF(inbuf,
2618 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
2619 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
2620 struct efx_filter_spec *spec;
2621 DEFINE_WAIT(wait);
2622 int rc;
2623
2624 /* Find the software table entry and mark it busy. Don't
2625 * remove it yet; any attempt to update while we're waiting
2626 * for the firmware must find the busy entry.
2627 */
2628 for (;;) {
2629 spin_lock_bh(&efx->filter_lock);
2630 if (!(table->entry[filter_idx].spec &
2631 EFX_EF10_FILTER_FLAG_BUSY))
2632 break;
2633 prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
2634 spin_unlock_bh(&efx->filter_lock);
2635 schedule();
2636 }
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002637
Ben Hutchings8127d662013-08-29 19:19:29 +01002638 spec = efx_ef10_filter_entry_spec(table, filter_idx);
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002639 if (!spec ||
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00002640 (!by_index &&
Ben Hutchings8127d662013-08-29 19:19:29 +01002641 efx_ef10_filter_rx_match_pri(table, spec->match_flags) !=
2642 filter_id / HUNT_FILTER_TBL_ROWS)) {
2643 rc = -ENOENT;
2644 goto out_unlock;
2645 }
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002646
2647 if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO &&
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002648 priority_mask == (1U << EFX_FILTER_PRI_AUTO)) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002649 /* Just remove flags */
2650 spec->flags &= ~EFX_FILTER_FLAG_RX_OVER_AUTO;
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00002651 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002652 rc = 0;
2653 goto out_unlock;
2654 }
2655
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002656 if (!(priority_mask & (1U << spec->priority))) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002657 rc = -ENOENT;
2658 goto out_unlock;
2659 }
2660
Ben Hutchings8127d662013-08-29 19:19:29 +01002661 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
2662 spin_unlock_bh(&efx->filter_lock);
2663
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002664 if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00002665 /* Reset to an automatic filter */
Ben Hutchings8127d662013-08-29 19:19:29 +01002666
2667 struct efx_filter_spec new_spec = *spec;
2668
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002669 new_spec.priority = EFX_FILTER_PRI_AUTO;
Ben Hutchings8127d662013-08-29 19:19:29 +01002670 new_spec.flags = (EFX_FILTER_FLAG_RX |
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002671 EFX_FILTER_FLAG_RX_RSS);
Ben Hutchings8127d662013-08-29 19:19:29 +01002672 new_spec.dmaq_id = 0;
2673 new_spec.rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
2674 rc = efx_ef10_filter_push(efx, &new_spec,
2675 &table->entry[filter_idx].handle,
2676 true);
2677
2678 spin_lock_bh(&efx->filter_lock);
2679 if (rc == 0)
2680 *spec = new_spec;
2681 } else {
2682 /* Really remove the filter */
2683
2684 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2685 efx_ef10_filter_is_exclusive(spec) ?
2686 MC_CMD_FILTER_OP_IN_OP_REMOVE :
2687 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
2688 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2689 table->entry[filter_idx].handle);
2690 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
2691 inbuf, sizeof(inbuf), NULL, 0, NULL);
2692
2693 spin_lock_bh(&efx->filter_lock);
2694 if (rc == 0) {
2695 kfree(spec);
2696 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
2697 }
2698 }
Ben Hutchings7665d1a2013-11-21 19:02:18 +00002699
Ben Hutchings8127d662013-08-29 19:19:29 +01002700 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
2701 wake_up_all(&table->waitq);
2702out_unlock:
2703 spin_unlock_bh(&efx->filter_lock);
2704 finish_wait(&table->waitq, &wait);
2705 return rc;
2706}
2707
2708static int efx_ef10_filter_remove_safe(struct efx_nic *efx,
2709 enum efx_filter_priority priority,
2710 u32 filter_id)
2711{
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002712 return efx_ef10_filter_remove_internal(efx, 1U << priority,
2713 filter_id, false);
Ben Hutchings8127d662013-08-29 19:19:29 +01002714}
2715
2716static int efx_ef10_filter_get_safe(struct efx_nic *efx,
2717 enum efx_filter_priority priority,
2718 u32 filter_id, struct efx_filter_spec *spec)
2719{
2720 unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
2721 struct efx_ef10_filter_table *table = efx->filter_state;
2722 const struct efx_filter_spec *saved_spec;
2723 int rc;
2724
2725 spin_lock_bh(&efx->filter_lock);
2726 saved_spec = efx_ef10_filter_entry_spec(table, filter_idx);
2727 if (saved_spec && saved_spec->priority == priority &&
2728 efx_ef10_filter_rx_match_pri(table, saved_spec->match_flags) ==
2729 filter_id / HUNT_FILTER_TBL_ROWS) {
2730 *spec = *saved_spec;
2731 rc = 0;
2732 } else {
2733 rc = -ENOENT;
2734 }
2735 spin_unlock_bh(&efx->filter_lock);
2736 return rc;
2737}
2738
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002739static int efx_ef10_filter_clear_rx(struct efx_nic *efx,
Ben Hutchings8127d662013-08-29 19:19:29 +01002740 enum efx_filter_priority priority)
2741{
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002742 unsigned int priority_mask;
2743 unsigned int i;
2744 int rc;
2745
2746 priority_mask = (((1U << (priority + 1)) - 1) &
2747 ~(1U << EFX_FILTER_PRI_AUTO));
2748
2749 for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
2750 rc = efx_ef10_filter_remove_internal(efx, priority_mask,
2751 i, true);
2752 if (rc && rc != -ENOENT)
2753 return rc;
2754 }
2755
2756 return 0;
Ben Hutchings8127d662013-08-29 19:19:29 +01002757}
2758
2759static u32 efx_ef10_filter_count_rx_used(struct efx_nic *efx,
2760 enum efx_filter_priority priority)
2761{
2762 struct efx_ef10_filter_table *table = efx->filter_state;
2763 unsigned int filter_idx;
2764 s32 count = 0;
2765
2766 spin_lock_bh(&efx->filter_lock);
2767 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
2768 if (table->entry[filter_idx].spec &&
2769 efx_ef10_filter_entry_spec(table, filter_idx)->priority ==
2770 priority)
2771 ++count;
2772 }
2773 spin_unlock_bh(&efx->filter_lock);
2774 return count;
2775}
2776
2777static u32 efx_ef10_filter_get_rx_id_limit(struct efx_nic *efx)
2778{
2779 struct efx_ef10_filter_table *table = efx->filter_state;
2780
2781 return table->rx_match_count * HUNT_FILTER_TBL_ROWS;
2782}
2783
2784static s32 efx_ef10_filter_get_rx_ids(struct efx_nic *efx,
2785 enum efx_filter_priority priority,
2786 u32 *buf, u32 size)
2787{
2788 struct efx_ef10_filter_table *table = efx->filter_state;
2789 struct efx_filter_spec *spec;
2790 unsigned int filter_idx;
2791 s32 count = 0;
2792
2793 spin_lock_bh(&efx->filter_lock);
2794 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
2795 spec = efx_ef10_filter_entry_spec(table, filter_idx);
2796 if (spec && spec->priority == priority) {
2797 if (count == size) {
2798 count = -EMSGSIZE;
2799 break;
2800 }
2801 buf[count++] = (efx_ef10_filter_rx_match_pri(
2802 table, spec->match_flags) *
2803 HUNT_FILTER_TBL_ROWS +
2804 filter_idx);
2805 }
2806 }
2807 spin_unlock_bh(&efx->filter_lock);
2808 return count;
2809}
2810
2811#ifdef CONFIG_RFS_ACCEL
2812
2813static efx_mcdi_async_completer efx_ef10_filter_rfs_insert_complete;
2814
2815static s32 efx_ef10_filter_rfs_insert(struct efx_nic *efx,
2816 struct efx_filter_spec *spec)
2817{
2818 struct efx_ef10_filter_table *table = efx->filter_state;
2819 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
2820 struct efx_filter_spec *saved_spec;
2821 unsigned int hash, i, depth = 1;
2822 bool replacing = false;
2823 int ins_index = -1;
2824 u64 cookie;
2825 s32 rc;
2826
2827 /* Must be an RX filter without RSS and not for a multicast
2828 * destination address (RFS only works for connected sockets).
2829 * These restrictions allow us to pass only a tiny amount of
2830 * data through to the completion function.
2831 */
2832 EFX_WARN_ON_PARANOID(spec->flags !=
2833 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_RX_SCATTER));
2834 EFX_WARN_ON_PARANOID(spec->priority != EFX_FILTER_PRI_HINT);
2835 EFX_WARN_ON_PARANOID(efx_filter_is_mc_recipient(spec));
2836
2837 hash = efx_ef10_filter_hash(spec);
2838
2839 spin_lock_bh(&efx->filter_lock);
2840
2841 /* Find any existing filter with the same match tuple or else
2842 * a free slot to insert at. If an existing filter is busy,
2843 * we have to give up.
2844 */
2845 for (;;) {
2846 i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
2847 saved_spec = efx_ef10_filter_entry_spec(table, i);
2848
2849 if (!saved_spec) {
2850 if (ins_index < 0)
2851 ins_index = i;
2852 } else if (efx_ef10_filter_equal(spec, saved_spec)) {
2853 if (table->entry[i].spec & EFX_EF10_FILTER_FLAG_BUSY) {
2854 rc = -EBUSY;
2855 goto fail_unlock;
2856 }
Ben Hutchings8127d662013-08-29 19:19:29 +01002857 if (spec->priority < saved_spec->priority) {
2858 rc = -EPERM;
2859 goto fail_unlock;
2860 }
2861 ins_index = i;
2862 break;
2863 }
2864
2865 /* Once we reach the maximum search depth, use the
2866 * first suitable slot or return -EBUSY if there was
2867 * none
2868 */
2869 if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
2870 if (ins_index < 0) {
2871 rc = -EBUSY;
2872 goto fail_unlock;
2873 }
2874 break;
2875 }
2876
2877 ++depth;
2878 }
2879
2880 /* Create a software table entry if necessary, and mark it
2881 * busy. We might yet fail to insert, but any attempt to
2882 * insert a conflicting filter while we're waiting for the
2883 * firmware must find the busy entry.
2884 */
2885 saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
2886 if (saved_spec) {
2887 replacing = true;
2888 } else {
2889 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
2890 if (!saved_spec) {
2891 rc = -ENOMEM;
2892 goto fail_unlock;
2893 }
2894 *saved_spec = *spec;
2895 }
2896 efx_ef10_filter_set_entry(table, ins_index, saved_spec,
2897 EFX_EF10_FILTER_FLAG_BUSY);
2898
2899 spin_unlock_bh(&efx->filter_lock);
2900
2901 /* Pack up the variables needed on completion */
2902 cookie = replacing << 31 | ins_index << 16 | spec->dmaq_id;
2903
2904 efx_ef10_filter_push_prep(efx, spec, inbuf,
2905 table->entry[ins_index].handle, replacing);
2906 efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
2907 MC_CMD_FILTER_OP_OUT_LEN,
2908 efx_ef10_filter_rfs_insert_complete, cookie);
2909
2910 return ins_index;
2911
2912fail_unlock:
2913 spin_unlock_bh(&efx->filter_lock);
2914 return rc;
2915}
2916
2917static void
2918efx_ef10_filter_rfs_insert_complete(struct efx_nic *efx, unsigned long cookie,
2919 int rc, efx_dword_t *outbuf,
2920 size_t outlen_actual)
2921{
2922 struct efx_ef10_filter_table *table = efx->filter_state;
2923 unsigned int ins_index, dmaq_id;
2924 struct efx_filter_spec *spec;
2925 bool replacing;
2926
2927 /* Unpack the cookie */
2928 replacing = cookie >> 31;
2929 ins_index = (cookie >> 16) & (HUNT_FILTER_TBL_ROWS - 1);
2930 dmaq_id = cookie & 0xffff;
2931
2932 spin_lock_bh(&efx->filter_lock);
2933 spec = efx_ef10_filter_entry_spec(table, ins_index);
2934 if (rc == 0) {
2935 table->entry[ins_index].handle =
2936 MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
2937 if (replacing)
2938 spec->dmaq_id = dmaq_id;
2939 } else if (!replacing) {
2940 kfree(spec);
2941 spec = NULL;
2942 }
2943 efx_ef10_filter_set_entry(table, ins_index, spec, 0);
2944 spin_unlock_bh(&efx->filter_lock);
2945
2946 wake_up_all(&table->waitq);
2947}
2948
2949static void
2950efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
2951 unsigned long filter_idx,
2952 int rc, efx_dword_t *outbuf,
2953 size_t outlen_actual);
2954
2955static bool efx_ef10_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
2956 unsigned int filter_idx)
2957{
2958 struct efx_ef10_filter_table *table = efx->filter_state;
2959 struct efx_filter_spec *spec =
2960 efx_ef10_filter_entry_spec(table, filter_idx);
2961 MCDI_DECLARE_BUF(inbuf,
2962 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
2963 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
2964
2965 if (!spec ||
2966 (table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAG_BUSY) ||
2967 spec->priority != EFX_FILTER_PRI_HINT ||
2968 !rps_may_expire_flow(efx->net_dev, spec->dmaq_id,
2969 flow_id, filter_idx))
2970 return false;
2971
2972 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
2973 MC_CMD_FILTER_OP_IN_OP_REMOVE);
2974 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
2975 table->entry[filter_idx].handle);
2976 if (efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf), 0,
2977 efx_ef10_filter_rfs_expire_complete, filter_idx))
2978 return false;
2979
2980 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
2981 return true;
2982}
2983
2984static void
2985efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
2986 unsigned long filter_idx,
2987 int rc, efx_dword_t *outbuf,
2988 size_t outlen_actual)
2989{
2990 struct efx_ef10_filter_table *table = efx->filter_state;
2991 struct efx_filter_spec *spec =
2992 efx_ef10_filter_entry_spec(table, filter_idx);
2993
2994 spin_lock_bh(&efx->filter_lock);
2995 if (rc == 0) {
2996 kfree(spec);
2997 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
2998 }
2999 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
3000 wake_up_all(&table->waitq);
3001 spin_unlock_bh(&efx->filter_lock);
3002}
3003
3004#endif /* CONFIG_RFS_ACCEL */
3005
3006static int efx_ef10_filter_match_flags_from_mcdi(u32 mcdi_flags)
3007{
3008 int match_flags = 0;
3009
3010#define MAP_FLAG(gen_flag, mcdi_field) { \
3011 u32 old_mcdi_flags = mcdi_flags; \
3012 mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
3013 mcdi_field ## _LBN); \
3014 if (mcdi_flags != old_mcdi_flags) \
3015 match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
3016 }
3017 MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
3018 MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
3019 MAP_FLAG(REM_HOST, SRC_IP);
3020 MAP_FLAG(LOC_HOST, DST_IP);
3021 MAP_FLAG(REM_MAC, SRC_MAC);
3022 MAP_FLAG(REM_PORT, SRC_PORT);
3023 MAP_FLAG(LOC_MAC, DST_MAC);
3024 MAP_FLAG(LOC_PORT, DST_PORT);
3025 MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
3026 MAP_FLAG(INNER_VID, INNER_VLAN);
3027 MAP_FLAG(OUTER_VID, OUTER_VLAN);
3028 MAP_FLAG(IP_PROTO, IP_PROTO);
3029#undef MAP_FLAG
3030
3031 /* Did we map them all? */
3032 if (mcdi_flags)
3033 return -EINVAL;
3034
3035 return match_flags;
3036}
3037
3038static int efx_ef10_filter_table_probe(struct efx_nic *efx)
3039{
3040 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
3041 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
3042 unsigned int pd_match_pri, pd_match_count;
3043 struct efx_ef10_filter_table *table;
3044 size_t outlen;
3045 int rc;
3046
3047 table = kzalloc(sizeof(*table), GFP_KERNEL);
3048 if (!table)
3049 return -ENOMEM;
3050
3051 /* Find out which RX filter types are supported, and their priorities */
3052 MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
3053 MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
3054 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
3055 inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
3056 &outlen);
3057 if (rc)
3058 goto fail;
3059 pd_match_count = MCDI_VAR_ARRAY_LEN(
3060 outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
3061 table->rx_match_count = 0;
3062
3063 for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
3064 u32 mcdi_flags =
3065 MCDI_ARRAY_DWORD(
3066 outbuf,
3067 GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
3068 pd_match_pri);
3069 rc = efx_ef10_filter_match_flags_from_mcdi(mcdi_flags);
3070 if (rc < 0) {
3071 netif_dbg(efx, probe, efx->net_dev,
3072 "%s: fw flags %#x pri %u not supported in driver\n",
3073 __func__, mcdi_flags, pd_match_pri);
3074 } else {
3075 netif_dbg(efx, probe, efx->net_dev,
3076 "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
3077 __func__, mcdi_flags, pd_match_pri,
3078 rc, table->rx_match_count);
3079 table->rx_match_flags[table->rx_match_count++] = rc;
3080 }
3081 }
3082
3083 table->entry = vzalloc(HUNT_FILTER_TBL_ROWS * sizeof(*table->entry));
3084 if (!table->entry) {
3085 rc = -ENOMEM;
3086 goto fail;
3087 }
3088
3089 efx->filter_state = table;
3090 init_waitqueue_head(&table->waitq);
3091 return 0;
3092
3093fail:
3094 kfree(table);
3095 return rc;
3096}
3097
3098static void efx_ef10_filter_table_restore(struct efx_nic *efx)
3099{
3100 struct efx_ef10_filter_table *table = efx->filter_state;
3101 struct efx_ef10_nic_data *nic_data = efx->nic_data;
3102 struct efx_filter_spec *spec;
3103 unsigned int filter_idx;
3104 bool failed = false;
3105 int rc;
3106
3107 if (!nic_data->must_restore_filters)
3108 return;
3109
3110 spin_lock_bh(&efx->filter_lock);
3111
3112 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
3113 spec = efx_ef10_filter_entry_spec(table, filter_idx);
3114 if (!spec)
3115 continue;
3116
3117 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
3118 spin_unlock_bh(&efx->filter_lock);
3119
3120 rc = efx_ef10_filter_push(efx, spec,
3121 &table->entry[filter_idx].handle,
3122 false);
3123 if (rc)
3124 failed = true;
3125
3126 spin_lock_bh(&efx->filter_lock);
3127 if (rc) {
3128 kfree(spec);
3129 efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
3130 } else {
3131 table->entry[filter_idx].spec &=
3132 ~EFX_EF10_FILTER_FLAG_BUSY;
3133 }
3134 }
3135
3136 spin_unlock_bh(&efx->filter_lock);
3137
3138 if (failed)
3139 netif_err(efx, hw, efx->net_dev,
3140 "unable to restore all filters\n");
3141 else
3142 nic_data->must_restore_filters = false;
3143}
3144
3145static void efx_ef10_filter_table_remove(struct efx_nic *efx)
3146{
3147 struct efx_ef10_filter_table *table = efx->filter_state;
3148 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
3149 struct efx_filter_spec *spec;
3150 unsigned int filter_idx;
3151 int rc;
3152
3153 for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
3154 spec = efx_ef10_filter_entry_spec(table, filter_idx);
3155 if (!spec)
3156 continue;
3157
3158 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
3159 efx_ef10_filter_is_exclusive(spec) ?
3160 MC_CMD_FILTER_OP_IN_OP_REMOVE :
3161 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
3162 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
3163 table->entry[filter_idx].handle);
3164 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
3165 NULL, 0, NULL);
Ben Hutchings48ce5632013-11-01 16:42:44 +00003166 if (rc)
3167 netdev_WARN(efx->net_dev,
3168 "filter_idx=%#x handle=%#llx\n",
3169 filter_idx,
3170 table->entry[filter_idx].handle);
Ben Hutchings8127d662013-08-29 19:19:29 +01003171 kfree(spec);
3172 }
3173
3174 vfree(table->entry);
3175 kfree(table);
3176}
3177
3178static void efx_ef10_filter_sync_rx_mode(struct efx_nic *efx)
3179{
3180 struct efx_ef10_filter_table *table = efx->filter_state;
3181 struct net_device *net_dev = efx->net_dev;
3182 struct efx_filter_spec spec;
3183 bool remove_failed = false;
3184 struct netdev_hw_addr *uc;
3185 struct netdev_hw_addr *mc;
3186 unsigned int filter_idx;
3187 int i, n, rc;
3188
3189 if (!efx_dev_registered(efx))
3190 return;
3191
3192 /* Mark old filters that may need to be removed */
3193 spin_lock_bh(&efx->filter_lock);
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003194 n = table->dev_uc_count < 0 ? 1 : table->dev_uc_count;
Ben Hutchings8127d662013-08-29 19:19:29 +01003195 for (i = 0; i < n; i++) {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003196 filter_idx = table->dev_uc_list[i].id % HUNT_FILTER_TBL_ROWS;
3197 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_AUTO_OLD;
Ben Hutchings8127d662013-08-29 19:19:29 +01003198 }
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003199 n = table->dev_mc_count < 0 ? 1 : table->dev_mc_count;
Ben Hutchings8127d662013-08-29 19:19:29 +01003200 for (i = 0; i < n; i++) {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003201 filter_idx = table->dev_mc_list[i].id % HUNT_FILTER_TBL_ROWS;
3202 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_AUTO_OLD;
Ben Hutchings8127d662013-08-29 19:19:29 +01003203 }
3204 spin_unlock_bh(&efx->filter_lock);
3205
3206 /* Copy/convert the address lists; add the primary station
3207 * address and broadcast address
3208 */
3209 netif_addr_lock_bh(net_dev);
3210 if (net_dev->flags & IFF_PROMISC ||
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003211 netdev_uc_count(net_dev) >= EFX_EF10_FILTER_DEV_UC_MAX) {
3212 table->dev_uc_count = -1;
Ben Hutchings8127d662013-08-29 19:19:29 +01003213 } else {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003214 table->dev_uc_count = 1 + netdev_uc_count(net_dev);
Edward Creecd84ff42014-03-07 18:27:41 +00003215 ether_addr_copy(table->dev_uc_list[0].addr, net_dev->dev_addr);
Ben Hutchings8127d662013-08-29 19:19:29 +01003216 i = 1;
3217 netdev_for_each_uc_addr(uc, net_dev) {
Edward Creecd84ff42014-03-07 18:27:41 +00003218 ether_addr_copy(table->dev_uc_list[i].addr, uc->addr);
Ben Hutchings8127d662013-08-29 19:19:29 +01003219 i++;
3220 }
3221 }
3222 if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003223 netdev_mc_count(net_dev) >= EFX_EF10_FILTER_DEV_MC_MAX) {
3224 table->dev_mc_count = -1;
Ben Hutchings8127d662013-08-29 19:19:29 +01003225 } else {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003226 table->dev_mc_count = 1 + netdev_mc_count(net_dev);
3227 eth_broadcast_addr(table->dev_mc_list[0].addr);
Ben Hutchings8127d662013-08-29 19:19:29 +01003228 i = 1;
3229 netdev_for_each_mc_addr(mc, net_dev) {
Edward Creecd84ff42014-03-07 18:27:41 +00003230 ether_addr_copy(table->dev_mc_list[i].addr, mc->addr);
Ben Hutchings8127d662013-08-29 19:19:29 +01003231 i++;
3232 }
3233 }
3234 netif_addr_unlock_bh(net_dev);
3235
3236 /* Insert/renew unicast filters */
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003237 if (table->dev_uc_count >= 0) {
3238 for (i = 0; i < table->dev_uc_count; i++) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003239 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
3240 EFX_FILTER_FLAG_RX_RSS,
Ben Hutchings8127d662013-08-29 19:19:29 +01003241 0);
3242 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003243 table->dev_uc_list[i].addr);
Ben Hutchings8127d662013-08-29 19:19:29 +01003244 rc = efx_ef10_filter_insert(efx, &spec, true);
3245 if (rc < 0) {
3246 /* Fall back to unicast-promisc */
3247 while (i--)
3248 efx_ef10_filter_remove_safe(
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003249 efx, EFX_FILTER_PRI_AUTO,
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003250 table->dev_uc_list[i].id);
3251 table->dev_uc_count = -1;
Ben Hutchings8127d662013-08-29 19:19:29 +01003252 break;
3253 }
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003254 table->dev_uc_list[i].id = rc;
Ben Hutchings8127d662013-08-29 19:19:29 +01003255 }
3256 }
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003257 if (table->dev_uc_count < 0) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003258 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
3259 EFX_FILTER_FLAG_RX_RSS,
Ben Hutchings8127d662013-08-29 19:19:29 +01003260 0);
3261 efx_filter_set_uc_def(&spec);
3262 rc = efx_ef10_filter_insert(efx, &spec, true);
3263 if (rc < 0) {
3264 WARN_ON(1);
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003265 table->dev_uc_count = 0;
Ben Hutchings8127d662013-08-29 19:19:29 +01003266 } else {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003267 table->dev_uc_list[0].id = rc;
Ben Hutchings8127d662013-08-29 19:19:29 +01003268 }
3269 }
3270
3271 /* Insert/renew multicast filters */
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003272 if (table->dev_mc_count >= 0) {
3273 for (i = 0; i < table->dev_mc_count; i++) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003274 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
3275 EFX_FILTER_FLAG_RX_RSS,
Ben Hutchings8127d662013-08-29 19:19:29 +01003276 0);
3277 efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003278 table->dev_mc_list[i].addr);
Ben Hutchings8127d662013-08-29 19:19:29 +01003279 rc = efx_ef10_filter_insert(efx, &spec, true);
3280 if (rc < 0) {
3281 /* Fall back to multicast-promisc */
3282 while (i--)
3283 efx_ef10_filter_remove_safe(
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003284 efx, EFX_FILTER_PRI_AUTO,
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003285 table->dev_mc_list[i].id);
3286 table->dev_mc_count = -1;
Ben Hutchings8127d662013-08-29 19:19:29 +01003287 break;
3288 }
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003289 table->dev_mc_list[i].id = rc;
Ben Hutchings8127d662013-08-29 19:19:29 +01003290 }
3291 }
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003292 if (table->dev_mc_count < 0) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003293 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
3294 EFX_FILTER_FLAG_RX_RSS,
Ben Hutchings8127d662013-08-29 19:19:29 +01003295 0);
3296 efx_filter_set_mc_def(&spec);
3297 rc = efx_ef10_filter_insert(efx, &spec, true);
3298 if (rc < 0) {
3299 WARN_ON(1);
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003300 table->dev_mc_count = 0;
Ben Hutchings8127d662013-08-29 19:19:29 +01003301 } else {
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003302 table->dev_mc_list[0].id = rc;
Ben Hutchings8127d662013-08-29 19:19:29 +01003303 }
3304 }
3305
3306 /* Remove filters that weren't renewed. Since nothing else
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003307 * changes the AUTO_OLD flag or removes these filters, we
Ben Hutchings8127d662013-08-29 19:19:29 +01003308 * don't need to hold the filter_lock while scanning for
3309 * these filters.
3310 */
3311 for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
3312 if (ACCESS_ONCE(table->entry[i].spec) &
Ben Hutchingsb59e6ef2013-11-21 19:02:22 +00003313 EFX_EF10_FILTER_FLAG_AUTO_OLD) {
Ben Hutchings7665d1a2013-11-21 19:02:18 +00003314 if (efx_ef10_filter_remove_internal(
Ben Hutchingsfbd79122013-11-21 19:15:03 +00003315 efx, 1U << EFX_FILTER_PRI_AUTO,
3316 i, true) < 0)
Ben Hutchings8127d662013-08-29 19:19:29 +01003317 remove_failed = true;
3318 }
3319 }
3320 WARN_ON(remove_failed);
3321}
3322
3323static int efx_ef10_mac_reconfigure(struct efx_nic *efx)
3324{
3325 efx_ef10_filter_sync_rx_mode(efx);
3326
3327 return efx_mcdi_set_mac(efx);
3328}
3329
Jon Cooper74cd60a2013-09-16 14:18:51 +01003330static int efx_ef10_start_bist(struct efx_nic *efx, u32 bist_type)
3331{
3332 MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
3333
3334 MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_type);
3335 return efx_mcdi_rpc(efx, MC_CMD_START_BIST, inbuf, sizeof(inbuf),
3336 NULL, 0, NULL);
3337}
3338
3339/* MC BISTs follow a different poll mechanism to phy BISTs.
3340 * The BIST is done in the poll handler on the MC, and the MCDI command
3341 * will block until the BIST is done.
3342 */
3343static int efx_ef10_poll_bist(struct efx_nic *efx)
3344{
3345 int rc;
3346 MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_LEN);
3347 size_t outlen;
3348 u32 result;
3349
3350 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
3351 outbuf, sizeof(outbuf), &outlen);
3352 if (rc != 0)
3353 return rc;
3354
3355 if (outlen < MC_CMD_POLL_BIST_OUT_LEN)
3356 return -EIO;
3357
3358 result = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
3359 switch (result) {
3360 case MC_CMD_POLL_BIST_PASSED:
3361 netif_dbg(efx, hw, efx->net_dev, "BIST passed.\n");
3362 return 0;
3363 case MC_CMD_POLL_BIST_TIMEOUT:
3364 netif_err(efx, hw, efx->net_dev, "BIST timed out\n");
3365 return -EIO;
3366 case MC_CMD_POLL_BIST_FAILED:
3367 netif_err(efx, hw, efx->net_dev, "BIST failed.\n");
3368 return -EIO;
3369 default:
3370 netif_err(efx, hw, efx->net_dev,
3371 "BIST returned unknown result %u", result);
3372 return -EIO;
3373 }
3374}
3375
3376static int efx_ef10_run_bist(struct efx_nic *efx, u32 bist_type)
3377{
3378 int rc;
3379
3380 netif_dbg(efx, drv, efx->net_dev, "starting BIST type %u\n", bist_type);
3381
3382 rc = efx_ef10_start_bist(efx, bist_type);
3383 if (rc != 0)
3384 return rc;
3385
3386 return efx_ef10_poll_bist(efx);
3387}
3388
3389static int
3390efx_ef10_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
3391{
3392 int rc, rc2;
3393
3394 efx_reset_down(efx, RESET_TYPE_WORLD);
3395
3396 rc = efx_mcdi_rpc(efx, MC_CMD_ENABLE_OFFLINE_BIST,
3397 NULL, 0, NULL, 0, NULL);
3398 if (rc != 0)
3399 goto out;
3400
3401 tests->memory = efx_ef10_run_bist(efx, MC_CMD_MC_MEM_BIST) ? -1 : 1;
3402 tests->registers = efx_ef10_run_bist(efx, MC_CMD_REG_BIST) ? -1 : 1;
3403
3404 rc = efx_mcdi_reset(efx, RESET_TYPE_WORLD);
3405
3406out:
3407 rc2 = efx_reset_up(efx, RESET_TYPE_WORLD, rc == 0);
3408 return rc ? rc : rc2;
3409}
3410
Ben Hutchings8127d662013-08-29 19:19:29 +01003411#ifdef CONFIG_SFC_MTD
3412
3413struct efx_ef10_nvram_type_info {
3414 u16 type, type_mask;
3415 u8 port;
3416 const char *name;
3417};
3418
3419static const struct efx_ef10_nvram_type_info efx_ef10_nvram_types[] = {
3420 { NVRAM_PARTITION_TYPE_MC_FIRMWARE, 0, 0, "sfc_mcfw" },
3421 { NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 0, 0, "sfc_mcfw_backup" },
3422 { NVRAM_PARTITION_TYPE_EXPANSION_ROM, 0, 0, "sfc_exp_rom" },
3423 { NVRAM_PARTITION_TYPE_STATIC_CONFIG, 0, 0, "sfc_static_cfg" },
3424 { NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 0, 0, "sfc_dynamic_cfg" },
3425 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 0, 0, "sfc_exp_rom_cfg" },
3426 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 0, 1, "sfc_exp_rom_cfg" },
3427 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 0, 2, "sfc_exp_rom_cfg" },
3428 { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 0, 3, "sfc_exp_rom_cfg" },
Ben Hutchingsa84f3bf92013-10-09 14:14:41 +01003429 { NVRAM_PARTITION_TYPE_LICENSE, 0, 0, "sfc_license" },
Ben Hutchings8127d662013-08-29 19:19:29 +01003430 { NVRAM_PARTITION_TYPE_PHY_MIN, 0xff, 0, "sfc_phy_fw" },
3431};
3432
3433static int efx_ef10_mtd_probe_partition(struct efx_nic *efx,
3434 struct efx_mcdi_mtd_partition *part,
3435 unsigned int type)
3436{
3437 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_METADATA_IN_LEN);
3438 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_METADATA_OUT_LENMAX);
3439 const struct efx_ef10_nvram_type_info *info;
3440 size_t size, erase_size, outlen;
3441 bool protected;
3442 int rc;
3443
3444 for (info = efx_ef10_nvram_types; ; info++) {
3445 if (info ==
3446 efx_ef10_nvram_types + ARRAY_SIZE(efx_ef10_nvram_types))
3447 return -ENODEV;
3448 if ((type & ~info->type_mask) == info->type)
3449 break;
3450 }
3451 if (info->port != efx_port_num(efx))
3452 return -ENODEV;
3453
3454 rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
3455 if (rc)
3456 return rc;
3457 if (protected)
3458 return -ENODEV; /* hide it */
3459
3460 part->nvram_type = type;
3461
3462 MCDI_SET_DWORD(inbuf, NVRAM_METADATA_IN_TYPE, type);
3463 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_METADATA, inbuf, sizeof(inbuf),
3464 outbuf, sizeof(outbuf), &outlen);
3465 if (rc)
3466 return rc;
3467 if (outlen < MC_CMD_NVRAM_METADATA_OUT_LENMIN)
3468 return -EIO;
3469 if (MCDI_DWORD(outbuf, NVRAM_METADATA_OUT_FLAGS) &
3470 (1 << MC_CMD_NVRAM_METADATA_OUT_SUBTYPE_VALID_LBN))
3471 part->fw_subtype = MCDI_DWORD(outbuf,
3472 NVRAM_METADATA_OUT_SUBTYPE);
3473
3474 part->common.dev_type_name = "EF10 NVRAM manager";
3475 part->common.type_name = info->name;
3476
3477 part->common.mtd.type = MTD_NORFLASH;
3478 part->common.mtd.flags = MTD_CAP_NORFLASH;
3479 part->common.mtd.size = size;
3480 part->common.mtd.erasesize = erase_size;
3481
3482 return 0;
3483}
3484
3485static int efx_ef10_mtd_probe(struct efx_nic *efx)
3486{
3487 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_PARTITIONS_OUT_LENMAX);
3488 struct efx_mcdi_mtd_partition *parts;
3489 size_t outlen, n_parts_total, i, n_parts;
3490 unsigned int type;
3491 int rc;
3492
3493 ASSERT_RTNL();
3494
3495 BUILD_BUG_ON(MC_CMD_NVRAM_PARTITIONS_IN_LEN != 0);
3496 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_PARTITIONS, NULL, 0,
3497 outbuf, sizeof(outbuf), &outlen);
3498 if (rc)
3499 return rc;
3500 if (outlen < MC_CMD_NVRAM_PARTITIONS_OUT_LENMIN)
3501 return -EIO;
3502
3503 n_parts_total = MCDI_DWORD(outbuf, NVRAM_PARTITIONS_OUT_NUM_PARTITIONS);
3504 if (n_parts_total >
3505 MCDI_VAR_ARRAY_LEN(outlen, NVRAM_PARTITIONS_OUT_TYPE_ID))
3506 return -EIO;
3507
3508 parts = kcalloc(n_parts_total, sizeof(*parts), GFP_KERNEL);
3509 if (!parts)
3510 return -ENOMEM;
3511
3512 n_parts = 0;
3513 for (i = 0; i < n_parts_total; i++) {
3514 type = MCDI_ARRAY_DWORD(outbuf, NVRAM_PARTITIONS_OUT_TYPE_ID,
3515 i);
3516 rc = efx_ef10_mtd_probe_partition(efx, &parts[n_parts], type);
3517 if (rc == 0)
3518 n_parts++;
3519 else if (rc != -ENODEV)
3520 goto fail;
3521 }
3522
3523 rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
3524fail:
3525 if (rc)
3526 kfree(parts);
3527 return rc;
3528}
3529
3530#endif /* CONFIG_SFC_MTD */
3531
3532static void efx_ef10_ptp_write_host_time(struct efx_nic *efx, u32 host_time)
3533{
3534 _efx_writed(efx, cpu_to_le32(host_time), ER_DZ_MC_DB_LWRD);
3535}
3536
Jon Cooperbd9a2652013-11-18 12:54:41 +00003537static int efx_ef10_rx_enable_timestamping(struct efx_channel *channel,
3538 bool temp)
3539{
3540 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_TIME_EVENT_SUBSCRIBE_LEN);
3541 int rc;
3542
3543 if (channel->sync_events_state == SYNC_EVENTS_REQUESTED ||
3544 channel->sync_events_state == SYNC_EVENTS_VALID ||
3545 (temp && channel->sync_events_state == SYNC_EVENTS_DISABLED))
3546 return 0;
3547 channel->sync_events_state = SYNC_EVENTS_REQUESTED;
3548
3549 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_TIME_EVENT_SUBSCRIBE);
3550 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
3551 MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_SUBSCRIBE_QUEUE,
3552 channel->channel);
3553
3554 rc = efx_mcdi_rpc(channel->efx, MC_CMD_PTP,
3555 inbuf, sizeof(inbuf), NULL, 0, NULL);
3556
3557 if (rc != 0)
3558 channel->sync_events_state = temp ? SYNC_EVENTS_QUIESCENT :
3559 SYNC_EVENTS_DISABLED;
3560
3561 return rc;
3562}
3563
3564static int efx_ef10_rx_disable_timestamping(struct efx_channel *channel,
3565 bool temp)
3566{
3567 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_TIME_EVENT_UNSUBSCRIBE_LEN);
3568 int rc;
3569
3570 if (channel->sync_events_state == SYNC_EVENTS_DISABLED ||
3571 (temp && channel->sync_events_state == SYNC_EVENTS_QUIESCENT))
3572 return 0;
3573 if (channel->sync_events_state == SYNC_EVENTS_QUIESCENT) {
3574 channel->sync_events_state = SYNC_EVENTS_DISABLED;
3575 return 0;
3576 }
3577 channel->sync_events_state = temp ? SYNC_EVENTS_QUIESCENT :
3578 SYNC_EVENTS_DISABLED;
3579
3580 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_TIME_EVENT_UNSUBSCRIBE);
3581 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
3582 MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_UNSUBSCRIBE_CONTROL,
3583 MC_CMD_PTP_IN_TIME_EVENT_UNSUBSCRIBE_SINGLE);
3584 MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_UNSUBSCRIBE_QUEUE,
3585 channel->channel);
3586
3587 rc = efx_mcdi_rpc(channel->efx, MC_CMD_PTP,
3588 inbuf, sizeof(inbuf), NULL, 0, NULL);
3589
3590 return rc;
3591}
3592
3593static int efx_ef10_ptp_set_ts_sync_events(struct efx_nic *efx, bool en,
3594 bool temp)
3595{
3596 int (*set)(struct efx_channel *channel, bool temp);
3597 struct efx_channel *channel;
3598
3599 set = en ?
3600 efx_ef10_rx_enable_timestamping :
3601 efx_ef10_rx_disable_timestamping;
3602
3603 efx_for_each_channel(channel, efx) {
3604 int rc = set(channel, temp);
3605 if (en && rc != 0) {
3606 efx_ef10_ptp_set_ts_sync_events(efx, false, temp);
3607 return rc;
3608 }
3609 }
3610
3611 return 0;
3612}
3613
3614static int efx_ef10_ptp_set_ts_config(struct efx_nic *efx,
3615 struct hwtstamp_config *init)
3616{
3617 int rc;
3618
3619 switch (init->rx_filter) {
3620 case HWTSTAMP_FILTER_NONE:
3621 efx_ef10_ptp_set_ts_sync_events(efx, false, false);
3622 /* if TX timestamping is still requested then leave PTP on */
3623 return efx_ptp_change_mode(efx,
3624 init->tx_type != HWTSTAMP_TX_OFF, 0);
3625 case HWTSTAMP_FILTER_ALL:
3626 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3627 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3628 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3629 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3630 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3631 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3632 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3633 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3634 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3635 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3636 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3637 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3638 init->rx_filter = HWTSTAMP_FILTER_ALL;
3639 rc = efx_ptp_change_mode(efx, true, 0);
3640 if (!rc)
3641 rc = efx_ef10_ptp_set_ts_sync_events(efx, true, false);
3642 if (rc)
3643 efx_ptp_change_mode(efx, false, 0);
3644 return rc;
3645 default:
3646 return -ERANGE;
3647 }
3648}
3649
Ben Hutchings8127d662013-08-29 19:19:29 +01003650const struct efx_nic_type efx_hunt_a0_nic_type = {
3651 .mem_map_size = efx_ef10_mem_map_size,
3652 .probe = efx_ef10_probe,
3653 .remove = efx_ef10_remove,
3654 .dimension_resources = efx_ef10_dimension_resources,
3655 .init = efx_ef10_init_nic,
3656 .fini = efx_port_dummy_op_void,
3657 .map_reset_reason = efx_mcdi_map_reset_reason,
3658 .map_reset_flags = efx_ef10_map_reset_flags,
Jon Cooper3e336262014-01-17 19:48:06 +00003659 .reset = efx_ef10_reset,
Ben Hutchings8127d662013-08-29 19:19:29 +01003660 .probe_port = efx_mcdi_port_probe,
3661 .remove_port = efx_mcdi_port_remove,
3662 .fini_dmaq = efx_ef10_fini_dmaq,
Edward Creee2835462014-04-16 19:27:48 +01003663 .prepare_flr = efx_ef10_prepare_flr,
3664 .finish_flr = efx_port_dummy_op_void,
Ben Hutchings8127d662013-08-29 19:19:29 +01003665 .describe_stats = efx_ef10_describe_stats,
3666 .update_stats = efx_ef10_update_stats,
3667 .start_stats = efx_mcdi_mac_start_stats,
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01003668 .pull_stats = efx_mcdi_mac_pull_stats,
Ben Hutchings8127d662013-08-29 19:19:29 +01003669 .stop_stats = efx_mcdi_mac_stop_stats,
3670 .set_id_led = efx_mcdi_set_id_led,
3671 .push_irq_moderation = efx_ef10_push_irq_moderation,
3672 .reconfigure_mac = efx_ef10_mac_reconfigure,
3673 .check_mac_fault = efx_mcdi_mac_check_fault,
3674 .reconfigure_port = efx_mcdi_port_reconfigure,
3675 .get_wol = efx_ef10_get_wol,
3676 .set_wol = efx_ef10_set_wol,
3677 .resume_wol = efx_port_dummy_op_void,
Jon Cooper74cd60a2013-09-16 14:18:51 +01003678 .test_chip = efx_ef10_test_chip,
Ben Hutchings8127d662013-08-29 19:19:29 +01003679 .test_nvram = efx_mcdi_nvram_test_all,
3680 .mcdi_request = efx_ef10_mcdi_request,
3681 .mcdi_poll_response = efx_ef10_mcdi_poll_response,
3682 .mcdi_read_response = efx_ef10_mcdi_read_response,
3683 .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
3684 .irq_enable_master = efx_port_dummy_op_void,
3685 .irq_test_generate = efx_ef10_irq_test_generate,
3686 .irq_disable_non_ev = efx_port_dummy_op_void,
3687 .irq_handle_msi = efx_ef10_msi_interrupt,
3688 .irq_handle_legacy = efx_ef10_legacy_interrupt,
3689 .tx_probe = efx_ef10_tx_probe,
3690 .tx_init = efx_ef10_tx_init,
3691 .tx_remove = efx_ef10_tx_remove,
3692 .tx_write = efx_ef10_tx_write,
Andrew Rybchenkod43050c2013-11-14 09:00:27 +04003693 .rx_push_rss_config = efx_ef10_rx_push_rss_config,
Ben Hutchings8127d662013-08-29 19:19:29 +01003694 .rx_probe = efx_ef10_rx_probe,
3695 .rx_init = efx_ef10_rx_init,
3696 .rx_remove = efx_ef10_rx_remove,
3697 .rx_write = efx_ef10_rx_write,
3698 .rx_defer_refill = efx_ef10_rx_defer_refill,
3699 .ev_probe = efx_ef10_ev_probe,
3700 .ev_init = efx_ef10_ev_init,
3701 .ev_fini = efx_ef10_ev_fini,
3702 .ev_remove = efx_ef10_ev_remove,
3703 .ev_process = efx_ef10_ev_process,
3704 .ev_read_ack = efx_ef10_ev_read_ack,
3705 .ev_test_generate = efx_ef10_ev_test_generate,
3706 .filter_table_probe = efx_ef10_filter_table_probe,
3707 .filter_table_restore = efx_ef10_filter_table_restore,
3708 .filter_table_remove = efx_ef10_filter_table_remove,
3709 .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
3710 .filter_insert = efx_ef10_filter_insert,
3711 .filter_remove_safe = efx_ef10_filter_remove_safe,
3712 .filter_get_safe = efx_ef10_filter_get_safe,
3713 .filter_clear_rx = efx_ef10_filter_clear_rx,
3714 .filter_count_rx_used = efx_ef10_filter_count_rx_used,
3715 .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
3716 .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
3717#ifdef CONFIG_RFS_ACCEL
3718 .filter_rfs_insert = efx_ef10_filter_rfs_insert,
3719 .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
3720#endif
3721#ifdef CONFIG_SFC_MTD
3722 .mtd_probe = efx_ef10_mtd_probe,
3723 .mtd_rename = efx_mcdi_mtd_rename,
3724 .mtd_read = efx_mcdi_mtd_read,
3725 .mtd_erase = efx_mcdi_mtd_erase,
3726 .mtd_write = efx_mcdi_mtd_write,
3727 .mtd_sync = efx_mcdi_mtd_sync,
3728#endif
3729 .ptp_write_host_time = efx_ef10_ptp_write_host_time,
Jon Cooperbd9a2652013-11-18 12:54:41 +00003730 .ptp_set_ts_sync_events = efx_ef10_ptp_set_ts_sync_events,
3731 .ptp_set_ts_config = efx_ef10_ptp_set_ts_config,
Shradha Shah7fa8d542015-05-06 00:55:13 +01003732#ifdef CONFIG_SFC_SRIOV
Shradha Shah834e23d2015-05-06 00:55:58 +01003733 .sriov_configure = efx_ef10_sriov_configure,
Shradha Shahd98a4ff2014-11-05 12:16:46 +00003734 .sriov_init = efx_ef10_sriov_init,
3735 .sriov_fini = efx_ef10_sriov_fini,
3736 .sriov_mac_address_changed = efx_ef10_sriov_mac_address_changed,
3737 .sriov_wanted = efx_ef10_sriov_wanted,
3738 .sriov_reset = efx_ef10_sriov_reset,
Shradha Shah7fa8d542015-05-06 00:55:13 +01003739 .sriov_flr = efx_ef10_sriov_flr,
3740 .sriov_set_vf_mac = efx_ef10_sriov_set_vf_mac,
3741 .sriov_set_vf_vlan = efx_ef10_sriov_set_vf_vlan,
3742 .sriov_set_vf_spoofchk = efx_ef10_sriov_set_vf_spoofchk,
3743 .sriov_get_vf_config = efx_ef10_sriov_get_vf_config,
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01003744 .vswitching_probe = efx_ef10_vswitching_probe,
3745 .vswitching_restore = efx_ef10_vswitching_restore,
3746 .vswitching_remove = efx_ef10_vswitching_remove,
Shradha Shah7fa8d542015-05-06 00:55:13 +01003747#endif
Ben Hutchings8127d662013-08-29 19:19:29 +01003748
3749 .revision = EFX_REV_HUNT_A0,
3750 .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
3751 .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
3752 .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
Jon Cooperbd9a2652013-11-18 12:54:41 +00003753 .rx_ts_offset = ES_DZ_RX_PREFIX_TSTAMP_OFST,
Ben Hutchings8127d662013-08-29 19:19:29 +01003754 .can_rx_scatter = true,
3755 .always_rx_scatter = true,
3756 .max_interrupt_mode = EFX_INT_MODE_MSIX,
3757 .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
3758 .offload_features = (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3759 NETIF_F_RXHASH | NETIF_F_NTUPLE),
3760 .mcdi_max_ver = 2,
3761 .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
Jon Cooperbd9a2652013-11-18 12:54:41 +00003762 .hwtstamp_filters = 1 << HWTSTAMP_FILTER_NONE |
3763 1 << HWTSTAMP_FILTER_ALL,
Ben Hutchings8127d662013-08-29 19:19:29 +01003764};