lan743x_ethtool.c 21 KB

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