1/* Intel Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2014 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11 * more details.
12 *
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
15 *
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19 */
20
21#include <linux/vmalloc.h>
22
23#include "fm10k.h"
24
25struct fm10k_stats {
26	char stat_string[ETH_GSTRING_LEN];
27	int sizeof_stat;
28	int stat_offset;
29};
30
31#define FM10K_NETDEV_STAT(_net_stat) { \
32	.stat_string = #_net_stat, \
33	.sizeof_stat = FIELD_SIZEOF(struct net_device_stats, _net_stat), \
34	.stat_offset = offsetof(struct net_device_stats, _net_stat) \
35}
36
37static const struct fm10k_stats fm10k_gstrings_net_stats[] = {
38	FM10K_NETDEV_STAT(tx_packets),
39	FM10K_NETDEV_STAT(tx_bytes),
40	FM10K_NETDEV_STAT(tx_errors),
41	FM10K_NETDEV_STAT(rx_packets),
42	FM10K_NETDEV_STAT(rx_bytes),
43	FM10K_NETDEV_STAT(rx_errors),
44	FM10K_NETDEV_STAT(rx_dropped),
45
46	/* detailed Rx errors */
47	FM10K_NETDEV_STAT(rx_length_errors),
48	FM10K_NETDEV_STAT(rx_crc_errors),
49	FM10K_NETDEV_STAT(rx_fifo_errors),
50};
51
52#define FM10K_NETDEV_STATS_LEN	ARRAY_SIZE(fm10k_gstrings_net_stats)
53
54#define FM10K_STAT(_name, _stat) { \
55	.stat_string = _name, \
56	.sizeof_stat = FIELD_SIZEOF(struct fm10k_intfc, _stat), \
57	.stat_offset = offsetof(struct fm10k_intfc, _stat) \
58}
59
60static const struct fm10k_stats fm10k_gstrings_stats[] = {
61	FM10K_STAT("tx_restart_queue", restart_queue),
62	FM10K_STAT("tx_busy", tx_busy),
63	FM10K_STAT("tx_csum_errors", tx_csum_errors),
64	FM10K_STAT("rx_alloc_failed", alloc_failed),
65	FM10K_STAT("rx_csum_errors", rx_csum_errors),
66	FM10K_STAT("rx_errors", rx_errors),
67
68	FM10K_STAT("tx_packets_nic", tx_packets_nic),
69	FM10K_STAT("tx_bytes_nic", tx_bytes_nic),
70	FM10K_STAT("rx_packets_nic", rx_packets_nic),
71	FM10K_STAT("rx_bytes_nic", rx_bytes_nic),
72	FM10K_STAT("rx_drops_nic", rx_drops_nic),
73	FM10K_STAT("rx_overrun_pf", rx_overrun_pf),
74	FM10K_STAT("rx_overrun_vf", rx_overrun_vf),
75
76	FM10K_STAT("timeout", stats.timeout.count),
77	FM10K_STAT("ur", stats.ur.count),
78	FM10K_STAT("ca", stats.ca.count),
79	FM10K_STAT("um", stats.um.count),
80	FM10K_STAT("xec", stats.xec.count),
81	FM10K_STAT("vlan_drop", stats.vlan_drop.count),
82	FM10K_STAT("loopback_drop", stats.loopback_drop.count),
83	FM10K_STAT("nodesc_drop", stats.nodesc_drop.count),
84
85	FM10K_STAT("swapi_status", hw.swapi.status),
86	FM10K_STAT("mac_rules_used", hw.swapi.mac.used),
87	FM10K_STAT("mac_rules_avail", hw.swapi.mac.avail),
88
89	FM10K_STAT("mbx_tx_busy", hw.mbx.tx_busy),
90	FM10K_STAT("mbx_tx_dropped", hw.mbx.tx_dropped),
91	FM10K_STAT("mbx_tx_messages", hw.mbx.tx_messages),
92	FM10K_STAT("mbx_tx_dwords", hw.mbx.tx_dwords),
93	FM10K_STAT("mbx_rx_messages", hw.mbx.rx_messages),
94	FM10K_STAT("mbx_rx_dwords", hw.mbx.rx_dwords),
95	FM10K_STAT("mbx_rx_parse_err", hw.mbx.rx_parse_err),
96
97	FM10K_STAT("tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
98};
99
100#define FM10K_GLOBAL_STATS_LEN ARRAY_SIZE(fm10k_gstrings_stats)
101
102#define FM10K_QUEUE_STATS_LEN \
103	  (MAX_QUEUES * 2 * (sizeof(struct fm10k_queue_stats) / sizeof(u64)))
104
105#define FM10K_STATS_LEN (FM10K_GLOBAL_STATS_LEN + \
106			 FM10K_NETDEV_STATS_LEN + \
107			 FM10K_QUEUE_STATS_LEN)
108
109static const char fm10k_gstrings_test[][ETH_GSTRING_LEN] = {
110	"Mailbox test (on/offline)"
111};
112
113#define FM10K_TEST_LEN (sizeof(fm10k_gstrings_test) / ETH_GSTRING_LEN)
114
115enum fm10k_self_test_types {
116	FM10K_TEST_MBX,
117	FM10K_TEST_MAX = FM10K_TEST_LEN
118};
119
120static void fm10k_get_strings(struct net_device *dev, u32 stringset,
121			      u8 *data)
122{
123	char *p = (char *)data;
124	int i;
125
126	switch (stringset) {
127	case ETH_SS_TEST:
128		memcpy(data, *fm10k_gstrings_test,
129		       FM10K_TEST_LEN * ETH_GSTRING_LEN);
130		break;
131	case ETH_SS_STATS:
132		for (i = 0; i < FM10K_NETDEV_STATS_LEN; i++) {
133			memcpy(p, fm10k_gstrings_net_stats[i].stat_string,
134			       ETH_GSTRING_LEN);
135			p += ETH_GSTRING_LEN;
136		}
137		for (i = 0; i < FM10K_GLOBAL_STATS_LEN; i++) {
138			memcpy(p, fm10k_gstrings_stats[i].stat_string,
139			       ETH_GSTRING_LEN);
140			p += ETH_GSTRING_LEN;
141		}
142
143		for (i = 0; i < MAX_QUEUES; i++) {
144			sprintf(p, "tx_queue_%u_packets", i);
145			p += ETH_GSTRING_LEN;
146			sprintf(p, "tx_queue_%u_bytes", i);
147			p += ETH_GSTRING_LEN;
148			sprintf(p, "rx_queue_%u_packets", i);
149			p += ETH_GSTRING_LEN;
150			sprintf(p, "rx_queue_%u_bytes", i);
151			p += ETH_GSTRING_LEN;
152		}
153		break;
154	}
155}
156
157static int fm10k_get_sset_count(struct net_device *dev, int sset)
158{
159	switch (sset) {
160	case ETH_SS_TEST:
161		return FM10K_TEST_LEN;
162	case ETH_SS_STATS:
163		return FM10K_STATS_LEN;
164	default:
165		return -EOPNOTSUPP;
166	}
167}
168
169static void fm10k_get_ethtool_stats(struct net_device *netdev,
170				    struct ethtool_stats *stats, u64 *data)
171{
172	const int stat_count = sizeof(struct fm10k_queue_stats) / sizeof(u64);
173	struct fm10k_intfc *interface = netdev_priv(netdev);
174	struct net_device_stats *net_stats = &netdev->stats;
175	char *p;
176	int i, j;
177
178	fm10k_update_stats(interface);
179
180	for (i = 0; i < FM10K_NETDEV_STATS_LEN; i++) {
181		p = (char *)net_stats + fm10k_gstrings_net_stats[i].stat_offset;
182		*(data++) = (fm10k_gstrings_net_stats[i].sizeof_stat ==
183			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
184	}
185
186	for (i = 0; i < FM10K_GLOBAL_STATS_LEN; i++) {
187		p = (char *)interface + fm10k_gstrings_stats[i].stat_offset;
188		*(data++) = (fm10k_gstrings_stats[i].sizeof_stat ==
189			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
190	}
191
192	for (i = 0; i < MAX_QUEUES; i++) {
193		struct fm10k_ring *ring;
194		u64 *queue_stat;
195
196		ring = interface->tx_ring[i];
197		if (ring)
198			queue_stat = (u64 *)&ring->stats;
199		for (j = 0; j < stat_count; j++)
200			*(data++) = ring ? queue_stat[j] : 0;
201
202		ring = interface->rx_ring[i];
203		if (ring)
204			queue_stat = (u64 *)&ring->stats;
205		for (j = 0; j < stat_count; j++)
206			*(data++) = ring ? queue_stat[j] : 0;
207	}
208}
209
210/* If function below adds more registers this define needs to be updated */
211#define FM10K_REGS_LEN_Q 29
212
213static void fm10k_get_reg_q(struct fm10k_hw *hw, u32 *buff, int i)
214{
215	int idx = 0;
216
217	buff[idx++] = fm10k_read_reg(hw, FM10K_RDBAL(i));
218	buff[idx++] = fm10k_read_reg(hw, FM10K_RDBAH(i));
219	buff[idx++] = fm10k_read_reg(hw, FM10K_RDLEN(i));
220	buff[idx++] = fm10k_read_reg(hw, FM10K_TPH_RXCTRL(i));
221	buff[idx++] = fm10k_read_reg(hw, FM10K_RDH(i));
222	buff[idx++] = fm10k_read_reg(hw, FM10K_RDT(i));
223	buff[idx++] = fm10k_read_reg(hw, FM10K_RXQCTL(i));
224	buff[idx++] = fm10k_read_reg(hw, FM10K_RXDCTL(i));
225	buff[idx++] = fm10k_read_reg(hw, FM10K_RXINT(i));
226	buff[idx++] = fm10k_read_reg(hw, FM10K_SRRCTL(i));
227	buff[idx++] = fm10k_read_reg(hw, FM10K_QPRC(i));
228	buff[idx++] = fm10k_read_reg(hw, FM10K_QPRDC(i));
229	buff[idx++] = fm10k_read_reg(hw, FM10K_QBRC_L(i));
230	buff[idx++] = fm10k_read_reg(hw, FM10K_QBRC_H(i));
231	buff[idx++] = fm10k_read_reg(hw, FM10K_TDBAL(i));
232	buff[idx++] = fm10k_read_reg(hw, FM10K_TDBAH(i));
233	buff[idx++] = fm10k_read_reg(hw, FM10K_TDLEN(i));
234	buff[idx++] = fm10k_read_reg(hw, FM10K_TPH_TXCTRL(i));
235	buff[idx++] = fm10k_read_reg(hw, FM10K_TDH(i));
236	buff[idx++] = fm10k_read_reg(hw, FM10K_TDT(i));
237	buff[idx++] = fm10k_read_reg(hw, FM10K_TXDCTL(i));
238	buff[idx++] = fm10k_read_reg(hw, FM10K_TXQCTL(i));
239	buff[idx++] = fm10k_read_reg(hw, FM10K_TXINT(i));
240	buff[idx++] = fm10k_read_reg(hw, FM10K_QPTC(i));
241	buff[idx++] = fm10k_read_reg(hw, FM10K_QBTC_L(i));
242	buff[idx++] = fm10k_read_reg(hw, FM10K_QBTC_H(i));
243	buff[idx++] = fm10k_read_reg(hw, FM10K_TQDLOC(i));
244	buff[idx++] = fm10k_read_reg(hw, FM10K_TX_SGLORT(i));
245	buff[idx++] = fm10k_read_reg(hw, FM10K_PFVTCTL(i));
246
247	BUG_ON(idx != FM10K_REGS_LEN_Q);
248}
249
250/* If function above adds more registers this define needs to be updated */
251#define FM10K_REGS_LEN_VSI 43
252
253static void fm10k_get_reg_vsi(struct fm10k_hw *hw, u32 *buff, int i)
254{
255	int idx = 0, j;
256
257	buff[idx++] = fm10k_read_reg(hw, FM10K_MRQC(i));
258	for (j = 0; j < 10; j++)
259		buff[idx++] = fm10k_read_reg(hw, FM10K_RSSRK(i, j));
260	for (j = 0; j < 32; j++)
261		buff[idx++] = fm10k_read_reg(hw, FM10K_RETA(i, j));
262
263	BUG_ON(idx != FM10K_REGS_LEN_VSI);
264}
265
266static void fm10k_get_regs(struct net_device *netdev,
267			   struct ethtool_regs *regs, void *p)
268{
269	struct fm10k_intfc *interface = netdev_priv(netdev);
270	struct fm10k_hw *hw = &interface->hw;
271	u32 *buff = p;
272	u16 i;
273
274	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
275
276	switch (hw->mac.type) {
277	case fm10k_mac_pf:
278		/* General PF Registers */
279		*(buff++) = fm10k_read_reg(hw, FM10K_CTRL);
280		*(buff++) = fm10k_read_reg(hw, FM10K_CTRL_EXT);
281		*(buff++) = fm10k_read_reg(hw, FM10K_GCR);
282		*(buff++) = fm10k_read_reg(hw, FM10K_GCR_EXT);
283
284		for (i = 0; i < 8; i++) {
285			*(buff++) = fm10k_read_reg(hw, FM10K_DGLORTMAP(i));
286			*(buff++) = fm10k_read_reg(hw, FM10K_DGLORTDEC(i));
287		}
288
289		for (i = 0; i < 65; i++) {
290			fm10k_get_reg_vsi(hw, buff, i);
291			buff += FM10K_REGS_LEN_VSI;
292		}
293
294		*(buff++) = fm10k_read_reg(hw, FM10K_DMA_CTRL);
295		*(buff++) = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
296
297		for (i = 0; i < FM10K_MAX_QUEUES_PF; i++) {
298			fm10k_get_reg_q(hw, buff, i);
299			buff += FM10K_REGS_LEN_Q;
300		}
301
302		*(buff++) = fm10k_read_reg(hw, FM10K_TPH_CTRL);
303
304		for (i = 0; i < 8; i++)
305			*(buff++) = fm10k_read_reg(hw, FM10K_INT_MAP(i));
306
307		/* Interrupt Throttling Registers */
308		for (i = 0; i < 130; i++)
309			*(buff++) = fm10k_read_reg(hw, FM10K_ITR(i));
310
311		break;
312	case fm10k_mac_vf:
313		/* General VF registers */
314		*(buff++) = fm10k_read_reg(hw, FM10K_VFCTRL);
315		*(buff++) = fm10k_read_reg(hw, FM10K_VFINT_MAP);
316		*(buff++) = fm10k_read_reg(hw, FM10K_VFSYSTIME);
317
318		/* Interrupt Throttling Registers */
319		for (i = 0; i < 8; i++)
320			*(buff++) = fm10k_read_reg(hw, FM10K_VFITR(i));
321
322		fm10k_get_reg_vsi(hw, buff, 0);
323		buff += FM10K_REGS_LEN_VSI;
324
325		for (i = 0; i < FM10K_MAX_QUEUES_POOL; i++) {
326			if (i < hw->mac.max_queues)
327				fm10k_get_reg_q(hw, buff, i);
328			else
329				memset(buff, 0, sizeof(u32) * FM10K_REGS_LEN_Q);
330			buff += FM10K_REGS_LEN_Q;
331		}
332
333		break;
334	default:
335		return;
336	}
337}
338
339/* If function above adds more registers these define need to be updated */
340#define FM10K_REGS_LEN_PF \
341(162 + (65 * FM10K_REGS_LEN_VSI) + (FM10K_MAX_QUEUES_PF * FM10K_REGS_LEN_Q))
342#define FM10K_REGS_LEN_VF \
343(11 + FM10K_REGS_LEN_VSI + (FM10K_MAX_QUEUES_POOL * FM10K_REGS_LEN_Q))
344
345static int fm10k_get_regs_len(struct net_device *netdev)
346{
347	struct fm10k_intfc *interface = netdev_priv(netdev);
348	struct fm10k_hw *hw = &interface->hw;
349
350	switch (hw->mac.type) {
351	case fm10k_mac_pf:
352		return FM10K_REGS_LEN_PF * sizeof(u32);
353	case fm10k_mac_vf:
354		return FM10K_REGS_LEN_VF * sizeof(u32);
355	default:
356		return 0;
357	}
358}
359
360static void fm10k_get_drvinfo(struct net_device *dev,
361			      struct ethtool_drvinfo *info)
362{
363	struct fm10k_intfc *interface = netdev_priv(dev);
364
365	strncpy(info->driver, fm10k_driver_name,
366		sizeof(info->driver) - 1);
367	strncpy(info->version, fm10k_driver_version,
368		sizeof(info->version) - 1);
369	strncpy(info->bus_info, pci_name(interface->pdev),
370		sizeof(info->bus_info) - 1);
371
372	info->n_stats = FM10K_STATS_LEN;
373
374	info->regdump_len = fm10k_get_regs_len(dev);
375}
376
377static void fm10k_get_pauseparam(struct net_device *dev,
378				 struct ethtool_pauseparam *pause)
379{
380	struct fm10k_intfc *interface = netdev_priv(dev);
381
382	/* record fixed values for autoneg and tx pause */
383	pause->autoneg = 0;
384	pause->tx_pause = 1;
385
386	pause->rx_pause = interface->rx_pause ? 1 : 0;
387}
388
389static int fm10k_set_pauseparam(struct net_device *dev,
390				struct ethtool_pauseparam *pause)
391{
392	struct fm10k_intfc *interface = netdev_priv(dev);
393	struct fm10k_hw *hw = &interface->hw;
394
395	if (pause->autoneg || !pause->tx_pause)
396		return -EINVAL;
397
398	/* we can only support pause on the PF to avoid head-of-line blocking */
399	if (hw->mac.type == fm10k_mac_pf)
400		interface->rx_pause = pause->rx_pause ? ~0 : 0;
401	else if (pause->rx_pause)
402		return -EINVAL;
403
404	if (netif_running(dev))
405		fm10k_update_rx_drop_en(interface);
406
407	return 0;
408}
409
410static u32 fm10k_get_msglevel(struct net_device *netdev)
411{
412	struct fm10k_intfc *interface = netdev_priv(netdev);
413
414	return interface->msg_enable;
415}
416
417static void fm10k_set_msglevel(struct net_device *netdev, u32 data)
418{
419	struct fm10k_intfc *interface = netdev_priv(netdev);
420
421	interface->msg_enable = data;
422}
423
424static void fm10k_get_ringparam(struct net_device *netdev,
425				struct ethtool_ringparam *ring)
426{
427	struct fm10k_intfc *interface = netdev_priv(netdev);
428
429	ring->rx_max_pending = FM10K_MAX_RXD;
430	ring->tx_max_pending = FM10K_MAX_TXD;
431	ring->rx_mini_max_pending = 0;
432	ring->rx_jumbo_max_pending = 0;
433	ring->rx_pending = interface->rx_ring_count;
434	ring->tx_pending = interface->tx_ring_count;
435	ring->rx_mini_pending = 0;
436	ring->rx_jumbo_pending = 0;
437}
438
439static int fm10k_set_ringparam(struct net_device *netdev,
440			       struct ethtool_ringparam *ring)
441{
442	struct fm10k_intfc *interface = netdev_priv(netdev);
443	struct fm10k_ring *temp_ring;
444	int i, err = 0;
445	u32 new_rx_count, new_tx_count;
446
447	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
448		return -EINVAL;
449
450	new_tx_count = clamp_t(u32, ring->tx_pending,
451			       FM10K_MIN_TXD, FM10K_MAX_TXD);
452	new_tx_count = ALIGN(new_tx_count, FM10K_REQ_TX_DESCRIPTOR_MULTIPLE);
453
454	new_rx_count = clamp_t(u32, ring->rx_pending,
455			       FM10K_MIN_RXD, FM10K_MAX_RXD);
456	new_rx_count = ALIGN(new_rx_count, FM10K_REQ_RX_DESCRIPTOR_MULTIPLE);
457
458	if ((new_tx_count == interface->tx_ring_count) &&
459	    (new_rx_count == interface->rx_ring_count)) {
460		/* nothing to do */
461		return 0;
462	}
463
464	while (test_and_set_bit(__FM10K_RESETTING, &interface->state))
465		usleep_range(1000, 2000);
466
467	if (!netif_running(interface->netdev)) {
468		for (i = 0; i < interface->num_tx_queues; i++)
469			interface->tx_ring[i]->count = new_tx_count;
470		for (i = 0; i < interface->num_rx_queues; i++)
471			interface->rx_ring[i]->count = new_rx_count;
472		interface->tx_ring_count = new_tx_count;
473		interface->rx_ring_count = new_rx_count;
474		goto clear_reset;
475	}
476
477	/* allocate temporary buffer to store rings in */
478	i = max_t(int, interface->num_tx_queues, interface->num_rx_queues);
479	temp_ring = vmalloc(i * sizeof(struct fm10k_ring));
480
481	if (!temp_ring) {
482		err = -ENOMEM;
483		goto clear_reset;
484	}
485
486	fm10k_down(interface);
487
488	/* Setup new Tx resources and free the old Tx resources in that order.
489	 * We can then assign the new resources to the rings via a memcpy.
490	 * The advantage to this approach is that we are guaranteed to still
491	 * have resources even in the case of an allocation failure.
492	 */
493	if (new_tx_count != interface->tx_ring_count) {
494		for (i = 0; i < interface->num_tx_queues; i++) {
495			memcpy(&temp_ring[i], interface->tx_ring[i],
496			       sizeof(struct fm10k_ring));
497
498			temp_ring[i].count = new_tx_count;
499			err = fm10k_setup_tx_resources(&temp_ring[i]);
500			if (err) {
501				while (i) {
502					i--;
503					fm10k_free_tx_resources(&temp_ring[i]);
504				}
505				goto err_setup;
506			}
507		}
508
509		for (i = 0; i < interface->num_tx_queues; i++) {
510			fm10k_free_tx_resources(interface->tx_ring[i]);
511
512			memcpy(interface->tx_ring[i], &temp_ring[i],
513			       sizeof(struct fm10k_ring));
514		}
515
516		interface->tx_ring_count = new_tx_count;
517	}
518
519	/* Repeat the process for the Rx rings if needed */
520	if (new_rx_count != interface->rx_ring_count) {
521		for (i = 0; i < interface->num_rx_queues; i++) {
522			memcpy(&temp_ring[i], interface->rx_ring[i],
523			       sizeof(struct fm10k_ring));
524
525			temp_ring[i].count = new_rx_count;
526			err = fm10k_setup_rx_resources(&temp_ring[i]);
527			if (err) {
528				while (i) {
529					i--;
530					fm10k_free_rx_resources(&temp_ring[i]);
531				}
532				goto err_setup;
533			}
534		}
535
536		for (i = 0; i < interface->num_rx_queues; i++) {
537			fm10k_free_rx_resources(interface->rx_ring[i]);
538
539			memcpy(interface->rx_ring[i], &temp_ring[i],
540			       sizeof(struct fm10k_ring));
541		}
542
543		interface->rx_ring_count = new_rx_count;
544	}
545
546err_setup:
547	fm10k_up(interface);
548	vfree(temp_ring);
549clear_reset:
550	clear_bit(__FM10K_RESETTING, &interface->state);
551	return err;
552}
553
554static int fm10k_get_coalesce(struct net_device *dev,
555			      struct ethtool_coalesce *ec)
556{
557	struct fm10k_intfc *interface = netdev_priv(dev);
558
559	ec->use_adaptive_tx_coalesce =
560		!!(interface->tx_itr & FM10K_ITR_ADAPTIVE);
561	ec->tx_coalesce_usecs = interface->tx_itr & ~FM10K_ITR_ADAPTIVE;
562
563	ec->use_adaptive_rx_coalesce =
564		!!(interface->rx_itr & FM10K_ITR_ADAPTIVE);
565	ec->rx_coalesce_usecs = interface->rx_itr & ~FM10K_ITR_ADAPTIVE;
566
567	return 0;
568}
569
570static int fm10k_set_coalesce(struct net_device *dev,
571			      struct ethtool_coalesce *ec)
572{
573	struct fm10k_intfc *interface = netdev_priv(dev);
574	struct fm10k_q_vector *qv;
575	u16 tx_itr, rx_itr;
576	int i;
577
578	/* verify limits */
579	if ((ec->rx_coalesce_usecs > FM10K_ITR_MAX) ||
580	    (ec->tx_coalesce_usecs > FM10K_ITR_MAX))
581		return -EINVAL;
582
583	/* record settings */
584	tx_itr = ec->tx_coalesce_usecs;
585	rx_itr = ec->rx_coalesce_usecs;
586
587	/* set initial values for adaptive ITR */
588	if (ec->use_adaptive_tx_coalesce)
589		tx_itr = FM10K_ITR_ADAPTIVE | FM10K_ITR_10K;
590
591	if (ec->use_adaptive_rx_coalesce)
592		rx_itr = FM10K_ITR_ADAPTIVE | FM10K_ITR_20K;
593
594	/* update interface */
595	interface->tx_itr = tx_itr;
596	interface->rx_itr = rx_itr;
597
598	/* update q_vectors */
599	for (i = 0; i < interface->num_q_vectors; i++) {
600		qv = interface->q_vector[i];
601		qv->tx.itr = tx_itr;
602		qv->rx.itr = rx_itr;
603	}
604
605	return 0;
606}
607
608static int fm10k_get_rss_hash_opts(struct fm10k_intfc *interface,
609				   struct ethtool_rxnfc *cmd)
610{
611	cmd->data = 0;
612
613	/* Report default options for RSS on fm10k */
614	switch (cmd->flow_type) {
615	case TCP_V4_FLOW:
616	case TCP_V6_FLOW:
617		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
618		/* fall through */
619	case UDP_V4_FLOW:
620		if (interface->flags & FM10K_FLAG_RSS_FIELD_IPV4_UDP)
621			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
622		/* fall through */
623	case SCTP_V4_FLOW:
624	case SCTP_V6_FLOW:
625	case AH_ESP_V4_FLOW:
626	case AH_ESP_V6_FLOW:
627	case AH_V4_FLOW:
628	case AH_V6_FLOW:
629	case ESP_V4_FLOW:
630	case ESP_V6_FLOW:
631	case IPV4_FLOW:
632	case IPV6_FLOW:
633		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
634		break;
635	case UDP_V6_FLOW:
636		if (interface->flags & FM10K_FLAG_RSS_FIELD_IPV6_UDP)
637			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
638		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
639		break;
640	default:
641		return -EINVAL;
642	}
643
644	return 0;
645}
646
647static int fm10k_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
648			   u32 *rule_locs)
649{
650	struct fm10k_intfc *interface = netdev_priv(dev);
651	int ret = -EOPNOTSUPP;
652
653	switch (cmd->cmd) {
654	case ETHTOOL_GRXRINGS:
655		cmd->data = interface->num_rx_queues;
656		ret = 0;
657		break;
658	case ETHTOOL_GRXFH:
659		ret = fm10k_get_rss_hash_opts(interface, cmd);
660		break;
661	default:
662		break;
663	}
664
665	return ret;
666}
667
668#define UDP_RSS_FLAGS (FM10K_FLAG_RSS_FIELD_IPV4_UDP | \
669		       FM10K_FLAG_RSS_FIELD_IPV6_UDP)
670static int fm10k_set_rss_hash_opt(struct fm10k_intfc *interface,
671				  struct ethtool_rxnfc *nfc)
672{
673	u32 flags = interface->flags;
674
675	/* RSS does not support anything other than hashing
676	 * to queues on src and dst IPs and ports
677	 */
678	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
679			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
680		return -EINVAL;
681
682	switch (nfc->flow_type) {
683	case TCP_V4_FLOW:
684	case TCP_V6_FLOW:
685		if (!(nfc->data & RXH_IP_SRC) ||
686		    !(nfc->data & RXH_IP_DST) ||
687		    !(nfc->data & RXH_L4_B_0_1) ||
688		    !(nfc->data & RXH_L4_B_2_3))
689			return -EINVAL;
690		break;
691	case UDP_V4_FLOW:
692		if (!(nfc->data & RXH_IP_SRC) ||
693		    !(nfc->data & RXH_IP_DST))
694			return -EINVAL;
695		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
696		case 0:
697			flags &= ~FM10K_FLAG_RSS_FIELD_IPV4_UDP;
698			break;
699		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
700			flags |= FM10K_FLAG_RSS_FIELD_IPV4_UDP;
701			break;
702		default:
703			return -EINVAL;
704		}
705		break;
706	case UDP_V6_FLOW:
707		if (!(nfc->data & RXH_IP_SRC) ||
708		    !(nfc->data & RXH_IP_DST))
709			return -EINVAL;
710		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
711		case 0:
712			flags &= ~FM10K_FLAG_RSS_FIELD_IPV6_UDP;
713			break;
714		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
715			flags |= FM10K_FLAG_RSS_FIELD_IPV6_UDP;
716			break;
717		default:
718			return -EINVAL;
719		}
720		break;
721	case AH_ESP_V4_FLOW:
722	case AH_V4_FLOW:
723	case ESP_V4_FLOW:
724	case SCTP_V4_FLOW:
725	case AH_ESP_V6_FLOW:
726	case AH_V6_FLOW:
727	case ESP_V6_FLOW:
728	case SCTP_V6_FLOW:
729		if (!(nfc->data & RXH_IP_SRC) ||
730		    !(nfc->data & RXH_IP_DST) ||
731		    (nfc->data & RXH_L4_B_0_1) ||
732		    (nfc->data & RXH_L4_B_2_3))
733			return -EINVAL;
734		break;
735	default:
736		return -EINVAL;
737	}
738
739	/* if we changed something we need to update flags */
740	if (flags != interface->flags) {
741		struct fm10k_hw *hw = &interface->hw;
742		u32 mrqc;
743
744		if ((flags & UDP_RSS_FLAGS) &&
745		    !(interface->flags & UDP_RSS_FLAGS))
746			netif_warn(interface, drv, interface->netdev,
747				   "enabling UDP RSS: fragmented packets may arrive out of order to the stack above\n");
748
749		interface->flags = flags;
750
751		/* Perform hash on these packet types */
752		mrqc = FM10K_MRQC_IPV4 |
753		       FM10K_MRQC_TCP_IPV4 |
754		       FM10K_MRQC_IPV6 |
755		       FM10K_MRQC_TCP_IPV6;
756
757		if (flags & FM10K_FLAG_RSS_FIELD_IPV4_UDP)
758			mrqc |= FM10K_MRQC_UDP_IPV4;
759		if (flags & FM10K_FLAG_RSS_FIELD_IPV6_UDP)
760			mrqc |= FM10K_MRQC_UDP_IPV6;
761
762		fm10k_write_reg(hw, FM10K_MRQC(0), mrqc);
763	}
764
765	return 0;
766}
767
768static int fm10k_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
769{
770	struct fm10k_intfc *interface = netdev_priv(dev);
771	int ret = -EOPNOTSUPP;
772
773	switch (cmd->cmd) {
774	case ETHTOOL_SRXFH:
775		ret = fm10k_set_rss_hash_opt(interface, cmd);
776		break;
777	default:
778		break;
779	}
780
781	return ret;
782}
783
784static int fm10k_mbx_test(struct fm10k_intfc *interface, u64 *data)
785{
786	struct fm10k_hw *hw = &interface->hw;
787	struct fm10k_mbx_info *mbx = &hw->mbx;
788	u32 attr_flag, test_msg[6];
789	unsigned long timeout;
790	int err;
791
792	/* For now this is a VF only feature */
793	if (hw->mac.type != fm10k_mac_vf)
794		return 0;
795
796	/* loop through both nested and unnested attribute types */
797	for (attr_flag = (1 << FM10K_TEST_MSG_UNSET);
798	     attr_flag < (1 << (2 * FM10K_TEST_MSG_NESTED));
799	     attr_flag += attr_flag) {
800		/* generate message to be tested */
801		fm10k_tlv_msg_test_create(test_msg, attr_flag);
802
803		fm10k_mbx_lock(interface);
804		mbx->test_result = FM10K_NOT_IMPLEMENTED;
805		err = mbx->ops.enqueue_tx(hw, mbx, test_msg);
806		fm10k_mbx_unlock(interface);
807
808		/* wait up to 1 second for response */
809		timeout = jiffies + HZ;
810		do {
811			if (err < 0)
812				goto err_out;
813
814			usleep_range(500, 1000);
815
816			fm10k_mbx_lock(interface);
817			mbx->ops.process(hw, mbx);
818			fm10k_mbx_unlock(interface);
819
820			err = mbx->test_result;
821			if (!err)
822				break;
823		} while (time_is_after_jiffies(timeout));
824
825		/* reporting errors */
826		if (err)
827			goto err_out;
828	}
829
830err_out:
831	*data = err < 0 ? (attr_flag) : (err > 0);
832	return err;
833}
834
835static void fm10k_self_test(struct net_device *dev,
836			    struct ethtool_test *eth_test, u64 *data)
837{
838	struct fm10k_intfc *interface = netdev_priv(dev);
839	struct fm10k_hw *hw = &interface->hw;
840
841	memset(data, 0, sizeof(*data) * FM10K_TEST_LEN);
842
843	if (FM10K_REMOVED(hw)) {
844		netif_err(interface, drv, dev,
845			  "Interface removed - test blocked\n");
846		eth_test->flags |= ETH_TEST_FL_FAILED;
847		return;
848	}
849
850	if (fm10k_mbx_test(interface, &data[FM10K_TEST_MBX]))
851		eth_test->flags |= ETH_TEST_FL_FAILED;
852}
853
854static u32 fm10k_get_reta_size(struct net_device *netdev)
855{
856	return FM10K_RETA_SIZE * FM10K_RETA_ENTRIES_PER_REG;
857}
858
859static int fm10k_get_reta(struct net_device *netdev, u32 *indir)
860{
861	struct fm10k_intfc *interface = netdev_priv(netdev);
862	int i;
863
864	if (!indir)
865		return 0;
866
867	for (i = 0; i < FM10K_RETA_SIZE; i++, indir += 4) {
868		u32 reta = interface->reta[i];
869
870		indir[0] = (reta << 24) >> 24;
871		indir[1] = (reta << 16) >> 24;
872		indir[2] = (reta <<  8) >> 24;
873		indir[3] = (reta) >> 24;
874	}
875
876	return 0;
877}
878
879static int fm10k_set_reta(struct net_device *netdev, const u32 *indir)
880{
881	struct fm10k_intfc *interface = netdev_priv(netdev);
882	struct fm10k_hw *hw = &interface->hw;
883	int i;
884	u16 rss_i;
885
886	if (!indir)
887		return 0;
888
889	/* Verify user input. */
890	rss_i = interface->ring_feature[RING_F_RSS].indices;
891	for (i = fm10k_get_reta_size(netdev); i--;) {
892		if (indir[i] < rss_i)
893			continue;
894		return -EINVAL;
895	}
896
897	/* record entries to reta table */
898	for (i = 0; i < FM10K_RETA_SIZE; i++, indir += 4) {
899		u32 reta = indir[0] |
900			   (indir[1] << 8) |
901			   (indir[2] << 16) |
902			   (indir[3] << 24);
903
904		if (interface->reta[i] == reta)
905			continue;
906
907		interface->reta[i] = reta;
908		fm10k_write_reg(hw, FM10K_RETA(0, i), reta);
909	}
910
911	return 0;
912}
913
914static u32 fm10k_get_rssrk_size(struct net_device *netdev)
915{
916	return FM10K_RSSRK_SIZE * FM10K_RSSRK_ENTRIES_PER_REG;
917}
918
919static int fm10k_get_rssh(struct net_device *netdev, u32 *indir, u8 *key)
920{
921	struct fm10k_intfc *interface = netdev_priv(netdev);
922	int i, err;
923
924	err = fm10k_get_reta(netdev, indir);
925	if (err || !key)
926		return err;
927
928	for (i = 0; i < FM10K_RSSRK_SIZE; i++, key += 4)
929		*(__le32 *)key = cpu_to_le32(interface->rssrk[i]);
930
931	return 0;
932}
933
934static int fm10k_set_rssh(struct net_device *netdev, const u32 *indir,
935			  const u8 *key)
936{
937	struct fm10k_intfc *interface = netdev_priv(netdev);
938	struct fm10k_hw *hw = &interface->hw;
939	int i, err;
940
941	err = fm10k_set_reta(netdev, indir);
942	if (err || !key)
943		return err;
944
945	for (i = 0; i < FM10K_RSSRK_SIZE; i++, key += 4) {
946		u32 rssrk = le32_to_cpu(*(__le32 *)key);
947
948		if (interface->rssrk[i] == rssrk)
949			continue;
950
951		interface->rssrk[i] = rssrk;
952		fm10k_write_reg(hw, FM10K_RSSRK(0, i), rssrk);
953	}
954
955	return 0;
956}
957
958static unsigned int fm10k_max_channels(struct net_device *dev)
959{
960	struct fm10k_intfc *interface = netdev_priv(dev);
961	unsigned int max_combined = interface->hw.mac.max_queues;
962	u8 tcs = netdev_get_num_tc(dev);
963
964	/* For QoS report channels per traffic class */
965	if (tcs > 1)
966		max_combined = 1 << (fls(max_combined / tcs) - 1);
967
968	return max_combined;
969}
970
971static void fm10k_get_channels(struct net_device *dev,
972			       struct ethtool_channels *ch)
973{
974	struct fm10k_intfc *interface = netdev_priv(dev);
975	struct fm10k_hw *hw = &interface->hw;
976
977	/* report maximum channels */
978	ch->max_combined = fm10k_max_channels(dev);
979
980	/* report info for other vector */
981	ch->max_other = NON_Q_VECTORS(hw);
982	ch->other_count = ch->max_other;
983
984	/* record RSS queues */
985	ch->combined_count = interface->ring_feature[RING_F_RSS].indices;
986}
987
988static int fm10k_set_channels(struct net_device *dev,
989			      struct ethtool_channels *ch)
990{
991	struct fm10k_intfc *interface = netdev_priv(dev);
992	unsigned int count = ch->combined_count;
993	struct fm10k_hw *hw = &interface->hw;
994
995	/* verify they are not requesting separate vectors */
996	if (!count || ch->rx_count || ch->tx_count)
997		return -EINVAL;
998
999	/* verify other_count has not changed */
1000	if (ch->other_count != NON_Q_VECTORS(hw))
1001		return -EINVAL;
1002
1003	/* verify the number of channels does not exceed hardware limits */
1004	if (count > fm10k_max_channels(dev))
1005		return -EINVAL;
1006
1007	interface->ring_feature[RING_F_RSS].limit = count;
1008
1009	/* use setup TC to update any traffic class queue mapping */
1010	return fm10k_setup_tc(dev, netdev_get_num_tc(dev));
1011}
1012
1013static int fm10k_get_ts_info(struct net_device *dev,
1014			   struct ethtool_ts_info *info)
1015{
1016	struct fm10k_intfc *interface = netdev_priv(dev);
1017
1018	info->so_timestamping =
1019		SOF_TIMESTAMPING_TX_SOFTWARE |
1020		SOF_TIMESTAMPING_RX_SOFTWARE |
1021		SOF_TIMESTAMPING_SOFTWARE |
1022		SOF_TIMESTAMPING_TX_HARDWARE |
1023		SOF_TIMESTAMPING_RX_HARDWARE |
1024		SOF_TIMESTAMPING_RAW_HARDWARE;
1025
1026	if (interface->ptp_clock)
1027		info->phc_index = ptp_clock_index(interface->ptp_clock);
1028	else
1029		info->phc_index = -1;
1030
1031	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1032			 (1 << HWTSTAMP_TX_ON);
1033
1034	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1035			   (1 << HWTSTAMP_FILTER_ALL);
1036
1037	return 0;
1038}
1039
1040static const struct ethtool_ops fm10k_ethtool_ops = {
1041	.get_strings		= fm10k_get_strings,
1042	.get_sset_count		= fm10k_get_sset_count,
1043	.get_ethtool_stats      = fm10k_get_ethtool_stats,
1044	.get_drvinfo		= fm10k_get_drvinfo,
1045	.get_link		= ethtool_op_get_link,
1046	.get_pauseparam		= fm10k_get_pauseparam,
1047	.set_pauseparam		= fm10k_set_pauseparam,
1048	.get_msglevel		= fm10k_get_msglevel,
1049	.set_msglevel		= fm10k_set_msglevel,
1050	.get_ringparam		= fm10k_get_ringparam,
1051	.set_ringparam		= fm10k_set_ringparam,
1052	.get_coalesce		= fm10k_get_coalesce,
1053	.set_coalesce		= fm10k_set_coalesce,
1054	.get_rxnfc		= fm10k_get_rxnfc,
1055	.set_rxnfc		= fm10k_set_rxnfc,
1056	.get_regs               = fm10k_get_regs,
1057	.get_regs_len           = fm10k_get_regs_len,
1058	.self_test		= fm10k_self_test,
1059	.get_rxfh_indir_size	= fm10k_get_reta_size,
1060	.get_rxfh_key_size	= fm10k_get_rssrk_size,
1061	.get_rxfh		= fm10k_get_rssh,
1062	.set_rxfh		= fm10k_set_rssh,
1063	.get_channels		= fm10k_get_channels,
1064	.set_channels		= fm10k_set_channels,
1065	.get_ts_info            = fm10k_get_ts_info,
1066};
1067
1068void fm10k_set_ethtool_ops(struct net_device *dev)
1069{
1070	dev->ethtool_ops = &fm10k_ethtool_ops;
1071}
1072