be2net: Support for WoL using magic packet after suspend.
[deliverable/linux.git] / drivers / net / benet / be_ethtool.c
1 /*
2 * Copyright (C) 2005 - 2009 ServerEngines
3 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
9 *
10 * Contact Information:
11 * linux-drivers@serverengines.com
12 *
13 * ServerEngines
14 * 209 N. Fair Oaks Ave
15 * Sunnyvale, CA 94085
16 */
17
18 #include "be.h"
19 #include "be_cmds.h"
20 #include <linux/ethtool.h>
21
22 struct be_ethtool_stat {
23 char desc[ETH_GSTRING_LEN];
24 int type;
25 int size;
26 int offset;
27 };
28
29 enum {NETSTAT, PORTSTAT, MISCSTAT, DRVSTAT, ERXSTAT};
30 #define FIELDINFO(_struct, field) FIELD_SIZEOF(_struct, field), \
31 offsetof(_struct, field)
32 #define NETSTAT_INFO(field) #field, NETSTAT,\
33 FIELDINFO(struct net_device_stats,\
34 field)
35 #define DRVSTAT_INFO(field) #field, DRVSTAT,\
36 FIELDINFO(struct be_drvr_stats, field)
37 #define MISCSTAT_INFO(field) #field, MISCSTAT,\
38 FIELDINFO(struct be_rxf_stats, field)
39 #define PORTSTAT_INFO(field) #field, PORTSTAT,\
40 FIELDINFO(struct be_port_rxf_stats, \
41 field)
42 #define ERXSTAT_INFO(field) #field, ERXSTAT,\
43 FIELDINFO(struct be_erx_stats, field)
44
45 static const struct be_ethtool_stat et_stats[] = {
46 {NETSTAT_INFO(rx_packets)},
47 {NETSTAT_INFO(tx_packets)},
48 {NETSTAT_INFO(rx_bytes)},
49 {NETSTAT_INFO(tx_bytes)},
50 {NETSTAT_INFO(rx_errors)},
51 {NETSTAT_INFO(tx_errors)},
52 {NETSTAT_INFO(rx_dropped)},
53 {NETSTAT_INFO(tx_dropped)},
54 {DRVSTAT_INFO(be_tx_reqs)},
55 {DRVSTAT_INFO(be_tx_stops)},
56 {DRVSTAT_INFO(be_fwd_reqs)},
57 {DRVSTAT_INFO(be_tx_wrbs)},
58 {DRVSTAT_INFO(be_rx_polls)},
59 {DRVSTAT_INFO(be_tx_events)},
60 {DRVSTAT_INFO(be_rx_events)},
61 {DRVSTAT_INFO(be_tx_compl)},
62 {DRVSTAT_INFO(be_rx_compl)},
63 {DRVSTAT_INFO(be_ethrx_post_fail)},
64 {DRVSTAT_INFO(be_802_3_dropped_frames)},
65 {DRVSTAT_INFO(be_802_3_malformed_frames)},
66 {DRVSTAT_INFO(be_tx_rate)},
67 {DRVSTAT_INFO(be_rx_rate)},
68 {PORTSTAT_INFO(rx_unicast_frames)},
69 {PORTSTAT_INFO(rx_multicast_frames)},
70 {PORTSTAT_INFO(rx_broadcast_frames)},
71 {PORTSTAT_INFO(rx_crc_errors)},
72 {PORTSTAT_INFO(rx_alignment_symbol_errors)},
73 {PORTSTAT_INFO(rx_pause_frames)},
74 {PORTSTAT_INFO(rx_control_frames)},
75 {PORTSTAT_INFO(rx_in_range_errors)},
76 {PORTSTAT_INFO(rx_out_range_errors)},
77 {PORTSTAT_INFO(rx_frame_too_long)},
78 {PORTSTAT_INFO(rx_address_match_errors)},
79 {PORTSTAT_INFO(rx_vlan_mismatch)},
80 {PORTSTAT_INFO(rx_dropped_too_small)},
81 {PORTSTAT_INFO(rx_dropped_too_short)},
82 {PORTSTAT_INFO(rx_dropped_header_too_small)},
83 {PORTSTAT_INFO(rx_dropped_tcp_length)},
84 {PORTSTAT_INFO(rx_dropped_runt)},
85 {PORTSTAT_INFO(rx_fifo_overflow)},
86 {PORTSTAT_INFO(rx_input_fifo_overflow)},
87 {PORTSTAT_INFO(rx_ip_checksum_errs)},
88 {PORTSTAT_INFO(rx_tcp_checksum_errs)},
89 {PORTSTAT_INFO(rx_udp_checksum_errs)},
90 {PORTSTAT_INFO(rx_non_rss_packets)},
91 {PORTSTAT_INFO(rx_ipv4_packets)},
92 {PORTSTAT_INFO(rx_ipv6_packets)},
93 {PORTSTAT_INFO(tx_unicastframes)},
94 {PORTSTAT_INFO(tx_multicastframes)},
95 {PORTSTAT_INFO(tx_broadcastframes)},
96 {PORTSTAT_INFO(tx_pauseframes)},
97 {PORTSTAT_INFO(tx_controlframes)},
98 {MISCSTAT_INFO(rx_drops_no_pbuf)},
99 {MISCSTAT_INFO(rx_drops_no_txpb)},
100 {MISCSTAT_INFO(rx_drops_no_erx_descr)},
101 {MISCSTAT_INFO(rx_drops_no_tpre_descr)},
102 {MISCSTAT_INFO(rx_drops_too_many_frags)},
103 {MISCSTAT_INFO(rx_drops_invalid_ring)},
104 {MISCSTAT_INFO(forwarded_packets)},
105 {MISCSTAT_INFO(rx_drops_mtu)},
106 {ERXSTAT_INFO(rx_drops_no_fragments)},
107 };
108 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats)
109
110 static void
111 be_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
112 {
113 struct be_adapter *adapter = netdev_priv(netdev);
114
115 strcpy(drvinfo->driver, DRV_NAME);
116 strcpy(drvinfo->version, DRV_VER);
117 strncpy(drvinfo->fw_version, adapter->fw_ver, FW_VER_LEN);
118 strcpy(drvinfo->bus_info, pci_name(adapter->pdev));
119 drvinfo->testinfo_len = 0;
120 drvinfo->regdump_len = 0;
121 drvinfo->eedump_len = 0;
122 }
123
124 static int
125 be_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
126 {
127 struct be_adapter *adapter = netdev_priv(netdev);
128 struct be_eq_obj *rx_eq = &adapter->rx_eq;
129 struct be_eq_obj *tx_eq = &adapter->tx_eq;
130
131 coalesce->rx_coalesce_usecs = rx_eq->cur_eqd;
132 coalesce->rx_coalesce_usecs_high = rx_eq->max_eqd;
133 coalesce->rx_coalesce_usecs_low = rx_eq->min_eqd;
134
135 coalesce->tx_coalesce_usecs = tx_eq->cur_eqd;
136 coalesce->tx_coalesce_usecs_high = tx_eq->max_eqd;
137 coalesce->tx_coalesce_usecs_low = tx_eq->min_eqd;
138
139 coalesce->use_adaptive_rx_coalesce = rx_eq->enable_aic;
140 coalesce->use_adaptive_tx_coalesce = tx_eq->enable_aic;
141
142 return 0;
143 }
144
145 /*
146 * This routine is used to set interrup coalescing delay
147 */
148 static int
149 be_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
150 {
151 struct be_adapter *adapter = netdev_priv(netdev);
152 struct be_eq_obj *rx_eq = &adapter->rx_eq;
153 struct be_eq_obj *tx_eq = &adapter->tx_eq;
154 u32 tx_max, tx_min, tx_cur;
155 u32 rx_max, rx_min, rx_cur;
156 int status = 0;
157
158 if (coalesce->use_adaptive_tx_coalesce == 1)
159 return -EINVAL;
160
161 /* if AIC is being turned on now, start with an EQD of 0 */
162 if (rx_eq->enable_aic == 0 &&
163 coalesce->use_adaptive_rx_coalesce == 1) {
164 rx_eq->cur_eqd = 0;
165 }
166 rx_eq->enable_aic = coalesce->use_adaptive_rx_coalesce;
167
168 rx_max = coalesce->rx_coalesce_usecs_high;
169 rx_min = coalesce->rx_coalesce_usecs_low;
170 rx_cur = coalesce->rx_coalesce_usecs;
171
172 tx_max = coalesce->tx_coalesce_usecs_high;
173 tx_min = coalesce->tx_coalesce_usecs_low;
174 tx_cur = coalesce->tx_coalesce_usecs;
175
176 if (tx_cur > BE_MAX_EQD)
177 tx_cur = BE_MAX_EQD;
178 if (tx_eq->cur_eqd != tx_cur) {
179 status = be_cmd_modify_eqd(adapter, tx_eq->q.id, tx_cur);
180 if (!status)
181 tx_eq->cur_eqd = tx_cur;
182 }
183
184 if (rx_eq->enable_aic) {
185 if (rx_max > BE_MAX_EQD)
186 rx_max = BE_MAX_EQD;
187 if (rx_min > rx_max)
188 rx_min = rx_max;
189 rx_eq->max_eqd = rx_max;
190 rx_eq->min_eqd = rx_min;
191 if (rx_eq->cur_eqd > rx_max)
192 rx_eq->cur_eqd = rx_max;
193 if (rx_eq->cur_eqd < rx_min)
194 rx_eq->cur_eqd = rx_min;
195 } else {
196 if (rx_cur > BE_MAX_EQD)
197 rx_cur = BE_MAX_EQD;
198 if (rx_eq->cur_eqd != rx_cur) {
199 status = be_cmd_modify_eqd(adapter, rx_eq->q.id,
200 rx_cur);
201 if (!status)
202 rx_eq->cur_eqd = rx_cur;
203 }
204 }
205 return 0;
206 }
207
208 static u32 be_get_rx_csum(struct net_device *netdev)
209 {
210 struct be_adapter *adapter = netdev_priv(netdev);
211
212 return adapter->rx_csum;
213 }
214
215 static int be_set_rx_csum(struct net_device *netdev, uint32_t data)
216 {
217 struct be_adapter *adapter = netdev_priv(netdev);
218
219 if (data)
220 adapter->rx_csum = true;
221 else
222 adapter->rx_csum = false;
223
224 return 0;
225 }
226
227 static void
228 be_get_ethtool_stats(struct net_device *netdev,
229 struct ethtool_stats *stats, uint64_t *data)
230 {
231 struct be_adapter *adapter = netdev_priv(netdev);
232 struct be_drvr_stats *drvr_stats = &adapter->stats.drvr_stats;
233 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
234 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
235 struct be_port_rxf_stats *port_stats =
236 &rxf_stats->port[adapter->port_num];
237 struct net_device_stats *net_stats = &netdev->stats;
238 struct be_erx_stats *erx_stats = &hw_stats->erx;
239 void *p = NULL;
240 int i;
241
242 for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
243 switch (et_stats[i].type) {
244 case NETSTAT:
245 p = net_stats;
246 break;
247 case DRVSTAT:
248 p = drvr_stats;
249 break;
250 case PORTSTAT:
251 p = port_stats;
252 break;
253 case MISCSTAT:
254 p = rxf_stats;
255 break;
256 case ERXSTAT: /* Currently only one ERX stat is provided */
257 p = (u32 *)erx_stats + adapter->rx_obj.q.id;
258 break;
259 }
260
261 p = (u8 *)p + et_stats[i].offset;
262 data[i] = (et_stats[i].size == sizeof(u64)) ?
263 *(u64 *)p: *(u32 *)p;
264 }
265
266 return;
267 }
268
269 static void
270 be_get_stat_strings(struct net_device *netdev, uint32_t stringset,
271 uint8_t *data)
272 {
273 int i;
274 switch (stringset) {
275 case ETH_SS_STATS:
276 for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
277 memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN);
278 data += ETH_GSTRING_LEN;
279 }
280 break;
281 }
282 }
283
284 static int be_get_sset_count(struct net_device *netdev, int stringset)
285 {
286 switch (stringset) {
287 case ETH_SS_STATS:
288 return ETHTOOL_STATS_NUM;
289 default:
290 return -EINVAL;
291 }
292 }
293
294 static int be_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
295 {
296 struct be_adapter *adapter = netdev_priv(netdev);
297 u8 mac_speed = 0, connector = 0;
298 u16 link_speed = 0;
299 bool link_up = false;
300 int status;
301
302 if (adapter->link_speed < 0) {
303 status = be_cmd_link_status_query(adapter, &link_up,
304 &mac_speed, &link_speed);
305
306 /* link_speed is in units of 10 Mbps */
307 if (link_speed) {
308 ecmd->speed = link_speed*10;
309 } else {
310 switch (mac_speed) {
311 case PHY_LINK_SPEED_1GBPS:
312 ecmd->speed = SPEED_1000;
313 break;
314 case PHY_LINK_SPEED_10GBPS:
315 ecmd->speed = SPEED_10000;
316 break;
317 }
318 }
319
320 status = be_cmd_read_port_type(adapter, adapter->port_num,
321 &connector);
322 switch (connector) {
323 case 7:
324 ecmd->port = PORT_FIBRE;
325 break;
326 default:
327 ecmd->port = PORT_TP;
328 break;
329 }
330
331 /* Save for future use */
332 adapter->link_speed = ecmd->speed;
333 adapter->port_type = ecmd->port;
334 } else {
335 ecmd->speed = adapter->link_speed;
336 ecmd->port = adapter->port_type;
337 }
338
339 ecmd->duplex = DUPLEX_FULL;
340 ecmd->autoneg = AUTONEG_DISABLE;
341 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_TP);
342 ecmd->phy_address = adapter->port_num;
343 ecmd->transceiver = XCVR_INTERNAL;
344
345 return 0;
346 }
347
348 static void
349 be_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
350 {
351 struct be_adapter *adapter = netdev_priv(netdev);
352
353 ring->rx_max_pending = adapter->rx_obj.q.len;
354 ring->tx_max_pending = adapter->tx_obj.q.len;
355
356 ring->rx_pending = atomic_read(&adapter->rx_obj.q.used);
357 ring->tx_pending = atomic_read(&adapter->tx_obj.q.used);
358 }
359
360 static void
361 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
362 {
363 struct be_adapter *adapter = netdev_priv(netdev);
364
365 be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause);
366 ecmd->autoneg = 0;
367 }
368
369 static int
370 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
371 {
372 struct be_adapter *adapter = netdev_priv(netdev);
373 int status;
374
375 if (ecmd->autoneg != 0)
376 return -EINVAL;
377 adapter->tx_fc = ecmd->tx_pause;
378 adapter->rx_fc = ecmd->rx_pause;
379
380 status = be_cmd_set_flow_control(adapter,
381 adapter->tx_fc, adapter->rx_fc);
382 if (status)
383 dev_warn(&adapter->pdev->dev, "Pause param set failed.\n");
384
385 return status;
386 }
387
388 static int
389 be_phys_id(struct net_device *netdev, u32 data)
390 {
391 struct be_adapter *adapter = netdev_priv(netdev);
392 int status;
393 u32 cur;
394
395 be_cmd_get_beacon_state(adapter, adapter->port_num, &cur);
396
397 if (cur == BEACON_STATE_ENABLED)
398 return 0;
399
400 if (data < 2)
401 data = 2;
402
403 status = be_cmd_set_beacon_state(adapter, adapter->port_num, 0, 0,
404 BEACON_STATE_ENABLED);
405 set_current_state(TASK_INTERRUPTIBLE);
406 schedule_timeout(data*HZ);
407
408 status = be_cmd_set_beacon_state(adapter, adapter->port_num, 0, 0,
409 BEACON_STATE_DISABLED);
410
411 return status;
412 }
413
414 static void
415 be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
416 {
417 struct be_adapter *adapter = netdev_priv(netdev);
418
419 wol->supported = WAKE_MAGIC;
420 if (adapter->wol)
421 wol->wolopts = WAKE_MAGIC;
422 else
423 wol->wolopts = 0;
424 memset(&wol->sopass, 0, sizeof(wol->sopass));
425 return;
426 }
427
428 static int
429 be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
430 {
431 struct be_adapter *adapter = netdev_priv(netdev);
432
433 if (wol->wolopts & ~WAKE_MAGIC)
434 return -EINVAL;
435
436 if (wol->wolopts & WAKE_MAGIC)
437 adapter->wol = true;
438 else
439 adapter->wol = false;
440
441 return 0;
442 }
443
444 static int
445 be_do_flash(struct net_device *netdev, struct ethtool_flash *efl)
446 {
447 struct be_adapter *adapter = netdev_priv(netdev);
448 char file_name[ETHTOOL_FLASH_MAX_FILENAME];
449 u32 region;
450
451 file_name[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
452 strcpy(file_name, efl->data);
453 region = efl->region;
454
455 return be_load_fw(adapter, file_name);
456 }
457
458 const struct ethtool_ops be_ethtool_ops = {
459 .get_settings = be_get_settings,
460 .get_drvinfo = be_get_drvinfo,
461 .get_wol = be_get_wol,
462 .set_wol = be_set_wol,
463 .get_link = ethtool_op_get_link,
464 .get_coalesce = be_get_coalesce,
465 .set_coalesce = be_set_coalesce,
466 .get_ringparam = be_get_ringparam,
467 .get_pauseparam = be_get_pauseparam,
468 .set_pauseparam = be_set_pauseparam,
469 .get_rx_csum = be_get_rx_csum,
470 .set_rx_csum = be_set_rx_csum,
471 .get_tx_csum = ethtool_op_get_tx_csum,
472 .set_tx_csum = ethtool_op_set_tx_hw_csum,
473 .get_sg = ethtool_op_get_sg,
474 .set_sg = ethtool_op_set_sg,
475 .get_tso = ethtool_op_get_tso,
476 .set_tso = ethtool_op_set_tso,
477 .get_strings = be_get_stat_strings,
478 .phys_id = be_phys_id,
479 .get_sset_count = be_get_sset_count,
480 .get_ethtool_stats = be_get_ethtool_stats,
481 .flash_device = be_do_flash,
482 };
This page took 0.046757 seconds and 5 git commands to generate.