xref: /openbmc/linux/drivers/net/ethernet/sfc/nic.c (revision b0d1fe9b)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2874aeea5SJeff Kirsher /****************************************************************************
3f7a6d2c4SBen Hutchings  * Driver for Solarflare network controllers and boards
4874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
5f7a6d2c4SBen Hutchings  * Copyright 2006-2013 Solarflare Communications Inc.
6874aeea5SJeff Kirsher  */
7874aeea5SJeff Kirsher 
8874aeea5SJeff Kirsher #include <linux/bitops.h>
9874aeea5SJeff Kirsher #include <linux/delay.h>
10874aeea5SJeff Kirsher #include <linux/interrupt.h>
11874aeea5SJeff Kirsher #include <linux/pci.h>
12874aeea5SJeff Kirsher #include <linux/module.h>
13874aeea5SJeff Kirsher #include <linux/seq_file.h>
141899c111SBen Hutchings #include <linux/cpu_rmap.h>
15874aeea5SJeff Kirsher #include "net_driver.h"
16874aeea5SJeff Kirsher #include "bitfield.h"
17874aeea5SJeff Kirsher #include "efx.h"
18874aeea5SJeff Kirsher #include "nic.h"
19137b7922SBen Hutchings #include "ef10_regs.h"
20874aeea5SJeff Kirsher #include "io.h"
21874aeea5SJeff Kirsher #include "workarounds.h"
22d3142c19SEdward Cree #include "mcdi_pcol.h"
23874aeea5SJeff Kirsher 
24874aeea5SJeff Kirsher /**************************************************************************
25874aeea5SJeff Kirsher  *
26874aeea5SJeff Kirsher  * Generic buffer handling
27f7251a9cSBen Hutchings  * These buffers are used for interrupt status, MAC stats, etc.
28874aeea5SJeff Kirsher  *
29874aeea5SJeff Kirsher  **************************************************************************/
30874aeea5SJeff Kirsher 
efx_nic_alloc_buffer(struct efx_nic * efx,struct efx_buffer * buffer,unsigned int len,gfp_t gfp_flags)31874aeea5SJeff Kirsher int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
320d19a540SBen Hutchings 			 unsigned int len, gfp_t gfp_flags)
33874aeea5SJeff Kirsher {
34750afb08SLuis Chamberlain 	buffer->addr = dma_alloc_coherent(&efx->pci_dev->dev, len,
35ede23fa8SJoe Perches 					  &buffer->dma_addr, gfp_flags);
36874aeea5SJeff Kirsher 	if (!buffer->addr)
37874aeea5SJeff Kirsher 		return -ENOMEM;
38874aeea5SJeff Kirsher 	buffer->len = len;
39874aeea5SJeff Kirsher 	return 0;
40874aeea5SJeff Kirsher }
41874aeea5SJeff Kirsher 
efx_nic_free_buffer(struct efx_nic * efx,struct efx_buffer * buffer)42874aeea5SJeff Kirsher void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
43874aeea5SJeff Kirsher {
44874aeea5SJeff Kirsher 	if (buffer->addr) {
450e33d870SBen Hutchings 		dma_free_coherent(&efx->pci_dev->dev, buffer->len,
46874aeea5SJeff Kirsher 				  buffer->addr, buffer->dma_addr);
47874aeea5SJeff Kirsher 		buffer->addr = NULL;
48874aeea5SJeff Kirsher 	}
49874aeea5SJeff Kirsher }
50874aeea5SJeff Kirsher 
51874aeea5SJeff Kirsher /* Check whether an event is present in the eventq at the current
52874aeea5SJeff Kirsher  * read pointer.  Only useful for self-test.
53874aeea5SJeff Kirsher  */
efx_nic_event_present(struct efx_channel * channel)54874aeea5SJeff Kirsher bool efx_nic_event_present(struct efx_channel *channel)
55874aeea5SJeff Kirsher {
56874aeea5SJeff Kirsher 	return efx_event_present(efx_event(channel, channel->eventq_read_ptr));
57874aeea5SJeff Kirsher }
58874aeea5SJeff Kirsher 
efx_nic_event_test_start(struct efx_channel * channel)59eee6f6a9SBen Hutchings void efx_nic_event_test_start(struct efx_channel *channel)
60874aeea5SJeff Kirsher {
61dd40781eSBen Hutchings 	channel->event_test_cpu = -1;
62eee6f6a9SBen Hutchings 	smp_wmb();
6386094f7fSBen Hutchings 	channel->efx->type->ev_test_generate(channel);
64874aeea5SJeff Kirsher }
65874aeea5SJeff Kirsher 
efx_nic_irq_test_start(struct efx_nic * efx)66942e298eSJon Cooper int efx_nic_irq_test_start(struct efx_nic *efx)
67874aeea5SJeff Kirsher {
68eee6f6a9SBen Hutchings 	efx->last_irq_cpu = -1;
69eee6f6a9SBen Hutchings 	smp_wmb();
70942e298eSJon Cooper 	return efx->type->irq_test_generate(efx);
71874aeea5SJeff Kirsher }
72874aeea5SJeff Kirsher 
73874aeea5SJeff Kirsher /* Hook interrupt handler(s)
74874aeea5SJeff Kirsher  * Try MSI and then legacy interrupts.
75874aeea5SJeff Kirsher  */
efx_nic_init_interrupt(struct efx_nic * efx)76874aeea5SJeff Kirsher int efx_nic_init_interrupt(struct efx_nic *efx)
77874aeea5SJeff Kirsher {
78874aeea5SJeff Kirsher 	struct efx_channel *channel;
791899c111SBen Hutchings 	unsigned int n_irqs;
80874aeea5SJeff Kirsher 	int rc;
81874aeea5SJeff Kirsher 
82874aeea5SJeff Kirsher 	if (!EFX_INT_MODE_USE_MSI(efx)) {
8386094f7fSBen Hutchings 		rc = request_irq(efx->legacy_irq,
8486094f7fSBen Hutchings 				 efx->type->irq_handle_legacy, IRQF_SHARED,
85874aeea5SJeff Kirsher 				 efx->name, efx);
86874aeea5SJeff Kirsher 		if (rc) {
87874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
88874aeea5SJeff Kirsher 				  "failed to hook legacy IRQ %d\n",
89874aeea5SJeff Kirsher 				  efx->pci_dev->irq);
90874aeea5SJeff Kirsher 			goto fail1;
91874aeea5SJeff Kirsher 		}
928f03eeb6SÍñigo Huguet 		efx->irqs_hooked = true;
93874aeea5SJeff Kirsher 		return 0;
94874aeea5SJeff Kirsher 	}
95874aeea5SJeff Kirsher 
961899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL
971899c111SBen Hutchings 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
981899c111SBen Hutchings 		efx->net_dev->rx_cpu_rmap =
991899c111SBen Hutchings 			alloc_irq_cpu_rmap(efx->n_rx_channels);
1001899c111SBen Hutchings 		if (!efx->net_dev->rx_cpu_rmap) {
1011899c111SBen Hutchings 			rc = -ENOMEM;
1021899c111SBen Hutchings 			goto fail1;
1031899c111SBen Hutchings 		}
1041899c111SBen Hutchings 	}
1051899c111SBen Hutchings #endif
1061899c111SBen Hutchings 
107874aeea5SJeff Kirsher 	/* Hook MSI or MSI-X interrupt */
1081899c111SBen Hutchings 	n_irqs = 0;
109874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
11086094f7fSBen Hutchings 		rc = request_irq(channel->irq, efx->type->irq_handle_msi,
111874aeea5SJeff Kirsher 				 IRQF_PROBE_SHARED, /* Not shared */
112d8291187SBen Hutchings 				 efx->msi_context[channel->channel].name,
113d8291187SBen Hutchings 				 &efx->msi_context[channel->channel]);
114874aeea5SJeff Kirsher 		if (rc) {
115874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
116874aeea5SJeff Kirsher 				  "failed to hook IRQ %d\n", channel->irq);
117874aeea5SJeff Kirsher 			goto fail2;
118874aeea5SJeff Kirsher 		}
1191899c111SBen Hutchings 		++n_irqs;
1201899c111SBen Hutchings 
1211899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1221899c111SBen Hutchings 		if (efx->interrupt_mode == EFX_INT_MODE_MSIX &&
1231899c111SBen Hutchings 		    channel->channel < efx->n_rx_channels) {
1241899c111SBen Hutchings 			rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1251899c111SBen Hutchings 					      channel->irq);
1261899c111SBen Hutchings 			if (rc)
1271899c111SBen Hutchings 				goto fail2;
1281899c111SBen Hutchings 		}
1291899c111SBen Hutchings #endif
130874aeea5SJeff Kirsher 	}
131874aeea5SJeff Kirsher 
132e6a43910SEdward Cree 	efx->irqs_hooked = true;
133874aeea5SJeff Kirsher 	return 0;
134874aeea5SJeff Kirsher 
135874aeea5SJeff Kirsher  fail2:
1361899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1371899c111SBen Hutchings 	free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1381899c111SBen Hutchings 	efx->net_dev->rx_cpu_rmap = NULL;
1391899c111SBen Hutchings #endif
1401899c111SBen Hutchings 	efx_for_each_channel(channel, efx) {
1411899c111SBen Hutchings 		if (n_irqs-- == 0)
1421899c111SBen Hutchings 			break;
143d8291187SBen Hutchings 		free_irq(channel->irq, &efx->msi_context[channel->channel]);
1441899c111SBen Hutchings 	}
145874aeea5SJeff Kirsher  fail1:
146874aeea5SJeff Kirsher 	return rc;
147874aeea5SJeff Kirsher }
148874aeea5SJeff Kirsher 
efx_nic_fini_interrupt(struct efx_nic * efx)149874aeea5SJeff Kirsher void efx_nic_fini_interrupt(struct efx_nic *efx)
150874aeea5SJeff Kirsher {
151874aeea5SJeff Kirsher 	struct efx_channel *channel;
152874aeea5SJeff Kirsher 
1531899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1541899c111SBen Hutchings 	free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1551899c111SBen Hutchings 	efx->net_dev->rx_cpu_rmap = NULL;
1561899c111SBen Hutchings #endif
1571899c111SBen Hutchings 
158e6a43910SEdward Cree 	if (!efx->irqs_hooked)
159e6a43910SEdward Cree 		return;
1601c363900SNikolay Aleksandrov 	if (EFX_INT_MODE_USE_MSI(efx)) {
161874aeea5SJeff Kirsher 		/* Disable MSI/MSI-X interrupts */
1621899c111SBen Hutchings 		efx_for_each_channel(channel, efx)
1631c363900SNikolay Aleksandrov 			free_irq(channel->irq,
1641c363900SNikolay Aleksandrov 				 &efx->msi_context[channel->channel]);
1651c363900SNikolay Aleksandrov 	} else {
166874aeea5SJeff Kirsher 		/* Disable legacy interrupt */
167874aeea5SJeff Kirsher 		free_irq(efx->legacy_irq, efx);
168874aeea5SJeff Kirsher 	}
169e6a43910SEdward Cree 	efx->irqs_hooked = false;
1701c363900SNikolay Aleksandrov }
171874aeea5SJeff Kirsher 
172874aeea5SJeff Kirsher /* Register dump */
173874aeea5SJeff Kirsher 
174137b7922SBen Hutchings #define REGISTER_REVISION_ED	4
175137b7922SBen Hutchings #define REGISTER_REVISION_EZ	4	/* latest EF10 revision */
176874aeea5SJeff Kirsher 
177874aeea5SJeff Kirsher struct efx_nic_reg {
178874aeea5SJeff Kirsher 	u32 offset:24;
179137b7922SBen Hutchings 	u32 min_revision:3, max_revision:3;
180874aeea5SJeff Kirsher };
181874aeea5SJeff Kirsher 
182137b7922SBen Hutchings #define REGISTER(name, arch, min_rev, max_rev) {			\
183137b7922SBen Hutchings 	arch ## R_ ## min_rev ## max_rev ## _ ## name,			\
184137b7922SBen Hutchings 	REGISTER_REVISION_ ## arch ## min_rev,				\
185137b7922SBen Hutchings 	REGISTER_REVISION_ ## arch ## max_rev				\
186874aeea5SJeff Kirsher }
187137b7922SBen Hutchings #define REGISTER_DZ(name) REGISTER(name, E, D, Z)
188874aeea5SJeff Kirsher 
189874aeea5SJeff Kirsher static const struct efx_nic_reg efx_nic_regs[] = {
190874aeea5SJeff Kirsher 	/* XX_PRBS_CTL, XX_PRBS_CHK and XX_PRBS_ERR are not used */
191874aeea5SJeff Kirsher 	/* XX_CORE_STAT is partly RC */
192137b7922SBen Hutchings 	REGISTER_DZ(BIU_HW_REV_ID),
193137b7922SBen Hutchings 	REGISTER_DZ(MC_DB_LWRD),
194137b7922SBen Hutchings 	REGISTER_DZ(MC_DB_HWRD),
195874aeea5SJeff Kirsher };
196874aeea5SJeff Kirsher 
197874aeea5SJeff Kirsher struct efx_nic_reg_table {
198874aeea5SJeff Kirsher 	u32 offset:24;
199137b7922SBen Hutchings 	u32 min_revision:3, max_revision:3;
200874aeea5SJeff Kirsher 	u32 step:6, rows:21;
201874aeea5SJeff Kirsher };
202874aeea5SJeff Kirsher 
203137b7922SBen Hutchings #define REGISTER_TABLE_DIMENSIONS(_, offset, arch, min_rev, max_rev, step, rows) { \
204874aeea5SJeff Kirsher 	offset,								\
205137b7922SBen Hutchings 	REGISTER_REVISION_ ## arch ## min_rev,				\
206137b7922SBen Hutchings 	REGISTER_REVISION_ ## arch ## max_rev,				\
207874aeea5SJeff Kirsher 	step, rows							\
208874aeea5SJeff Kirsher }
209137b7922SBen Hutchings #define REGISTER_TABLE(name, arch, min_rev, max_rev)			\
210874aeea5SJeff Kirsher 	REGISTER_TABLE_DIMENSIONS(					\
211137b7922SBen Hutchings 		name, arch ## R_ ## min_rev ## max_rev ## _ ## name,	\
212137b7922SBen Hutchings 		arch, min_rev, max_rev,					\
213137b7922SBen Hutchings 		arch ## R_ ## min_rev ## max_rev ## _ ## name ## _STEP,	\
214137b7922SBen Hutchings 		arch ## R_ ## min_rev ## max_rev ## _ ## name ## _ROWS)
215137b7922SBen Hutchings #define REGISTER_TABLE_DZ(name) REGISTER_TABLE(name, E, D, Z)
216874aeea5SJeff Kirsher 
217874aeea5SJeff Kirsher static const struct efx_nic_reg_table efx_nic_reg_tables[] = {
218137b7922SBen Hutchings 	REGISTER_TABLE_DZ(BIU_MC_SFT_STATUS),
219874aeea5SJeff Kirsher };
220874aeea5SJeff Kirsher 
efx_nic_get_regs_len(struct efx_nic * efx)221874aeea5SJeff Kirsher size_t efx_nic_get_regs_len(struct efx_nic *efx)
222874aeea5SJeff Kirsher {
223874aeea5SJeff Kirsher 	const struct efx_nic_reg *reg;
224874aeea5SJeff Kirsher 	const struct efx_nic_reg_table *table;
225874aeea5SJeff Kirsher 	size_t len = 0;
226874aeea5SJeff Kirsher 
227874aeea5SJeff Kirsher 	for (reg = efx_nic_regs;
228874aeea5SJeff Kirsher 	     reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs);
229874aeea5SJeff Kirsher 	     reg++)
230874aeea5SJeff Kirsher 		if (efx->type->revision >= reg->min_revision &&
231874aeea5SJeff Kirsher 		    efx->type->revision <= reg->max_revision)
232874aeea5SJeff Kirsher 			len += sizeof(efx_oword_t);
233874aeea5SJeff Kirsher 
234874aeea5SJeff Kirsher 	for (table = efx_nic_reg_tables;
235874aeea5SJeff Kirsher 	     table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables);
236874aeea5SJeff Kirsher 	     table++)
237874aeea5SJeff Kirsher 		if (efx->type->revision >= table->min_revision &&
238874aeea5SJeff Kirsher 		    efx->type->revision <= table->max_revision)
239874aeea5SJeff Kirsher 			len += table->rows * min_t(size_t, table->step, 16);
240874aeea5SJeff Kirsher 
241874aeea5SJeff Kirsher 	return len;
242874aeea5SJeff Kirsher }
243874aeea5SJeff Kirsher 
efx_nic_get_regs(struct efx_nic * efx,void * buf)244874aeea5SJeff Kirsher void efx_nic_get_regs(struct efx_nic *efx, void *buf)
245874aeea5SJeff Kirsher {
246874aeea5SJeff Kirsher 	const struct efx_nic_reg *reg;
247874aeea5SJeff Kirsher 	const struct efx_nic_reg_table *table;
248874aeea5SJeff Kirsher 
249874aeea5SJeff Kirsher 	for (reg = efx_nic_regs;
250874aeea5SJeff Kirsher 	     reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs);
251874aeea5SJeff Kirsher 	     reg++) {
252874aeea5SJeff Kirsher 		if (efx->type->revision >= reg->min_revision &&
253874aeea5SJeff Kirsher 		    efx->type->revision <= reg->max_revision) {
254874aeea5SJeff Kirsher 			efx_reado(efx, (efx_oword_t *)buf, reg->offset);
255874aeea5SJeff Kirsher 			buf += sizeof(efx_oword_t);
256874aeea5SJeff Kirsher 		}
257874aeea5SJeff Kirsher 	}
258874aeea5SJeff Kirsher 
259874aeea5SJeff Kirsher 	for (table = efx_nic_reg_tables;
260874aeea5SJeff Kirsher 	     table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables);
261874aeea5SJeff Kirsher 	     table++) {
262874aeea5SJeff Kirsher 		size_t size, i;
263874aeea5SJeff Kirsher 
264874aeea5SJeff Kirsher 		if (!(efx->type->revision >= table->min_revision &&
265874aeea5SJeff Kirsher 		      efx->type->revision <= table->max_revision))
266874aeea5SJeff Kirsher 			continue;
267874aeea5SJeff Kirsher 
268874aeea5SJeff Kirsher 		size = min_t(size_t, table->step, 16);
269874aeea5SJeff Kirsher 
270874aeea5SJeff Kirsher 		for (i = 0; i < table->rows; i++) {
271874aeea5SJeff Kirsher 			switch (table->step) {
272778cdaf6SBen Hutchings 			case 4: /* 32-bit SRAM */
273778cdaf6SBen Hutchings 				efx_readd(efx, buf, table->offset + 4 * i);
274874aeea5SJeff Kirsher 				break;
275778cdaf6SBen Hutchings 			case 16: /* 128-bit-readable register */
276874aeea5SJeff Kirsher 				efx_reado_table(efx, buf, table->offset, i);
277874aeea5SJeff Kirsher 				break;
278874aeea5SJeff Kirsher 			case 32: /* 128-bit register, interleaved */
279874aeea5SJeff Kirsher 				efx_reado_table(efx, buf, table->offset, 2 * i);
280874aeea5SJeff Kirsher 				break;
281874aeea5SJeff Kirsher 			default:
282874aeea5SJeff Kirsher 				WARN_ON(1);
283874aeea5SJeff Kirsher 				return;
284874aeea5SJeff Kirsher 			}
285874aeea5SJeff Kirsher 			buf += size;
286874aeea5SJeff Kirsher 		}
287874aeea5SJeff Kirsher 	}
288874aeea5SJeff Kirsher }
289cd0ecc9aSBen Hutchings 
290cd0ecc9aSBen Hutchings /**
291cd0ecc9aSBen Hutchings  * efx_nic_describe_stats - Describe supported statistics for ethtool
292cd0ecc9aSBen Hutchings  * @desc: Array of &struct efx_hw_stat_desc describing the statistics
293cd0ecc9aSBen Hutchings  * @count: Length of the @desc array
294cd0ecc9aSBen Hutchings  * @mask: Bitmask of which elements of @desc are enabled
295cd0ecc9aSBen Hutchings  * @names: Buffer to copy names to, or %NULL.  The names are copied
296cd0ecc9aSBen Hutchings  *	starting at intervals of %ETH_GSTRING_LEN bytes.
297cd0ecc9aSBen Hutchings  *
298cd0ecc9aSBen Hutchings  * Returns the number of visible statistics, i.e. the number of set
299cd0ecc9aSBen Hutchings  * bits in the first @count bits of @mask for which a name is defined.
300cd0ecc9aSBen Hutchings  */
efx_nic_describe_stats(const struct efx_hw_stat_desc * desc,size_t count,const unsigned long * mask,u8 * names)301cd0ecc9aSBen Hutchings size_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
302cd0ecc9aSBen Hutchings 			      const unsigned long *mask, u8 *names)
303cd0ecc9aSBen Hutchings {
304cd0ecc9aSBen Hutchings 	size_t visible = 0;
305cd0ecc9aSBen Hutchings 	size_t index;
306cd0ecc9aSBen Hutchings 
307cd0ecc9aSBen Hutchings 	for_each_set_bit(index, mask, count) {
308cd0ecc9aSBen Hutchings 		if (desc[index].name) {
309cd0ecc9aSBen Hutchings 			if (names) {
310*f029c781SWolfram Sang 				strscpy(names, desc[index].name,
311cd0ecc9aSBen Hutchings 					ETH_GSTRING_LEN);
312cd0ecc9aSBen Hutchings 				names += ETH_GSTRING_LEN;
313cd0ecc9aSBen Hutchings 			}
314cd0ecc9aSBen Hutchings 			++visible;
315cd0ecc9aSBen Hutchings 		}
316cd0ecc9aSBen Hutchings 	}
317cd0ecc9aSBen Hutchings 
318cd0ecc9aSBen Hutchings 	return visible;
319cd0ecc9aSBen Hutchings }
320cd0ecc9aSBen Hutchings 
321cd0ecc9aSBen Hutchings /**
322d3142c19SEdward Cree  * efx_nic_copy_stats - Copy stats from the DMA buffer in to an
323d3142c19SEdward Cree  *	intermediate buffer. This is used to get a consistent
324d3142c19SEdward Cree  *	set of stats while the DMA buffer can be written at any time
325d3142c19SEdward Cree  *	by the NIC.
326d3142c19SEdward Cree  * @efx: The associated NIC.
327d3142c19SEdward Cree  * @dest: Destination buffer. Must be the same size as the DMA buffer.
328d3142c19SEdward Cree  */
efx_nic_copy_stats(struct efx_nic * efx,__le64 * dest)329d3142c19SEdward Cree int efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest)
330d3142c19SEdward Cree {
331d3142c19SEdward Cree 	__le64 *dma_stats = efx->stats_buffer.addr;
332d3142c19SEdward Cree 	__le64 generation_start, generation_end;
333d3142c19SEdward Cree 	int rc = 0, retry;
334d3142c19SEdward Cree 
335d3142c19SEdward Cree 	if (!dest)
336d3142c19SEdward Cree 		return 0;
337d3142c19SEdward Cree 
338d3142c19SEdward Cree 	if (!dma_stats)
339d3142c19SEdward Cree 		goto return_zeroes;
340d3142c19SEdward Cree 
341d3142c19SEdward Cree 	/* If we're unlucky enough to read statistics during the DMA, wait
342d3142c19SEdward Cree 	 * up to 10ms for it to finish (typically takes <500us)
343d3142c19SEdward Cree 	 */
344d3142c19SEdward Cree 	for (retry = 0; retry < 100; ++retry) {
345d3142c19SEdward Cree 		generation_end = dma_stats[efx->num_mac_stats - 1];
346d3142c19SEdward Cree 		if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
347d3142c19SEdward Cree 			goto return_zeroes;
348d3142c19SEdward Cree 		rmb();
349d3142c19SEdward Cree 		memcpy(dest, dma_stats, efx->num_mac_stats * sizeof(__le64));
350d3142c19SEdward Cree 		rmb();
351d3142c19SEdward Cree 		generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
352d3142c19SEdward Cree 		if (generation_end == generation_start)
353d3142c19SEdward Cree 			return 0; /* return good data */
354d3142c19SEdward Cree 		udelay(100);
355d3142c19SEdward Cree 	}
356d3142c19SEdward Cree 
357d3142c19SEdward Cree 	rc = -EIO;
358d3142c19SEdward Cree 
359d3142c19SEdward Cree return_zeroes:
360d3142c19SEdward Cree 	memset(dest, 0, efx->num_mac_stats * sizeof(u64));
361d3142c19SEdward Cree 	return rc;
362d3142c19SEdward Cree }
363d3142c19SEdward Cree 
364d3142c19SEdward Cree /**
365cd0ecc9aSBen Hutchings  * efx_nic_update_stats - Convert statistics DMA buffer to array of u64
366cd0ecc9aSBen Hutchings  * @desc: Array of &struct efx_hw_stat_desc describing the DMA buffer
367cd0ecc9aSBen Hutchings  *	layout.  DMA widths of 0, 16, 32 and 64 are supported; where
368cd0ecc9aSBen Hutchings  *	the width is specified as 0 the corresponding element of
369cd0ecc9aSBen Hutchings  *	@stats is not updated.
370cd0ecc9aSBen Hutchings  * @count: Length of the @desc array
371cd0ecc9aSBen Hutchings  * @mask: Bitmask of which elements of @desc are enabled
372cd0ecc9aSBen Hutchings  * @stats: Buffer to update with the converted statistics.  The length
37387648cc9SEdward Cree  *	of this array must be at least @count.
374cd0ecc9aSBen Hutchings  * @dma_buf: DMA buffer containing hardware statistics
375cd0ecc9aSBen Hutchings  * @accumulate: If set, the converted values will be added rather than
376cd0ecc9aSBen Hutchings  *	directly stored to the corresponding elements of @stats
377cd0ecc9aSBen Hutchings  */
efx_nic_update_stats(const struct efx_hw_stat_desc * desc,size_t count,const unsigned long * mask,u64 * stats,const void * dma_buf,bool accumulate)378cd0ecc9aSBen Hutchings void efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
379cd0ecc9aSBen Hutchings 			  const unsigned long *mask,
380cd0ecc9aSBen Hutchings 			  u64 *stats, const void *dma_buf, bool accumulate)
381cd0ecc9aSBen Hutchings {
382cd0ecc9aSBen Hutchings 	size_t index;
383cd0ecc9aSBen Hutchings 
384cd0ecc9aSBen Hutchings 	for_each_set_bit(index, mask, count) {
385cd0ecc9aSBen Hutchings 		if (desc[index].dma_width) {
386cd0ecc9aSBen Hutchings 			const void *addr = dma_buf + desc[index].offset;
387cd0ecc9aSBen Hutchings 			u64 val;
388cd0ecc9aSBen Hutchings 
389cd0ecc9aSBen Hutchings 			switch (desc[index].dma_width) {
390cd0ecc9aSBen Hutchings 			case 16:
391cd0ecc9aSBen Hutchings 				val = le16_to_cpup((__le16 *)addr);
392cd0ecc9aSBen Hutchings 				break;
393cd0ecc9aSBen Hutchings 			case 32:
394cd0ecc9aSBen Hutchings 				val = le32_to_cpup((__le32 *)addr);
395cd0ecc9aSBen Hutchings 				break;
396cd0ecc9aSBen Hutchings 			case 64:
397cd0ecc9aSBen Hutchings 				val = le64_to_cpup((__le64 *)addr);
398cd0ecc9aSBen Hutchings 				break;
399cd0ecc9aSBen Hutchings 			default:
400cd0ecc9aSBen Hutchings 				WARN_ON(1);
401cd0ecc9aSBen Hutchings 				val = 0;
402cd0ecc9aSBen Hutchings 				break;
403cd0ecc9aSBen Hutchings 			}
404cd0ecc9aSBen Hutchings 
405cd0ecc9aSBen Hutchings 			if (accumulate)
40687648cc9SEdward Cree 				stats[index] += val;
407cd0ecc9aSBen Hutchings 			else
40887648cc9SEdward Cree 				stats[index] = val;
409cd0ecc9aSBen Hutchings 		}
410cd0ecc9aSBen Hutchings 	}
411cd0ecc9aSBen Hutchings }
412f8f3b5aeSJon Cooper 
efx_nic_fix_nodesc_drop_stat(struct efx_nic * efx,u64 * rx_nodesc_drops)413f8f3b5aeSJon Cooper void efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *rx_nodesc_drops)
414f8f3b5aeSJon Cooper {
415f8f3b5aeSJon Cooper 	/* if down, or this is the first update after coming up */
416f8f3b5aeSJon Cooper 	if (!(efx->net_dev->flags & IFF_UP) || !efx->rx_nodesc_drops_prev_state)
417f8f3b5aeSJon Cooper 		efx->rx_nodesc_drops_while_down +=
418f8f3b5aeSJon Cooper 			*rx_nodesc_drops - efx->rx_nodesc_drops_total;
419f8f3b5aeSJon Cooper 	efx->rx_nodesc_drops_total = *rx_nodesc_drops;
420f8f3b5aeSJon Cooper 	efx->rx_nodesc_drops_prev_state = !!(efx->net_dev->flags & IFF_UP);
421f8f3b5aeSJon Cooper 	*rx_nodesc_drops -= efx->rx_nodesc_drops_while_down;
422f8f3b5aeSJon Cooper }
423