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