1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hardirq.h>
3 #include <linux/netdevice.h>
4 #include <linux/ethtool.h>
5 #include <linux/delay.h>
6 
7 #include "decl.h"
8 #include "cmd.h"
9 #include "mesh.h"
10 
11 
12 static void lbs_ethtool_get_drvinfo(struct net_device *dev,
13 					 struct ethtool_drvinfo *info)
14 {
15 	struct lbs_private *priv = dev->ml_priv;
16 
17 	snprintf(info->fw_version, sizeof(info->fw_version),
18 		"%u.%u.%u.p%u",
19 		priv->fwrelease >> 24 & 0xff,
20 		priv->fwrelease >> 16 & 0xff,
21 		priv->fwrelease >>  8 & 0xff,
22 		priv->fwrelease       & 0xff);
23 	strlcpy(info->driver, "libertas", sizeof(info->driver));
24 	strlcpy(info->version, lbs_driver_version, sizeof(info->version));
25 }
26 
27 /*
28  * All 8388 parts have 16KiB EEPROM size at the time of writing.
29  * In case that changes this needs fixing.
30  */
31 #define LBS_EEPROM_LEN 16384
32 
33 static int lbs_ethtool_get_eeprom_len(struct net_device *dev)
34 {
35 	return LBS_EEPROM_LEN;
36 }
37 
38 static int lbs_ethtool_get_eeprom(struct net_device *dev,
39                                   struct ethtool_eeprom *eeprom, u8 * bytes)
40 {
41 	struct lbs_private *priv = dev->ml_priv;
42 	struct cmd_ds_802_11_eeprom_access cmd;
43 	int ret;
44 
45 	if (eeprom->offset + eeprom->len > LBS_EEPROM_LEN ||
46 	    eeprom->len > LBS_EEPROM_READ_LEN)
47 		return -EINVAL;
48 
49 	cmd.hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_eeprom_access) -
50 		LBS_EEPROM_READ_LEN + eeprom->len);
51 	cmd.action = cpu_to_le16(CMD_ACT_GET);
52 	cmd.offset = cpu_to_le16(eeprom->offset);
53 	cmd.len    = cpu_to_le16(eeprom->len);
54 	ret = lbs_cmd_with_response(priv, CMD_802_11_EEPROM_ACCESS, &cmd);
55 	if (!ret)
56 		memcpy(bytes, cmd.value, eeprom->len);
57 
58 	return ret;
59 }
60 
61 static void lbs_ethtool_get_wol(struct net_device *dev,
62 				struct ethtool_wolinfo *wol)
63 {
64 	struct lbs_private *priv = dev->ml_priv;
65 
66 	wol->supported = WAKE_UCAST|WAKE_MCAST|WAKE_BCAST|WAKE_PHY;
67 
68 	if (priv->wol_criteria == EHS_REMOVE_WAKEUP)
69 		return;
70 
71 	if (priv->wol_criteria & EHS_WAKE_ON_UNICAST_DATA)
72 		wol->wolopts |= WAKE_UCAST;
73 	if (priv->wol_criteria & EHS_WAKE_ON_MULTICAST_DATA)
74 		wol->wolopts |= WAKE_MCAST;
75 	if (priv->wol_criteria & EHS_WAKE_ON_BROADCAST_DATA)
76 		wol->wolopts |= WAKE_BCAST;
77 	if (priv->wol_criteria & EHS_WAKE_ON_MAC_EVENT)
78 		wol->wolopts |= WAKE_PHY;
79 }
80 
81 static int lbs_ethtool_set_wol(struct net_device *dev,
82 			       struct ethtool_wolinfo *wol)
83 {
84 	struct lbs_private *priv = dev->ml_priv;
85 
86 	if (wol->wolopts & ~(WAKE_UCAST|WAKE_MCAST|WAKE_BCAST|WAKE_PHY))
87 		return -EOPNOTSUPP;
88 
89 	priv->wol_criteria = 0;
90 	if (wol->wolopts & WAKE_UCAST)
91 		priv->wol_criteria |= EHS_WAKE_ON_UNICAST_DATA;
92 	if (wol->wolopts & WAKE_MCAST)
93 		priv->wol_criteria |= EHS_WAKE_ON_MULTICAST_DATA;
94 	if (wol->wolopts & WAKE_BCAST)
95 		priv->wol_criteria |= EHS_WAKE_ON_BROADCAST_DATA;
96 	if (wol->wolopts & WAKE_PHY)
97 		priv->wol_criteria |= EHS_WAKE_ON_MAC_EVENT;
98 	if (wol->wolopts == 0)
99 		priv->wol_criteria |= EHS_REMOVE_WAKEUP;
100 	return 0;
101 }
102 
103 const struct ethtool_ops lbs_ethtool_ops = {
104 	.get_drvinfo = lbs_ethtool_get_drvinfo,
105 	.get_eeprom =  lbs_ethtool_get_eeprom,
106 	.get_eeprom_len = lbs_ethtool_get_eeprom_len,
107 #ifdef CONFIG_LIBERTAS_MESH
108 	.get_sset_count = lbs_mesh_ethtool_get_sset_count,
109 	.get_ethtool_stats = lbs_mesh_ethtool_get_stats,
110 	.get_strings = lbs_mesh_ethtool_get_strings,
111 #endif
112 	.get_wol = lbs_ethtool_get_wol,
113 	.set_wol = lbs_ethtool_set_wol,
114 };
115 
116