1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Copyright (C) 2018 Microchip Technology Inc. */
3 
4 #include <linux/netdevice.h>
5 #include "lan743x_main.h"
6 #include "lan743x_ethtool.h"
7 #include <linux/net_tstamp.h>
8 #include <linux/pci.h>
9 #include <linux/phy.h>
10 
11 /* eeprom */
12 #define LAN743X_EEPROM_MAGIC		    (0x74A5)
13 #define LAN743X_OTP_MAGIC		    (0x74F3)
14 #define EEPROM_INDICATOR_1		    (0xA5)
15 #define EEPROM_INDICATOR_2		    (0xAA)
16 #define EEPROM_MAC_OFFSET		    (0x01)
17 #define MAX_EEPROM_SIZE			    512
18 #define OTP_INDICATOR_1			    (0xF3)
19 #define OTP_INDICATOR_2			    (0xF7)
20 
21 static int lan743x_otp_write(struct lan743x_adapter *adapter, u32 offset,
22 			     u32 length, u8 *data)
23 {
24 	unsigned long timeout;
25 	u32 buf;
26 	int i;
27 
28 	buf = lan743x_csr_read(adapter, OTP_PWR_DN);
29 
30 	if (buf & OTP_PWR_DN_PWRDN_N_) {
31 		/* clear it and wait to be cleared */
32 		lan743x_csr_write(adapter, OTP_PWR_DN, 0);
33 
34 		timeout = jiffies + HZ;
35 		do {
36 			udelay(1);
37 			buf = lan743x_csr_read(adapter, OTP_PWR_DN);
38 			if (time_after(jiffies, timeout)) {
39 				netif_warn(adapter, drv, adapter->netdev,
40 					   "timeout on OTP_PWR_DN completion\n");
41 				return -EIO;
42 			}
43 		} while (buf & OTP_PWR_DN_PWRDN_N_);
44 	}
45 
46 	/* set to BYTE program mode */
47 	lan743x_csr_write(adapter, OTP_PRGM_MODE, OTP_PRGM_MODE_BYTE_);
48 
49 	for (i = 0; i < length; i++) {
50 		lan743x_csr_write(adapter, OTP_ADDR1,
51 				  ((offset + i) >> 8) &
52 				  OTP_ADDR1_15_11_MASK_);
53 		lan743x_csr_write(adapter, OTP_ADDR2,
54 				  ((offset + i) &
55 				  OTP_ADDR2_10_3_MASK_));
56 		lan743x_csr_write(adapter, OTP_PRGM_DATA, data[i]);
57 		lan743x_csr_write(adapter, OTP_TST_CMD, OTP_TST_CMD_PRGVRFY_);
58 		lan743x_csr_write(adapter, OTP_CMD_GO, OTP_CMD_GO_GO_);
59 
60 		timeout = jiffies + HZ;
61 		do {
62 			udelay(1);
63 			buf = lan743x_csr_read(adapter, OTP_STATUS);
64 			if (time_after(jiffies, timeout)) {
65 				netif_warn(adapter, drv, adapter->netdev,
66 					   "Timeout on OTP_STATUS completion\n");
67 				return -EIO;
68 			}
69 		} while (buf & OTP_STATUS_BUSY_);
70 	}
71 
72 	return 0;
73 }
74 
75 static int lan743x_eeprom_wait(struct lan743x_adapter *adapter)
76 {
77 	unsigned long start_time = jiffies;
78 	u32 val;
79 
80 	do {
81 		val = lan743x_csr_read(adapter, E2P_CMD);
82 
83 		if (!(val & E2P_CMD_EPC_BUSY_) ||
84 		    (val & E2P_CMD_EPC_TIMEOUT_))
85 			break;
86 		usleep_range(40, 100);
87 	} while (!time_after(jiffies, start_time + HZ));
88 
89 	if (val & (E2P_CMD_EPC_TIMEOUT_ | E2P_CMD_EPC_BUSY_)) {
90 		netif_warn(adapter, drv, adapter->netdev,
91 			   "EEPROM read operation timeout\n");
92 		return -EIO;
93 	}
94 
95 	return 0;
96 }
97 
98 static int lan743x_eeprom_confirm_not_busy(struct lan743x_adapter *adapter)
99 {
100 	unsigned long start_time = jiffies;
101 	u32 val;
102 
103 	do {
104 		val = lan743x_csr_read(adapter, E2P_CMD);
105 
106 		if (!(val & E2P_CMD_EPC_BUSY_))
107 			return 0;
108 
109 		usleep_range(40, 100);
110 	} while (!time_after(jiffies, start_time + HZ));
111 
112 	netif_warn(adapter, drv, adapter->netdev, "EEPROM is busy\n");
113 	return -EIO;
114 }
115 
116 static int lan743x_eeprom_read(struct lan743x_adapter *adapter,
117 			       u32 offset, u32 length, u8 *data)
118 {
119 	int retval;
120 	u32 val;
121 	int i;
122 
123 	retval = lan743x_eeprom_confirm_not_busy(adapter);
124 	if (retval)
125 		return retval;
126 
127 	for (i = 0; i < length; i++) {
128 		val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_READ_;
129 		val |= (offset & E2P_CMD_EPC_ADDR_MASK_);
130 		lan743x_csr_write(adapter, E2P_CMD, val);
131 
132 		retval = lan743x_eeprom_wait(adapter);
133 		if (retval < 0)
134 			return retval;
135 
136 		val = lan743x_csr_read(adapter, E2P_DATA);
137 		data[i] = val & 0xFF;
138 		offset++;
139 	}
140 
141 	return 0;
142 }
143 
144 static int lan743x_eeprom_write(struct lan743x_adapter *adapter,
145 				u32 offset, u32 length, u8 *data)
146 {
147 	int retval;
148 	u32 val;
149 	int i;
150 
151 	retval = lan743x_eeprom_confirm_not_busy(adapter);
152 	if (retval)
153 		return retval;
154 
155 	/* Issue write/erase enable command */
156 	val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_EWEN_;
157 	lan743x_csr_write(adapter, E2P_CMD, val);
158 
159 	retval = lan743x_eeprom_wait(adapter);
160 	if (retval < 0)
161 		return retval;
162 
163 	for (i = 0; i < length; i++) {
164 		/* Fill data register */
165 		val = data[i];
166 		lan743x_csr_write(adapter, E2P_DATA, val);
167 
168 		/* Send "write" command */
169 		val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_WRITE_;
170 		val |= (offset & E2P_CMD_EPC_ADDR_MASK_);
171 		lan743x_csr_write(adapter, E2P_CMD, val);
172 
173 		retval = lan743x_eeprom_wait(adapter);
174 		if (retval < 0)
175 			return retval;
176 
177 		offset++;
178 	}
179 
180 	return 0;
181 }
182 
183 static void lan743x_ethtool_get_drvinfo(struct net_device *netdev,
184 					struct ethtool_drvinfo *info)
185 {
186 	struct lan743x_adapter *adapter = netdev_priv(netdev);
187 
188 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
189 	strlcpy(info->bus_info,
190 		pci_name(adapter->pdev), sizeof(info->bus_info));
191 }
192 
193 static u32 lan743x_ethtool_get_msglevel(struct net_device *netdev)
194 {
195 	struct lan743x_adapter *adapter = netdev_priv(netdev);
196 
197 	return adapter->msg_enable;
198 }
199 
200 static void lan743x_ethtool_set_msglevel(struct net_device *netdev,
201 					 u32 msglevel)
202 {
203 	struct lan743x_adapter *adapter = netdev_priv(netdev);
204 
205 	adapter->msg_enable = msglevel;
206 }
207 
208 static int lan743x_ethtool_get_eeprom_len(struct net_device *netdev)
209 {
210 	return MAX_EEPROM_SIZE;
211 }
212 
213 static int lan743x_ethtool_get_eeprom(struct net_device *netdev,
214 				      struct ethtool_eeprom *ee, u8 *data)
215 {
216 	struct lan743x_adapter *adapter = netdev_priv(netdev);
217 
218 	return lan743x_eeprom_read(adapter, ee->offset, ee->len, data);
219 }
220 
221 static int lan743x_ethtool_set_eeprom(struct net_device *netdev,
222 				      struct ethtool_eeprom *ee, u8 *data)
223 {
224 	struct lan743x_adapter *adapter = netdev_priv(netdev);
225 	int ret = -EINVAL;
226 
227 	if (ee->magic == LAN743X_EEPROM_MAGIC)
228 		ret = lan743x_eeprom_write(adapter, ee->offset, ee->len,
229 					   data);
230 	/* Beware!  OTP is One Time Programming ONLY!
231 	 * So do some strict condition check before messing up
232 	 */
233 	else if ((ee->magic == LAN743X_OTP_MAGIC) &&
234 		 (ee->offset == 0) &&
235 		 (ee->len == MAX_EEPROM_SIZE) &&
236 		 (data[0] == OTP_INDICATOR_1))
237 		ret = lan743x_otp_write(adapter, ee->offset, ee->len, data);
238 
239 	return ret;
240 }
241 
242 static const char lan743x_set0_hw_cnt_strings[][ETH_GSTRING_LEN] = {
243 	"RX FCS Errors",
244 	"RX Alignment Errors",
245 	"Rx Fragment Errors",
246 	"RX Jabber Errors",
247 	"RX Undersize Frame Errors",
248 	"RX Oversize Frame Errors",
249 	"RX Dropped Frames",
250 	"RX Unicast Byte Count",
251 	"RX Broadcast Byte Count",
252 	"RX Multicast Byte Count",
253 	"RX Unicast Frames",
254 	"RX Broadcast Frames",
255 	"RX Multicast Frames",
256 	"RX Pause Frames",
257 	"RX 64 Byte Frames",
258 	"RX 65 - 127 Byte Frames",
259 	"RX 128 - 255 Byte Frames",
260 	"RX 256 - 511 Bytes Frames",
261 	"RX 512 - 1023 Byte Frames",
262 	"RX 1024 - 1518 Byte Frames",
263 	"RX Greater 1518 Byte Frames",
264 };
265 
266 static const char lan743x_set1_sw_cnt_strings[][ETH_GSTRING_LEN] = {
267 	"RX Queue 0 Frames",
268 	"RX Queue 1 Frames",
269 	"RX Queue 2 Frames",
270 	"RX Queue 3 Frames",
271 };
272 
273 static const char lan743x_set2_hw_cnt_strings[][ETH_GSTRING_LEN] = {
274 	"RX Total Frames",
275 	"EEE RX LPI Transitions",
276 	"EEE RX LPI Time",
277 	"RX Counter Rollover Status",
278 	"TX FCS Errors",
279 	"TX Excess Deferral Errors",
280 	"TX Carrier Errors",
281 	"TX Bad Byte Count",
282 	"TX Single Collisions",
283 	"TX Multiple Collisions",
284 	"TX Excessive Collision",
285 	"TX Late Collisions",
286 	"TX Unicast Byte Count",
287 	"TX Broadcast Byte Count",
288 	"TX Multicast Byte Count",
289 	"TX Unicast Frames",
290 	"TX Broadcast Frames",
291 	"TX Multicast Frames",
292 	"TX Pause Frames",
293 	"TX 64 Byte Frames",
294 	"TX 65 - 127 Byte Frames",
295 	"TX 128 - 255 Byte Frames",
296 	"TX 256 - 511 Bytes Frames",
297 	"TX 512 - 1023 Byte Frames",
298 	"TX 1024 - 1518 Byte Frames",
299 	"TX Greater 1518 Byte Frames",
300 	"TX Total Frames",
301 	"EEE TX LPI Transitions",
302 	"EEE TX LPI Time",
303 	"TX Counter Rollover Status",
304 };
305 
306 static const u32 lan743x_set0_hw_cnt_addr[] = {
307 	STAT_RX_FCS_ERRORS,
308 	STAT_RX_ALIGNMENT_ERRORS,
309 	STAT_RX_FRAGMENT_ERRORS,
310 	STAT_RX_JABBER_ERRORS,
311 	STAT_RX_UNDERSIZE_FRAME_ERRORS,
312 	STAT_RX_OVERSIZE_FRAME_ERRORS,
313 	STAT_RX_DROPPED_FRAMES,
314 	STAT_RX_UNICAST_BYTE_COUNT,
315 	STAT_RX_BROADCAST_BYTE_COUNT,
316 	STAT_RX_MULTICAST_BYTE_COUNT,
317 	STAT_RX_UNICAST_FRAMES,
318 	STAT_RX_BROADCAST_FRAMES,
319 	STAT_RX_MULTICAST_FRAMES,
320 	STAT_RX_PAUSE_FRAMES,
321 	STAT_RX_64_BYTE_FRAMES,
322 	STAT_RX_65_127_BYTE_FRAMES,
323 	STAT_RX_128_255_BYTE_FRAMES,
324 	STAT_RX_256_511_BYTES_FRAMES,
325 	STAT_RX_512_1023_BYTE_FRAMES,
326 	STAT_RX_1024_1518_BYTE_FRAMES,
327 	STAT_RX_GREATER_1518_BYTE_FRAMES,
328 };
329 
330 static const u32 lan743x_set2_hw_cnt_addr[] = {
331 	STAT_RX_TOTAL_FRAMES,
332 	STAT_EEE_RX_LPI_TRANSITIONS,
333 	STAT_EEE_RX_LPI_TIME,
334 	STAT_RX_COUNTER_ROLLOVER_STATUS,
335 	STAT_TX_FCS_ERRORS,
336 	STAT_TX_EXCESS_DEFERRAL_ERRORS,
337 	STAT_TX_CARRIER_ERRORS,
338 	STAT_TX_BAD_BYTE_COUNT,
339 	STAT_TX_SINGLE_COLLISIONS,
340 	STAT_TX_MULTIPLE_COLLISIONS,
341 	STAT_TX_EXCESSIVE_COLLISION,
342 	STAT_TX_LATE_COLLISIONS,
343 	STAT_TX_UNICAST_BYTE_COUNT,
344 	STAT_TX_BROADCAST_BYTE_COUNT,
345 	STAT_TX_MULTICAST_BYTE_COUNT,
346 	STAT_TX_UNICAST_FRAMES,
347 	STAT_TX_BROADCAST_FRAMES,
348 	STAT_TX_MULTICAST_FRAMES,
349 	STAT_TX_PAUSE_FRAMES,
350 	STAT_TX_64_BYTE_FRAMES,
351 	STAT_TX_65_127_BYTE_FRAMES,
352 	STAT_TX_128_255_BYTE_FRAMES,
353 	STAT_TX_256_511_BYTES_FRAMES,
354 	STAT_TX_512_1023_BYTE_FRAMES,
355 	STAT_TX_1024_1518_BYTE_FRAMES,
356 	STAT_TX_GREATER_1518_BYTE_FRAMES,
357 	STAT_TX_TOTAL_FRAMES,
358 	STAT_EEE_TX_LPI_TRANSITIONS,
359 	STAT_EEE_TX_LPI_TIME,
360 	STAT_TX_COUNTER_ROLLOVER_STATUS
361 };
362 
363 static void lan743x_ethtool_get_strings(struct net_device *netdev,
364 					u32 stringset, u8 *data)
365 {
366 	switch (stringset) {
367 	case ETH_SS_STATS:
368 		memcpy(data, lan743x_set0_hw_cnt_strings,
369 		       sizeof(lan743x_set0_hw_cnt_strings));
370 		memcpy(&data[sizeof(lan743x_set0_hw_cnt_strings)],
371 		       lan743x_set1_sw_cnt_strings,
372 		       sizeof(lan743x_set1_sw_cnt_strings));
373 		memcpy(&data[sizeof(lan743x_set0_hw_cnt_strings) +
374 		       sizeof(lan743x_set1_sw_cnt_strings)],
375 		       lan743x_set2_hw_cnt_strings,
376 		       sizeof(lan743x_set2_hw_cnt_strings));
377 		break;
378 	}
379 }
380 
381 static void lan743x_ethtool_get_ethtool_stats(struct net_device *netdev,
382 					      struct ethtool_stats *stats,
383 					      u64 *data)
384 {
385 	struct lan743x_adapter *adapter = netdev_priv(netdev);
386 	int data_index = 0;
387 	u32 buf;
388 	int i;
389 
390 	for (i = 0; i < ARRAY_SIZE(lan743x_set0_hw_cnt_addr); i++) {
391 		buf = lan743x_csr_read(adapter, lan743x_set0_hw_cnt_addr[i]);
392 		data[data_index++] = (u64)buf;
393 	}
394 	for (i = 0; i < ARRAY_SIZE(adapter->rx); i++)
395 		data[data_index++] = (u64)(adapter->rx[i].frame_count);
396 	for (i = 0; i < ARRAY_SIZE(lan743x_set2_hw_cnt_addr); i++) {
397 		buf = lan743x_csr_read(adapter, lan743x_set2_hw_cnt_addr[i]);
398 		data[data_index++] = (u64)buf;
399 	}
400 }
401 
402 static int lan743x_ethtool_get_sset_count(struct net_device *netdev, int sset)
403 {
404 	switch (sset) {
405 	case ETH_SS_STATS:
406 	{
407 		int ret;
408 
409 		ret = ARRAY_SIZE(lan743x_set0_hw_cnt_strings);
410 		ret += ARRAY_SIZE(lan743x_set1_sw_cnt_strings);
411 		ret += ARRAY_SIZE(lan743x_set2_hw_cnt_strings);
412 		return ret;
413 	}
414 	default:
415 		return -EOPNOTSUPP;
416 	}
417 }
418 
419 static int lan743x_ethtool_get_rxnfc(struct net_device *netdev,
420 				     struct ethtool_rxnfc *rxnfc,
421 				     u32 *rule_locs)
422 {
423 	switch (rxnfc->cmd) {
424 	case ETHTOOL_GRXFH:
425 		rxnfc->data = 0;
426 		switch (rxnfc->flow_type) {
427 		case TCP_V4_FLOW:case UDP_V4_FLOW:
428 		case TCP_V6_FLOW:case UDP_V6_FLOW:
429 			rxnfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
430 			/* fall through */
431 		case IPV4_FLOW: case IPV6_FLOW:
432 			rxnfc->data |= RXH_IP_SRC | RXH_IP_DST;
433 			return 0;
434 		}
435 		break;
436 	case ETHTOOL_GRXRINGS:
437 		rxnfc->data = LAN743X_USED_RX_CHANNELS;
438 		return 0;
439 	}
440 	return -EOPNOTSUPP;
441 }
442 
443 static u32 lan743x_ethtool_get_rxfh_key_size(struct net_device *netdev)
444 {
445 	return 40;
446 }
447 
448 static u32 lan743x_ethtool_get_rxfh_indir_size(struct net_device *netdev)
449 {
450 	return 128;
451 }
452 
453 static int lan743x_ethtool_get_rxfh(struct net_device *netdev,
454 				    u32 *indir, u8 *key, u8 *hfunc)
455 {
456 	struct lan743x_adapter *adapter = netdev_priv(netdev);
457 
458 	if (indir) {
459 		int dw_index;
460 		int byte_index = 0;
461 
462 		for (dw_index = 0; dw_index < 32; dw_index++) {
463 			u32 four_entries =
464 				lan743x_csr_read(adapter, RFE_INDX(dw_index));
465 
466 			byte_index = dw_index << 2;
467 			indir[byte_index + 0] =
468 				((four_entries >> 0) & 0x000000FF);
469 			indir[byte_index + 1] =
470 				((four_entries >> 8) & 0x000000FF);
471 			indir[byte_index + 2] =
472 				((four_entries >> 16) & 0x000000FF);
473 			indir[byte_index + 3] =
474 				((four_entries >> 24) & 0x000000FF);
475 		}
476 	}
477 	if (key) {
478 		int dword_index;
479 		int byte_index = 0;
480 
481 		for (dword_index = 0; dword_index < 10; dword_index++) {
482 			u32 four_entries =
483 				lan743x_csr_read(adapter,
484 						 RFE_HASH_KEY(dword_index));
485 
486 			byte_index = dword_index << 2;
487 			key[byte_index + 0] =
488 				((four_entries >> 0) & 0x000000FF);
489 			key[byte_index + 1] =
490 				((four_entries >> 8) & 0x000000FF);
491 			key[byte_index + 2] =
492 				((four_entries >> 16) & 0x000000FF);
493 			key[byte_index + 3] =
494 				((four_entries >> 24) & 0x000000FF);
495 		}
496 	}
497 	if (hfunc)
498 		(*hfunc) = ETH_RSS_HASH_TOP;
499 	return 0;
500 }
501 
502 static int lan743x_ethtool_set_rxfh(struct net_device *netdev,
503 				    const u32 *indir, const u8 *key,
504 				    const u8 hfunc)
505 {
506 	struct lan743x_adapter *adapter = netdev_priv(netdev);
507 
508 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
509 		return -EOPNOTSUPP;
510 
511 	if (indir) {
512 		u32 indir_value = 0;
513 		int dword_index = 0;
514 		int byte_index = 0;
515 
516 		for (dword_index = 0; dword_index < 32; dword_index++) {
517 			byte_index = dword_index << 2;
518 			indir_value =
519 				(((indir[byte_index + 0] & 0x000000FF) << 0) |
520 				((indir[byte_index + 1] & 0x000000FF) << 8) |
521 				((indir[byte_index + 2] & 0x000000FF) << 16) |
522 				((indir[byte_index + 3] & 0x000000FF) << 24));
523 			lan743x_csr_write(adapter, RFE_INDX(dword_index),
524 					  indir_value);
525 		}
526 	}
527 	if (key) {
528 		int dword_index = 0;
529 		int byte_index = 0;
530 		u32 key_value = 0;
531 
532 		for (dword_index = 0; dword_index < 10; dword_index++) {
533 			byte_index = dword_index << 2;
534 			key_value =
535 				((((u32)(key[byte_index + 0])) << 0) |
536 				(((u32)(key[byte_index + 1])) << 8) |
537 				(((u32)(key[byte_index + 2])) << 16) |
538 				(((u32)(key[byte_index + 3])) << 24));
539 			lan743x_csr_write(adapter, RFE_HASH_KEY(dword_index),
540 					  key_value);
541 		}
542 	}
543 	return 0;
544 }
545 
546 static int lan743x_ethtool_get_ts_info(struct net_device *netdev,
547 				       struct ethtool_ts_info *ts_info)
548 {
549 	struct lan743x_adapter *adapter = netdev_priv(netdev);
550 
551 	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
552 				   SOF_TIMESTAMPING_RX_SOFTWARE |
553 				   SOF_TIMESTAMPING_SOFTWARE |
554 				   SOF_TIMESTAMPING_TX_HARDWARE |
555 				   SOF_TIMESTAMPING_RX_HARDWARE |
556 				   SOF_TIMESTAMPING_RAW_HARDWARE;
557 
558 	if (adapter->ptp.ptp_clock)
559 		ts_info->phc_index = ptp_clock_index(adapter->ptp.ptp_clock);
560 	else
561 		ts_info->phc_index = -1;
562 
563 	ts_info->tx_types = BIT(HWTSTAMP_TX_OFF) |
564 			    BIT(HWTSTAMP_TX_ON) |
565 			    BIT(HWTSTAMP_TX_ONESTEP_SYNC);
566 	ts_info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
567 			      BIT(HWTSTAMP_FILTER_ALL);
568 	return 0;
569 }
570 
571 static int lan743x_ethtool_get_eee(struct net_device *netdev,
572 				   struct ethtool_eee *eee)
573 {
574 	struct lan743x_adapter *adapter = netdev_priv(netdev);
575 	struct phy_device *phydev = netdev->phydev;
576 	u32 buf;
577 	int ret;
578 
579 	if (!phydev)
580 		return -EIO;
581 	if (!phydev->drv) {
582 		netif_err(adapter, drv, adapter->netdev,
583 			  "Missing PHY Driver\n");
584 		return -EIO;
585 	}
586 
587 	ret = phy_ethtool_get_eee(phydev, eee);
588 	if (ret < 0)
589 		return ret;
590 
591 	buf = lan743x_csr_read(adapter, MAC_CR);
592 	if (buf & MAC_CR_EEE_EN_) {
593 		eee->eee_enabled = true;
594 		eee->eee_active = !!(eee->advertised & eee->lp_advertised);
595 		eee->tx_lpi_enabled = true;
596 		/* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */
597 		buf = lan743x_csr_read(adapter, MAC_EEE_TX_LPI_REQ_DLY_CNT);
598 		eee->tx_lpi_timer = buf;
599 	} else {
600 		eee->eee_enabled = false;
601 		eee->eee_active = false;
602 		eee->tx_lpi_enabled = false;
603 		eee->tx_lpi_timer = 0;
604 	}
605 
606 	return 0;
607 }
608 
609 static int lan743x_ethtool_set_eee(struct net_device *netdev,
610 				   struct ethtool_eee *eee)
611 {
612 	struct lan743x_adapter *adapter = netdev_priv(netdev);
613 	struct phy_device *phydev = NULL;
614 	u32 buf = 0;
615 	int ret = 0;
616 
617 	if (!netdev)
618 		return -EINVAL;
619 	adapter = netdev_priv(netdev);
620 	if (!adapter)
621 		return -EINVAL;
622 	phydev = netdev->phydev;
623 	if (!phydev)
624 		return -EIO;
625 	if (!phydev->drv) {
626 		netif_err(adapter, drv, adapter->netdev,
627 			  "Missing PHY Driver\n");
628 		return -EIO;
629 	}
630 
631 	if (eee->eee_enabled) {
632 		ret = phy_init_eee(phydev, 0);
633 		if (ret) {
634 			netif_err(adapter, drv, adapter->netdev,
635 				  "EEE initialization failed\n");
636 			return ret;
637 		}
638 
639 		buf = (u32)eee->tx_lpi_timer;
640 		lan743x_csr_write(adapter, MAC_EEE_TX_LPI_REQ_DLY_CNT, buf);
641 
642 		buf = lan743x_csr_read(adapter, MAC_CR);
643 		buf |= MAC_CR_EEE_EN_;
644 		lan743x_csr_write(adapter, MAC_CR, buf);
645 	} else {
646 		buf = lan743x_csr_read(adapter, MAC_CR);
647 		buf &= ~MAC_CR_EEE_EN_;
648 		lan743x_csr_write(adapter, MAC_CR, buf);
649 	}
650 
651 	return phy_ethtool_set_eee(phydev, eee);
652 }
653 
654 #ifdef CONFIG_PM
655 static void lan743x_ethtool_get_wol(struct net_device *netdev,
656 				    struct ethtool_wolinfo *wol)
657 {
658 	struct lan743x_adapter *adapter = netdev_priv(netdev);
659 
660 	wol->supported = 0;
661 	wol->wolopts = 0;
662 	phy_ethtool_get_wol(netdev->phydev, wol);
663 
664 	wol->supported |= WAKE_BCAST | WAKE_UCAST | WAKE_MCAST |
665 		WAKE_MAGIC | WAKE_PHY | WAKE_ARP;
666 
667 	wol->wolopts |= adapter->wolopts;
668 }
669 
670 static int lan743x_ethtool_set_wol(struct net_device *netdev,
671 				   struct ethtool_wolinfo *wol)
672 {
673 	struct lan743x_adapter *adapter = netdev_priv(netdev);
674 
675 	adapter->wolopts = 0;
676 	if (wol->wolopts & WAKE_UCAST)
677 		adapter->wolopts |= WAKE_UCAST;
678 	if (wol->wolopts & WAKE_MCAST)
679 		adapter->wolopts |= WAKE_MCAST;
680 	if (wol->wolopts & WAKE_BCAST)
681 		adapter->wolopts |= WAKE_BCAST;
682 	if (wol->wolopts & WAKE_MAGIC)
683 		adapter->wolopts |= WAKE_MAGIC;
684 	if (wol->wolopts & WAKE_PHY)
685 		adapter->wolopts |= WAKE_PHY;
686 	if (wol->wolopts & WAKE_ARP)
687 		adapter->wolopts |= WAKE_ARP;
688 
689 	device_set_wakeup_enable(&adapter->pdev->dev, (bool)wol->wolopts);
690 
691 	phy_ethtool_set_wol(netdev->phydev, wol);
692 
693 	return 0;
694 }
695 #endif /* CONFIG_PM */
696 
697 const struct ethtool_ops lan743x_ethtool_ops = {
698 	.get_drvinfo = lan743x_ethtool_get_drvinfo,
699 	.get_msglevel = lan743x_ethtool_get_msglevel,
700 	.set_msglevel = lan743x_ethtool_set_msglevel,
701 	.get_link = ethtool_op_get_link,
702 
703 	.get_eeprom_len = lan743x_ethtool_get_eeprom_len,
704 	.get_eeprom = lan743x_ethtool_get_eeprom,
705 	.set_eeprom = lan743x_ethtool_set_eeprom,
706 	.get_strings = lan743x_ethtool_get_strings,
707 	.get_ethtool_stats = lan743x_ethtool_get_ethtool_stats,
708 	.get_sset_count = lan743x_ethtool_get_sset_count,
709 	.get_rxnfc = lan743x_ethtool_get_rxnfc,
710 	.get_rxfh_key_size = lan743x_ethtool_get_rxfh_key_size,
711 	.get_rxfh_indir_size = lan743x_ethtool_get_rxfh_indir_size,
712 	.get_rxfh = lan743x_ethtool_get_rxfh,
713 	.set_rxfh = lan743x_ethtool_set_rxfh,
714 	.get_ts_info = lan743x_ethtool_get_ts_info,
715 	.get_eee = lan743x_ethtool_get_eee,
716 	.set_eee = lan743x_ethtool_set_eee,
717 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
718 	.set_link_ksettings = phy_ethtool_set_link_ksettings,
719 #ifdef CONFIG_PM
720 	.get_wol = lan743x_ethtool_get_wol,
721 	.set_wol = lan743x_ethtool_set_wol,
722 #endif
723 };
724