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